Server.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <iostream>
  2. #include <cstring>
  3. #include <thread>
  4. #include <mutex>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <unistd.h>
  8. #include <poll.h>
  9. #include "server/network/Server.h"
  10. #include "server/network/Socket.h"
  11. Server::Server(const Socket& connectionListener, u16 maxClients, GameServer& gameServer) :
  12. connectionListener(connectionListener), gameServer(gameServer), running(true),
  13. listenerThread(&Server::listenForClients, this) {
  14. (void) maxClients;
  15. }
  16. Server::~Server() {
  17. running = false;
  18. listenerThread.join();
  19. }
  20. void Server::listenForClients() const {
  21. while(running) {
  22. WaitResult result = connectionListener.waitForConnection(100);
  23. if(result == WaitResult::ERROR) {
  24. break;
  25. } else if(result == WaitResult::TIMEOUT) {
  26. continue;
  27. }
  28. int clientSocket = connectionListener.acceptConnection();
  29. if(clientSocket < 0) {
  30. continue;
  31. }
  32. /*if(addClient(clientSocket)) {
  33. if(close(clientSocket) == -1) {
  34. printError("cannot close client socket");
  35. }
  36. }*/
  37. }
  38. }
  39. /*static void printError(const char* message) {
  40. std::cout << message << ": " << strerror(errno) << "\n";
  41. }
  42. struct ConnectedClient final {
  43. ~ConnectedClient() {
  44. if(socket != -1) {
  45. if(shutdown(socket, SHUT_RDWR)) {
  46. printError("cannot shutdown client socket");
  47. }
  48. if(close(socket) == -1) {
  49. printError("cannot close client socket");
  50. }
  51. }
  52. if(th.joinable()) {
  53. th.join();
  54. } else {
  55. std::cout << "cannot join client connection thread\n";
  56. }
  57. }
  58. std::thread th = std::thread([]() {
  59. });
  60. int socket = -1;
  61. };
  62. struct InternServer final {
  63. InternServer() : clients(nullptr) {
  64. }
  65. ~InternServer() {
  66. if(clients != nullptr) {
  67. delete[] clients;
  68. }
  69. }
  70. ConnectedClient* clients;
  71. };
  72. static InternServer server;
  73. static void defaultFullServerClientConnect(int) {
  74. std::cout << "default onFullServerClientConnectFunction\n";
  75. }
  76. static void defaultClientConnect(int) {
  77. std::cout << "default onClientConnectFunction\n";
  78. }
  79. static void defaultClientPackage(int, Stream&) {
  80. std::cout << "default onClientPackageFunction\n";
  81. }
  82. static void defaultClientDisconnect(int) {
  83. std::cout << "default onClientDisconnectFunction\n";
  84. }
  85. static std::mutex clientMutex;
  86. static Server::FullServerClientConnectFunction onFullServerClientConnect = defaultFullServerClientConnect;
  87. static Server::ClientConnectFunction onClientConnect = defaultClientConnect;
  88. static Server::ClientPackageFunction onClientPackage = defaultClientPackage;
  89. static Server::ClientDisconnectFunction onClientDisconnect = defaultClientDisconnect;
  90. void Server::setFullServerClientConnectFunction(Server::FullServerClientConnectFunction f) {
  91. onFullServerClientConnect = f;
  92. }
  93. void Server::setClientConnectFunction(Server::ClientConnectFunction f) {
  94. onClientConnect = f;
  95. }
  96. void Server::setClientPackageFunction(Server::ClientPackageFunction f) {
  97. onClientPackage = f;
  98. }
  99. void Server::setClientDisconnectFunction(Server::ClientDisconnectFunction f) {
  100. onClientDisconnect = f;
  101. }
  102. static void listenOnClient(ConnectedClient& cc) {
  103. // poll data
  104. struct pollfd fds;
  105. fds.fd = cc.socket; // file descriptor for polling
  106. fds.events = POLLIN; // wait until data is ready to read
  107. fds.revents = 0; // return events - none
  108. //onClientConnect(cc.socket);
  109. Stream st;
  110. while(server.shouldRun) {
  111. // nfds_t - 1 - amount of passed in structs
  112. // timeout - 100 - milliseconds to wait until an event occurs
  113. // returns 0 on timeout, -1 on error, and >0 on success
  114. int pollData = poll(&fds, 1, 100);
  115. if(pollData > 0) {
  116. st.readSocket(cc.socket);
  117. if(st.hasData()) {
  118. onClientPackage(cc.socket, st);
  119. } else {
  120. // client closed connection
  121. break;
  122. }
  123. } else if(pollData == -1) {
  124. printError("cannot poll from client");
  125. break;
  126. }
  127. }
  128. onClientDisconnect(cc.socket);
  129. // reset slot for another client
  130. if(server.shouldRun) {
  131. std::lock_guard<std::mutex> lg(clientMutex);
  132. if(close(cc.socket) == -1) {
  133. printError("cannot close socket of client");
  134. }
  135. cc.socket = -1;
  136. clientAmount--;
  137. }
  138. }
  139. static bool addClient(int clientSocket) {
  140. std::lock_guard<std::mutex> lg(clientMutex);
  141. if(clientAmount >= maxClients) {
  142. onFullServerClientConnect(clientSocket);
  143. return true;
  144. } else {
  145. // search for free slot
  146. uint16_t index = 0;
  147. while(index < maxClients) {
  148. if(server.clients[index].socket == -1) {
  149. break;
  150. }
  151. index++;
  152. }
  153. if(index >= maxClients) {
  154. std::cout << "cannot find free slot - even if there should be one\n";
  155. return true;
  156. }
  157. //ensure old thread has ended
  158. if(!server.clients[index].th.joinable()) {
  159. std::cout << "cannot join thread of non used client connection\n";
  160. return true;
  161. }
  162. server.clients[index].th.join();
  163. server.clients[index].socket = clientSocket;
  164. server.clients[index].th = std::thread(listenOnClient, std::ref(server.clients[index]));
  165. clientAmount++;
  166. return false;
  167. }
  168. }*/