Midnight Sun Firmware
Loading...
Searching...
No Matches
server.h
1#pragma once
2
3/************************************************************************************************
4 * @file server.h
5 *
6 * @brief Header file defining the Server class
7 *
8 * @date 2025-01-04
9 * @author Aryan Kashem
10 ************************************************************************************************/
11
12/* Standard library Headers */
13#include <atomic>
14#include <functional>
15#include <memory>
16#include <unordered_map>
17#include <vector>
18
19/* Inter-component Headers */
20#include <arpa/inet.h>
21#include <pthread.h>
22#include <sys/epoll.h>
23
24/* Intra-component Headers */
25#include "client_connection.h"
26
39class Server {
40 private:
42 using messageCallback = std::function<void(Server *srv, ClientConnection *src, std::string &)>;
44 using connectCallback = std::function<void(Server *srv, ClientConnection *src)>;
45
46 static const constexpr unsigned int MAX_SERVER_EPOLL_EVENTS = 64U;
47 static const constexpr size_t MAX_CLIENT_READ_SIZE = 256U;
50 pthread_t m_epollClientsId;
51 pthread_mutex_t m_mutex;
56 std::unordered_map<std::string, ClientConnection *> m_connections;
58 std::atomic<bool> m_serverListening;
62 struct sockaddr_in m_serverAddress;
67 public:
73 Server();
74
80 ~Server();
81
90
97
102 void stop();
103
112
118 void messageReceived(ClientConnection *client, std::string &message);
119
125 void sendMessage(ClientConnection *client, const std::string &message);
126
131 void broadcastMessage(const std::string &message);
132
138 void updateClientName(ClientConnection *client, std::string newName);
139
144 void removeClient(ClientConnection *client);
145
151 ClientConnection *getClientByName(std::string &clientName);
152
156 void dumpClientList();
157};
158
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