Server.h 864 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 addClient(int clientSocket);
  23. void listenOnClient(ConnectedClient& cc);
  24. volatile bool shouldRun;
  25. uint16_t port;
  26. uint16_t maxClients;
  27. const IServerListener& serverListener;
  28. int listenerSocket;
  29. std::thread listenerThread;
  30. uint16_t clientAmount;
  31. ConnectedClient* clients;
  32. std::mutex clientMutex;
  33. };
  34. #endif