Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
secure_messaging_udp_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
14#include <array>
15#include <atomic>
16#include <functional>
17#include <future>
18#include <memory>
19#include <mutex>
20#include <optional>
21#include <string>
22#include <string_view>
23#include <unordered_map>
24#include <vector>
25
26#include <asio.hpp>
27#include <openssl/ssl.h>
28
31
33{
34 class dtls_socket;
35}
36
38{
86 class secure_messaging_udp_server : public std::enable_shared_from_this<secure_messaging_udp_server>
87 {
88 public:
90 using udp_receive_callback_t = std::function<void(const std::vector<uint8_t>&,
91 const asio::ip::udp::endpoint&)>;
92 using udp_client_callback_t = std::function<void(const asio::ip::udp::endpoint&)>;
93
98 explicit secure_messaging_udp_server(std::string_view server_id);
99
104
105 // Non-copyable
108
116 auto set_certificate_chain_file(const std::string& file_path) -> VoidResult;
117
125 auto set_private_key_file(const std::string& file_path) -> VoidResult;
126
135 auto start_server(uint16_t port) -> VoidResult;
136
141 auto stop_server() -> VoidResult;
142
146 auto wait_for_stop() -> void;
147
154 auto async_send_to(
155 std::vector<uint8_t>&& data,
156 const asio::ip::udp::endpoint& endpoint,
157 std::function<void(std::error_code, std::size_t)> handler = nullptr) -> void;
158
164 auto set_receive_callback(udp_receive_callback_t callback) -> void;
165
170 auto set_error_callback(std::function<void(std::error_code)> callback) -> void;
171
177
183
188 [[nodiscard]] auto is_running() const noexcept -> bool
189 {
190 return is_running_.load(std::memory_order_acquire);
191 }
192
197 [[nodiscard]] auto server_id() const -> const std::string& { return server_id_; }
198
199 private:
204 {
205 std::shared_ptr<internal::dtls_socket> socket;
207 };
208
214
218 auto do_receive() -> void;
219
223 auto process_session_data(const std::vector<uint8_t>& data,
224 const asio::ip::udp::endpoint& sender) -> void;
225
229 auto create_session(const asio::ip::udp::endpoint& client_endpoint)
230 -> std::shared_ptr<dtls_session>;
231
236 {
237 std::size_t operator()(const asio::ip::udp::endpoint& ep) const
238 {
239 auto addr_hash = std::hash<std::string>{}(ep.address().to_string());
240 auto port_hash = std::hash<unsigned short>{}(ep.port());
241 return addr_hash ^ (port_hash << 1);
242 }
243 };
244
245 private:
246 // Lifecycle management (consistent with other messaging classes)
247 std::string server_id_;
248 std::atomic<bool> is_running_{false};
249 std::optional<std::promise<void>> stop_promise_;
250 std::future<void> stop_future_;
252 // DTLS protocol-specific members
253 std::unique_ptr<asio::io_context> io_context_;
254 std::unique_ptr<asio::ip::udp::socket> socket_;
256 SSL_CTX* ssl_ctx_{nullptr};
257 std::string cert_file_;
258 std::string key_file_;
260 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
261 std::future<void> io_context_future_;
263 std::array<uint8_t, 65536> read_buffer_;
264 asio::ip::udp::endpoint sender_endpoint_;
266 std::mutex sessions_mutex_;
267 std::unordered_map<asio::ip::udp::endpoint, std::shared_ptr<dtls_session>, endpoint_hash>
270 // UDP-specific callbacks (different signature from TCP servers)
271 mutable std::mutex callback_mutex_;
273 std::function<void(std::error_code)> error_callback_;
276 };
277
278} // namespace kcenon::network::core
A secure UDP server using DTLS (Datagram TLS) for encrypted communication.
Definition ssl.cppm:583
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 an encrypted datagram to a specific client.
auto server_id() const -> const std::string &
Returns the server identifier.
auto set_receive_callback(udp_receive_callback_t callback) -> void
Sets a UDP-specific callback to handle received decrypted datagrams.
auto wait_for_stop() -> void
Blocks the calling thread until the server is stopped.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto create_session(const asio::ip::udp::endpoint &client_endpoint) -> std::shared_ptr< dtls_session >
Creates a new DTLS session for a client.
auto start_server(uint16_t port) -> VoidResult
Starts the server and begins listening for DTLS connections.
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> udp_receive_callback_t
UDP-specific callback types with endpoint information.
auto stop_server() -> VoidResult
Stops the server and releases all resources.
auto is_running() const noexcept -> bool
Returns whether the server is currently running.
std::unordered_map< asio::ip::udp::endpoint, std::shared_ptr< dtls_session >, endpoint_hash > sessions_
auto set_private_key_file(const std::string &file_path) -> VoidResult
Sets the private key file for TLS.
auto init_ssl_context() -> VoidResult
Initializes the SSL context for DTLS server.
auto set_client_disconnected_callback(udp_client_callback_t callback) -> void
Sets a UDP-specific callback for client disconnection.
std::function< void(const asio::ip::udp::endpoint &)> udp_client_callback_t
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets a callback to handle errors.
auto set_certificate_chain_file(const std::string &file_path) -> VoidResult
Sets the certificate chain file for TLS.
secure_messaging_udp_server(std::string_view server_id)
Constructs a secure_messaging_udp_server with an identifier.
auto set_client_connected_callback(udp_client_callback_t callback) -> void
Sets a UDP-specific callback for new client connection.
~secure_messaging_udp_server() noexcept
Destructor. Automatically calls stop_server() if still running.
auto do_receive() -> void
Handles incoming datagrams and routes them to appropriate sessions.
auto process_session_data(const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &sender) -> void
Processes received data for an existing session.
struct ssl_ctx_st SSL_CTX
Definition crypto.h:20
constexpr std::string_view to_string(connection_state state) noexcept
Convert connection state to string.
Definition core.cppm:397
Result< std::monostate > VoidResult
Network-specific error and result type definitions.
std::size_t operator()(const asio::ip::udp::endpoint &ep) const
Thread system integration interface for network_system.