|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
Manages a single connected secure (TLS/SSL) client session on the server side, providing asynchronous encrypted read/write operations. More...
#include <secure_session.h>


Public Member Functions | |
| secure_session (asio::ip::tcp::socket socket, asio::ssl::context &ssl_context, std::string_view server_id) | |
Constructs a secure session with a given socket, SSL context, and server_id. | |
| ~secure_session () noexcept | |
Destructor; calls stop_session() if not already stopped. | |
| auto | start_session () -> void |
| Starts the session: performs SSL handshake, sets up read/error callbacks, and begins reading data. | |
| auto | stop_session () -> void |
| Stops the session by closing the socket and marking the session as inactive. | |
| auto | is_stopped () const noexcept -> bool |
| Checks if the session has been stopped. | |
| auto | send_packet (std::vector< uint8_t > &&data) -> void |
| Sends data to the connected client with encryption. | |
| auto | set_receive_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets the callback for received data. | |
| auto | set_disconnection_callback (std::function< void(const std::string &)> callback) -> void |
| Sets the callback for disconnection. | |
| auto | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets the callback for errors. | |
| auto | server_id () const -> const std::string & |
| Gets the server identifier. | |
Private Member Functions | |
| auto | on_receive (const std::vector< uint8_t > &data) -> void |
| Callback for when encrypted data arrives from the client. | |
| auto | on_error (std::error_code ec) -> void |
Callback for handling socket errors from secure_tcp_socket. | |
| auto | process_next_message () -> void |
| Processes pending messages from the queue. | |
Private Attributes | |
| std::string | server_id_ |
| std::shared_ptr< internal::secure_tcp_socket > | socket_ |
| std::atomic< bool > | is_stopped_ |
| std::deque< std::vector< uint8_t > > | pending_messages_ |
| Queue of pending received messages awaiting processing. | |
| std::mutex | queue_mutex_ |
| Mutex protecting access to pending_messages_ queue. | |
| std::function< void(const std::vector< uint8_t > &)> | receive_callback_ |
| Callbacks for session events. | |
| std::function< void(const std::string &)> | disconnection_callback_ |
| std::function< void(std::error_code)> | error_callback_ |
| std::mutex | callback_mutex_ |
| Mutex protecting callback access. | |
Static Private Attributes | |
| static constexpr size_t | max_pending_messages_ = 1000 |
| Maximum number of pending messages before applying backpressure. | |
Manages a single connected secure (TLS/SSL) client session on the server side, providing asynchronous encrypted read/write operations.
secure_tcp_socket for non-blocking encrypted I/O.pipeline_ before sending, and can do the reverse upon receiving data (if needed).asio::ip::tcp::socket and SSL context.start_session() performs SSL handshake and begins reading.stop_session() closes the underlying socket, stopping further I/O.Definition at line 63 of file secure_session.h.
| kcenon::network::session::secure_session::secure_session | ( | asio::ip::tcp::socket | socket, |
| asio::ssl::context & | ssl_context, | ||
| std::string_view | server_id ) |
Constructs a secure session with a given socket, SSL context, and server_id.
| socket | The asio::ip::tcp::socket (already connected). |
| ssl_context | SSL context for encryption settings. |
| server_id | An identifier for this server instance or context. |
Definition at line 11 of file secure_session.cpp.
References socket_.
|
noexcept |
Destructor; calls stop_session() if not already stopped.
Definition at line 20 of file secure_session.cpp.
References stop_session().

|
inlinenodiscardnoexcept |
Checks if the session has been stopped.
Definition at line 98 of file secure_session.h.
References is_stopped_.
|
private |
Callback for handling socket errors from secure_tcp_socket.
| ec | The std::error_code describing the error. |
By default, logs the error and calls stop_session().
Definition at line 145 of file secure_session.cpp.
References NETWORK_LOG_ERROR.
|
private |
Callback for when encrypted data arrives from the client.
| data | A vector containing a chunk of received bytes. |
Override or extend the logic here to parse messages, handle commands, etc. If decompression/decryption is needed, apply pipeline_ accordingly.
Definition at line 107 of file secure_session.cpp.
References NETWORK_LOG_DEBUG, NETWORK_LOG_ERROR, and NETWORK_LOG_WARN.
|
private |
Processes pending messages from the queue.
This method dequeues and processes messages one at a time. Implement actual message handling logic here.
Definition at line 164 of file secure_session.cpp.
References kcenon::network::message, NETWORK_LOG_DEBUG, and NETWORK_LOG_ERROR.
| auto kcenon::network::session::secure_session::send_packet | ( | std::vector< uint8_t > && | data | ) | -> void |
Sends data to the connected client with encryption.
| data | The raw bytes to transmit (moved for efficiency). |
compress_mode_ or encrypt_mode_ is true, the data will be processed by the pipeline's compress/encrypt functions before writing.Definition at line 90 of file secure_session.cpp.
References NETWORK_LOG_DEBUG, and NETWORK_LOG_ERROR.
|
inlinenodiscard |
Gets the server identifier.
Definition at line 140 of file secure_session.h.
References server_id_.
| auto kcenon::network::session::secure_session::set_disconnection_callback | ( | std::function< void(const std::string &)> | callback | ) | -> void |
Sets the callback for disconnection.
| callback | Function called when the session stops. |
Definition at line 213 of file secure_session.cpp.
| auto kcenon::network::session::secure_session::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets the callback for errors.
| callback | Function called when an error occurs. |
Definition at line 219 of file secure_session.cpp.
| auto kcenon::network::session::secure_session::set_receive_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets the callback for received data.
| callback | Function called when data is received. |
Definition at line 207 of file secure_session.cpp.
| auto kcenon::network::session::secure_session::start_session | ( | ) | -> void |
Starts the session: performs SSL handshake, sets up read/error callbacks, and begins reading data.
Definition at line 29 of file secure_session.cpp.
References NETWORK_LOG_ERROR, and NETWORK_LOG_INFO.
| auto kcenon::network::session::secure_session::stop_session | ( | ) | -> void |
Stops the session by closing the socket and marking the session as inactive.
Definition at line 63 of file secure_session.cpp.
References NETWORK_LOG_ERROR, and NETWORK_LOG_INFO.
Referenced by ~secure_session().

|
mutableprivate |
Mutex protecting callback access.
Definition at line 209 of file secure_session.h.
|
private |
Definition at line 203 of file secure_session.h.
|
private |
Definition at line 204 of file secure_session.h.
|
private |
Indicates whether this session is stopped.
Definition at line 177 of file secure_session.h.
Referenced by is_stopped().
|
staticconstexprprivate |
Maximum number of pending messages before applying backpressure.
When this limit is reached, a warning is logged. If doubled, the session is disconnected.
Definition at line 197 of file secure_session.h.
|
private |
Queue of pending received messages awaiting processing.
Definition at line 184 of file secure_session.h.
|
mutableprivate |
Mutex protecting access to pending_messages_ queue.
Definition at line 189 of file secure_session.h.
|
private |
Callbacks for session events.
Definition at line 202 of file secure_session.h.
|
private |
Identifier for the server side.
Definition at line 172 of file secure_session.h.
Referenced by server_id().
|
private |
The wrapped secure TCP socket for this session.
Definition at line 175 of file secure_session.h.
Referenced by secure_session().