|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
A lightweight wrapper around asio::ssl::stream<asio::ip::tcp::socket>, enabling asynchronous TLS/SSL encrypted read and write operations.
More...
#include <secure_tcp_socket.h>


Public Types | |
| using | ssl_socket = asio::ssl::stream<asio::ip::tcp::socket> |
Public Member Functions | |
| secure_tcp_socket (asio::ip::tcp::socket socket, asio::ssl::context &ssl_context) | |
Constructs a secure_tcp_socket by taking ownership of a moved socket and SSL context. | |
| ~secure_tcp_socket ()=default | |
| Default destructor (no special cleanup needed). | |
| auto | async_handshake (asio::ssl::stream_base::handshake_type type, std::function< void(std::error_code)> handler) -> void |
| Performs asynchronous SSL handshake. | |
| auto | set_receive_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets a callback to receive inbound data chunks. | |
| auto | set_receive_callback_view (std::function< void(std::span< const uint8_t >)> callback) -> void |
| Sets a zero-copy callback to receive inbound data as a view. | |
| auto | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets a callback to handle socket errors (e.g., read/write failures). | |
| auto | start_read () -> void |
| Begins the continuous asynchronous read loop. | |
| auto | async_send (std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> void |
Initiates an asynchronous write of the given data buffer with encryption. | |
| auto | stream () -> ssl_socket & |
| Provides direct access to the underlying SSL stream. | |
| auto | socket () -> ssl_socket::lowest_layer_type & |
| Provides access to the underlying TCP socket. | |
| auto | stop_read () -> void |
| Stops the read loop to prevent further async operations. | |
| auto | close () -> void |
| Safely closes the socket and stops all async operations. | |
| auto | is_closed () const -> bool |
| Checks if the socket has been closed. | |
Private Types | |
| using | receive_callback_t = std::function<void(const std::vector<uint8_t>&)> |
| Callback type aliases for lock-free storage. | |
| using | receive_callback_view_t = std::function<void(std::span<const uint8_t>)> |
| using | error_callback_t = std::function<void(std::error_code)> |
Private Member Functions | |
| auto | do_read () -> void |
Internal function to handle the read logic with async_read_some(). | |
Private Attributes | |
| ssl_socket | ssl_stream_ |
| std::array< uint8_t, 4096 > | read_buffer_ |
| std::shared_ptr< receive_callback_t > | receive_callback_ |
| std::shared_ptr< receive_callback_view_t > | receive_callback_view_ |
| std::shared_ptr< error_callback_t > | error_callback_ |
| std::mutex | callback_mutex_ |
| std::atomic< bool > | is_reading_ {false} |
| std::atomic< bool > | is_closed_ {false} |
A lightweight wrapper around asio::ssl::stream<asio::ip::tcp::socket>, enabling asynchronous TLS/SSL encrypted read and write operations.
ssl_stream_ (from ASIO SSL) for secure TCP communication.set_receive_callback() to handle inbound encrypted data and set_error_callback() for error handling.start_read() begins an ongoing loop of async_read_some().async_send() performs an async_write of a given data buffer with encryption.Definition at line 46 of file secure_tcp_socket.h.
|
private |
Definition at line 217 of file secure_tcp_socket.h.
|
private |
Callback type aliases for lock-free storage.
Definition at line 215 of file secure_tcp_socket.h.
|
private |
Definition at line 216 of file secure_tcp_socket.h.
| using kcenon::network::internal::secure_tcp_socket::ssl_socket = asio::ssl::stream<asio::ip::tcp::socket> |
Definition at line 49 of file secure_tcp_socket.h.
| kcenon::network::internal::secure_tcp_socket::secure_tcp_socket | ( | asio::ip::tcp::socket | socket, |
| asio::ssl::context & | ssl_context ) |
Constructs a secure_tcp_socket by taking ownership of a moved socket and SSL context.
| socket | An asio::ip::tcp::socket that must be open/connected or at least valid. |
| ssl_context | SSL context for encryption settings |
After construction, you must call async_handshake() before using the socket for data transmission.
Definition at line 14 of file secure_tcp_socket.cpp.
|
default |
Default destructor (no special cleanup needed).
| auto kcenon::network::internal::secure_tcp_socket::async_handshake | ( | asio::ssl::stream_base::handshake_type | type, |
| std::function< void(std::error_code)> | handler ) -> void |
Performs asynchronous SSL handshake.
| type | Handshake type (client or server) |
| handler | Completion handler with signature void(std::error_code) |
Must be called before start_read() or async_send().
Definition at line 20 of file secure_tcp_socket.cpp.
| auto kcenon::network::internal::secure_tcp_socket::async_send | ( | std::vector< uint8_t > && | data, |
| std::function< void(std::error_code, std::size_t)> | handler ) -> void |
Initiates an asynchronous write of the given data buffer with encryption.
| data | The buffer to send over TLS (moved for efficiency). |
| handler | A completion handler with signature void(std::error_code, std::size_t) that is invoked upon success or failure. |
The handler receives:
ec : the std::error_code from the write operation,bytes_transferred : how many bytes were actually written.Definition at line 169 of file secure_tcp_socket.cpp.
| auto kcenon::network::internal::secure_tcp_socket::close | ( | ) | -> void |
Safely closes the socket and stops all async operations.
This method atomically sets the closed flag before closing the socket, preventing data races between the close operation and async read operations. Thread-safe with respect to concurrent async operations.
Definition at line 76 of file secure_tcp_socket.cpp.
|
private |
Internal function to handle the read logic with async_read_some().
Upon success, it calls receive_callback_ if set, then schedules another read. On error, it calls error_callback_ if available.
Definition at line 93 of file secure_tcp_socket.cpp.
|
nodiscard |
Checks if the socket has been closed.
Definition at line 88 of file secure_tcp_socket.cpp.
References is_closed_.
| auto kcenon::network::internal::secure_tcp_socket::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets a callback to handle socket errors (e.g., read/write failures).
| 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 (beyond stopping reads).
Definition at line 55 of file secure_tcp_socket.cpp.
| auto kcenon::network::internal::secure_tcp_socket::set_receive_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets a callback to receive inbound data chunks.
| callback | A function with signature void(const
std::vector<uint8_t>&), called whenever a chunk of data is successfully read and decrypted. |
If no callback is set, received data is effectively discarded.
Definition at line 39 of file secure_tcp_socket.cpp.
| auto kcenon::network::internal::secure_tcp_socket::set_receive_callback_view | ( | std::function< void(std::span< const uint8_t >)> | callback | ) | -> void |
Sets a zero-copy callback to receive inbound data as a view.
| callback | A function with signature void(std::span<const
uint8_t>), called whenever a chunk of data is successfully read and decrypted. |
Unlike set_receive_callback(), this callback receives data as a non-owning view directly into the internal read buffer, avoiding per-read std::vector allocations and copies.
Definition at line 47 of file secure_tcp_socket.cpp.
|
inline |
Provides access to the underlying TCP socket.
Definition at line 178 of file secure_tcp_socket.h.
References ssl_stream_.
| auto kcenon::network::internal::secure_tcp_socket::start_read | ( | ) | -> void |
Begins the continuous asynchronous read loop.
Once called, the class repeatedly calls async_read_some(). If an error occurs, on_error() is triggered, stopping further reads.
Definition at line 63 of file secure_tcp_socket.cpp.
| auto kcenon::network::internal::secure_tcp_socket::stop_read | ( | ) | -> void |
Stops the read loop to prevent further async operations.
Definition at line 70 of file secure_tcp_socket.cpp.
|
inline |
Provides direct access to the underlying SSL stream.
asio::ssl::stream. Definition at line 172 of file secure_tcp_socket.h.
References ssl_stream_.
|
private |
Protects callback registration only.
Definition at line 232 of file secure_tcp_socket.h.
|
private |
Definition at line 230 of file secure_tcp_socket.h.
|
private |
Flag to indicate socket is closed.
Definition at line 235 of file secure_tcp_socket.h.
Referenced by is_closed().
|
private |
|
private |
Buffer for receiving data in do_read().
Definition at line 222 of file secure_tcp_socket.h.
|
private |
Lock-free callback storage using shared_ptr + atomic operations. This eliminates mutex contention on the receive hot path.
Definition at line 228 of file secure_tcp_socket.h.
|
private |
Definition at line 229 of file secure_tcp_socket.h.
|
private |
The underlying ASIO SSL stream.
Definition at line 219 of file secure_tcp_socket.h.