Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1#pragma once
2
3#include <enet/enet.h>
6
7#include <memory>
8#include <string>
9
18class Client {
19 public:
26 explicit Client(std::reference_wrapper<Router> router);
27
32 ~Client() noexcept;
33
38 void poll() noexcept;
39
44 void send(const Message& message) noexcept;
45
53 void connect(const std::string& host_ip, int connection_port);
54
58 void disconnect() noexcept;
59
63 [[nodiscard]] ConnectionState get_connection_state() const noexcept;
64
65 [[nodiscard]] std::string get_uuid() const noexcept;
66
67 [[nodiscard]] std::string get_host_ip() const noexcept;
68
69 private:
70 std::string local_uuid_;
71 std::string host_ip_{"0.0.0.0"};
72 std::reference_wrapper<Router> router_;
73 ENetPeer* server_peer_{nullptr};
74 ENetHost* client_{nullptr};
75 ConnectionState connection_state_{ConnectionState::NONE};
76
77 void register_on_connect_handler() noexcept;
78 void register_on_disconnect_handler() noexcept;
79 void register_on_snapshot_full_handler() noexcept;
80 void register_on_snapshot_delta_handler() noexcept;
81};
High-level ENet-based network client. Handles connecting, disconnecting, sending messages,...
Definition client.h:18
Client(std::reference_wrapper< Router > router)
Construct a Client with a shared Router.
void connect(const std::string &host_ip, int connection_port)
Initiates connection to a remote host.
std::string get_uuid() const noexcept
void poll() noexcept
Polls the network for incoming ENet events. Routes received messages and updates connection state.
~Client() noexcept
Safely destroys the client, unregisters handlers and frees ENet resources.
void send(const Message &message) noexcept
Sends a message to the connected server peer.
void disconnect() noexcept
Begins a clean disconnect by sending a disconnect message.
std::string get_host_ip() const noexcept
ConnectionState get_connection_state() const noexcept
Returns current connection state.
ConnectionState
Definition connection_state.h:4
Represents a complete network message including type and payload.
Definition network_message.h:105