MainClient.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using ErgometerLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace ErgometerDoctorApplication
  10. {
  11. class MainClient
  12. {
  13. public static TcpClient Server { get; set; }
  14. public static bool loggedin;
  15. private static Thread t;
  16. private static bool running;
  17. public static int Session;
  18. public static string HOST = "127.0.0.1";
  19. public static int PORT = 8888;
  20. //Server information
  21. public static List<ClientThread> clients;
  22. public static Dictionary<string, string> users;
  23. public static List<Tuple<string, double, int>> oldSessionsData;
  24. public static void RemoveActiveClient(ClientThread clientThread)
  25. {
  26. clients.Remove(clientThread);
  27. }
  28. public static Dictionary<int, string> activesessions;
  29. static MainClient()
  30. {
  31. Server = new TcpClient();
  32. loggedin = false;
  33. clients = new List<ClientThread>();
  34. users = new Dictionary<string, string>();
  35. activesessions = new Dictionary<int, string>();
  36. oldSessionsData = new List<Tuple<string, double, int>>();
  37. }
  38. public static bool Connect(string password, out string error)
  39. {
  40. error = "Succes";
  41. if (Server == null || !Server.Connected)
  42. {
  43. if (Server == null)
  44. Server = new TcpClient();
  45. try
  46. {
  47. Server.Connect(HOST, PORT);
  48. }
  49. catch (Exception e)
  50. {
  51. error = "Server is niet online.";
  52. return false;
  53. }
  54. NetCommand net = NetHelper.ReadNetCommand(Server);
  55. if (net.Type == NetCommand.CommandType.SESSION)
  56. Session = net.Session;
  57. else
  58. throw new Exception("Session not assigned");
  59. running = true;
  60. t = new Thread(run);
  61. t.IsBackground = true;
  62. t.Start();
  63. }
  64. if (!loggedin)
  65. {
  66. NetCommand command = new NetCommand("Doctor0tVfW", true, password, Session);
  67. NetHelper.SendNetCommand(Server, command);
  68. NetCommand response = NetHelper.ReadNetCommand(Server);
  69. if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
  70. {
  71. loggedin = false;
  72. error = "Het wachtwoord is onjuist.";
  73. return false;
  74. }
  75. loggedin = true;
  76. }
  77. SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, Session));
  78. Thread.Sleep(15);
  79. SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, Session));
  80. Thread.Sleep(200);
  81. return true;
  82. }
  83. public static void Disconnect()
  84. {
  85. if (Server != null && Server.Connected)
  86. {
  87. NetHelper.SendNetCommand(Server, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
  88. loggedin = false;
  89. running = false;
  90. Server.Close();
  91. Server = null;
  92. }
  93. }
  94. public static void run()
  95. {
  96. while (running)
  97. {
  98. if (loggedin && Server.Connected && Server.Available > 0)
  99. {
  100. NetCommand command = NetHelper.ReadNetCommand(Server);
  101. HandleNetCommand(command);
  102. }
  103. }
  104. if(Server != null)
  105. Server.Close();
  106. }
  107. private static bool UsersBeingSent = false;
  108. private static int UsersSent = 0;
  109. private static int UsersLength = 0;
  110. private static bool SessionsBeingSent = false;
  111. private static int SessionsSent = 0;
  112. private static int SessionsLength = 0;
  113. private static bool ActiveSessionsBeingSent = false;
  114. private static int ActiveSessionsSent = 0;
  115. private static int ActiveSessionsLength = 0;
  116. private static void HandleNetCommand(NetCommand command)
  117. {
  118. switch (command.Type)
  119. {
  120. case NetCommand.CommandType.LENGTH:
  121. switch (command.Length)
  122. {
  123. case NetCommand.LengthType.USERS:
  124. users.Clear();
  125. UsersBeingSent = true;
  126. UsersSent = 0;
  127. UsersLength = command.LengthValue;
  128. break;
  129. case NetCommand.LengthType.SESSIONS:
  130. oldSessionsData.Clear();
  131. SessionsBeingSent = true;
  132. SessionsSent = 0;
  133. SessionsLength = command.LengthValue;
  134. break;
  135. case NetCommand.LengthType.SESSIONDATA:
  136. activesessions.Clear();
  137. ActiveSessionsBeingSent = true;
  138. ActiveSessionsSent = 0;
  139. ActiveSessionsLength = command.LengthValue;
  140. break;
  141. default:
  142. throw new FormatException("Error in NetCommand: Length type is not recognised");
  143. }
  144. break;
  145. case NetCommand.CommandType.ERROR:
  146. Console.WriteLine("An error occured, ignoring");
  147. break;
  148. case NetCommand.CommandType.USER:
  149. if(UsersBeingSent)
  150. {
  151. users.Add(command.DisplayName, command.Password);
  152. UsersSent++;
  153. if (UsersSent >= UsersLength)
  154. UsersBeingSent = false;
  155. }
  156. break;
  157. case NetCommand.CommandType.SESSIONDATA:
  158. if (ActiveSessionsBeingSent)
  159. {
  160. activesessions.Add(command.Session, command.DisplayName);
  161. ActiveSessionsSent++;
  162. if (ActiveSessionsSent >= ActiveSessionsLength)
  163. ActiveSessionsBeingSent = false;
  164. }
  165. if (SessionsBeingSent)
  166. {
  167. oldSessionsData.Add(new Tuple<string, double, int>(command.DisplayName, command.Timestamp, command.Session));
  168. SessionsSent++;
  169. if (SessionsSent >= SessionsLength)
  170. SessionsBeingSent = false;
  171. }
  172. break;
  173. default:
  174. HandToClient(command);
  175. break;
  176. }
  177. }
  178. private static void HandToClient(NetCommand command)
  179. {
  180. foreach (ClientThread cl in clients)
  181. {
  182. if (cl.Session == command.Session)
  183. {
  184. cl.HandleCommand(command);
  185. }
  186. }
  187. }
  188. public static void SendNetCommand(NetCommand command)
  189. {
  190. if(! NetHelper.SendNetCommand(Server, command))
  191. {
  192. Disconnect();
  193. }
  194. }
  195. private static bool IsSessionRunning(int session)
  196. {
  197. foreach (ClientThread cl in clients)
  198. {
  199. if (cl.Session == session)
  200. return true;
  201. }
  202. return false;
  203. }
  204. public static void StartNewClient(string name, int session)
  205. {
  206. if (IsSessionRunning(session))
  207. return;
  208. //Start new client
  209. ClientThread cl = new ClientThread(name, session, false);
  210. clients.Add(cl);
  211. //Run client on new thread
  212. Thread thread = new Thread(new ThreadStart(cl.run));
  213. thread.IsBackground = true;
  214. thread.Start();
  215. }
  216. public static void StartOldClient(string name, int session)
  217. {
  218. if (IsSessionRunning(session))
  219. return;
  220. SendNetCommand(new NetCommand(NetCommand.RequestType.OLDDATA, session));
  221. SendNetCommand(new NetCommand(NetCommand.RequestType.CHAT, session));
  222. //Start new client
  223. ClientThread cl = new ClientThread(name, session, true);
  224. clients.Add(cl);
  225. //Run client on new thread
  226. Thread thread = new Thread(new ThreadStart(cl.run));
  227. thread.IsBackground = true;
  228. thread.Start();
  229. }
  230. }
  231. }