Network
-
class Message
A class for serializing and deserializing messages using
DataBuffer.Public Types
Public Functions
-
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.
-
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.
-
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
Messageobjects.- 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
Messageobjects.
Private Members
-
mutable DataBuffer buffer_
The data buffer for serialization.
-
Message &operator<<(const std::string &str)
-
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 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.
-
Client()
-
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
Private Members
-
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.
-
Server()