TCPConnection.cs 6.5 KB

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