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.h
Go to the documentation of this file.
1#pragma once
2
3#include <enet/enet.h>
6
7#include <chrono>
8#include <memory>
9#include <string>
10#include <unordered_map>
11
21class Host {
22 public:
31 Host(std::reference_wrapper<Router> router, int connection_port,
32 int max_clients);
33
37 ~Host() noexcept;
38
45
50 void poll() noexcept;
51
56 void broadcast(const Message& message) noexcept;
57
61 void disconnect() noexcept;
62
66 [[nodiscard]] ConnectionState get_connection_state() const noexcept;
67
68 [[nodiscard]] std::string get_uuid() const noexcept;
69
73 void set_max_clients(int amount) noexcept;
74
78 [[nodiscard]] int get_client_amount() const noexcept;
79
80 [[nodiscard]] std::string get_ip() const noexcept;
81
85 void set_connection_port(int port) noexcept;
86
92 void send_to_peer_via_uuid(const std::string& uuid,
93 const Message& message) noexcept;
94
98 void sync() noexcept;
99
100 private:
101 ENetHost* server_{nullptr};
102 int connection_port_{0};
103 int max_clients_{0};
104 ConnectionState connection_state_{ConnectionState::NONE};
105
106 std::string ip_;
107 std::string local_uuid_;
108 std::reference_wrapper<Router> router_;
109
110 // Maps client UUID to the peer object assigned by ENet.
111 std::unordered_map<std::string, ENetPeer*> clients_;
112
113 std::chrono::milliseconds snapshot_interval_{100};
114 std::chrono::steady_clock::time_point last_snapshot_time_{
115 std::chrono::steady_clock::now()};
116
122 void send_to_peer(const Message& message, ENetPeer* peer) noexcept;
123
124 void set_client_disconnect_handler() noexcept;
125 void set_client_connect_handler() noexcept;
126};
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
Definition uuid.h:4
Represents a complete network message including type and payload.
Definition network_message.h:105