Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 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. // Destination initiated the disconnection or server did or the connection timed out
  71. disconnectedEndPoints.Enqueue(c.EndPoint);
  72. };
  73.  
  74. // Server command line interface
  75. while (true)
  76. {
  77. Console.WriteLine("Enter command");
  78. Console.WriteLine("1. List clients");
  79. Console.WriteLine("2. Read chat");
  80. Console.WriteLine("3. Send 'hello world' to clients");
  81. Console.WriteLine("4. Disconnect clients");
  82.  
  83. var key = Console.ReadKey().Key;
  84.  
  85. Console.Clear();
  86.  
  87. // Process clients that just disconnected or connected. This would be done in a game loop normally
  88. IPEndPoint disconnectedEndPoint;
  89. while (disconnectedEndPoints.TryDequeue(out disconnectedEndPoint))
  90. {
  91. Client client;
  92. if (clients.TryGetValue(disconnectedEndPoint, out client))
  93. {
  94. // Recycle the client ID
  95. clientIDs.Enqueue(client.ID);
  96. clients.Remove(disconnectedEndPoint);
  97. }
  98. }
  99.  
  100. Client connectedClient;
  101. while (pendingClients.TryDequeue(out connectedClient))
  102. {
  103. // Assign a unique client ID to the client
  104. connectedClient.ID = clientIDs.Dequeue();
  105. clients.Add(connectedClient.EndPoint, connectedClient);
  106. }
  107.  
  108. switch (key)
  109. {
  110. case ConsoleKey.D1:
  111. Console.WriteLine("Clients:");
  112. foreach (Client client in clients.Values)
  113. {
  114. Console.WriteLine(client.Username);
  115. }
  116. break;
  117. case ConsoleKey.D2:
  118. Console.WriteLine("Chat:");
  119. EndPointPacket endPointPacket;
  120. while (socket[Channels.Chat].Packets.TryDequeue(out endPointPacket))
  121. {
  122. ushort clientID;
  123. string message;
  124. if (endPointPacket.Packet.ReadUInt16(out clientID) && endPointPacket.Packet.ReadString(out message))
  125. {
  126. Client client;
  127. if (clientID == ushort.MaxValue && clients.TryGetValue(endPointPacket.EndPoint, out client))
  128. {
  129. // TODO: Remove non-visible characters
  130. Console.WriteLine(client.Username + ": " + message.Trim());
  131. }
  132. }
  133. }
  134. break;
  135. case ConsoleKey.D3:
  136. foreach (IPEndPoint endPoint in clients.Keys)
  137. {
  138. var packet = socket[Channels.Chat].CreatePacket();
  139. packet.WriteUInt16(ushort.MaxValue); // Client ID, ushort.MaxValue is the Server
  140. packet.WriteString("hello world");
  141. socket[Channels.Chat].Send(packet, endPoint);
  142. }
  143. Console.WriteLine("Messages sent");
  144. break;
  145. case ConsoleKey.D4:
  146. foreach (IPEndPoint endPoint in clients.Keys)
  147. {
  148. socket.Disconnect(endPoint);
  149. }
  150. Console.WriteLine("Disconnected clients");
  151. break;
  152. }
  153. Console.WriteLine();
  154. Console.WriteLine("Press any key...");
  155. Console.ReadKey(true);
  156. }
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement