Guest User

Untitled

a guest
Apr 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using WORR.Shared;
  6. using WORR.Shared.Network;
  7. using WORR.Shared.Misc;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Data;
  12. using System.Data.SqlClient;
  13. using System.Reflection;
  14. using System.IO;
  15. using WORR.Shared.Database;
  16.  
  17. namespace WORR.LoginServer.Network
  18. {
  19. public partial class loginServer : BaseSocket
  20. {
  21.  
  22. public List<Client> listClient = new List<Client>();
  23. public List<Server> listServer = new List<Server>();
  24. int login_acceslevel;
  25. bool CreateLoginAccount;
  26. bool LogEnable;
  27. string ServerPassword;
  28. string ClientLogPath;
  29. string ServerLogPath;
  30. public Database logindb;
  31.  
  32. public loginServer(string ip, int Client_passive_port, int Server_passive_port, string ServerPass, int Acceslvl, bool CreateLoginAcc, string host_name, string database_name, bool logenable, string logpath)
  33. : base(ip, Client_passive_port, Server_passive_port)
  34. {
  35. Log.log(Log.LogType.MSG_SQL, "Starting database...");
  36. logindb = new Database(host_name, database_name,"root","omgrofl");
  37. Log.log(Log.LogType.MSG_SQL, "Database successfully started...");
  38. login_acceslevel = Acceslvl;
  39. CreateLoginAccount = CreateLoginAcc;
  40. ServerPassword = ServerPass;
  41. LogEnable = logenable;
  42. ClientLogPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Client." + logpath;
  43. ServerLogPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Server." + logpath;
  44. //Read thread start before the first connection
  45. Thread getReadClients = new Thread(new ThreadStart(CgetRead));
  46. getReadClients.Start();
  47. Thread getofflineClients = new Thread(new ThreadStart(CCheckIfStillConnected));
  48. getofflineClients.Start();
  49. Thread Cmainloop = new Thread(new ThreadStart(CAcceptloop));
  50. Cmainloop.Start();
  51.  
  52. Thread getReadServer = new Thread(new ThreadStart(SgetRead));
  53. getReadServer.Start();
  54.  
  55. Thread getofflineServers = new Thread(new ThreadStart(SCheckIfStillConnected));
  56. getofflineServers.Start();
  57.  
  58. Thread Smainloop = new Thread(new ThreadStart(SAcceptloop));
  59. Smainloop.Start();
  60.  
  61. }
  62.  
  63.  
  64.  
  65. //Where servers , channels are handles
  66. #region ServerCore
  67. //Main loop which accept client
  68. void SAcceptloop()
  69. {
  70.  
  71. while (true)
  72. {
  73. CurrentServer = localListenSock.Accept();
  74. SAddServer(CurrentServer);
  75. }
  76.  
  77. }
  78.  
  79. //Add an user when a client is connect
  80. public void SAddServer(Socket sock)
  81. {
  82.  
  83. Server newserver = new Server();
  84. newserver.serversock = new WORRSocket();
  85. newserver.serversock.socket = sock;
  86. listServer.Add(newserver);
  87. Log.log(Log.LogType.MSG_WARNING, "New world server try connect from " + sock.RemoteEndPoint, ServerLogPath);
  88.  
  89. }
  90. //Check if server are still connect
  91. private void SCheckIfStillConnected()
  92. {
  93. while (true)
  94. {
  95.  
  96. for (int i = 0; i < listServer.Count; i++)
  97. {
  98.  
  99. if (listServer[i].serversock.socket.Poll(10, SelectMode.SelectRead) && listServer[i].serversock.socket.Available == 0)
  100. {
  101. if (!ServerReadlock)
  102. {
  103. SDisconnectServer(i);
  104.  
  105. }
  106.  
  107. }
  108. }
  109. Thread.Sleep(5);
  110.  
  111. }
  112.  
  113. }
  114. //Disconnect an user
  115. public void SDisconnectServer(int server_index)
  116. {
  117. Log.log(Log.LogType.MSG_NOTICE, "World Server disconnect (IP : " + listServer[server_index].serversock.socket.RemoteEndPoint + ")", ServerLogPath);
  118. listServer[server_index].serversock.socket.Close();
  119. listServer.RemoveAt(server_index);
  120. /*if (listClient[server_index] != null)
  121. {
  122. listClient.RemoveAt(server_index);
  123. }*/
  124.  
  125. }
  126. //Read all packets
  127. void SgetRead()
  128. {
  129. while (true)
  130. {
  131. ServerReadlock = true;
  132. for (int i = 0; i != listServer.Count; i++)
  133. {
  134. if (listServer[i].serversock.socket.Available > 0)
  135. {
  136.  
  137. Packet serverPacket = listServer[i].serversock.ReceiveData();
  138. OnReceiveSPacket(serverPacket, listServer[i]);
  139.  
  140. }
  141. }
  142. ServerReadlock = false;
  143. Thread.Sleep(10);
  144. }
  145.  
  146. }
  147. //Handle packets
  148. void OnReceiveSPacket(Packet serverPacket, Server server)
  149. {
  150. switch (serverPacket.Command)
  151. {
  152. case 0x500:
  153. pakConnectionRequest(ref server, serverPacket);
  154. break;
  155. case 0x501:
  156. pakGetWorldInfo(ref server, serverPacket);
  157. break;
  158. case 0x502:
  159. pakGetChannelsInfo(ref server, serverPacket);
  160. break;
  161. default:
  162. Log.log(Log.LogType.MSG_WARNING, "Login Server Received unknown packet. Command: " + String.Format("{0:x}", serverPacket.Command) + " Size: " + serverPacket.Size);
  163. break;
  164. }
  165. }
  166.  
  167. #endregion
  168.  
  169.  
  170. #region ClientCore
  171. //Main loop which accept client
  172. void CAcceptloop()
  173. {
  174. while (true)
  175. {
  176. CurrentClient = ClientSock.Accept();
  177. CAddUser(CurrentClient);
  178. }
  179.  
  180. }
  181.  
  182. //Add an user when a client is connect
  183. public void CAddUser(Socket sock)
  184. {
  185.  
  186. Client newuser = new Client();
  187. newuser.clientsock = new WORRSocket();
  188. newuser.clientsock.socket = sock;
  189. listClient.Add(newuser);
  190. Log.log(Log.LogType.MSG_NOTICE, "New client connect (IP : " + newuser.clientsock.socket.RemoteEndPoint.ToString() + ")", ClientLogPath);
  191.  
  192. }
  193. //Check if clients are still connect
  194. private void CCheckIfStillConnected()
  195. {
  196. while (true)
  197. {
  198.  
  199. for (int i = 0; i < listClient.Count; i++)
  200. {
  201.  
  202. if (listClient[i].clientsock.socket.Poll(10, SelectMode.SelectRead) && listClient[i].clientsock.socket.Available == 0)
  203. {
  204. if (!ClientReadlock)
  205. {
  206. CDisconnectUser(i);
  207.  
  208. }
  209.  
  210. }
  211. }
  212. Thread.Sleep(5);
  213.  
  214. }
  215.  
  216. }
  217. //Disconnect an user
  218. public void CDisconnectUser(int user_index)
  219. {
  220. Log.log(Log.LogType.MSG_NOTICE, "Client disconnect (IP : " + listClient[user_index].clientsock.socket.RemoteEndPoint.ToString() + ")", ClientLogPath);
  221. listClient[user_index].clientsock.socket.Close();
  222. listClient.RemoveAt(user_index);
  223. }
  224. //Read all packets
  225. void CgetRead()
  226. {
  227. while (true)
  228. {
  229. ClientReadlock = true;
  230. for (int i = 0; i != listClient.Count; i++)
  231. {
  232. if (listClient[i].clientsock.socket.Available > 0)
  233. {
  234.  
  235. Packet userPacket = listClient[i].clientsock.ReceiveData();
  236. OnReceiveCPacket(userPacket, listClient[i]);
  237.  
  238. }
  239. }
  240. ClientReadlock = false;
  241. Thread.Sleep(10);
  242. }
  243.  
  244. }
  245. //Handle packets
  246. void OnReceiveCPacket(Packet userPacket, Client user)
  247. {
  248. switch (userPacket.Command)
  249. {
  250. case 0x703:
  251. pakEncryptionRequest(user);
  252. break;
  253. case 0x708:
  254. pakUserLogin(ref user, userPacket);
  255. break;
  256. case 0x704:
  257. pakGetChannels(ref user, userPacket);
  258. break;
  259. case 0x70a:
  260. pakGetIP(ref user, userPacket);
  261. break;
  262. default:
  263. Log.log(Log.LogType.MSG_WARNING, "Login Server Received unknown packet. Command: " + String.Format("{0:x}", userPacket.Command) + " Size: " + userPacket.Size);
  264. break;
  265. }
  266. }
  267.  
  268. #endregion
  269.  
  270. //Packets
  271.  
  272. }
  273.  
  274. }
Add Comment
Please, Sign In to add comment