Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_udp_messaging_client.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 <atomic>
8#include <concepts>
9#include <functional>
10#include <future>
11#include <memory>
12#include <mutex>
13#include <string>
14#include <string_view>
15#include <type_traits>
16#include <vector>
17
18#include <asio.hpp>
19
28
29#ifdef BUILD_TLS_SUPPORT
30#include <openssl/ssl.h>
31#endif
32
34{
35class udp_socket;
36#ifdef BUILD_TLS_SUPPORT
37class dtls_socket;
38#endif
39} // namespace kcenon::network::internal
40
42{
43
82template <policy::TlsPolicy TlsPolicy = policy::no_tls>
84 : public std::enable_shared_from_this<unified_udp_messaging_client<TlsPolicy>>
86{
87public:
90
93 std::function<void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&)>;
95 using connected_callback_t = std::function<void()>;
97 using disconnected_callback_t = std::function<void()>;
99 using error_callback_t = std::function<void(std::error_code)>;
100
107 explicit unified_udp_messaging_client(std::string_view client_id)
109
117 unified_udp_messaging_client(std::string_view client_id, const TlsPolicy& tls_config)
119
123 ~unified_udp_messaging_client() noexcept override;
124
125 // Non-copyable, non-movable
130
131 // =====================================================================
132 // Lifecycle Management
133 // =====================================================================
134
144 [[nodiscard]] auto start_client(std::string_view host, uint16_t port) -> VoidResult;
145
151 [[nodiscard]] auto stop_client() -> VoidResult;
152
157 [[nodiscard]] auto client_id() const -> const std::string&;
158
159 // =====================================================================
160 // i_network_component interface implementation
161 // =====================================================================
162
167 [[nodiscard]] auto is_running() const -> bool override;
168
172 auto wait_for_stop() -> void override;
173
174 // =====================================================================
175 // i_udp_client interface implementation
176 // =====================================================================
177
178 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
179 [[nodiscard]] auto stop() -> VoidResult override;
180 [[nodiscard]] auto send(std::vector<uint8_t>&& data,
181 interfaces::i_udp_client::send_callback_t handler = nullptr)
182 -> VoidResult override;
183 [[nodiscard]] auto set_target(std::string_view host, uint16_t port) -> VoidResult override;
184 auto set_receive_callback(interfaces::i_udp_client::receive_callback_t callback)
185 -> void override;
186 auto set_error_callback(error_callback_t callback) -> void override;
187
188 // =====================================================================
189 // Extended API (not in interface)
190 // =====================================================================
191
198 [[nodiscard]] auto is_connected() const noexcept -> bool;
199
205 [[nodiscard]] auto send_packet(std::vector<uint8_t>&& data) -> VoidResult;
206
212
218
224
225private:
226 // =====================================================================
227 // Internal Socket Type Selection
228 // =====================================================================
229
230#ifdef BUILD_TLS_SUPPORT
231 using socket_type =
232 std::conditional_t<is_secure, internal::dtls_socket, internal::udp_socket>;
233#else
235#endif
236
237 // =====================================================================
238 // Internal Implementation Methods
239 // =====================================================================
240
241 auto do_start_impl(std::string_view host, uint16_t port) -> VoidResult;
243
244#ifdef BUILD_TLS_SUPPORT
245 auto init_ssl_context() -> VoidResult
246 requires is_secure;
247 auto do_handshake() -> VoidResult
248 requires is_secure;
249#endif
250
251 // =====================================================================
252 // Internal Callback Helpers
253 // =====================================================================
254
255 auto set_connected(bool connected) -> void;
256 auto invoke_receive_callback(const std::vector<uint8_t>& data,
257 const asio::ip::udp::endpoint& endpoint) -> void;
260 auto invoke_error_callback(std::error_code ec) -> void;
261
262 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
263 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
264
265private:
268
270 using callbacks_t = utils::callback_manager<receive_callback_t,
274
275 // =====================================================================
276 // Member Variables
277 // =====================================================================
278
279 std::string client_id_;
280 utils::lifecycle_manager lifecycle_;
282 std::atomic<bool> is_connected_{false};
284 std::unique_ptr<asio::io_context> io_context_;
285 std::shared_ptr<socket_type> socket_;
287 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
288 std::future<void> io_context_future_;
290 std::mutex endpoint_mutex_;
291 asio::ip::udp::endpoint target_endpoint_;
293 mutable std::mutex socket_mutex_;
295#ifdef BUILD_TLS_SUPPORT
296 SSL_CTX* ssl_ctx_{nullptr};
297 [[no_unique_address]] TlsPolicy tls_config_;
298#endif
299};
300
301// =====================================================================
302// Type Aliases for Convenience
303// =====================================================================
304
309
310#ifdef BUILD_TLS_SUPPORT
315#endif
316
317} // namespace kcenon::network::core
318
319// Include template implementation
320#include "unified_udp_messaging_client.inl"
Thread-safe callback registration and invocation manager.
Unified UDP client template parameterized by TLS policy.
auto set_receive_callback(interfaces::i_udp_client::receive_callback_t callback) -> void override
Sets the callback for received datagrams.
static constexpr bool is_secure
Indicates whether TLS (DTLS) is enabled for this client.
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> receive_callback_t
Callback type for received datagrams with sender endpoint.
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends a datagram to the configured target endpoint.
auto wait_for_stop() -> void override
Blocks until stop_client() is called.
auto is_running() const -> bool override
Checks if the client is currently running.
auto invoke_error_callback(std::error_code ec) -> void
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
unified_udp_messaging_client(std::string_view client_id)
Constructs a plain UDP client with a given identifier.
auto set_connected_callback(connected_callback_t callback) -> void
Sets the callback for connection established (DTLS handshake complete).
std::function< void()> disconnected_callback_t
Callback type for disconnection.
auto start_client(std::string_view host, uint16_t port) -> VoidResult
Starts the client by resolving target host and port.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto stop_client() -> VoidResult
Stops the client and releases all resources.
auto set_target(std::string_view host, uint16_t port) -> VoidResult override
Changes the target endpoint for future sends.
auto set_disconnected_callback(disconnected_callback_t callback) -> void
Sets the callback for disconnection.
unified_udp_messaging_client(std::string_view client_id, const TlsPolicy &tls_config)
Constructs a secure UDP client (DTLS) with TLS configuration.
auto do_start_impl(std::string_view host, uint16_t port) -> VoidResult
auto send(std::vector< uint8_t > &&data, interfaces::i_udp_client::send_callback_t handler=nullptr) -> VoidResult override
Sends a datagram to the configured target endpoint.
~unified_udp_messaging_client() noexcept override
Destructor; automatically calls stop_client() if still running.
std::function< void()> connected_callback_t
Callback type for connection established (DTLS handshake complete)
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto get_receive_callback() const -> receive_callback_t
auto client_id() const -> const std::string &
Returns the client identifier.
auto get_error_callback() const -> error_callback_t
auto invoke_receive_callback(const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &endpoint) -> void
auto stop() -> VoidResult override
Stops the UDP client.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the UDP client targeting the specified endpoint.
auto is_connected() const noexcept -> bool
Checks if the client is connected (DTLS handshake complete).
Interface for UDP client components.
std::function< void(std::error_code, std::size_t)> send_callback_t
Callback type for send completion.
A lightweight wrapper around asio::ip::udp::socket, enabling asynchronous datagram operations.
Definition udp_socket.h:47
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
Result< std::monostate > VoidResult
unified_udp_client_callback
Callback indices for unified_udp_messaging_client.
Protocol tag types for compile-time protocol selection.
Network-specific error and result type definitions.
Thread system integration interface for network_system.
Policy-based TLS configuration (no_tls, require_tls, optional_tls).