Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ChannelSockets;
  6. using System.Net;
  7. using System.Threading;
  8.  
  9. namespace ExampleClient
  10. {
  11. class Program
  12. {
  13. public struct Channels
  14. {
  15. public const byte UnreliableUpdate = 0;
  16. public const byte ReliableUpdate = 1;
  17. public const byte Chat = 2;
  18. public const byte Downloads = 3;
  19. }
  20.  
  21. public static ChannelSocket socket = new ChannelSocket(IPAddress.Loopback, 30303);
  22.  
  23. static void Main(string[] args)
  24. {
  25. socket.Add(Channels.UnreliableUpdate, ChannelType.Unreliable);
  26. socket.Add(Channels.ReliableUpdate, ChannelType.Reliable);
  27. socket.Add(Channels.Chat, ChannelType.Reliable);
  28. socket.Add(Channels.Downloads, ChannelType.Reliable);
  29.  
  30. string username = "foo";
  31. string password = "bar";
  32.  
  33. AutoResetEvent waitConnectEvent = new AutoResetEvent(false);
  34. socket.Connect(
  35. (p) =>
  36. {
  37. p.WriteString(username);
  38. p.WriteString(password);
  39. },
  40. () =>
  41. {
  42. // Success
  43. Console.WriteLine("Successfully connected");
  44. waitConnectEvent.Set();
  45. },
  46. () =>
  47. {
  48. // Failure
  49. Console.WriteLine("Failure connecting");
  50. waitConnectEvent.Set();
  51. });
  52. waitConnectEvent.WaitOne();
  53.  
  54. // Client command line interface
  55. while (true)
  56. {
  57. Console.WriteLine("Enter command");
  58. Console.WriteLine("1. Read chat");
  59. Console.WriteLine("2. Send 'hello world' to server");
  60. Console.WriteLine("3. Disconnect");
  61.  
  62. var key = Console.ReadKey().Key;
  63.  
  64. Console.Clear();
  65.  
  66. switch (key)
  67. {
  68. case ConsoleKey.D1:
  69. EndPointPacket endPointPacket;
  70. while (socket[Channels.Chat].Packets.TryDequeue(out endPointPacket))
  71. {
  72. ushort clientID;
  73. string message;
  74. if (endPointPacket.Packet.ReadUInt16(out clientID) && endPointPacket.Packet.ReadString(out message))
  75. {
  76. Console.WriteLine(clientID + ": " + message.Trim());
  77. }
  78. }
  79. break;
  80. case ConsoleKey.D2:
  81. var packet = socket[Channels.Chat].CreatePacket();
  82. packet.WriteUInt16(ushort.MaxValue); // Client ID, ushort.MaxValue is the Server
  83. packet.WriteString("hello world");
  84. socket[Channels.Chat].Send(packet, socket.EndPoint);
  85. Console.WriteLine("Message sent");
  86. break;
  87. case ConsoleKey.D3:
  88. AutoResetEvent waitDisconnectEvent = new AutoResetEvent(false);
  89. socket.Disconnect(() =>
  90. {
  91. Console.WriteLine("Completed");
  92. waitDisconnectEvent.Set();
  93. });
  94. waitDisconnectEvent.WaitOne();
  95. break;
  96. }
  97. Console.WriteLine();
  98. Console.WriteLine("Press any key...");
  99. Console.ReadKey(true);
  100. }
  101.  
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement