Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using ChannelSockets;
  8. using System.Collections.Concurrent;
  9.  
  10. namespace ExampleServer
  11. {
  12. public class Client
  13. {
  14. public IPEndPoint EndPoint { get; set; }
  15. public string Username { get; set; }
  16. public ushort ID { get; set; }
  17. }
  18.  
  19. class Program
  20. {
  21. public struct Channels
  22. {
  23. public const byte UnreliableUpdate = 0;
  24. public const byte ReliableUpdate = 1;
  25. public const byte Chat = 2;
  26. public const byte Downloads = 3;
  27. }
  28.  
  29. public static ChannelSocket socket = new ChannelSocket(30303);
  30.  
  31. static Dictionary<IPEndPoint, Client> clients = new Dictionary<IPEndPoint,Client>();
  32. static Queue<ushort> clientIDs = new Queue<ushort>(Enumerable.Range(0, 1000).Select(i => (ushort)i));
  33.  
  34. static ConcurrentQueue<Client> pendingClients = new ConcurrentQueue<Client>();
  35. static ConcurrentQueue<IPEndPoint> disconnectedEndPoints = new ConcurrentQueue<IPEndPoint>();
  36.  
  37. static void Main(string[] args)
  38. {
  39. socket.Add(Channels.UnreliableUpdate, ChannelType.Unreliable);
  40. socket.Add(Channels.ReliableUpdate, ChannelType.Reliable);
  41. socket.Add(Channels.Chat, ChannelType.Reliable);
  42. socket.Add(Channels.Downloads, ChannelType.Reliable);
  43.  
  44. socket.Connection = c =>
  45. {
  46. // Ban connections like this
  47. if (!c.EndPoint.Equals(IPAddress.Parse("111.111.111.111")))
  48. {
  49. c.Allow = false;
  50. return;
  51. }
  52.  
  53. // Authenticate users like this
  54. string username;
  55. string password;
  56. if (c.Packet.ReadString(out username) && c.Packet.ReadString(out password) &&
  57. username == "foo" && password == "bar")
  58. {
  59. pendingClients.Enqueue(new Client() { EndPoint = c.EndPoint, Username = username });
  60. }
  61. else
  62. {
  63. c.Allow = false;
  64. }
  65. };
  66.  
  67. // Callback if an endpoint times out or disconnects
  68. socket.Disconnection = c =>
  69. {
  70. if (c.Clean)
  71. {
  72. // Destination initiated the disconnection or server did
  73. }
  74. else
  75. {
  76. // Timed out
  77. }
  78. disconnectedEndPoints.Enqueue(c.EndPoint);
  79. };
  80.  
  81. // Server command line interface
  82. while (true)
  83. {
  84. Console.WriteLine("Enter command");
  85. Console.WriteLine("1. List clients");
  86. Console.WriteLine("2. Disconnect clients");
  87. Console.WriteLine("3. Send 'hello world' to clients");
  88.  
  89. var key = Console.ReadKey().Key;
  90.  
  91. Console.Clear();
  92.  
  93. // Process clients that just disconnected or connected. This would be done in a game loop normally
  94. IPEndPoint disconnectedEndPoint;
  95. while (disconnectedEndPoints.TryDequeue(out disconnectedEndPoint))
  96. {
  97. Client client;
  98. if (clients.TryGetValue(disconnectedEndPoint, out client))
  99. {
  100. // Recycle the client ID
  101. clientIDs.Enqueue(client.ID);
  102. clients.Remove(disconnectedEndPoint);
  103. }
  104. }
  105.  
  106. Client connectedClient;
  107. while (pendingClients.TryDequeue(out connectedClient))
  108. {
  109. // Assign a unique client ID to the client
  110. connectedClient.ID = clientIDs.Dequeue();
  111. clients.Add(connectedClient.EndPoint, connectedClient);
  112. }
  113.  
  114. EndPointPacket endPointPacket;
  115. while (socket[Channels.Chat].Packets.TryDequeue(out endPointPacket))
  116. {
  117. ushort clientID;
  118. string message;
  119. if (endPointPacket.Packet.ReadUInt16(out clientID) && endPointPacket.Packet.ReadString(out message))
  120. {
  121. Client client;
  122. if (clientID == ushort.MaxValue && clients.TryGetValue(endPointPacket.EndPoint, out client))
  123. {
  124. // TODO: Remove non-visible characters
  125. Console.WriteLine(client.Username + ": " + message.Trim());
  126. }
  127. }
  128. }
  129.  
  130. switch (key)
  131. {
  132. case ConsoleKey.D1:
  133. lock (clients)
  134. {
  135. Console.WriteLine("Clients:");
  136. foreach (Client client in clients.Values)
  137. {
  138. Console.WriteLine(client.Username);
  139. }
  140. }
  141. break;
  142. case ConsoleKey.D2:
  143. lock (clients)
  144. {
  145. foreach (IPEndPoint endPoint in clients.Keys)
  146. {
  147. socket.Disconnect(endPoint);
  148. }
  149. Console.WriteLine("Disconnected clients");
  150. }
  151. break;
  152. case ConsoleKey.D3:
  153. lock (clients)
  154. {
  155. foreach (IPEndPoint endPoint in clients.Keys)
  156. {
  157. var packet = socket[Channels.Chat].CreatePacket();
  158. packet.WriteUInt16(ushort.MaxValue); // Client ID, ushort.MaxValue is the Server
  159. packet.WriteString("hello world");
  160. socket[Channels.Chat].Send(packet, endPoint);
  161. }
  162. Console.WriteLine("Messages sent");
  163. }
  164. break;
  165. }
  166. Console.WriteLine();
  167. Console.WriteLine("Press any key...");
  168. Console.ReadKey(true);
  169. }
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement