1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #ifndef SERVER_H
- #define SERVER_H
- #include <thread>
- #include <mutex>
- #include "server/network/IServerListener.h"
- class Server
- {
- private:
- struct ConnectedClient
- {
- ConnectedClient();
- std::thread th;
- int socket;
- };
-
- public:
- Server(uint16_t port, uint16_t maxClients, const IServerListener& listener);
- virtual ~Server();
- bool isRunning() const;
-
- private:
- void printError(const char* message) const;
- void listenForClients();
- bool addClientThreadsafe(int clientSocket);
- bool addClient(int clientSocket);
- void listenOnClient(ConnectedClient& cc);
-
- volatile bool shouldRun;
-
- uint16_t port;
- uint16_t maxClients;
-
- const IServerListener& serverListener;
-
- int listenerSocket;
- std::thread listenerThread;
-
- uint16_t clientAmount;
- ConnectedClient* clients;
- std::mutex clientMutex;
- };
- #endif
|