Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
secure_messaging_udp_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
14#include <atomic>
15#include <functional>
16#include <future>
17#include <memory>
18#include <mutex>
19#include <string>
20#include <string_view>
21#include <vector>
22
23#include <asio.hpp>
24#include <openssl/ssl.h>
25
31
33{
34 class dtls_socket;
35}
36
38{
82 class secure_messaging_udp_client
83 : public std::enable_shared_from_this<secure_messaging_udp_client>
84 {
85 public:
87 using receive_callback_t = std::function<void(const std::vector<uint8_t>&)>;
89 using connected_callback_t = std::function<void()>;
91 using disconnected_callback_t = std::function<void()>;
93 using error_callback_t = std::function<void(std::error_code)>;
94
96 using udp_receive_callback_t = std::function<void(const std::vector<uint8_t>&,
97 const asio::ip::udp::endpoint&)>;
98
104 secure_messaging_udp_client(std::string_view client_id, bool verify_cert = true);
105
110
111 // Non-copyable, non-movable
116
117 // =====================================================================
118 // Lifecycle Management
119 // =====================================================================
120
130 [[nodiscard]] auto start_client(std::string_view host, unsigned short port) -> VoidResult;
131
137 [[nodiscard]] auto stop_client() -> VoidResult;
138
142 auto wait_for_stop() -> void;
143
148 [[nodiscard]] auto is_running() const noexcept -> bool;
149
154 [[nodiscard]] auto is_connected() const noexcept -> bool;
155
160 [[nodiscard]] auto client_id() const -> const std::string&;
161
162 // =====================================================================
163 // Data Transfer
164 // =====================================================================
165
171 [[nodiscard]] auto send_packet(std::vector<uint8_t>&& data) -> VoidResult;
172
180 std::vector<uint8_t>&& data,
181 std::function<void(std::error_code, std::size_t)> handler) -> VoidResult;
182
183 // =====================================================================
184 // Callback Setters
185 // =====================================================================
186
196
201 auto set_receive_callback(receive_callback_t callback) -> void;
202
207 auto set_connected_callback(connected_callback_t callback) -> void;
208
214
219 auto set_error_callback(error_callback_t callback) -> void;
220
221 private:
222 // =====================================================================
223 // Internal Implementation Methods
224 // =====================================================================
225
232 auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult;
233
238 auto do_stop_impl() -> VoidResult;
239
245 auto do_send_impl(std::vector<uint8_t>&& data) -> VoidResult;
246
252
257 auto do_handshake() -> VoidResult;
258
259 // =====================================================================
260 // Internal Callback Helpers
261 // =====================================================================
262
267 auto set_connected(bool connected) -> void;
268
272 auto invoke_receive_callback(const std::vector<uint8_t>& data) -> void;
273
277 auto invoke_connected_callback() -> void;
278
282 auto invoke_disconnected_callback() -> void;
283
287 auto invoke_error_callback(std::error_code ec) -> void;
288
289 private:
292
294 using callbacks_t = utils::callback_manager<
299 >;
300
301 // =====================================================================
302 // Member Variables
303 // =====================================================================
304
305 std::string client_id_;
306 utils::lifecycle_manager lifecycle_;
308 std::atomic<bool> is_connected_{false};
310 bool verify_cert_{true};
312 std::unique_ptr<asio::io_context> io_context_;
313 std::shared_ptr<internal::dtls_socket> socket_;
315 SSL_CTX* ssl_ctx_{nullptr};
317 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
318 std::future<void> io_context_future_;
320 std::mutex endpoint_mutex_;
321 asio::ip::udp::endpoint target_endpoint_;
326 };
327
328} // namespace kcenon::network::core
Thread-safe callback registration and invocation manager.
A secure UDP client using DTLS (Datagram TLS) for encrypted communication.
Definition ssl.cppm:471
auto invoke_disconnected_callback() -> void
Invokes the disconnected callback.
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for errors.
auto set_udp_receive_callback(udp_receive_callback_t callback) -> void
Sets a UDP-specific callback to handle received decrypted datagrams.
auto send_packet_with_handler(std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> VoidResult
Sends an encrypted datagram with a completion handler.
auto set_disconnected_callback(disconnected_callback_t callback) -> void
Sets the callback for disconnection.
auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult
DTLS-specific implementation of client start.
auto init_ssl_context() -> VoidResult
Initializes the SSL context for DTLS.
auto do_send_impl(std::vector< uint8_t > &&data) -> VoidResult
DTLS-specific implementation of data send.
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client and connects to the specified host and port.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
~secure_messaging_udp_client() noexcept
Destructor. Automatically calls stop_client() if still running.
auto invoke_connected_callback() -> void
Invokes the connected callback.
auto do_stop_impl() -> VoidResult
DTLS-specific implementation of client stop.
std::function< void()> connected_callback_t
Callback type for connection established.
std::mutex udp_callback_mutex_
UDP-specific receive callback (includes sender endpoint)
secure_messaging_udp_client(std::string_view client_id, bool verify_cert=true)
Constructs a secure_messaging_udp_client with an identifier.
auto stop_client() -> VoidResult
Stops the client and disconnects.
auto invoke_error_callback(std::error_code ec) -> void
Invokes the error callback.
auto invoke_receive_callback(const std::vector< uint8_t > &data) -> void
Invokes the receive callback.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
auto client_id() const -> const std::string &
Returns the client identifier.
auto is_connected() const noexcept -> bool
Checks if the client is connected to the server.
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends an encrypted datagram to the server.
auto is_running() const noexcept -> bool
Checks if the client is currently running.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto set_receive_callback(receive_callback_t callback) -> void
Sets the callback for received data.
auto set_connected(bool connected) -> void
Sets the connected state.
auto wait_for_stop() -> void
Blocks until stop_client() is called.
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> udp_receive_callback_t
UDP-specific receive callback type with sender endpoint.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto do_handshake() -> VoidResult
Performs the DTLS handshake.
auto set_connected_callback(connected_callback_t callback) -> void
Sets the callback for connection established.
struct ssl_ctx_st SSL_CTX
Definition crypto.h:20
Component lifecycle management (start, stop, restart).
secure_udp_client_callback
Callback indices for secure_messaging_udp_client.
Network-specific error and result type definitions.
Thread system integration interface for network_system.