Server.h 912 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3. #include <thread>
  4. #include <mutex>
  5. #include "server/network/IServerListener.h"
  6. class Server
  7. {
  8. private:
  9. struct ConnectedClient
  10. {
  11. ConnectedClient();
  12. std::thread th;
  13. int socket;
  14. };
  15. public:
  16. Server(uint16_t port, uint16_t maxClients, const IServerListener& listener);
  17. virtual ~Server();
  18. bool isRunning() const;
  19. private:
  20. void printError(const char* message) const;
  21. void listenForClients();
  22. bool addClientThreadsafe(int clientSocket);
  23. bool addClient(int clientSocket);
  24. void listenOnClient(ConnectedClient& cc);
  25. volatile bool shouldRun;
  26. uint16_t port;
  27. uint16_t maxClients;
  28. const IServerListener& serverListener;
  29. int listenerSocket;
  30. std::thread listenerThread;
  31. uint16_t clientAmount;
  32. ConnectedClient* clients;
  33. std::mutex clientMutex;
  34. };
  35. #endif