Server.h 885 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3. #include <thread>
  4. #include <mutex>
  5. #include <atomic>
  6. #include "server/network/IServerListener.h"
  7. class Server
  8. {
  9. private:
  10. struct ConnectedClient
  11. {
  12. ConnectedClient();
  13. std::thread th;
  14. int socket;
  15. };
  16. public:
  17. Server(uint16_t port, uint16_t maxClients, const IServerListener& listener);
  18. virtual ~Server();
  19. bool isRunning() const;
  20. private:
  21. void printError(const char* message) const;
  22. void listenForClients();
  23. bool addClient(int clientSocket);
  24. void listenOnClient(ConnectedClient& cc);
  25. std::atomic_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