79 ,
public std::enable_shared_from_this<messaging_session>
107 [[nodiscard]]
auto id() const -> std::string_view
override {
119 return !
is_stopped_.load(std::memory_order_acquire);
129 [[nodiscard]]
auto send(std::vector<uint8_t>&& data) ->
VoidResult override;
159 return is_stopped_.load(std::memory_order_relaxed);
175 auto send_packet(std::vector<uint8_t>&& data) -> void;
186 std::function<
void(
const std::vector<uint8_t>&)> callback) -> void;
195 std::function<
void(
const std::string&)> callback) -> void;
204 std::function<
void(std::error_code)> callback) -> void;
210 [[nodiscard]]
auto server_id() const -> const std::
string& {
231 auto on_receive(std::span<const uint8_t> data) -> void;
239 auto on_error(std::error_code ec) -> void;
252 std::shared_ptr<internal::tcp_socket>
Interface for a single client session on the server side.
A lightweight wrapper around asio::ip::tcp::socket, enabling asynchronous read and write operations.
Manages a single connected client session on the server side, providing asynchronous read/write opera...
auto start_session() -> void
Starts the session: sets up read/error callbacks and begins reading data.
std::deque< std::vector< uint8_t > > pending_messages_
Queue of pending received messages awaiting processing.
static constexpr size_t max_pending_messages_
Maximum number of pending messages before applying backpressure.
auto id() const -> std::string_view override
Gets the unique identifier for this session.
auto server_id() const -> const std::string &
Gets the server identifier.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the client.
auto process_next_message() -> void
Processes pending messages from the queue.
std::function< void(const std::vector< uint8_t > &)> receive_callback_
Callbacks for session events.
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.
std::function< void(const std::string &)> disconnection_callback_
std::function< void(std::error_code)> error_callback_
std::mutex queue_mutex_
Mutex protecting access to pending_messages_ queue.
std::atomic< bool > is_stopped_
auto on_error(std::error_code ec) -> void
Callback for handling socket errors from tcp_socket.
~messaging_session() noexcept override
Destructor; calls stop_session() if not already stopped.
auto is_connected() const -> bool override
Checks if the session is currently connected.
auto on_receive(std::span< const uint8_t > data) -> void
Callback for when data arrives from the client.
auto close() -> void override
Closes the session.
std::mutex callback_mutex_
Mutex protecting callback access.
messaging_session(asio::ip::tcp::socket socket, std::string_view server_id)
Constructs a session with a given socket and server_id.
auto set_disconnection_callback(std::function< void(const std::string &)> callback) -> void
Sets the callback for disconnection.
auto is_stopped() const noexcept -> bool
Checks if the session has been stopped.
auto stop_session() -> void
Stops the session by closing the socket and marking the session as inactive.
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets the callback for errors.
std::shared_ptr< internal::tcp_socket > socket_
Session interface representing an active client-server connection.