TCPConnection.cs.orig 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using FietsLibrary.JSONObjecten;
  2. using FietsLibrary;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net.Security;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using System.Security.Cryptography.X509Certificates;
  13. using System.Security.Authentication;
  14. <<<<<<< HEAD
  15. =======
  16. using System.Security.Permissions;
  17. >>>>>>> master
  18. using System.IO;
  19. namespace FietsClient
  20. {
  21. public class TcpConnection
  22. {
  23. public TcpClient client;
  24. public bool isConnectedFlag { private set; get; }
  25. private SslStream sslStream;
  26. public CurrentData currentData { private set; get; }
  27. public string userID { private set; get; }
  28. private Thread receiveThread;
  29. public delegate void ChatmassegeDelegate(string[] data);
  30. public event ChatmassegeDelegate IncomingChatmessageEvent;
  31. public TcpConnection()
  32. {
  33. client = new TcpClient();
  34. connect();
  35. }
  36. private void onIncomingChatMessage(string[] data)
  37. {
  38. ChatmassegeDelegate cMD = IncomingChatmessageEvent;
  39. if (cMD != null)
  40. {
  41. cMD(data);
  42. }
  43. }
  44. public bool isConnected()
  45. {
  46. return isConnectedFlag;
  47. }
  48. public void connect()
  49. {
  50. try
  51. {
  52. client.Connect("brdk.nl", 1288);
  53. // create streams
  54. sslStream = new SslStream(client.GetStream(), false,
  55. new RemoteCertificateValidationCallback(CertificateValidationCallback),
  56. new LocalCertificateSelectionCallback(CertificateSelectionCallback));
  57. bool authenticationPassed = true;
  58. try
  59. {
  60. string serverName = System.Environment.MachineName;
  61. X509Certificate cert = GetServerCert();
  62. X509CertificateCollection certs = new X509CertificateCollection();
  63. certs.Add(cert);
  64. sslStream.AuthenticateAsClient(
  65. serverName,
  66. certs,
  67. SslProtocols.Default,
  68. false); // check cert revokation
  69. }
  70. catch (AuthenticationException)
  71. {
  72. authenticationPassed = false;
  73. }
  74. if (authenticationPassed)
  75. {
  76. receiveThread = new Thread(receive);
  77. receiveThread.Start();
  78. isConnectedFlag = true;
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. Console.WriteLine(ex);
  84. Thread.Sleep(1000);
  85. isConnectedFlag = false;
  86. }
  87. }
  88. static X509Certificate CertificateSelectionCallback(object sender,
  89. string targetHost,
  90. X509CertificateCollection localCertificates,
  91. X509Certificate remoteCertificate,
  92. string[] acceptableIssuers)
  93. {
  94. return localCertificates[0];
  95. }
  96. private X509Certificate GetServerCert()
  97. {
  98. X509Certificate cert = new X509Certificate2(
  99. @"testcert.pfx",
  100. "jancoow");
  101. return cert;
  102. }
  103. private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  104. {
  105. return true;
  106. }
  107. public void disconnect()
  108. {
  109. <<<<<<< HEAD
  110. sslStream.Close();
  111. client.Close();
  112. receiveThread.Abort();
  113. =======
  114. sslStream.Close();
  115. client.Close();
  116. //receiveThread.Abort();
  117. >>>>>>> master
  118. isConnectedFlag = false;
  119. }
  120. public void receive()
  121. {
  122. while (client.Connected)
  123. {
  124. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  125. try
  126. {
  127. sslStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
  128. }
  129. catch (IOException e)
  130. {
  131. <<<<<<< HEAD
  132. Console.WriteLine(e.StackTrace);
  133. break;
  134. }
  135. =======
  136. // debug
  137. Console.WriteLine(e.StackTrace);
  138. break;
  139. }
  140. >>>>>>> master
  141. string response = Encoding.ASCII.GetString(bytesFrom);
  142. string[] response_parts = response.Split('|');
  143. if (response_parts.Length > 0)
  144. {
  145. switch (response_parts[0])
  146. {
  147. case "0": //login and display correct window after login
  148. if (response_parts.Length == 4)
  149. {
  150. if (response_parts[1] == "1" && response_parts[2] == "1")
  151. {
  152. currentData = new CurrentData(userID);
  153. currentData.isDoctor = true;
  154. SendGet(1);
  155. }
  156. else if (response_parts[2] == "0" && response_parts[1] == "1")
  157. {
  158. currentData = new CurrentData(userID);
  159. currentData.isDoctor = false;
  160. SendGet(1);
  161. }
  162. else
  163. new Login("Geen gebruiker gevonden");
  164. }
  165. break;
  166. case "1":
  167. response_parts[1] = response_parts[1].TrimEnd('\0');
  168. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  169. if (currentData.isDoctor == true)
  170. {
  171. Form activeForm = Form.ActiveForm;
  172. activeForm.Invoke((MethodInvoker)delegate ()
  173. {
  174. DoctorForm doctorForm = new DoctorForm(this);
  175. activeForm.Hide();
  176. doctorForm.Show();
  177. });
  178. }
  179. else
  180. {
  181. Form activeForm = Form.ActiveForm;
  182. if (activeForm != null)
  183. {
  184. activeForm.Invoke((MethodInvoker)delegate ()
  185. {
  186. PatientForm patientForm = new PatientForm(this);
  187. activeForm.Hide();
  188. patientForm.Show();
  189. });
  190. }
  191. }
  192. break;
  193. case "2":
  194. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  195. break;
  196. case "7":
  197. // sender receiver message
  198. onIncomingChatMessage(new string[] { response_parts[1], response_parts[2], response_parts[3].TrimEnd('\0') });
  199. break;
  200. case "8":
  201. if (response_parts[1].TrimEnd('\0') != "-1")
  202. {
  203. DoctorModel.doctorModel.onlinePatients = response_parts[1].TrimEnd('\0').Split('\t').ToList();
  204. }
  205. else if (response_parts[1].TrimEnd('\0') == "-1")
  206. {
  207. DoctorModel.doctorModel.onlinePatients = new List<String>();
  208. }
  209. break;
  210. case "9":
  211. JsonConvert.DeserializeObject<List<User>>(response_parts[1]);
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. public void SendLogin(string username, string password)
  218. {
  219. // send command ( cmdID | username | password )
  220. this.userID = username;
  221. SendString("0|" + username + "|" + password + "|");
  222. }
  223. public void SendGet(int GetWhat)
  224. {
  225. // send command ( cmdID | username )
  226. SendString(GetWhat + "|" + userID + "|");
  227. }
  228. public void SendNewSession()
  229. {
  230. // send command ( cmdID | username )
  231. SendString("3|" + userID + FietsLibrary.JsonConverter.SerializeSession(currentData.GetSessions().Last()) + "|");
  232. }
  233. public void SendNewPatient(User user)
  234. {
  235. // send command ( cmdID | username )
  236. SendString("4|" + user.id + "|" + user.password + "|" + user.age + "|" + user.gender + "|" + user.weight + "|");
  237. }
  238. public void SendNewMeasurement()
  239. {
  240. // send command ( cmdID | username )
  241. SendString("5|" + userID + FietsLibrary.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()) + "|");
  242. }
  243. public void SendChatMessage(string[] data)
  244. {
  245. String receiverID = data[1];
  246. if (currentData != null)
  247. {
  248. String message = data[0];
  249. // send command ( cmdID | username sender | username receiverID | message )
  250. string protocol = "6|" + this.userID + "|" + receiverID + "|" + message;
  251. SendString(protocol);
  252. }
  253. }
  254. public void SendGetActivePatients()
  255. {
  256. SendString("8|" + userID + "|");
  257. }
  258. public void SendDistance(int distance)
  259. {
  260. SendString("10|" + userID + "|" + distance + "|");
  261. }
  262. public void SendTime(int Minutes, int seconds)
  263. {
  264. SendString("11|" + userID + "|" + Minutes + ":" + seconds + "|");
  265. }
  266. public void SendPower(int power)
  267. {
  268. SendString("12|" + userID + "|" + power + "|");
  269. }
  270. public void SendString(string s)
  271. {
  272. byte[] b = Encoding.ASCII.GetBytes(s);
  273. sslStream.Write(b, 0, b.Length);
  274. sslStream.Flush();
  275. }
  276. }
  277. }