ORE Studio 0.0.4
Loading...
Searching...
No Matches
server_session.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_COMMS_NET_SERVER_SESSION_HPP
21#define ORES_COMMS_NET_SERVER_SESSION_HPP
22
23#include <mutex>
24#include <queue>
25#include <atomic>
26#include <memory>
27#include <string>
28#include <chrono>
29#include <boost/asio/steady_timer.hpp>
30#include <boost/asio/strand.hpp>
31#include "ores.comms/net/connection.hpp"
32#include "ores.logging/make_logger.hpp"
33#include "ores.comms/messaging/message_dispatcher.hpp"
34#include "ores.comms/service/auth_session_service.hpp"
35
36namespace ores::comms::service { class subscription_manager; }
37
38namespace ores::comms::net {
39
46class server_session final {
47private:
48 inline static std::string_view logger_name = "ores.comms.net.server_session";
49
50 static auto& lg() {
51 using namespace ores::logging;
52 static auto instance = make_logger(logger_name);
53 return instance;
54 }
55
56public:
61 std::string event_type;
62 std::chrono::system_clock::time_point timestamp;
63 std::vector<std::string> entity_ids;
64 };
65
70 bool available;
71 std::string error_message;
72 std::chrono::system_clock::time_point timestamp;
73 };
74
85 explicit server_session(std::unique_ptr<connection> conn, std::string server_id,
86 std::shared_ptr<messaging::message_dispatcher> dispatcher,
87 boost::asio::any_io_executor io_executor,
88 std::shared_ptr<service::auth_session_service> sessions,
89 std::shared_ptr<service::subscription_manager> subscription_mgr = nullptr);
90
96 boost::asio::awaitable<void> run();
97
103 void stop();
104
116 bool queue_notification(const std::string& event_type,
117 std::chrono::system_clock::time_point timestamp,
118 const std::vector<std::string>& entity_ids = {});
119
130 bool queue_database_status(bool available, const std::string& error_message,
131 std::chrono::system_clock::time_point timestamp);
132
133private:
139 boost::asio::awaitable<bool> perform_handshake();
140
144 boost::asio::awaitable<void> process_messages();
145
152 boost::asio::awaitable<void> run_notification_writer();
153
160 boost::asio::awaitable<void> send_pending_notifications();
161
167 boost::asio::awaitable<void> send_pending_database_status();
168
174 boost::asio::awaitable<void> write_frame_serialized(const messaging::frame& frame);
175
179 void register_with_subscription_manager();
180
184 void unregister_from_subscription_manager();
185
186 std::unique_ptr<connection> conn_;
187 std::string server_id_;
188 std::shared_ptr<messaging::message_dispatcher> dispatcher_;
189 std::shared_ptr<service::auth_session_service> sessions_;
190 std::shared_ptr<service::subscription_manager> subscription_mgr_;
191 std::uint32_t sequence_number_;
192 bool handshake_complete_;
193 std::atomic<bool> active_{false};
194
195 // Strand for serializing write operations
196 boost::asio::strand<boost::asio::any_io_executor> write_strand_;
197
198 // Atomic flag to serialize write operations (prevents concurrent SSL writes)
199 std::atomic<bool> write_in_progress_{false};
200
201 // Timer used as async signal for notification availability
202 boost::asio::steady_timer notification_signal_;
203
204 // Thread-safe notification queue
205 mutable std::mutex notification_mutex_;
206 std::queue<pending_notification> pending_notifications_;
207 std::queue<pending_database_status> pending_database_status_;
208
209 // Session compression type negotiated during handshake
210 messaging::compression_type session_compression_{messaging::compression_type::none};
211};
212
213}
214
215#endif
compression_type
Compression algorithm used for payload compression.
Definition message_types.hpp:259
Main server application for ORE Studio.
Definition application.hpp:30
Contains the networking elements of the comms library.
Definition client.hpp:48
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Complete frame with header and payload.
Definition frame.hpp:77
Represents a client session on the server side.
Definition server_session.hpp:46
bool queue_notification(const std::string &event_type, std::chrono::system_clock::time_point timestamp, const std::vector< std::string > &entity_ids={})
Queue a notification to be sent to this session's client.
Definition server_session.cpp:67
void stop()
Stop the session by closing its connection.
Definition server_session.cpp:54
boost::asio::awaitable< void > run()
Run the session.
Definition server_session.cpp:112
bool queue_database_status(bool available, const std::string &error_message, std::chrono::system_clock::time_point timestamp)
Queue a database status notification to be sent to this client.
Definition server_session.cpp:91
Pending notification to be sent to client.
Definition server_session.hpp:60
Pending database status notification to be sent to client.
Definition server_session.hpp:69