|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
Manages a single connected client session on the server side, providing asynchronous read/write operations and pipeline transformations. More...
#include <messaging_session.h>


Public Member Functions | |
| messaging_session (asio::ip::tcp::socket socket, std::string_view server_id) | |
Constructs a session with a given socket and server_id. | |
| ~messaging_session () noexcept override | |
Destructor; calls stop_session() if not already stopped. | |
| auto | id () const -> std::string_view override |
| Gets the unique identifier for this session. | |
| auto | is_connected () const -> bool override |
| Checks if the session is currently connected. | |
| auto | send (std::vector< uint8_t > &&data) -> VoidResult override |
| Sends data to the client. | |
| auto | close () -> void override |
| Closes the session. | |
| auto | start_session () -> void |
| Starts the session: 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, optionally using compression/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. | |
Public Member Functions inherited from kcenon::network::interfaces::i_session | |
| virtual | ~i_session ()=default |
| Virtual destructor for proper cleanup. | |
| i_session (const i_session &)=delete | |
| i_session & | operator= (const i_session &)=delete |
| i_session (i_session &&)=delete | |
| i_session & | operator= (i_session &&)=delete |
Private Member Functions | |
| auto | on_receive (std::span< const uint8_t > data) -> void |
| Callback for when data arrives from the client. | |
| auto | on_error (std::error_code ec) -> void |
Callback for handling socket errors from tcp_socket. | |
| auto | process_next_message () -> void |
| Processes pending messages from the queue. | |
Private Attributes | |
| std::string | server_id_ |
| std::shared_ptr< internal::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. | |
Additional Inherited Members | |
Protected Member Functions inherited from kcenon::network::interfaces::i_session | |
| i_session ()=default | |
| Default constructor (only for derived classes) | |
Manages a single connected client session on the server side, providing asynchronous read/write operations and pipeline transformations.
This class implements the i_session interface, providing a composition-based design for TCP sessions. It replaces the previous inheritance-heavy approach with a cleaner interface-based model.
tcp_socket for non-blocking I/O.pipeline_ before sending, and can do the reverse upon receiving data (if needed).on_receive, on_error) for data handling and error detection.asio::ip::tcp::socket.start_session() sets up callbacks and begins socket_->start_read().stop_session() closes the underlying socket, stopping further I/O.This class implements interfaces::i_session for composition-based usage.
Definition at line 77 of file messaging_session.h.
| kcenon::network::session::messaging_session::messaging_session | ( | asio::ip::tcp::socket | socket, |
| std::string_view | server_id ) |
Constructs a session with a given socket and server_id.
| socket | The asio::ip::tcp::socket (already connected). |
| server_id | An identifier for this server instance or context. |
Definition at line 18 of file messaging_session.cpp.
References socket_.
|
overridenoexcept |
Destructor; calls stop_session() if not already stopped.
Definition at line 26 of file messaging_session.cpp.
References stop_session().

|
inlineoverridevirtual |
Closes the session.
Implements i_session::close(). Delegates to stop_session().
Implements kcenon::network::interfaces::i_session.
Definition at line 136 of file messaging_session.h.
References stop_session().

|
inlinenodiscardoverridevirtual |
Gets the unique identifier for this session.
Implements i_session::id(). Returns the server_id that was provided during construction.
Implements kcenon::network::interfaces::i_session.
Definition at line 107 of file messaging_session.h.
References server_id_.
|
inlinenodiscardoverridevirtual |
Checks if the session is currently connected.
Implements i_session::is_connected(). Returns the opposite of is_stopped().
Implements kcenon::network::interfaces::i_session.
Definition at line 118 of file messaging_session.h.
References is_stopped_.
|
inlinenodiscardnoexcept |
Checks if the session has been stopped.
Definition at line 158 of file messaging_session.h.
References is_stopped_.
|
private |
Callback for handling socket errors from tcp_socket.
| ec | The std::error_code describing the error. |
By default, logs the error and calls stop_session().
Definition at line 180 of file messaging_session.cpp.
|
private |
Callback for when data arrives from the client.
| data | A span view containing a chunk of received bytes. |
The span provides a non-owning view directly into the socket's internal read buffer, avoiding per-read vector allocations.
Override or extend the logic here to parse messages, handle commands, etc. If decompression/decryption is needed, apply pipeline_ accordingly.
Definition at line 145 of file messaging_session.cpp.
References kcenon::network::metrics::metric_reporter::report_bytes_received().

|
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 204 of file messaging_session.cpp.
References kcenon::network::message.
|
nodiscardoverridevirtual |
Sends data to the client.
| data | The data to send. |
Implements i_session::send(). Wraps send_packet() with Result type.
Implements kcenon::network::interfaces::i_session.
Definition at line 121 of file messaging_session.cpp.
References kcenon::network::error_codes::network_system::connection_closed, kcenon::network::error_void(), kcenon::network::error_codes::common_errors::invalid_argument, and kcenon::network::ok().

| auto kcenon::network::session::messaging_session::send_packet | ( | std::vector< uint8_t > && | data | ) | -> void |
Sends data to the connected client, optionally using compression/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 103 of file messaging_session.cpp.
References kcenon::network::metrics::metric_reporter::report_bytes_sent().

|
inlinenodiscard |
Gets the server identifier.
Definition at line 210 of file messaging_session.h.
References server_id_.
| auto kcenon::network::session::messaging_session::set_disconnection_callback | ( | std::function< void(const std::string &)> | callback | ) | -> void |
Sets the callback for disconnection.
| callback | Function called when the session stops. |
The callback receives the server_id as identification.
Definition at line 257 of file messaging_session.cpp.
| auto kcenon::network::session::messaging_session::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets the callback for errors.
| callback | Function called when an error occurs. |
The callback receives the error code.
Definition at line 264 of file messaging_session.cpp.
| auto kcenon::network::session::messaging_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. |
The callback receives a const reference to the received data. It is invoked on the I/O thread, so keep processing minimal or dispatch to a worker thread.
Definition at line 250 of file messaging_session.cpp.
| auto kcenon::network::session::messaging_session::start_session | ( | ) | -> void |
Starts the session: sets up read/error callbacks and begins reading data.
Definition at line 39 of file messaging_session.cpp.
| auto kcenon::network::session::messaging_session::stop_session | ( | ) | -> void |
Stops the session by closing the socket and marking the session as inactive.
Definition at line 73 of file messaging_session.cpp.
Referenced by close(), and ~messaging_session().

|
mutableprivate |
Mutex protecting callback access.
Definition at line 287 of file messaging_session.h.
|
private |
Definition at line 281 of file messaging_session.h.
|
private |
Definition at line 282 of file messaging_session.h.
|
private |
Indicates whether this session is stopped.
Definition at line 255 of file messaging_session.h.
Referenced by is_connected(), and 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 275 of file messaging_session.h.
|
private |
Queue of pending received messages awaiting processing.
Definition at line 262 of file messaging_session.h.
|
mutableprivate |
Mutex protecting access to pending_messages_ queue.
Definition at line 267 of file messaging_session.h.
|
private |
Callbacks for session events.
Definition at line 280 of file messaging_session.h.
|
private |
Identifier for the server side.
Definition at line 250 of file messaging_session.h.
Referenced by id(), and server_id().
|
private |
The wrapped TCP socket for this session.
Definition at line 253 of file messaging_session.h.
Referenced by messaging_session().