Server.cpp 5.4 KB

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