Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
secure_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 <string>
28#include <string_view>
29#include <vector>
30
31#include <asio.hpp>
32#include <asio/ssl.hpp>
33
34#include "kcenon/network/internal/tcp/secure_tcp_socket.h"
35
37{
38
64 : public std::enable_shared_from_this<secure_session>
65 {
66 public:
73 secure_session(asio::ip::tcp::socket socket,
74 asio::ssl::context& ssl_context,
75 std::string_view server_id);
76
80 ~secure_session() noexcept;
81
86 auto start_session() -> void;
87
92 auto stop_session() -> void;
93
98 [[nodiscard]] auto is_stopped() const noexcept -> bool {
99 return is_stopped_.load(std::memory_order_relaxed);
100 }
101
113 auto send_packet(std::vector<uint8_t>&& data) -> void;
114
120 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
121
127 std::function<void(const std::string&)> callback) -> void;
128
134 std::function<void(std::error_code)> callback) -> void;
135
140 [[nodiscard]] auto server_id() const -> const std::string& {
141 return server_id_;
142 }
143
144 private:
153 auto on_receive(const std::vector<uint8_t>& data) -> void;
154
161 auto on_error(std::error_code ec) -> void;
162
169 auto process_next_message() -> void;
170
171 private:
172 std::string server_id_;
174 std::shared_ptr<internal::secure_tcp_socket>
177 std::atomic<bool> is_stopped_{
178 false
179 };
184 std::deque<std::vector<uint8_t>> pending_messages_;
185
189 mutable std::mutex queue_mutex_;
190
197 static constexpr size_t max_pending_messages_ = 1000;
198
202 std::function<void(const std::vector<uint8_t>&)> receive_callback_;
203 std::function<void(const std::string&)> disconnection_callback_;
204 std::function<void(std::error_code)> error_callback_;
205
209 mutable std::mutex callback_mutex_;
210 };
211
212} // namespace kcenon::network::session
Manages a single connected secure (TLS/SSL) client session on the server side, providing asynchronous...
auto stop_session() -> void
Stops the session by closing the socket and marking the session as inactive.
std::function< void(const std::vector< uint8_t > &)> receive_callback_
Callbacks for session events.
auto process_next_message() -> void
Processes pending messages from the queue.
auto is_stopped() const noexcept -> bool
Checks if the session has been stopped.
std::mutex callback_mutex_
Mutex protecting callback access.
std::mutex queue_mutex_
Mutex protecting access to pending_messages_ queue.
std::shared_ptr< internal::secure_tcp_socket > socket_
~secure_session() noexcept
Destructor; calls stop_session() if not already stopped.
std::function< void(const std::string &)> disconnection_callback_
std::deque< std::vector< uint8_t > > pending_messages_
Queue of pending received messages awaiting processing.
std::function< void(std::error_code)> error_callback_
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets the callback for errors.
auto set_disconnection_callback(std::function< void(const std::string &)> callback) -> void
Sets the callback for disconnection.
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
Sets the callback for received data.
auto start_session() -> void
Starts the session: performs SSL handshake, sets up read/error callbacks, and begins reading data.
auto on_error(std::error_code ec) -> void
Callback for handling socket errors from secure_tcp_socket.
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.
auto send_packet(std::vector< uint8_t > &&data) -> void
Sends data to the connected client with encryption.
auto server_id() const -> const std::string &
Gets the server identifier.
static constexpr size_t max_pending_messages_
Maximum number of pending messages before applying backpressure.
auto on_receive(const std::vector< uint8_t > &data) -> void
Callback for when encrypted data arrives from the client.