16#include <unordered_map>
25#include "client_connection.h"
Class that represents a connection between the server and a client.
Definition: client_connection.h:37
Class that represents the central server that connects to multiple clients.
Definition: server.h:39
ClientConnection * getClientByName(std::string &clientName)
Get a client by its name.
Definition: server.cc:223
std::function< void(Server *srv, ClientConnection *src, std::string &)> messageCallback
The message callback function definition.
Definition: server.h:42
messageCallback m_messageCallback
Definition: server.h:53
void broadcastMessage(const std::string &message)
Function wrapper to broadcast a message to all clients.
Definition: server.cc:213
void listenClients(int port, messageCallback messageCallback, connectCallback connectCallback)
Initiate the server to listen to clients on a provided port.
Definition: server.cc:149
void removeClient(ClientConnection *client)
Remove a client.
Definition: server.cc:171
std::unordered_map< std::string, ClientConnection * > m_connections
Definition: server.h:56
struct sockaddr_in m_serverAddress
Definition: server.h:62
void listenNewClientsProcedure()
Thread procedure for listening for new client connections.
Definition: server.cc:35
void sendMessage(ClientConnection *client, const std::string &message)
Function wrapper to transmit a message.
Definition: server.cc:209
int m_epollFd
Definition: server.h:64
void messageReceived(ClientConnection *client, std::string &message)
Function wrapper around the message callback.
Definition: server.cc:205
pthread_t m_epollClientsId
Definition: server.h:50
pthread_t m_listenNewClientsId
Definition: server.h:49
int m_listeningSocket
Definition: server.h:61
void stop()
Stops the server.
Definition: server.cc:241
std::atomic< bool > m_serverListening
Definition: server.h:58
int m_listenPort
Definition: server.h:60
~Server()
Destructs a Server object.
Definition: server.cc:31
struct epoll_event m_epollEvents[MAX_SERVER_EPOLL_EVENTS]
Definition: server.h:65
static const constexpr size_t MAX_CLIENT_READ_SIZE
Definition: server.h:47
connectCallback m_connectCallback
Definition: server.h:54
void epollClientsProcedure()
Thread procedure for reading incoming client data.
Definition: server.cc:88
std::function< void(Server *srv, ClientConnection *src)> connectCallback
The connection callback function definition.
Definition: server.h:44
void dumpClientList()
Print a list of all connected clients.
Definition: server.cc:233
static const constexpr unsigned int MAX_SERVER_EPOLL_EVENTS
Definition: server.h:46
Server()
Constructs a Server object.
Definition: server.cc:23
pthread_mutex_t m_mutex
Definition: server.h:51
void updateClientName(ClientConnection *client, std::string newName)
Update a clients name.
Definition: server.cc:182