Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
MultiplayerService Class Reference

High-level peer-to-peer networking service managing host/client mode, message routing, and network lifecycle operations. More...

#include <multiplayer_service.h>

Inheritance diagram for MultiplayerService:
[legend]

Public Member Functions

 MultiplayerService ()
 Initializes ENet and prepares the internal Router.
 
void register_handler (MessageType type, const std::function< void(const Message &)> &handler)
 Registers a callback handler for a specific message type.
 
void unregister_handler (MessageType type)
 Removes a previously registered handler for a message type.
 
void set_host ()
 Switches the service into host-mode. Invalidates any client instance.
 
void set_client ()
 Switches the service into client-mode. Invalidates any host instance.
 
PeerType get_peer_type () const
 Returns the current peer type (host, client, or none).
 
void poll ()
 Polls the network. Behavior depends on host/client mode.
 
void send (const Message &message)
 Sends a message. Host broadcasts; client sends to server.
 
void send_to_peer_via_uuid (const std::string &uuid, const Message &message)
 Sends a message to a specific connected client (host mode only).
 
void start_server ()
 Starts the host server. Only valid in host-mode.
 
void connect (const std::string &address)
 Connect to a remote server. Only valid in client-mode.
 
void disconnect (std::function< void()> on_disconnected=nullptr)
 Disconnect the active host or client gracefully.
 
ConnectionState get_connection_state () const noexcept
 Returns connection state of the current mode.
 
std::string get_uuid () const
 
void set_max_clients (int amount) noexcept
 
int get_client_amount () const noexcept
 
void set_connection_port (int port) noexcept
 
int get_connection_port () const noexcept
 
std::string get_host_ip () const
 
- Public Member Functions inherited from IEngineService
virtual ~IEngineService ()=default
 

Static Public Attributes

static constexpr int16_t default_connection_port = 1024
 
static constexpr int16_t default_max_clients = 4
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MultiplayerService()

MultiplayerService::MultiplayerService ( )

Initializes ENet and prepares the internal Router.

Exceptions
std::runtime_errorif ENet initialization fails.

Member Function Documentation

◆ 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_errorif 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()

ConnectionState MultiplayerService::get_connection_state ( ) const
noexcept

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
typeMessage type
handlerFunction 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
uuidTarget client's UUID.
messageMessage to send.

◆ set_client()

void MultiplayerService::set_client ( )

Switches the service into client-mode. Invalidates any host instance.

Exceptions
std::runtime_errorif 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_errorif 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_errorif called while acting as client.

◆ unregister_handler()

void MultiplayerService::unregister_handler ( MessageType  type)

Removes a previously registered handler for a message type.

Member Data Documentation

◆ 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: