Midnight Sun Firmware
|
Class that represents the central server that connects to multiple clients. More...
#include <server.h>
Public Member Functions | |
Server () | |
Constructs a Server object. More... | |
~Server () | |
Destructs a Server object. More... | |
void | listenNewClientsProcedure () |
Thread procedure for listening for new client connections. More... | |
void | epollClientsProcedure () |
Thread procedure for reading incoming client data. More... | |
void | stop () |
Stops the server. More... | |
void | listenClients (int port, messageCallback messageCallback, connectCallback connectCallback) |
Initiate the server to listen to clients on a provided port. More... | |
void | messageReceived (ClientConnection *client, std::string &message) |
Function wrapper around the message callback. More... | |
void | sendMessage (ClientConnection *client, const std::string &message) |
Function wrapper to transmit a message. More... | |
void | broadcastMessage (const std::string &message) |
Function wrapper to broadcast a message to all clients. More... | |
void | updateClientName (ClientConnection *client, std::string newName) |
Update a clients name. More... | |
void | removeClient (ClientConnection *client) |
Remove a client. More... | |
ClientConnection * | getClientByName (std::string &clientName) |
Get a client by its name. More... | |
void | dumpClientList () |
Print a list of all connected clients. | |
Private Types | |
using | messageCallback = std::function< void(Server *srv, ClientConnection *src, std::string &)> |
The message callback function definition. | |
using | connectCallback = std::function< void(Server *srv, ClientConnection *src)> |
The connection callback function definition. | |
Private Attributes | |
pthread_t | m_listenNewClientsId |
pthread_t | m_epollClientsId |
pthread_mutex_t | m_mutex |
messageCallback | m_messageCallback |
connectCallback | m_connectCallback |
std::unordered_map< std::string, ClientConnection * > | m_connections |
std::atomic< bool > | m_serverListening |
int | m_listenPort |
int | m_listeningSocket |
struct sockaddr_in | m_serverAddress |
int | m_epollFd |
struct epoll_event | m_epollEvents [MAX_SERVER_EPOLL_EVENTS] |
Static Private Attributes | |
static const constexpr unsigned int | MAX_SERVER_EPOLL_EVENTS = 64U |
static const constexpr size_t | MAX_CLIENT_READ_SIZE = 256U |
Class that represents the central server that connects to multiple clients.
This class is responsible for managing all clients connections, accepting connections, monitoring client activity, and sending messages between the server and the client
Server | ( | ) |
Constructs a Server object.
Initializes the server. The constructor sets up internal variables and prepares the server to accept and communicate
~Server | ( | ) |
Destructs a Server object.
If using TCP this closes all existing socket connections This shall also delete the mutex, and terminate both running threads
void broadcastMessage | ( | const std::string & | message | ) |
Function wrapper to broadcast a message to all clients.
message | String message value to be sent |
void epollClientsProcedure | ( | ) |
Thread procedure for reading incoming client data.
This thread shall be blocked while no new client data is available Upon receiving a message, the message callback shall be called
ClientConnection * getClientByName | ( | std::string & | clientName | ) |
Get a client by its name.
clientName | String value of the clients name |
void listenClients | ( | int | port, |
messageCallback | messageCallback, | ||
connectCallback | connectCallback | ||
) |
Initiate the server to listen to clients on a provided port.
This shall start the listenNewClientsProcedure and epollClientsProcedure
port | Port for the server to listen on |
messageCallback | Function pointer to a message callback |
connectCallback | Function pointer to a connection callback |
void listenNewClientsProcedure | ( | ) |
Thread procedure for listening for new client connections.
This thread shall be blocked while no new clients are available Upon connection, the client shall be declared as non-blocking, and will be added to the EPOLL list as an input FD. Further, the connection callback shall be called
void messageReceived | ( | ClientConnection * | client, |
std::string & | message | ||
) |
Function wrapper around the message callback.
client | Pointer to the client which has received a message |
message | String message value that has been received |
void removeClient | ( | ClientConnection * | client | ) |
Remove a client.
client | Pointer to the client which shall be removed |
void sendMessage | ( | ClientConnection * | client, |
const std::string & | message | ||
) |
Function wrapper to transmit a message.
client | Pointer to the client which shall be written to |
message | String message value to be sent |
void stop | ( | ) |
Stops the server.
This shall terminate all connections safely, and terminate running threads
void updateClientName | ( | ClientConnection * | client, |
std::string | newName | ||
) |
Update a clients name.
client | Pointer to the client which shall be updated |
newName | String value of the clients new name |
|
private |
Function pointer to store the connection callback
|
private |
Hash-map to store connections based on their string names
|
private |
Thread Id for reading incoming client data
|
private |
An array to store all EPOLL events as bitmasks
|
private |
The servers EPOLL FD to manage reading all clients
|
private |
The servers listening socket FD
|
private |
Thread Id for listening to new clients
|
private |
The servers port to listen on
|
private |
Function pointer to store the message callback
|
private |
Mutex to protect m_connections map
|
private |
The servers address
|
private |
Boolean flag to indicate the servers status
|
staticconstexprprivate |
Maximum permitted read size for all clients
|
staticconstexprprivate |
Maximum permitted EPOLL events for tracking clients