TCPConnection.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. private NetworkStream serverStream;
  16. private CurrentData currentData;
  17. private string userID;
  18. private bool isConnectedFlag;
  19. public TcpConnection()
  20. {
  21. // create a connection
  22. client = new TcpClient();
  23. connect();
  24. }
  25. public bool isConnected()
  26. {
  27. return isConnectedFlag;
  28. }
  29. public void connect()
  30. {
  31. try
  32. {
  33. client.Connect("127.0.0.1", 1288);
  34. // create streams
  35. serverStream = client.GetStream();
  36. Thread t = new Thread(recieve);
  37. t.Start();
  38. isConnectedFlag = true;
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine(ex);
  43. Thread.Sleep(1000);
  44. isConnectedFlag = false;
  45. }
  46. }
  47. public void recieve ()
  48. {
  49. while (true)
  50. {
  51. byte[] bytesFrom = new byte[(int)client.ReceiveBufferSize];
  52. serverStream.Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
  53. String response = Encoding.ASCII.GetString(bytesFrom);
  54. String[] response_parts = response.Split('|');
  55. if (response_parts.Length > 0)
  56. {
  57. switch (response_parts[0])
  58. {
  59. case "0": //login
  60. if (response_parts.Length == 4)
  61. {
  62. if (response_parts[1] == "1" && response_parts[2] == "1")
  63. {
  64. new DoctorForm().Show();
  65. currentData = new CurrentData(userID);
  66. }
  67. else if(response_parts[2] == "0" && response_parts[1] == "1")
  68. {
  69. PatientForm form = new PatientForm(this);
  70. Form activeForm = Form.ActiveForm;
  71. activeForm.Invoke((MethodInvoker)delegate () {
  72. activeForm.Hide();
  73. form.Show();
  74. });
  75. currentData = new CurrentData(userID);
  76. }
  77. else
  78. {
  79. new Login("Geen gebruiker gevonden");
  80. }
  81. }
  82. break;
  83. case "1":
  84. currentData.setSessionList(JsonConvert.DeserializeObject<List<Session>>(response_parts[1]));
  85. break;
  86. case "2":
  87. currentData.GetSessions().Last().AddMeasurement(JsonConvert.DeserializeObject<Measurement>(response_parts[1]));
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. public void SendLogin(string username, string password)
  94. {
  95. // send command ( cmdID | username | password )
  96. this.userID = username;
  97. SendString("0|" + username + "|" + password + "|");
  98. }
  99. public void SendGet(int GetWhat)
  100. {
  101. // send command ( cmdID | username )
  102. SendString( GetWhat + "|" + userID );
  103. }
  104. public void SendNewSession()
  105. {
  106. // send command ( cmdID | username )
  107. SendString("3|" + userID + lib.JsonConverter.SerializeSession(currentData.GetSessions().Last()));
  108. }
  109. public void SendNewMeasurement()
  110. {
  111. // send command ( cmdID | username )
  112. SendString("5|" + userID + lib.JsonConverter.SerializeLastMeasurement(currentData.GetSessions().Last().GetLastMeasurement()));
  113. }
  114. public void SendString(string s)
  115. {
  116. byte[] b = Encoding.ASCII.GetBytes(s);
  117. serverStream.Write(b, 0, b.Length);
  118. serverStream.Flush();
  119. }
  120. public void SendChatMessage(string message)
  121. {
  122. // send command ( cmdID | username sender | username patient | message )
  123. string protocol = "6 | " + this.userID +" | "/* + idPatient */ + " | " + message;
  124. SendString(protocol);
  125. }
  126. }
  127. }