Network

class Message

A class for serializing and deserializing messages using DataBuffer.

Public Types

enum class Type

Enum to represent the type of message.

Values:

enumerator Int
enumerator String
enumerator Double

Public Functions

Message(int type)

Constructor for Message.

Parameters:

type – The type of the message.

Message &operator<<(const std::string &str)

Serialize data into the message.

Template Parameters:

T – The type of the data to serialize.

Parameters:

data – The data to serialize.

Returns:

Reference to the current message for chaining.

template<typename T>
inline Message &operator<<(const T &data)
Message operator>>(std::string &str) const

Deserialize data from the message.

Template Parameters:

T – The type of the data to deserialize.

Parameters:

data – The variable to hold deserialized data.

Throws:

std::runtime_error – if the deserialization fails.

Returns:

Reference to the current message for chaining.

template<typename T>
inline Message operator>>(T &data) const
int type() const

Returns the type of the message.

Returns:

The type of the message.

std::vector<uint8_t> getSerializedData() const

Returns the serialized data.

Returns:

The serialized data.

Public Static Functions

static std::vector<Message> deserializeMessages(const std::vector<uint8_t> &data)

Deserialize a buffer into multiple Message objects.

Parameters:

data – The raw data buffer containing one or more serialized messages.

Throws:

std::runtime_error – if the data is invalid or incomplete.

Returns:

A vector of deserialized Message objects.

Private Members

Type message_type_

The type of the message.

mutable DataBuffer buffer_

The data buffer for serialization.

class Client

Represents a network client capable of connecting to a server, sending messages, and handling server responses.

Public Functions

Client()

Default constructor.

~Client()

Destructor, ensures proper cleanup.

void connect(const std::string &address, const size_t &port)

Connects the client to a specific server.

Parameters:
  • address – The server address to connect to.

  • port – The server port to connect to.

Throws:

std::runtime_error – if connection fails.

void disconnect()

Disconnects the client from the server.

void defineAction(const Message::Type &messageType, const std::function<void(const Message&)> &action)

Subscribes an action to a specific message type.

Parameters:
  • messageType – The type of message to handle.

  • action – The action to execute for the given message type.

void defineAction(const int messageType, const std::function<void(const Message&)> &action)
void send(const Message &message)

Sends a message to the server.

Parameters:

message – The message to send.

void update()

Processes all received messages and executes their associated actions.

Private Members

int sock_fd_
bool is_connected_
Observer<Message::Type, const Message&> message_observer_

Observer for message type actions.

class Server

Represents a network server capable of managing multiple clients, routing messages, and executing actions.

Public Functions

Server()

Default constructor.

~Server()

Destructor, ensures proper cleanup.

void start(const size_t &port)

Starts the server at the specified port.

Parameters:

port – The port to listen on.

Throws:

std::runtime_error – if the server fails to start.

void defineAction(const Message::Type &messageType, const std::function<void(long long&, const Message&)> &action)

Subscribes an action to a specific message type.

Parameters:
  • messageType – The type of message to handle.

  • action – The action to execute for the given message type.

void defineAction(const int messageType, const std::function<void(long long&, const Message&)> &action)
void sendTo(const Message &message, long long clientID)

Sends a message to a specific client.

Parameters:
  • message – The message to send.

  • clientID – The ID of the client to send the message to.

void sendToArray(const Message &message, const std::vector<long long> &clientIDs)

Sends a message to multiple clients.

Parameters:
  • message – The message to send.

  • clientIDs – A vector of client IDs to send the message to.

void sendToAll(const Message &message)

Sends a message to all connected clients.

Parameters:

message – The message to send.

void update()

Processes all received messages and executes their associated actions.

Private Functions

void _acceptLoop()

Thread function for accepting new clients.

void _clientHandler(long long clientID)

Thread function for handling a specific client.

void _removeClient(long long clientID)

Thread function for removing existing clients.

Private Members

int server_fd_

Server’s listening socket file descriptor.

std::mutex client_mutex_

Protects access to the client_sockets_ map.

std::unordered_map<long long, int> client_sockets_

Mapping of client IDs to their socket descriptors.

std::set<long long> connected_clients_

List of active clients.

Observer<Message::Type, long long&, const Message&> message_observer_

Observer for message type actions.

PersistentWorker worker_

Worker for accepting connections.

long long id_count_

ID count.