ORE Studio 0.0.4
Loading...
Searching...
No Matches
server.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_HPP
21#define ORES_COMMS_NET_SERVER_HPP
22
23#include <list>
24#include <mutex>
25#include <memory>
26#include <atomic>
27#include <functional>
28#include <string_view>
29#include <boost/asio/ssl.hpp>
30#include <boost/asio/ip/tcp.hpp>
31#include <boost/asio/awaitable.hpp>
32#include <boost/asio/io_context.hpp>
33#include <boost/asio/cancellation_signal.hpp>
34#include "ores.utility/log/make_logger.hpp"
35#include "ores.comms/net/server_options.hpp"
36#include "ores.comms/messaging/message_dispatcher.hpp"
37#include "ores.comms/messaging/message_handler.hpp"
38#include "ores.comms/service/auth_session_service.hpp"
39
40namespace ores::comms::net { class server_session; }
41namespace ores::comms::service { class subscription_manager; }
42
43namespace ores::comms::net {
44
45using tcp = boost::asio::ip::tcp;
46namespace ssl = boost::asio::ssl;
47
53class server final : public std::enable_shared_from_this<server> {
54private:
55 inline static std::string_view logger_name =
56 "ores.comms.net.server";
57
58 static auto& lg() {
59 using namespace ores::utility::log;
60 static auto instance = make_logger(logger_name);
61 return instance;
62 }
63
64public:
71 explicit server(server_options options,
72 std::shared_ptr<service::subscription_manager> subscription_mgr = nullptr);
73
80 std::shared_ptr<messaging::message_handler> handler);
81
88 [[nodiscard]] std::shared_ptr<service::auth_session_service> sessions() const {
89 return sessions_;
90 }
91
100 boost::asio::awaitable<void> run(boost::asio::io_context& io_context,
101 std::function<void(std::uint16_t)> on_listening = nullptr);
102
108 void stop();
109
119 void broadcast_database_status(bool available, const std::string& error_message);
120
121private:
125 boost::asio::awaitable<void> accept_loop(boost::asio::io_context& io_context,
126 std::function<void(std::uint16_t)> on_listening);
127
131 void setup_ssl_context();
132
136 boost::asio::awaitable<void> watch_for_stop_signals(boost::asio::io_context& io_context);
137
138 server_options options_;
139 ssl::context ssl_ctx_;
140 std::shared_ptr<service::auth_session_service> sessions_;
141 std::shared_ptr<messaging::message_dispatcher> dispatcher_;
142 std::shared_ptr<service::subscription_manager> subscription_mgr_;
143 std::atomic<std::size_t> active_connections_{0};
144 boost::asio::cancellation_signal stop_signal_;
145 std::list<std::shared_ptr<server_session>> active_sessions_;
146 std::mutex sessions_mutex_;
147};
148
149}
150
151#endif
Contains the networking elements of the comms library.
Definition client.hpp:42
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
Range of message types handled by a subsystem.
Definition message_handler.hpp:37
ORES protocol server.
Definition server.hpp:53
void register_handler(messaging::message_type_range range, std::shared_ptr< messaging::message_handler > handler)
Register a message handler for a range of message types.
Definition server.cpp:47
boost::asio::awaitable< void > run(boost::asio::io_context &io_context, std::function< void(std::uint16_t)> on_listening=nullptr)
Run the server.
Definition server.cpp:136
void stop()
Stop the server.
Definition server.cpp:52
void broadcast_database_status(bool available, const std::string &error_message)
Broadcast database status to all connected clients.
Definition server.cpp:71
std::shared_ptr< service::auth_session_service > sessions() const
Get the shared auth session service.
Definition server.hpp:88
Configuration for the server.
Definition server_options.hpp:33