|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
A wrapper around ASIO UDP socket with OpenSSL DTLS encryption. More...
#include <dtls_socket.h>


Public Types | |
| enum class | handshake_type { client , server } |
| Handshake type enumeration. More... | |
Public Member Functions | |
| dtls_socket (asio::ip::udp::socket socket, SSL_CTX *ssl_ctx) | |
Constructs a dtls_socket with an existing UDP socket. | |
| ~dtls_socket () | |
| Destructor. Cleans up OpenSSL resources. | |
| dtls_socket (const dtls_socket &)=delete | |
| dtls_socket & | operator= (const dtls_socket &)=delete |
| dtls_socket (dtls_socket &&)=delete | |
| dtls_socket & | operator= (dtls_socket &&)=delete |
| auto | async_handshake (handshake_type type, std::function< void(std::error_code)> handler) -> void |
| Performs asynchronous DTLS handshake. | |
| auto | set_receive_callback (std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> callback) -> void |
| Sets a callback to receive decrypted inbound datagrams. | |
| auto | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets a callback to handle socket errors. | |
| auto | start_receive () -> void |
| Begins the continuous asynchronous receive loop. | |
| auto | stop_receive () -> void |
| Stops the receive loop. | |
| auto | async_send (std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> void |
| Initiates an asynchronous encrypted send. | |
| 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) -> void |
| Initiates an asynchronous encrypted send to a specific endpoint. | |
| auto | set_peer_endpoint (const asio::ip::udp::endpoint &endpoint) -> void |
| Sets the peer endpoint for connected mode. | |
| auto | peer_endpoint () const -> asio::ip::udp::endpoint |
| Returns the peer endpoint. | |
| auto | socket () -> asio::ip::udp::socket & |
| Provides direct access to the underlying UDP socket. | |
| auto | is_handshake_complete () const -> bool |
| Checks if the DTLS handshake is complete. | |
Private Member Functions | |
| auto | do_receive () -> void |
| Internal function to handle the receive logic. | |
| auto | process_received_data (const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &sender) -> void |
| Processes received encrypted data through DTLS. | |
| auto | flush_bio_output () -> void |
| Flushes pending DTLS output to the network. | |
| auto | continue_handshake () -> void |
| Continues the handshake process. | |
| auto | make_ssl_error () const -> std::error_code |
| Creates an OpenSSL error code from the current error state. | |
Private Attributes | |
| asio::ip::udp::socket | socket_ |
| asio::ip::udp::endpoint | peer_endpoint_ |
| asio::ip::udp::endpoint | sender_endpoint_ |
| SSL_CTX * | ssl_ctx_ |
| SSL * | ssl_ |
| BIO * | rbio_ |
| BIO * | wbio_ |
| std::array< uint8_t, 65536 > | read_buffer_ |
| std::mutex | ssl_mutex_ |
| std::mutex | callback_mutex_ |
| std::mutex | endpoint_mutex_ |
| std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> | receive_callback_ |
| std::function< void(std::error_code)> | error_callback_ |
| std::function< void(std::error_code)> | handshake_callback_ |
| std::atomic< bool > | is_receiving_ {false} |
| std::atomic< bool > | handshake_complete_ {false} |
| std::atomic< bool > | handshake_in_progress_ {false} |
| handshake_type | handshake_type_ {handshake_type::client} |
A wrapper around ASIO UDP socket with OpenSSL DTLS encryption.
set_receive_callback() to handle decrypted inbound datagrams and set_error_callback() for error handling.start_receive() begins an ongoing loop of receiving encrypted datagrams.async_send() encrypts and sends data to the configured peer.Definition at line 48 of file dtls_socket.h.
|
strong |
Handshake type enumeration.
| Enumerator | |
|---|---|
| client | Client-side handshake |
| server | Server-side handshake |
Definition at line 54 of file dtls_socket.h.
| kcenon::network::internal::dtls_socket::dtls_socket | ( | asio::ip::udp::socket | socket, |
| SSL_CTX * | ssl_ctx ) |
Constructs a dtls_socket with an existing UDP socket.
| socket | An asio::ip::udp::socket that must be open. |
| ssl_ctx | The OpenSSL SSL_CTX configured for DTLS. |
The socket should be connected (for client) or bound (for server) before calling handshake methods.
Definition at line 42 of file dtls_socket.cpp.
| kcenon::network::internal::dtls_socket::~dtls_socket | ( | ) |
Destructor. Cleans up OpenSSL resources.
Definition at line 78 of file dtls_socket.cpp.
References ssl_, and stop_receive().

|
delete |
|
delete |
| auto kcenon::network::internal::dtls_socket::async_handshake | ( | handshake_type | type, |
| std::function< void(std::error_code)> | handler ) -> void |
Performs asynchronous DTLS handshake.
| type | Handshake type (client or server) |
| handler | Completion handler with signature void(std::error_code) |
Must be called before start_receive() or async_send(). The handshake involves multiple round-trips over UDP.
Definition at line 89 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::async_send | ( | std::vector< uint8_t > && | data, |
| std::function< void(std::error_code, std::size_t)> | handler ) -> void |
Initiates an asynchronous encrypted send.
| data | The plaintext data to encrypt and send (moved for efficiency). |
| handler | A completion handler with signature void(std::error_code, std::size_t) invoked upon completion. |
The data is encrypted using DTLS before transmission.
Definition at line 371 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::async_send_to | ( | std::vector< uint8_t > && | data, |
| const asio::ip::udp::endpoint & | endpoint, | ||
| std::function< void(std::error_code, std::size_t)> | handler ) -> void |
Initiates an asynchronous encrypted send to a specific endpoint.
| data | The plaintext data to encrypt and send (moved for efficiency). |
| endpoint | The target endpoint. |
| handler | A completion handler invoked upon completion. |
Useful for server responding to different clients.
Definition at line 384 of file dtls_socket.cpp.
|
private |
Continues the handshake process.
Definition at line 117 of file dtls_socket.cpp.
|
private |
Internal function to handle the receive logic.
Definition at line 211 of file dtls_socket.cpp.
|
private |
Flushes pending DTLS output to the network.
Definition at line 330 of file dtls_socket.cpp.
|
inline |
Checks if the DTLS handshake is complete.
Definition at line 177 of file dtls_socket.h.
References handshake_complete_.
|
private |
Creates an OpenSSL error code from the current error state.
Definition at line 472 of file dtls_socket.cpp.
|
delete |
|
delete |
| auto kcenon::network::internal::dtls_socket::peer_endpoint | ( | ) | const -> asio::ip::udp::endpoint |
Returns the peer endpoint.
Definition at line 466 of file dtls_socket.cpp.
References endpoint_mutex_, and peer_endpoint_.
|
private |
Processes received encrypted data through DTLS.
| data | The encrypted datagram. |
| sender | The sender's endpoint. |
Definition at line 258 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets a callback to handle socket errors.
| callback | A function with signature void(std::error_code), invoked when any asynchronous operation fails. |
If no callback is set, errors are not explicitly handled here.
Definition at line 190 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::set_peer_endpoint | ( | const asio::ip::udp::endpoint & | endpoint | ) | -> void |
Sets the peer endpoint for connected mode.
| endpoint | The peer's UDP endpoint. |
Definition at line 460 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::set_receive_callback | ( | std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> | callback | ) | -> void |
Sets a callback to receive decrypted inbound datagrams.
| callback | A function with signature void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&), called whenever a datagram is successfully received and decrypted. |
If no callback is set, received data is effectively discarded.
Definition at line 182 of file dtls_socket.cpp.
|
inline |
Provides direct access to the underlying UDP socket.
asio::ip::udp::socket. Definition at line 171 of file dtls_socket.h.
References socket_.
| auto kcenon::network::internal::dtls_socket::start_receive | ( | ) | -> void |
Begins the continuous asynchronous receive loop.
Once called, the class repeatedly receives encrypted datagrams, decrypts them, and invokes the receive callback. If an error occurs, the error callback is triggered.
Definition at line 197 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::stop_receive | ( | ) | -> void |
Stops the receive loop.
Definition at line 206 of file dtls_socket.cpp.
Referenced by ~dtls_socket().

|
private |
Protects callback registration.
Definition at line 225 of file dtls_socket.h.
|
private |
Protects endpoint access.
Definition at line 226 of file dtls_socket.h.
Referenced by peer_endpoint().
|
private |
Error callback.
Definition at line 231 of file dtls_socket.h.
|
private |
Handshake completion callback.
Definition at line 233 of file dtls_socket.h.
|
private |
Handshake completed flag.
Definition at line 236 of file dtls_socket.h.
Referenced by is_handshake_complete().
|
private |
|
private |
|
private |
|
private |
Peer endpoint for connected mode.
Definition at line 214 of file dtls_socket.h.
Referenced by peer_endpoint().
|
private |
|
private |
Buffer for receiving datagrams.
Definition at line 222 of file dtls_socket.h.
|
private |
Inbound data callback.
Definition at line 229 of file dtls_socket.h.
|
private |
Sender endpoint for receives.
Definition at line 215 of file dtls_socket.h.
|
private |
|
private |
OpenSSL SSL object.
Definition at line 218 of file dtls_socket.h.
Referenced by dtls_socket(), and ~dtls_socket().
|
private |
OpenSSL context (not owned).
Definition at line 217 of file dtls_socket.h.
Referenced by dtls_socket().
|
private |
Protects SSL operations.
Definition at line 224 of file dtls_socket.h.
|
private |