Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_udp_messaging_server.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
7#include <array>
8#include <atomic>
9#include <concepts>
10#include <functional>
11#include <future>
12#include <memory>
13#include <mutex>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <type_traits>
18#include <unordered_map>
19#include <vector>
20
21#include <asio.hpp>
22
31
32#ifdef BUILD_TLS_SUPPORT
33#include <openssl/ssl.h>
34#endif
35
37{
38class udp_socket;
39#ifdef BUILD_TLS_SUPPORT
40class dtls_socket;
41#endif
42} // namespace kcenon::network::internal
43
45{
46
87template <policy::TlsPolicy TlsPolicy = policy::no_tls>
89 : public std::enable_shared_from_this<unified_udp_messaging_server<TlsPolicy>>
91{
92public:
95
98 std::function<void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&)>;
100 using client_connected_callback_t = std::function<void(const asio::ip::udp::endpoint&)>;
102 using client_disconnected_callback_t = std::function<void(const asio::ip::udp::endpoint&)>;
104 using error_callback_t = std::function<void(std::error_code)>;
105
112 explicit unified_udp_messaging_server(std::string_view server_id)
114
123 unified_udp_messaging_server(std::string_view server_id, const TlsPolicy& tls_config)
125
129 ~unified_udp_messaging_server() noexcept override;
130
131 // Non-copyable, non-movable
136
137 // =====================================================================
138 // Lifecycle Management
139 // =====================================================================
140
149 [[nodiscard]] auto start_server(uint16_t port) -> VoidResult;
150
156 [[nodiscard]] auto stop_server() -> VoidResult;
157
162 [[nodiscard]] auto server_id() const -> const std::string&;
163
164 // =====================================================================
165 // i_network_component interface implementation
166 // =====================================================================
167
172 [[nodiscard]] auto is_running() const -> bool override;
173
177 auto wait_for_stop() -> void override;
178
179 // =====================================================================
180 // i_udp_server interface implementation
181 // =====================================================================
182
183 [[nodiscard]] auto start(uint16_t port) -> VoidResult override;
184 [[nodiscard]] auto stop() -> VoidResult override;
185 [[nodiscard]] auto send_to(const interfaces::i_udp_server::endpoint_info& endpoint,
186 std::vector<uint8_t>&& data,
187 interfaces::i_udp_server::send_callback_t handler = nullptr)
188 -> VoidResult override;
189 auto set_receive_callback(interfaces::i_udp_server::receive_callback_t callback)
190 -> void override;
191 auto set_error_callback(error_callback_t callback) -> void override;
192
193 // =====================================================================
194 // Extended API (not in interface)
195 // =====================================================================
196
202
210
218
225 auto async_send_to(std::vector<uint8_t>&& data,
226 const asio::ip::udp::endpoint& endpoint,
227 std::function<void(std::error_code, std::size_t)> handler = nullptr)
228 -> void;
229
230private:
231#ifdef BUILD_TLS_SUPPORT
235 struct dtls_session
236 {
237 std::shared_ptr<internal::dtls_socket> socket;
238 bool handshake_complete{false};
239 };
240
244 struct endpoint_hash
245 {
246 std::size_t operator()(const asio::ip::udp::endpoint& ep) const
247 {
248 auto addr_hash = std::hash<std::string>{}(ep.address().to_string());
249 auto port_hash = std::hash<unsigned short>{}(ep.port());
250 return addr_hash ^ (port_hash << 1);
251 }
252 };
253#endif
254
255 // =====================================================================
256 // Internal Implementation Methods
257 // =====================================================================
258
259 auto do_start_impl(uint16_t port) -> VoidResult;
261
262#ifdef BUILD_TLS_SUPPORT
263 auto init_ssl_context() -> VoidResult
264 requires is_secure;
265 auto do_receive() -> void
266 requires is_secure;
267 auto process_session_data(const std::vector<uint8_t>& data,
268 const asio::ip::udp::endpoint& sender) -> void
269 requires is_secure;
270 auto create_session(const asio::ip::udp::endpoint& client_endpoint)
271 -> std::shared_ptr<dtls_session>
272 requires is_secure;
273#endif
274
275 // =====================================================================
276 // Internal Callback Helpers
277 // =====================================================================
278
279 auto invoke_receive_callback(const std::vector<uint8_t>& data,
280 const asio::ip::udp::endpoint& endpoint) -> void;
281 auto invoke_error_callback(std::error_code ec) -> void;
282
283 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
284 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
285
286private:
289
291 using callbacks_t = utils::callback_manager<receive_callback_t,
295
296 // =====================================================================
297 // Member Variables
298 // =====================================================================
299
300 std::string server_id_;
301 utils::lifecycle_manager lifecycle_;
304 std::unique_ptr<asio::io_context> io_context_;
306 // Plain UDP mode uses udp_socket
307 std::shared_ptr<internal::udp_socket> socket_;
309 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
310 std::future<void> io_context_future_;
312#ifdef BUILD_TLS_SUPPORT
313 // DTLS mode uses raw socket + session management
314 std::unique_ptr<asio::ip::udp::socket> dtls_socket_;
315 SSL_CTX* ssl_ctx_{nullptr};
316 [[no_unique_address]] TlsPolicy tls_config_;
318 std::array<uint8_t, 65536> read_buffer_;
319 asio::ip::udp::endpoint sender_endpoint_;
320 std::mutex sessions_mutex_;
321 std::unordered_map<asio::ip::udp::endpoint,
322 std::shared_ptr<dtls_session>,
323 endpoint_hash>
324 sessions_;
325#endif
326};
327
328// =====================================================================
329// Type Aliases for Convenience
330// =====================================================================
331
336
337#ifdef BUILD_TLS_SUPPORT
342#endif
343
344} // namespace kcenon::network::core
345
346// Include template implementation
347#include "unified_udp_messaging_server.inl"
Thread-safe callback registration and invocation manager.
Unified UDP server template parameterized by TLS policy.
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> receive_callback_t
Callback type for received datagrams with sender endpoint.
std::function< void(const asio::ip::udp::endpoint &)> client_disconnected_callback_t
Callback type for client disconnection.
auto async_send_to(std::vector< uint8_t > &&data, const asio::ip::udp::endpoint &endpoint, std::function< void(std::error_code, std::size_t)> handler=nullptr) -> void
Sends a datagram to the specified endpoint (async version).
auto send_to(const interfaces::i_udp_server::endpoint_info &endpoint, std::vector< uint8_t > &&data, interfaces::i_udp_server::send_callback_t handler=nullptr) -> VoidResult override
Sends a datagram to the specified endpoint.
auto is_running() const -> bool override
Checks if the server is currently running.
auto invoke_receive_callback(const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &endpoint) -> void
auto set_client_connected_callback(client_connected_callback_t callback) -> void
Sets the callback for client connections (DTLS handshake complete).
auto start_server(uint16_t port) -> VoidResult
Starts the server on the specified port.
auto server_id() const -> const std::string &
Returns the server identifier.
auto stop() -> VoidResult override
Stops the UDP server.
auto get_error_callback() const -> error_callback_t
auto stop_server() -> VoidResult
Stops the server and releases all resources.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
static constexpr bool is_secure
Indicates whether TLS (DTLS) is enabled for this server.
auto set_client_disconnected_callback(client_disconnected_callback_t callback) -> void
Sets the callback for client disconnections.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
unified_udp_messaging_server(std::string_view server_id, const TlsPolicy &tls_config)
Constructs a secure UDP server (DTLS) with TLS configuration.
auto set_receive_callback(interfaces::i_udp_server::receive_callback_t callback) -> void override
Sets the callback for received datagrams.
std::function< void(const asio::ip::udp::endpoint &)> client_connected_callback_t
Callback type for client connection (DTLS handshake complete)
auto start(uint16_t port) -> VoidResult override
Starts the UDP server on the specified port.
auto invoke_error_callback(std::error_code ec) -> void
auto do_start_impl(uint16_t port) -> VoidResult
unified_udp_messaging_server(std::string_view server_id)
Constructs a plain UDP server with a given identifier.
~unified_udp_messaging_server() noexcept override
Destructor; automatically calls stop_server() if still running.
auto get_receive_callback() const -> receive_callback_t
auto wait_for_stop() -> void override
Blocks until stop_server() is called.
Interface for UDP server components.
std::function< void(std::error_code, std::size_t)> send_callback_t
Callback type for send completion.
struct ssl_ctx_st SSL_CTX
Definition crypto.h:20
Component lifecycle management (start, stop, restart).
constexpr bool is_tls_enabled_v
Helper variable template to check if TLS is enabled at compile time.
Definition tls_policy.h:96
const char * to_string(validation_result result)
Convert validation result to string.
Result< std::monostate > VoidResult
unified_udp_server_callback
Callback indices for unified_udp_messaging_server.
Protocol tag types for compile-time protocol selection.
Network-specific error and result type definitions.
Endpoint information for UDP datagrams.
Thread system integration interface for network_system.
Policy-based TLS configuration (no_tls, require_tls, optional_tls).