10#include <unordered_map>
31 Host(std::reference_wrapper<Router> router,
int connection_port,
68 [[nodiscard]] std::
string get_uuid() const noexcept;
80 [[nodiscard]] std::
string get_ip() const noexcept;
93 const
Message& message) noexcept;
101 ENetHost* server_{
nullptr};
102 int connection_port_{0};
107 std::string local_uuid_;
108 std::reference_wrapper<Router> router_;
111 std::unordered_map<std::string, ENetPeer*> clients_;
113 std::chrono::milliseconds snapshot_interval_{100};
114 std::chrono::steady_clock::time_point last_snapshot_time_{
115 std::chrono::steady_clock::now()};
122 void send_to_peer(
const Message& message, ENetPeer* peer)
noexcept;
124 void set_client_disconnect_handler() noexcept;
125 void set_client_connect_handler() noexcept;
High-level ENet-based server host for handling multiple client connections, broadcasting messages,...
Definition host.h:21
std::string get_uuid() const noexcept
void disconnect() noexcept
Disconnects all clients and destroys the ENet server.
ConnectionState get_connection_state() const noexcept
Returns the current server connection state.
void send_to_peer_via_uuid(const std::string &uuid, const Message &message) noexcept
Sends a message to a connected client identified by UUID.
~Host() noexcept
Destructs the Host and releases ENet resources safely.
int get_client_amount() const noexcept
Returns the number of currently connected clients.
void set_max_clients(int amount) noexcept
Sets the maximum number of clients allowed.
void set_connection_port(int port) noexcept
Sets the server port. Only effective before start_server().
std::string get_ip() const noexcept
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.
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 sync() noexcept
Broadcasts delta snapshots of the scene to all connected clients.
ConnectionState
Definition connection_state.h:4
Represents a complete network message including type and payload.
Definition network_message.h:105