12 asio::ssl::context &ssl_context,
13 std::string_view server_id)
14 : server_id_(server_id) {
16 socket_ = std::make_shared<internal::secure_tcp_socket>(std::move(socket),
30 if (is_stopped_.load()) {
35 auto self = shared_from_this();
36 socket_->async_handshake(
37 asio::ssl::stream_base::server, [
this, self](std::error_code ec) {
48 socket_->set_receive_callback(
49 [
this, self](
const std::vector<uint8_t> &data) {
52 socket_->set_error_callback(
53 [
this, self](std::error_code ec) { on_error(ec); });
56 socket_->start_read();
64 if (is_stopped_.exchange(
true)) {
75 std::lock_guard<std::mutex> lock(callback_mutex_);
76 if (disconnection_callback_) {
78 disconnection_callback_(server_id_);
79 }
catch (
const std::exception &e) {
81 "[secure_session] Exception in disconnection callback: " +
82 std::string(e.what()));
91 if (is_stopped_.load()) {
97 std::move(data), [](std::error_code ec, std::size_t bytes_transferred) {
102 std::to_string(bytes_transferred) +
" bytes");
108 if (is_stopped_.load()) {
117 std::lock_guard<std::mutex> lock(queue_mutex_);
118 size_t queue_size = pending_messages_.size();
121 if (queue_size >= max_pending_messages_) {
123 std::to_string(queue_size) +
") reached limit (" +
124 std::to_string(max_pending_messages_) +
125 "). Applying backpressure.");
128 if (queue_size >= max_pending_messages_ * 2) {
130 std::to_string(queue_size) +
131 "). Disconnecting abusive client.");
138 pending_messages_.push_back(data);
142 process_next_message();
150 std::lock_guard<std::mutex> lock(callback_mutex_);
151 if (error_callback_) {
154 }
catch (
const std::exception &e) {
156 std::string(e.what()));
169 std::lock_guard<std::mutex> lock(queue_mutex_);
170 if (pending_messages_.empty()) {
174 message = std::move(pending_messages_.front());
175 pending_messages_.pop_front();
180 "[secure_session] Processing message of " +
181 std::to_string(
message.size()) +
182 " bytes. Queue remaining: " + std::to_string(pending_messages_.size()));
186 std::lock_guard<std::mutex> lock(callback_mutex_);
187 if (receive_callback_) {
190 }
catch (
const std::exception &e) {
192 std::string(e.what()));
199 std::lock_guard<std::mutex> lock(queue_mutex_);
200 if (!pending_messages_.empty()) {
208 std::function<
void(
const std::vector<uint8_t> &)> callback) ->
void {
209 std::lock_guard<std::mutex> lock(callback_mutex_);
210 receive_callback_ = std::move(callback);
214 std::function<
void(
const std::string &)> callback) ->
void {
215 std::lock_guard<std::mutex> lock(callback_mutex_);
216 disconnection_callback_ = std::move(callback);
220 std::function<
void(std::error_code)> callback) ->
void {
221 std::lock_guard<std::mutex> lock(callback_mutex_);
222 error_callback_ = std::move(callback);
auto stop_session() -> void
Stops the session by closing the socket and marking the session as inactive.
auto process_next_message() -> void
Processes pending messages from the queue.
std::shared_ptr< internal::secure_tcp_socket > socket_
~secure_session() noexcept
Destructor; calls stop_session() if not already stopped.
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 on_receive(const std::vector< uint8_t > &data) -> void
Callback for when encrypted data arrives from the client.
Logger system integration interface for network_system.
#define NETWORK_LOG_WARN(msg)
#define NETWORK_LOG_INFO(msg)
#define NETWORK_LOG_ERROR(msg)
#define NETWORK_LOG_DEBUG(msg)
TLS-secured session wrapper for encrypted communication.