Midnight Sun Firmware
Loading...
Searching...
No Matches

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...
 
ClientConnectiongetClientByName (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
 

Detailed Description

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

Constructor & Destructor Documentation

◆ Server()

Server ( )

Constructs a Server object.

Initializes the server. The constructor sets up internal variables and prepares the server to accept and communicate

◆ ~Server()

~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

Member Function Documentation

◆ broadcastMessage()

void broadcastMessage ( const std::string &  message)

Function wrapper to broadcast a message to all clients.

Parameters
messageString message value to be sent

◆ epollClientsProcedure()

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

◆ getClientByName()

ClientConnection * getClientByName ( std::string &  clientName)

Get a client by its name.

Parameters
clientNameString value of the clients name
Returns
Pointer to the respective client connection object

◆ listenClients()

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

Parameters
portPort for the server to listen on
messageCallbackFunction pointer to a message callback
connectCallbackFunction pointer to a connection callback

◆ listenNewClientsProcedure()

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

◆ messageReceived()

void messageReceived ( ClientConnection client,
std::string &  message 
)

Function wrapper around the message callback.

Parameters
clientPointer to the client which has received a message
messageString message value that has been received

◆ removeClient()

void removeClient ( ClientConnection client)

Remove a client.

Parameters
clientPointer to the client which shall be removed

◆ sendMessage()

void sendMessage ( ClientConnection client,
const std::string &  message 
)

Function wrapper to transmit a message.

Parameters
clientPointer to the client which shall be written to
messageString message value to be sent

◆ stop()

void stop ( )

Stops the server.

This shall terminate all connections safely, and terminate running threads

◆ updateClientName()

void updateClientName ( ClientConnection client,
std::string  newName 
)

Update a clients name.

Parameters
clientPointer to the client which shall be updated
newNameString value of the clients new name

Member Data Documentation

◆ m_connectCallback

connectCallback m_connectCallback
private

Function pointer to store the connection callback

◆ m_connections

std::unordered_map<std::string, ClientConnection *> m_connections
private

Hash-map to store connections based on their string names

◆ m_epollClientsId

pthread_t m_epollClientsId
private

Thread Id for reading incoming client data

◆ m_epollEvents

struct epoll_event m_epollEvents[MAX_SERVER_EPOLL_EVENTS]
private

An array to store all EPOLL events as bitmasks

◆ m_epollFd

int m_epollFd
private

The servers EPOLL FD to manage reading all clients

◆ m_listeningSocket

int m_listeningSocket
private

The servers listening socket FD

◆ m_listenNewClientsId

pthread_t m_listenNewClientsId
private

Thread Id for listening to new clients

◆ m_listenPort

int m_listenPort
private

The servers port to listen on

◆ m_messageCallback

messageCallback m_messageCallback
private

Function pointer to store the message callback

◆ m_mutex

pthread_mutex_t m_mutex
private

Mutex to protect m_connections map

◆ m_serverAddress

struct sockaddr_in m_serverAddress
private

The servers address

◆ m_serverListening

std::atomic<bool> m_serverListening
private

Boolean flag to indicate the servers status

◆ MAX_CLIENT_READ_SIZE

const constexpr size_t MAX_CLIENT_READ_SIZE = 256U
staticconstexprprivate

Maximum permitted read size for all clients

◆ MAX_SERVER_EPOLL_EVENTS

const constexpr unsigned int MAX_SERVER_EPOLL_EVENTS = 64U
staticconstexprprivate

Maximum permitted EPOLL events for tracking clients


The documentation for this class was generated from the following files: