Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Sockets;
  5.  
  6. namespace SimpleChat
  7. {
  8.  
  9. class Listener
  10. {
  11. private Socket mListener;
  12. public int Port { get; set; }
  13. public bool Listening { get; set; }
  14. public List<Client> Connections = new List<Client>();
  15.  
  16. public Listener(ushort pPort)
  17. {
  18. try
  19. {
  20. Port = pPort;
  21. mListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  22. mListener.Bind(new IPEndPoint(IPAddress.Any, Port));
  23. mListener.Listen(10);
  24. mListener.BeginAccept(new AsyncCallback(OnConnect), mListener);
  25. Listening = true;
  26. Log.WriteLine(ELogLevel.Info, "Server started at [{0}]", Port);
  27. }
  28. catch (SocketException ex)
  29. {
  30. Log.WriteLine(ELogLevel.Exception, ex.Message);
  31. Listening = false;
  32. }
  33. }
  34.  
  35. private void OnConnect(IAsyncResult ar)
  36. {
  37. if (!Listening) return;
  38. try
  39. {
  40. Socket lSock = mListener.EndAccept(ar);
  41. Client nClient = new Client(lSock);
  42. nClient.OnDisconnect += new Client.DPassClient(nClient_OnDisconnect);
  43. nClient.OnPacketReceived += new Client.DPassPacket(nClient_OnPacketReceived);
  44. Log.WriteLine(ELogLevel.Info, "Client connected: {0}", nClient.Host);
  45. AddConnection(nClient);
  46. nClient.SendPacket(PacketCreator.ChannelList());
  47. }
  48. catch (SocketException ex)
  49. {
  50. Log.WriteLine(ELogLevel.Exception, ex.Message);
  51. }
  52. finally
  53. {
  54. mListener.BeginAccept(new AsyncCallback(OnConnect), mListener); //start listening again
  55. }
  56. }
  57.  
  58. void nClient_OnPacketReceived(Client pClient, Packet pPacket)
  59. {
  60. switch (pPacket.Opcode)
  61. {
  62. default:
  63. Console.WriteLine("Unhandled packet {0} : {1}", pPacket.Opcode, HexTools.ToStringFromAscii(pPacket.InnerBuffer));
  64. break;
  65. case 1: //authenciate
  66. OnAuthenciateRequest(pClient, pPacket);
  67. break;
  68. case 2: //joined channel
  69. OnChannelJoined(pClient, pPacket);
  70. break;
  71.  
  72. case 3:
  73. OnChannelChat(pClient, pPacket);
  74. break;
  75. }
  76. }
  77.  
  78. void OnChannelChat(Client pClient, Packet pPacket)
  79. {
  80. if (pClient.User == null) return;
  81. byte id = 0;
  82. string message = "";
  83. if (!pPacket.ReadByte(out id) ||
  84. !pPacket.ReadString(out message))
  85. {
  86. Log.WriteLine(ELogLevel.Warn, "Could not read channelchat message from {0}", pClient.User.Username);
  87. return;
  88. }
  89. if (message.StartsWith("/"))
  90. {
  91. //command
  92. HandleUserCommand(pClient, message.Substring(1, message.Length - 1));
  93. return;
  94. }
  95. //TODO: filter?
  96. if (id > Program.Channels.Count) return; //invalid channel id
  97. Channel chan = Program.Channels.Find(d => d.ID == id);
  98. if (chan == null) return;
  99. Packet chat = PacketCreator.ChannelChat(id, pClient.User.Nick, message);
  100. foreach (User user in chan.Users)
  101. {
  102. user.mClient.SendPacket(chat);
  103. }
  104. }
  105.  
  106. void HandleUserCommand(Client pClient, string pCommand)
  107. {
  108. if (pClient.User == null) return;
  109. string[] command = pCommand.Split(' ');
  110. switch (command[0].ToLower())
  111. {
  112. default:
  113. Console.WriteLine("Invalid command used {0}", command[0]);
  114. break;
  115. case "nick":
  116. if (command.Length < 2) return;
  117. //todo: duplicate nicks!
  118. string oldnick = pClient.User.Nick;
  119. pClient.User.Nick = command[1];
  120. foreach (Channel chan in pClient.User.Channels)
  121. {
  122. Packet newnick = PacketCreator.ChannelInfo(chan.ID, oldnick + " is now known as " + command[1], 1);
  123. Packet userlist = PacketCreator.UserList(chan.ID, chan.Users);
  124. foreach (User chanuser in chan.Users)
  125. {
  126. chanuser.mClient.SendPacket(newnick);
  127. chanuser.mClient.SendPacket(userlist);
  128. }
  129. }
  130. break;
  131. }
  132. }
  133.  
  134. void OnAuthenciateRequest(Client pClient, Packet pPacket)
  135. {
  136. string username = "";
  137. string password = "";
  138. if (!pPacket.ReadString(out username) ||
  139. !pPacket.ReadString(out password))
  140. {
  141. Log.WriteLine(ELogLevel.Warn, "Someone messed with authenciation");
  142. pClient.Disconnect();
  143. }
  144. //TODO: authenciate correctly
  145. User mUser = new User();
  146. mUser.Username = username;
  147. mUser.Nick = username;
  148. pClient.SetUser(mUser);
  149. }
  150.  
  151. void OnChannelJoined(Client pClient, Packet pPacket)
  152. {
  153. if (pClient.User == null) return;
  154. byte ID = 0xff;
  155. if (!pPacket.ReadByte(out ID))
  156. {
  157. Log.WriteLine(ELogLevel.Warn, "Could not read joining channel ID");
  158. return;
  159. }
  160. if(ID > Program.Channels.Count){
  161. Log.WriteLine(ELogLevel.Warn, "Player joined channel out of range");
  162. return;
  163. }
  164. Channel joined = Program.Channels[ID];
  165. joined.AddUser(pClient.User);
  166. Log.WriteLine(ELogLevel.Debug, "Client joined {0}", joined.Name);
  167. }
  168.  
  169. void nClient_OnDisconnect(Client pClient)
  170. {
  171. RemoveConnection(pClient);
  172. if (pClient.User != null)
  173. {
  174. if (pClient.User.Channels.Count > 0)
  175. pClient.User.Channels.ForEach(d => d.RemoveUser(pClient.User));
  176. }
  177. Log.WriteLine(ELogLevel.Info, "Client disconnected {0}", pClient.Host);
  178. }
  179.  
  180. public void RemoveConnection(Client pCon)
  181. {
  182. lock (Connections)
  183. {
  184. Connections.Remove(pCon);
  185. }
  186. }
  187.  
  188. public void AddConnection(Client pCon)
  189. {
  190. lock (Connections)
  191. {
  192. Connections.Add(pCon);
  193. }
  194. }
  195.  
  196. public bool Stop()
  197. {
  198. if (!Listening) return true;
  199. try
  200. {
  201. mListener.Close();
  202. return true;
  203. }
  204. catch (SocketException ex)
  205. {
  206. Log.WriteLine(ELogLevel.Exception, ex.Message);
  207. return false;
  208. }
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement