TCPConnection.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. string[] data = { response_parts[1], response_parts[2], response_parts[3] };
  126. onIncomingChatMessage(data);
  127. break;
  128. case "8":
  129. if (response_parts[1].TrimEnd('\0') != "-1")
  130. {
  131. DoctorModel.doctorModel.onlinePatients = response_parts[1].TrimEnd('\0').Split('\t').ToList();
  132. } else if (response_parts[1].TrimEnd('\0') == "-1")
  133. {
  134. DoctorModel.doctorModel.onlinePatients = new List<String>();
  135. }
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. public void SendLogin(string username, string password)
  142. {
  143. // send command ( cmdID | username | password )
  144. this.userID = username;
  145. SendString("0|" + username + "|" + password + "|");
  146. }
  147. public void SendGet(int GetWhat)
  148. {
  149. // send command ( cmdID | username )
  150. SendString(GetWhat + "|" + userID + "|");
  151. }
  152. public void SendNewSession()
  153. {
  154. // send command ( cmdID | username )
  155. SendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()) + "|");
  156. }
  157. public void SendNewMeasurement()
  158. {
  159. // send command ( cmdID | username )
  160. SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()) + "|");
  161. }
  162. public void SendChatMessage(string[] data)
  163. {
  164. String receiverID = data[1];
  165. if (currentData != null)
  166. {
  167. String message = data[0];
  168. // send command ( cmdID | username sender | username receiverID | message )
  169. string protocol = "6|" + this.userID + "|" + receiverID + "|" + message;
  170. SendString(protocol);
  171. }
  172. }
  173. public void SendGetActivePatients()
  174. {
  175. SendString("8|" + userID + "|");
  176. }
  177. public void SendDistance(int distance)
  178. {
  179. SendString("10|" + userID + "|" + distance + "|");
  180. }
  181. public void SendTime(int Minutes, int seconds)
  182. {
  183. SendString("11|" + userID + "|" + Minutes + ":" + seconds + "|");
  184. }
  185. public void SendPower(int power)
  186. {
  187. SendString("12|" + userID + "|" + power + "|");
  188. }
  189. public void SendString(string s)
  190. {
  191. byte[] b = Encoding.ASCII.GetBytes(s);
  192. serverStream.Write(b, 0, b.Length);
  193. serverStream.Flush();
  194. }
  195. }
  196. }