ClientThread.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using ErgometerLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace ErgometerServer
  11. {
  12. class ClientThread
  13. {
  14. TcpClient client;
  15. Server server;
  16. public string name;
  17. public int session { get; }
  18. bool running;
  19. bool loggedin;
  20. List<Meting> metingen;
  21. List<ChatMessage> chat;
  22. public ClientThread(TcpClient client, Server server)
  23. {
  24. this.client = client;
  25. this.server = server;
  26. this.name = "Unknown";
  27. this.session = 0;
  28. this.running = false;
  29. this.loggedin = false;
  30. metingen = new List<Meting>();
  31. chat = new List<ChatMessage>();
  32. session = FileHandler.GenerateSession();
  33. Console.WriteLine("Generated new session: " + session);
  34. }
  35. public void run()
  36. {
  37. running = true;
  38. NetHelper.SendNetCommand(client, new NetCommand(session));
  39. while (running)
  40. {
  41. NetCommand input = NetHelper.ReadNetCommand(client);
  42. switch(input.Type)
  43. {
  44. case NetCommand.CommandType.SESSION:
  45. break;
  46. case NetCommand.CommandType.LOGIN:
  47. if(! server.CheckPassword(input.DisplayName, input.Password))
  48. {
  49. NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINWRONG, session));
  50. loggedin = false;
  51. }
  52. else
  53. {
  54. NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.LOGINOK, session));
  55. loggedin = true;
  56. if (input.IsDoctor)
  57. {
  58. server.ChangeClientToDoctor(client, this);
  59. Console.WriteLine("Doctor connected");
  60. running = false;
  61. }
  62. else
  63. {
  64. name = input.DisplayName;
  65. loggedin = true;
  66. FileHandler.CreateSession(session, name);
  67. }
  68. }
  69. break;
  70. case NetCommand.CommandType.UITLEG:
  71. server.SendToDoctor(input);
  72. break;
  73. case NetCommand.CommandType.TESTRESULT:
  74. FileHandler.SaveTestResult(input, session);
  75. break;
  76. case NetCommand.CommandType.DATA:
  77. if (loggedin)
  78. {
  79. metingen.Add(input.Meting);
  80. server.SendToDoctor(input);
  81. }
  82. else
  83. NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
  84. break;
  85. case NetCommand.CommandType.CHAT:
  86. if (loggedin)
  87. {
  88. chat.Add(new ChatMessage(name, input.ChatMessage, false));
  89. server.SendToDoctor(input);
  90. }
  91. else
  92. NetHelper.SendNetCommand(client, new NetCommand(NetCommand.ResponseType.NOTLOGGEDIN, session));
  93. break;
  94. case NetCommand.CommandType.LOGOUT:
  95. loggedin = false;
  96. running = false;
  97. Console.WriteLine(name + " logged out");
  98. FileHandler.WriteMetingen(session, metingen);
  99. FileHandler.WriteChat(session, chat);
  100. client.Close();
  101. break;
  102. case NetCommand.CommandType.ERROR:
  103. Console.WriteLine("An error occured, assuming client disconnected");
  104. loggedin = false;
  105. running = false;
  106. Console.WriteLine(name + " logged out due to an error");
  107. FileHandler.WriteMetingen(session, metingen);
  108. FileHandler.WriteChat(session, chat);
  109. client.Close();
  110. break;
  111. default:
  112. if(loggedin)
  113. throw new FormatException("Unknown command");
  114. break;
  115. }
  116. }
  117. server.RemoveActiveSession(this);
  118. }
  119. public void SendToClient(NetCommand command)
  120. {
  121. NetHelper.SendNetCommand(client, command);
  122. if (command.Type == NetCommand.CommandType.CHAT)
  123. {
  124. chat.Add(new ChatMessage("Doctor", command.ChatMessage, true));
  125. }
  126. }
  127. }
  128. }