TCPConnection.cs.orig 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. using System.IO;
  15. namespace FietsClient
  16. {
  17. public class TcpConnection
  18. {
  19. public TcpClient client;
  20. public bool isConnectedFlag { private set; get; }
  21. private SslStream sslStream;
  22. public CurrentData currentData { private set; get; }
  23. public string userID { private set; get; }
  24. private Thread receiveThread;
  25. public delegate void ChatmassegeDelegate(string[] data);
  26. public event ChatmassegeDelegate IncomingChatmessageEvent;
  27. public TcpConnection()
  28. {
  29. client = new TcpClient();
  30. connect();
  31. }
  32. private void onIncomingChatMessage(string[] data)
  33. {
  34. ChatmassegeDelegate cMD = IncomingChatmessageEvent;
  35. if (cMD != null)
  36. {
  37. cMD(data);
  38. }
  39. }
  40. public bool isConnected()
  41. {
  42. return isConnectedFlag;
  43. }
  44. public void connect()
  45. {
  46. try
  47. {
  48. <<<<<<< HEAD:Proftaak Remote Healthcare/FietsClientV2/TCPConnection.cs
  49. client.Connect("127.0.0.1", 1288);
  50. =======
  51. client.Connect("localhost", 1288);
  52. >>>>>>> origin/bufixes-ketler-reading:Proftaak Remote Healthcare/FietsClientV2/TcpConnection.cs
  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. sslStream.Close();
  110. client.Close();
  111. receiveThread.Abort();
  112. isConnectedFlag = false;
  113. }
  114. public void receive()
  115. {
  116. while (client.Connected)
  117. {
  118. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  119. try
  120. {
  121. sslStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
  122. }
  123. catch (IOException e)
  124. {
  125. Console.WriteLine(e.StackTrace);
  126. break;
  127. }
  128. string response = Encoding.ASCII.GetString(bytesFrom);
  129. string[] response_parts = response.Split('|');
  130. if (response_parts.Length > 0)
  131. {
  132. switch (response_parts[0])
  133. {
  134. case "0": //login and display correct window after login
  135. if (response_parts.Length == 4)
  136. {
  137. if (response_parts[1] == "1" && response_parts[2] == "1")
  138. {
  139. currentData = new CurrentData(userID);
  140. currentData.isDoctor = true;
  141. SendGet(1);
  142. }
  143. else if (response_parts[2] == "0" && response_parts[1] == "1")
  144. {
  145. currentData = new CurrentData(userID);
  146. currentData.isDoctor = false;
  147. SendGet(1);
  148. }
  149. else
  150. new Login("Geen gebruiker gevonden");
  151. }
  152. break;
  153. case "1":
  154. response_parts[1] = response_parts[1].TrimEnd('\0');
  155. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  156. if (currentData.isDoctor == true)
  157. {
  158. Form activeForm = Form.ActiveForm;
  159. activeForm.Invoke((MethodInvoker)delegate ()
  160. {
  161. DoctorForm doctorForm = new DoctorForm(this);
  162. activeForm.Hide();
  163. doctorForm.Show();
  164. });
  165. }
  166. else
  167. {
  168. Form activeForm = Form.ActiveForm;
  169. if (activeForm != null)
  170. {
  171. activeForm.Invoke((MethodInvoker)delegate ()
  172. {
  173. PatientForm patientForm = new PatientForm(this);
  174. activeForm.Hide();
  175. patientForm.Show();
  176. });
  177. }
  178. }
  179. break;
  180. case "2":
  181. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  182. break;
  183. case "7":
  184. // sender receiver message
  185. onIncomingChatMessage(new string[] { response_parts[1], response_parts[2], response_parts[3].TrimEnd('\0') });
  186. break;
  187. case "8":
  188. if (response_parts[1].TrimEnd('\0') != "-1")
  189. {
  190. DoctorModel.doctorModel.onlinePatients = response_parts[1].TrimEnd('\0').Split('\t').ToList();
  191. }
  192. else if (response_parts[1].TrimEnd('\0') == "-1")
  193. {
  194. DoctorModel.doctorModel.onlinePatients = new List<String>();
  195. }
  196. break;
  197. case "9":
  198. JsonConvert.DeserializeObject<List<User>>(response_parts[1]);
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. public void SendLogin(string username, string password)
  205. {
  206. // send command ( cmdID | username | password )
  207. this.userID = username;
  208. SendString("0|" + username + "|" + password + "|");
  209. }
  210. public void SendGet(int GetWhat)
  211. {
  212. // send command ( cmdID | username )
  213. SendString(GetWhat + "|" + userID + "|");
  214. }
  215. public void SendNewSession()
  216. {
  217. // send command ( cmdID | username )
  218. SendString("3|" + userID + FietsLibrary.JsonConverter.SerializeSession(currentData.GetSessions().Last()) + "|");
  219. }
  220. public void SendNewPatient(User user)
  221. {
  222. // send command ( cmdID | username )
  223. SendString("4|" + user.id + "|" + user.password + "|" + user.age + "|" + user.gender + "|" + user.weight + "|");
  224. }
  225. public void SendNewMeasurement()
  226. {
  227. // send command ( cmdID | username )
  228. SendString("5|" + userID + FietsLibrary.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()) + "|");
  229. }
  230. public void SendChatMessage(string[] data)
  231. {
  232. String receiverID = data[1];
  233. if (currentData != null)
  234. {
  235. String message = data[0];
  236. // send command ( cmdID | username sender | username receiverID | message )
  237. string protocol = "6|" + this.userID + "|" + receiverID + "|" + message;
  238. SendString(protocol);
  239. }
  240. }
  241. public void SendGetActivePatients()
  242. {
  243. SendString("8|" + userID + "|");
  244. }
  245. public void SendDistance(int distance)
  246. {
  247. SendString("10|" + userID + "|" + distance + "|");
  248. }
  249. public void SendTime(int Minutes, int seconds)
  250. {
  251. SendString("11|" + userID + "|" + Minutes + ":" + seconds + "|");
  252. }
  253. public void SendPower(int power)
  254. {
  255. SendString("12|" + userID + "|" + power + "|");
  256. }
  257. public void SendString(string s)
  258. {
  259. byte[] b = Encoding.ASCII.GetBytes(s);
  260. sslStream.Write(b, 0, b.Length);
  261. sslStream.Flush();
  262. }
  263. }
  264. }