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

High-level ENet-based server host for handling multiple client connections, broadcasting messages, and routing incoming packets. More...

#include <host.h>

Public Member Functions

 Host (std::reference_wrapper< Router > router, int connection_port, int max_clients)
 Constructs the Host with a Router, target port, and maximum number of clients.
 
 ~Host () noexcept
 Destructs the Host and releases ENet resources safely.
 
void start_server ()
 Starts the ENet server.
 
void poll () noexcept
 Polls ENet for incoming events (connect/disconnect/receive). Forwards packets through the Router.
 
void broadcast (const Message &message) noexcept
 Broadcasts a message to all connected peers.
 
void disconnect () noexcept
 Disconnects all clients and destroys the ENet server.
 
ConnectionState get_connection_state () const noexcept
 Returns the current server connection state.
 
std::string get_uuid () const noexcept
 
void set_max_clients (int amount) noexcept
 Sets the maximum number of clients allowed.
 
int get_client_amount () const noexcept
 Returns the number of currently connected clients.
 
std::string get_ip () const noexcept
 
void set_connection_port (int port) noexcept
 Sets the server port. Only effective before start_server().
 
void send_to_peer_via_uuid (const std::string &uuid, const Message &message) noexcept
 Sends a message to a connected client identified by UUID.
 
void sync () noexcept
 Broadcasts delta snapshots of the scene to all connected clients.
 

Detailed Description

High-level ENet-based server host for handling multiple client connections, broadcasting messages, and routing incoming packets.

The Host object owns its ENetHost and manages the lifecycle of all connected clients. It interacts with the Router to forward incoming messages to application-defined handlers.

Constructor & Destructor Documentation

◆ Host()

Host::Host ( std::reference_wrapper< Router router,
int  connection_port,
int  max_clients 
)

Constructs the Host with a Router, target port, and maximum number of clients.

Parameters
routerShared pointer to message routing system.
connection_portPort on which to listen for incoming clients.
max_clientsMaximum number of simultaneous connections allowed.

◆ ~Host()

Host::~Host ( )
noexcept

Destructs the Host and releases ENet resources safely.

Member Function Documentation

◆ broadcast()

void Host::broadcast ( const Message message)
noexcept

Broadcasts a message to all connected peers.

Parameters
messageMessage to broadcast.

◆ disconnect()

void Host::disconnect ( )
noexcept

Disconnects all clients and destroys the ENet server.

◆ get_client_amount()

int Host::get_client_amount ( ) const
noexcept

Returns the number of currently connected clients.

◆ get_connection_state()

ConnectionState Host::get_connection_state ( ) const
noexcept

Returns the current server connection state.

◆ get_ip()

std::string Host::get_ip ( ) const
noexcept

◆ get_uuid()

std::string Host::get_uuid ( ) const
noexcept

◆ poll()

void Host::poll ( )
noexcept

Polls ENet for incoming events (connect/disconnect/receive). Forwards packets through the Router.

◆ send_to_peer_via_uuid()

void Host::send_to_peer_via_uuid ( const std::string &  uuid,
const Message message 
)
noexcept

Sends a message to a connected client identified by UUID.

Parameters
uuidTarget client's UUID.
messageMessage to send.

◆ set_connection_port()

void Host::set_connection_port ( int  port)
noexcept

Sets the server port. Only effective before start_server().

◆ set_max_clients()

void Host::set_max_clients ( int  amount)
noexcept

Sets the maximum number of clients allowed.

◆ start_server()

void Host::start_server ( )

Starts the ENet server.

Exceptions
std::runtime_errorif ENet fails to create a server host.

◆ sync()

void Host::sync ( )
noexcept

Broadcasts delta snapshots of the scene to all connected clients.


The documentation for this class was generated from the following file: