TCPConnection.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 receive()
  58. {
  59. while (true)
  60. {
  61. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  62. serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  63. string response = Encoding.ASCII.GetString(bytesFrom);
  64. string[] response_parts = response.Split('|');
  65. if (response_parts.Length > 0)
  66. {
  67. switch (response_parts[0])
  68. {
  69. case "0": //login and display correct window after login
  70. if (response_parts.Length == 4)
  71. {
  72. SendGet(1);
  73. if (response_parts[1] == "1" && response_parts[2] == "1")
  74. {
  75. currentData = new CurrentData(userID);
  76. currentData.isDoctor = true;
  77. }
  78. else if (response_parts[2] == "0" && response_parts[1] == "1")
  79. {
  80. currentData = new CurrentData(userID);
  81. currentData.isDoctor = false;
  82. }
  83. else
  84. new Login("Geen gebruiker gevonden");
  85. }
  86. break;
  87. case "1":
  88. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  89. if (currentData.isDoctor == true)
  90. {
  91. Form activeForm = Form.ActiveForm;
  92. activeForm.Invoke((MethodInvoker) delegate()
  93. {
  94. DoctorForm doctorForm = new DoctorForm(this);
  95. activeForm.Hide();
  96. doctorForm.Show();
  97. });
  98. }
  99. else
  100. {
  101. Form activeForm = Form.ActiveForm;
  102. activeForm.Invoke((MethodInvoker)delegate ()
  103. {
  104. PatientForm patientForm = new PatientForm(this);
  105. activeForm.Hide();
  106. patientForm.Show();
  107. });
  108. }
  109. break;
  110. case "2":
  111. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  112. break;
  113. case "7":
  114. string[] data = { response_parts[1], response_parts[2], response_parts[3] };
  115. SendChatMessage(data);
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. public void SendLogin(string username, string password)
  122. {
  123. // send command ( cmdID | username | password )
  124. this.userID = username;
  125. SendString("0|" + username + "|" + password + "|");
  126. }
  127. public void SendGet(int GetWhat)
  128. {
  129. // send command ( cmdID | username )
  130. SendString(GetWhat + "|" + userID + "|");
  131. }
  132. public void SendNewSession()
  133. {
  134. // send command ( cmdID | username )
  135. SendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()) + "|");
  136. }
  137. public void SendNewMeasurement()
  138. {
  139. // send command ( cmdID | username )
  140. SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()) + "|");
  141. }
  142. public void SendChatMessage(string[] data)
  143. {
  144. String receiverID = data[0];
  145. if (currentData != null)
  146. {
  147. if (currentData.GetUserID() == receiverID)
  148. {
  149. String message = data[1];
  150. // send command ( cmdID | username sender | username patient | message )
  151. string protocol = "6 | " + this.userID + " | " + receiverID + " | " + message;
  152. SendString(protocol);
  153. }
  154. }
  155. }
  156. public void SendDistance(int distance)
  157. {
  158. SendString("10|" + userID + "|" + distance + "|");
  159. }
  160. public void SendTime(int Minutes, int seconds)
  161. {
  162. SendString("11|" + userID + "|" + Minutes + ":" + seconds + "|");
  163. }
  164. public void SendPower(int power)
  165. {
  166. SendString("12|" + userID + "|" + power + "|");
  167. }
  168. public void SendString(string s)
  169. {
  170. byte[] b = Encoding.ASCII.GetBytes(s);
  171. serverStream.Write(b, 0, b.Length);
  172. serverStream.Flush();
  173. }
  174. }
  175. }