Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
multiplayer_service.h
Go to the documentation of this file.
1#pragma once
2
3#include <enet/enet.h>
9
10#include <functional>
11#include <memory>
12#include <string>
13
14enum class PeerType : uint16_t { HOST, CLIENT, NONE };
15
26 public:
27 static constexpr int16_t default_connection_port = 1024;
28 static constexpr int16_t default_max_clients = 4;
29
35
42 const std::function<void(const Message&)>& handler);
43
48
54 void set_host();
55
62 void set_client();
63
69
73 void poll();
74
78 void send(const Message& message);
79
85 void send_to_peer_via_uuid(const std::string& uuid, const Message& message);
86
92
96 void connect(const std::string& address);
97
102 void disconnect(std::function<void()> on_disconnected = nullptr);
103
107 [[nodiscard]] ConnectionState get_connection_state() const noexcept;
108
109 [[nodiscard]] std::string get_uuid() const;
110
111 void set_max_clients(int amount) noexcept;
112 [[nodiscard]] int get_client_amount() const noexcept;
113
114 void set_connection_port(int port) noexcept;
115 [[nodiscard]] int get_connection_port() const noexcept;
116
117 [[nodiscard]] std::string get_host_ip() const;
118
119 private:
120 std::unique_ptr<Router> router_{nullptr};
121 std::unique_ptr<Client> client_{nullptr};
122 std::unique_ptr<Host> host_{nullptr};
123
124 int16_t connection_port_{default_connection_port};
125 int16_t max_clients_{default_max_clients};
126};
Definition iEngineService.h:4
High-level peer-to-peer networking service managing host/client mode, message routing,...
Definition multiplayer_service.h:25
void unregister_handler(MessageType type)
Removes a previously registered handler for a message type.
static constexpr int16_t default_max_clients
Definition multiplayer_service.h:28
void set_max_clients(int amount) noexcept
PeerType get_peer_type() const
Returns the current peer type (host, client, or none).
void register_handler(MessageType type, const std::function< void(const Message &)> &handler)
Registers a callback handler for a specific message type.
void disconnect(std::function< void()> on_disconnected=nullptr)
Disconnect the active host or client gracefully.
int get_client_amount() const noexcept
void set_connection_port(int port) noexcept
void poll()
Polls the network. Behavior depends on host/client mode.
void start_server()
Starts the host server. Only valid in host-mode.
std::string get_uuid() const
MultiplayerService()
Initializes ENet and prepares the internal Router.
void connect(const std::string &address)
Connect to a remote server. Only valid in client-mode.
static constexpr int16_t default_connection_port
Definition multiplayer_service.h:27
std::string get_host_ip() const
ConnectionState get_connection_state() const noexcept
Returns connection state of the current mode.
void set_client()
Switches the service into client-mode. Invalidates any host instance.
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 send(const Message &message)
Sends a message. Host broadcasts; client sends to server.
void set_host()
Switches the service into host-mode. Invalidates any client instance.
int get_connection_port() const noexcept
Routes incoming network messages to registered handlers.
Definition router.h:11
ConnectionState
Definition connection_state.h:4
PeerType
Definition multiplayer_service.h:14
Definition uuid.h:4
Represents a complete network message including type and payload.
Definition network_message.h:105
Represents either a default or custom message identifier.
Definition network_message.h:40