TCPConnection.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using FietsClient.JSONObjecten;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. namespace FietsClient
  11. {
  12. public class TcpConnection
  13. {
  14. public TcpClient client;
  15. public bool isConnectedFlag { private set; get; }
  16. private NetworkStream serverStream;
  17. public CurrentData currentData { private set; get; }
  18. public string userID { private set; get; }
  19. private Thread receiveThread;
  20. public delegate void ChatmassegeDelegate(string[] data);
  21. public event ChatmassegeDelegate IncomingChatmessageEvent;
  22. public TcpConnection()
  23. {
  24. client = new TcpClient();
  25. connect();
  26. }
  27. private void onIncomingChatMessage(string[] data)
  28. {
  29. ChatmassegeDelegate cMD = IncomingChatmessageEvent;
  30. if (cMD != null)
  31. {
  32. cMD(data);
  33. }
  34. }
  35. public bool isConnected()
  36. {
  37. return isConnectedFlag;
  38. }
  39. public void connect()
  40. {
  41. try
  42. {
  43. client.Connect("127.0.0.1", 1288);
  44. // create streams
  45. serverStream = client.GetStream();
  46. receiveThread = new Thread(receive);
  47. receiveThread.Start();
  48. isConnectedFlag = true;
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine(ex);
  53. Thread.Sleep(1000);
  54. isConnectedFlag = false;
  55. }
  56. }
  57. public void disconnect()
  58. {
  59. receiveThread.Abort();
  60. serverStream.Close();
  61. client.Close();
  62. isConnectedFlag = false;
  63. }
  64. public void receive()
  65. {
  66. while (true)
  67. {
  68. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  69. serverStream.Read(bytesFrom, 0, client.ReceiveBufferSize);
  70. string response = Encoding.ASCII.GetString(bytesFrom);
  71. string[] response_parts = response.Split('|');
  72. if (response_parts.Length > 0)
  73. {
  74. switch (response_parts[0])
  75. {
  76. case "0": //login and display correct window after login
  77. if (response_parts.Length == 4)
  78. {
  79. SendGet(1);
  80. if (response_parts[1] == "1" && response_parts[2] == "1")
  81. {
  82. currentData = new CurrentData(userID);
  83. currentData.isDoctor = true;
  84. }
  85. else if (response_parts[2] == "0" && response_parts[1] == "1")
  86. {
  87. currentData = new CurrentData(userID);
  88. currentData.isDoctor = false;
  89. }
  90. else
  91. new Login("Geen gebruiker gevonden");
  92. }
  93. break;
  94. case "1":
  95. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  96. if (currentData.isDoctor == true)
  97. {
  98. Form activeForm = Form.ActiveForm;
  99. activeForm.Invoke((MethodInvoker)delegate ()
  100. {
  101. DoctorForm doctorForm = new DoctorForm(this);
  102. activeForm.Hide();
  103. doctorForm.Show();
  104. });
  105. }
  106. else
  107. {
  108. Form activeForm = Form.ActiveForm;
  109. if (activeForm != null)
  110. {
  111. activeForm.Invoke((MethodInvoker)delegate ()
  112. {
  113. PatientForm patientForm = new PatientForm(this);
  114. activeForm.Hide();
  115. patientForm.Show();
  116. });
  117. }
  118. }
  119. break;
  120. case "2":
  121. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  122. break;
  123. case "7":
  124. // sender receiver message
  125. onIncomingChatMessage(new string[] { response_parts[1], response_parts[2], response_parts[3].TrimEnd('\0') } );
  126. break;
  127. case "8":
  128. if (response_parts[1].TrimEnd('\0') != "-1")
  129. {
  130. DoctorModel.doctorModel.onlinePatients = response_parts[1].TrimEnd('\0').Split('\t').ToList();
  131. } else if (response_parts[1].TrimEnd('\0') == "-1")
  132. {
  133. DoctorModel.doctorModel.onlinePatients = new List<String>();
  134. }
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. public void SendLogin(string username, string password)
  141. {
  142. // send command ( cmdID | username | password )
  143. this.userID = username;
  144. SendString("0|" + username + "|" + password + "|");
  145. }
  146. public void SendGet(int GetWhat)
  147. {
  148. // send command ( cmdID | username )
  149. SendString(GetWhat + "|" + userID + "|");
  150. }
  151. public void SendNewSession()
  152. {
  153. // send command ( cmdID | username )
  154. SendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()) + "|");
  155. }
  156. public void SendNewMeasurement()
  157. {
  158. // send command ( cmdID | username )
  159. SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()) + "|");
  160. }
  161. public void SendChatMessage(string[] data)
  162. {
  163. String receiverID = data[1];
  164. if (currentData != null)
  165. {
  166. String message = data[0];
  167. // send command ( cmdID | username sender | username receiverID | message )
  168. string protocol = "6|" + this.userID + "|" + receiverID + "|" + message;
  169. SendString(protocol);
  170. }
  171. }
  172. public void SendGetActivePatients()
  173. {
  174. SendString("8|" + userID + "|");
  175. }
  176. public void SendDistance(int distance)
  177. {
  178. SendString("10|" + userID + "|" + distance + "|");
  179. }
  180. public void SendTime(int Minutes, int seconds)
  181. {
  182. SendString("11|" + userID + "|" + Minutes + ":" + seconds + "|");
  183. }
  184. public void SendPower(int power)
  185. {
  186. SendString("12|" + userID + "|" + power + "|");
  187. }
  188. public void SendString(string s)
  189. {
  190. byte[] b = Encoding.ASCII.GetBytes(s);
  191. serverStream.Write(b, 0, b.Length);
  192. serverStream.Flush();
  193. }
  194. }
  195. }