23#include <unordered_map>
27#include <openssl/ssl.h>
86 class secure_messaging_udp_server :
public std::enable_shared_from_this<secure_messaging_udp_server>
91 const asio::ip::udp::endpoint&)>;
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;
190 return is_running_.load(std::memory_order_acquire);
205 std::shared_ptr<internal::dtls_socket>
socket;
224 const asio::ip::udp::endpoint& sender) -> void;
229 auto create_session(
const asio::ip::udp::endpoint& client_endpoint)
230 -> std::shared_ptr<dtls_session>;
237 std::size_t
operator()(
const asio::ip::udp::endpoint& ep)
const
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);
254 std::unique_ptr<asio::ip::udp::socket>
socket_;
267 std::unordered_map<asio::ip::udp::endpoint, std::shared_ptr<dtls_session>,
endpoint_hash>
A secure UDP server using DTLS (Datagram TLS) for encrypted communication.
std::future< void > stop_future_
asio::ip::udp::endpoint sender_endpoint_
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.
udp_receive_callback_t receive_callback_
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::unique_ptr< asio::ip::udp::socket > socket_
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> udp_receive_callback_t
UDP-specific callback types with endpoint information.
std::future< void > io_context_future_
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::mutex sessions_mutex_
std::atomic< bool > is_running_
std::function< void(std::error_code)> error_callback_
std::optional< std::promise< void > > stop_promise_
std::function< void(const asio::ip::udp::endpoint &)> udp_client_callback_t
udp_client_callback_t client_connected_callback_
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.
udp_client_callback_t client_disconnected_callback_
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.
std::mutex callback_mutex_
~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.
std::array< uint8_t, 65536 > read_buffer_
std::unique_ptr< asio::io_context > io_context_
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
constexpr std::string_view to_string(connection_state state) noexcept
Convert connection state to string.
Result< std::monostate > VoidResult
Network-specific error and result type definitions.
DTLS session for a client.
std::shared_ptr< internal::dtls_socket > socket
Hash function for endpoint (for unordered_map).
std::size_t operator()(const asio::ip::udp::endpoint &ep) const
Thread system integration interface for network_system.