Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
messaging_session.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
22#include <atomic>
23#include <deque>
24#include <functional>
25#include <memory>
26#include <mutex>
27#include <span>
28#include <string>
29#include <string_view>
30#include <vector>
31
32#include <asio.hpp>
33
35
37{
38 class tcp_socket;
39}
40
41// Use nested namespace definition in C++17
43{
44
79 , public std::enable_shared_from_this<messaging_session>
80 {
81 public:
87 // Using string_view in constructor for efficiency (C++17)
88 messaging_session(asio::ip::tcp::socket socket,
89 std::string_view server_id);
90
94 ~messaging_session() noexcept override;
95
96 // ========================================================================
97 // i_session interface implementation
98 // ========================================================================
99
107 [[nodiscard]] auto id() const -> std::string_view override {
108 return server_id_;
109 }
110
118 [[nodiscard]] auto is_connected() const -> bool override {
119 return !is_stopped_.load(std::memory_order_acquire);
120 }
121
129 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
130
136 auto close() -> void override { stop_session(); }
137
138 // ========================================================================
139 // Legacy API (maintained for backward compatibility)
140 // ========================================================================
141
146 auto start_session() -> void;
147
152 auto stop_session() -> void;
153
158 [[nodiscard]] auto is_stopped() const noexcept -> bool {
159 return is_stopped_.load(std::memory_order_relaxed);
160 }
161
175 auto send_packet(std::vector<uint8_t>&& data) -> void;
176
186 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
187
195 std::function<void(const std::string&)> callback) -> void;
196
204 std::function<void(std::error_code)> callback) -> void;
205
210 [[nodiscard]] auto server_id() const -> const std::string& {
211 return server_id_;
212 }
213
214 private:
231 auto on_receive(std::span<const uint8_t> data) -> void;
232
239 auto on_error(std::error_code ec) -> void;
240
247 auto process_next_message() -> void;
248
249 private:
250 std::string server_id_;
252 std::shared_ptr<internal::tcp_socket>
255 std::atomic<bool> is_stopped_{
256 false
257 };
262 std::deque<std::vector<uint8_t>> pending_messages_;
263
267 mutable std::mutex queue_mutex_;
268
275 static constexpr size_t max_pending_messages_ = 1000;
276
280 std::function<void(const std::vector<uint8_t>&)> receive_callback_;
281 std::function<void(const std::string&)> disconnection_callback_;
282 std::function<void(std::error_code)> error_callback_;
283
287 mutable std::mutex callback_mutex_;
288 };
289
290} // namespace kcenon::network::session
Interface for a single client session on the server side.
Definition i_session.h:49
A lightweight wrapper around asio::ip::tcp::socket, enabling asynchronous read and write operations.
Definition tcp_socket.h:44
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.
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.