Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
udp_server_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
8
9#include <memory>
10#include <mutex>
11#include <string>
12#include <unordered_map>
13
14namespace kcenon::network::core {
15 class messaging_udp_server;
16}
17
19
28public:
30 std::string_view session_id,
31 std::string_view address,
32 uint16_t port,
33 std::weak_ptr<core::messaging_udp_server> server);
34
35 [[nodiscard]] auto id() const -> std::string_view override;
36 [[nodiscard]] auto is_connected() const -> bool override;
37 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
38 auto close() -> void override;
39
40private:
41 std::string session_id_;
42 std::string address_;
43 uint16_t port_;
44 std::weak_ptr<core::messaging_udp_server> server_;
45};
46
69public:
74 explicit udp_server_adapter(std::string_view server_id);
75
79 ~udp_server_adapter() override;
80
81 // Non-copyable, non-movable
86
87 // =========================================================================
88 // i_network_component interface implementation
89 // =========================================================================
90
91 [[nodiscard]] auto is_running() const -> bool override;
92 auto wait_for_stop() -> void override;
93
94 // =========================================================================
95 // i_protocol_server interface implementation
96 // =========================================================================
97
98 [[nodiscard]] auto start(uint16_t port) -> VoidResult override;
99 [[nodiscard]] auto stop() -> VoidResult override;
100 [[nodiscard]] auto connection_count() const -> size_t override;
101
102 auto set_connection_callback(connection_callback_t callback) -> void override;
103 auto set_disconnection_callback(disconnection_callback_t callback) -> void override;
104 auto set_receive_callback(receive_callback_t callback) -> void override;
105 auto set_error_callback(error_callback_t callback) -> void override;
106
107private:
111 auto setup_internal_callbacks() -> void;
112
116 static auto make_session_id(const std::string& address, uint16_t port) -> std::string;
117
121 auto get_or_create_session(const std::string& address, uint16_t port)
122 -> std::shared_ptr<interfaces::i_session>;
123
124 std::string server_id_;
125 std::shared_ptr<core::messaging_udp_server> server_;
126
132
133 mutable std::mutex sessions_mutex_;
134 std::unordered_map<std::string, std::shared_ptr<udp_endpoint_session>> sessions_;
135};
136
137} // namespace kcenon::network::internal::adapters
Unified interface for all protocol server implementations.
std::function< void(std::string_view)> disconnection_callback_t
Callback type for disconnections (session_id)
std::function< void(std::string_view, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data (session_id, data)
std::function< void(std::string_view, std::error_code)> error_callback_t
Callback type for errors (session_id, error)
std::function< void(std::shared_ptr< i_session >)> connection_callback_t
Callback type for new connections.
Interface for a single client session on the server side.
Definition i_session.h:49
Session wrapper for UDP endpoint connections.
auto id() const -> std::string_view override
Gets the unique identifier for this session.
std::weak_ptr< core::messaging_udp_server > server_
auto is_connected() const -> bool override
Checks if the session is currently connected.
udp_endpoint_session(std::string_view session_id, std::string_view address, uint16_t port, std::weak_ptr< core::messaging_udp_server > server)
Adapter that wraps messaging_udp_server to implement i_protocol_server.
static auto make_session_id(const std::string &address, uint16_t port) -> std::string
Creates a unique session ID from endpoint info.
std::unordered_map< std::string, std::shared_ptr< udp_endpoint_session > > sessions_
auto set_connection_callback(connection_callback_t callback) -> void override
Sets the callback for new connections.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
udp_server_adapter(const udp_server_adapter &)=delete
std::shared_ptr< core::messaging_udp_server > server_
~udp_server_adapter() override
Destructor ensures proper cleanup.
auto connection_count() const -> size_t override
Gets the number of active client connections.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge UDP callbacks to i_protocol_server callbacks.
auto is_running() const -> bool override
Checks if the component is currently running.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
udp_server_adapter & operator=(const udp_server_adapter &)=delete
auto set_disconnection_callback(disconnection_callback_t callback) -> void override
Sets the callback for disconnections.
udp_server_adapter & operator=(udp_server_adapter &&)=delete
auto start(uint16_t port) -> VoidResult override
Starts the server and begins listening for connections.
udp_server_adapter(std::string_view server_id)
Constructs an adapter with a unique server ID.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto stop() -> VoidResult override
Stops the server and closes all connections.
auto get_or_create_session(const std::string &address, uint16_t port) -> std::shared_ptr< interfaces::i_session >
Gets or creates a session for the given endpoint.
Protocol-specific server interface extending i_server.
std::mutex mutex