High-level peer-to-peer networking service managing host/client mode, message routing, and network lifecycle operations.
More...
#include <multiplayer_service.h>
High-level peer-to-peer networking service managing host/client mode, message routing, and network lifecycle operations.
The MultiplayerService provides a unified API for switching between client-mode and host-mode, forwarding messages, and polling network events. It wraps ENet initialization and manages both Host and Client instances.
◆ MultiplayerService()
| MultiplayerService::MultiplayerService |
( |
| ) |
|
Initializes ENet and prepares the internal Router.
- Exceptions
-
| std::runtime_error | if ENet initialization fails. |
◆ connect()
| void MultiplayerService::connect |
( |
const std::string & |
address | ) |
|
Connect to a remote server. Only valid in client-mode.
◆ disconnect()
| void MultiplayerService::disconnect |
( |
std::function< void()> |
on_disconnected = nullptr | ) |
|
Disconnect the active host or client gracefully.
- Exceptions
-
| std::runtime_error | if not acting as host or client. |
◆ get_client_amount()
| int MultiplayerService::get_client_amount |
( |
| ) |
const |
|
noexcept |
◆ get_connection_port()
| int MultiplayerService::get_connection_port |
( |
| ) |
const |
|
noexcept |
◆ get_connection_state()
Returns connection state of the current mode.
◆ get_host_ip()
| std::string MultiplayerService::get_host_ip |
( |
| ) |
const |
◆ get_peer_type()
| PeerType MultiplayerService::get_peer_type |
( |
| ) |
const |
Returns the current peer type (host, client, or none).
- Returns
- PeerType indicating the current mode of operation.
◆ get_uuid()
| std::string MultiplayerService::get_uuid |
( |
| ) |
const |
◆ poll()
| void MultiplayerService::poll |
( |
| ) |
|
Polls the network. Behavior depends on host/client mode.
◆ register_handler()
| void MultiplayerService::register_handler |
( |
MessageType |
type, |
|
|
const std::function< void(const Message &)> & |
handler |
|
) |
| |
Registers a callback handler for a specific message type.
- Parameters
-
| type | Message type |
| handler | Function invoked on message reception |
◆ send()
| void MultiplayerService::send |
( |
const Message & |
message | ) |
|
Sends a message. Host broadcasts; client sends to server.
◆ send_to_peer_via_uuid()
| void MultiplayerService::send_to_peer_via_uuid |
( |
const std::string & |
uuid, |
|
|
const Message & |
message |
|
) |
| |
Sends a message to a specific connected client (host mode only).
- Parameters
-
| uuid | Target client's UUID. |
| message | Message to send. |
◆ set_client()
| void MultiplayerService::set_client |
( |
| ) |
|
Switches the service into client-mode. Invalidates any host instance.
- Exceptions
-
| std::runtime_error | if the current host has active clients or running server. |
◆ set_connection_port()
| void MultiplayerService::set_connection_port |
( |
int |
port | ) |
|
|
noexcept |
◆ set_host()
| void MultiplayerService::set_host |
( |
| ) |
|
Switches the service into host-mode. Invalidates any client instance.
- Exceptions
-
| std::runtime_error | if the current client has an active connection. |
◆ set_max_clients()
| void MultiplayerService::set_max_clients |
( |
int |
amount | ) |
|
|
noexcept |
◆ start_server()
| void MultiplayerService::start_server |
( |
| ) |
|
Starts the host server. Only valid in host-mode.
- Exceptions
-
| std::runtime_error | if called while acting as client. |
◆ unregister_handler()
| void MultiplayerService::unregister_handler |
( |
MessageType |
type | ) |
|
Removes a previously registered handler for a message type.
◆ default_connection_port
| constexpr int16_t MultiplayerService::default_connection_port = 1024 |
|
staticconstexpr |
◆ default_max_clients
| constexpr int16_t MultiplayerService::default_max_clients = 4 |
|
staticconstexpr |
The documentation for this class was generated from the following file: