18#include <unordered_map>
32#ifdef BUILD_TLS_SUPPORT
33#include <openssl/ssl.h>
39#ifdef BUILD_TLS_SUPPORT
87template <policy::TlsPolicy TlsPolicy = policy::no_tls>
89 :
public std::enable_shared_from_this<unified_udp_messaging_server<TlsPolicy>>
98 std::function<void(
const std::vector<uint8_t>&,
const asio::ip::udp::endpoint&)>;
162 [[nodiscard]] auto
server_id() const -> const std::
string&;
186 std::vector<uint8_t>&& data,
226 const asio::ip::
udp::endpoint& endpoint,
227 std::function<
void(std::error_code, std::
size_t)> handler =
nullptr)
231#ifdef BUILD_TLS_SUPPORT
237 std::shared_ptr<internal::dtls_socket> socket;
238 bool handshake_complete{
false};
246 std::size_t operator()(
const asio::ip::udp::endpoint& ep)
const
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);
262#ifdef BUILD_TLS_SUPPORT
265 auto do_receive() ->
void
267 auto process_session_data(
const std::vector<uint8_t>& data,
268 const asio::ip::udp::endpoint& sender) ->
void
270 auto create_session(
const asio::ip::udp::endpoint& client_endpoint)
271 -> std::shared_ptr<dtls_session>
280 const asio::ip::udp::endpoint& endpoint) -> void;
307 std::shared_ptr<internal::udp_socket>
socket_;
312#ifdef BUILD_TLS_SUPPORT
314 std::unique_ptr<asio::ip::udp::socket> dtls_socket_;
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>,
337#ifdef BUILD_TLS_SUPPORT
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.
utils::lifecycle_manager lifecycle_
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::unique_ptr< asio::io_context > io_context_
std::shared_ptr< integration::thread_pool_interface > thread_pool_
std::shared_ptr< internal::udp_socket > socket_
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.
std::future< void > io_context_future_
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.
auto do_stop_impl() -> VoidResult
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
Component lifecycle management (start, stop, restart).
constexpr bool is_tls_enabled_v
Helper variable template to check if TLS is enabled at compile time.
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.
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).