Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
secure_messaging_server.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
5#pragma once
6
15
16#include <atomic>
17#include <functional>
18#include <future>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <string_view>
23#include <vector>
24
25#include <asio.hpp>
26#include <asio/ssl.hpp>
27
33
34// Optional monitoring support via common_system
35#if KCENON_WITH_COMMON_SYSTEM
36 #include <kcenon/common/interfaces/monitoring_interface.h>
37#endif // KCENON_WITH_COMMON_SYSTEM
38
40 class secure_session;
41}
42
44{
45
88 class secure_messaging_server
89 : public std::enable_shared_from_this<secure_messaging_server>
90 {
91 public:
93 using connection_callback_t = std::function<void(std::shared_ptr<session::secure_session>)>;
95 using disconnection_callback_t = std::function<void(const std::string&)>;
97 using receive_callback_t = std::function<void(std::shared_ptr<session::secure_session>,
98 const std::vector<uint8_t>&)>;
100 using error_callback_t = std::function<void(std::shared_ptr<session::secure_session>,
101 std::error_code)>;
102
109 secure_messaging_server(std::string_view server_id,
110 const std::string& cert_file,
111 const std::string& key_file);
112
117 ~secure_messaging_server() noexcept;
118
119 // Non-copyable, non-movable
124
125 // =====================================================================
126 // Lifecycle Management
127 // =====================================================================
128
137 [[nodiscard]] auto start_server(unsigned short port) -> VoidResult;
138
145 [[nodiscard]] auto stop_server() -> VoidResult;
146
150 auto wait_for_stop() -> void;
151
156 [[nodiscard]] auto is_running() const noexcept -> bool;
157
162 [[nodiscard]] auto server_id() const -> const std::string&;
163
164 // =====================================================================
165 // Callback Setters
166 // =====================================================================
167
172 auto set_connection_callback(connection_callback_t callback) -> void;
173
179
184 auto set_receive_callback(receive_callback_t callback) -> void;
185
190 auto set_error_callback(error_callback_t callback) -> void;
191
192#if KCENON_WITH_COMMON_SYSTEM
197 auto set_monitor(kcenon::common::interfaces::IMonitor* monitor) -> void;
198
203 auto get_monitor() const -> kcenon::common::interfaces::IMonitor*;
204#endif // KCENON_WITH_COMMON_SYSTEM
205
206 private:
207 // =====================================================================
208 // Internal Implementation Methods
209 // =====================================================================
210
218 auto do_start_impl(unsigned short port) -> VoidResult;
219
225 auto do_stop_impl() -> VoidResult;
226
227 // =====================================================================
228 // Internal Callback Helpers
229 // =====================================================================
230
235 [[nodiscard]] auto get_connection_callback() const -> connection_callback_t;
236
241 [[nodiscard]] auto get_disconnection_callback() const -> disconnection_callback_t;
242
247 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
248
253 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
254
259 auto invoke_connection_callback(std::shared_ptr<session::secure_session> session) -> void;
260
261 // =====================================================================
262 // Internal Connection Handlers
263 // =====================================================================
264
271 auto do_accept() -> void;
272
283 auto on_accept(std::error_code ec, asio::ip::tcp::socket socket)
284 -> void;
285
291 auto cleanup_dead_sessions() -> void;
292
299 auto start_cleanup_timer() -> void;
300
301 private:
304
306 using callbacks_t = utils::callback_manager<
311 >;
312
313 // =====================================================================
314 // Member Variables
315 // =====================================================================
316
317 std::string server_id_;
318 utils::lifecycle_manager lifecycle_;
321 std::unique_ptr<asio::io_context>
323 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
325 std::unique_ptr<asio::ip::tcp::acceptor>
328 std::shared_ptr<integration::thread_pool_interface>
330 std::future<void>
333 std::unique_ptr<asio::ssl::context>
339 std::vector<std::shared_ptr<kcenon::network::session::secure_session>> sessions_;
340
345
349 std::unique_ptr<asio::steady_timer> cleanup_timer_;
350
351#if KCENON_WITH_COMMON_SYSTEM
355 kcenon::common::interfaces::IMonitor* monitor_ = nullptr;
356
360 std::atomic<uint64_t> messages_received_{0};
361 std::atomic<uint64_t> messages_sent_{0};
362 std::atomic<uint64_t> connection_errors_{0};
363#endif // KCENON_WITH_COMMON_SYSTEM
364 };
365
366} // namespace kcenon::network::core
Thread-safe callback registration and invocation manager.
A secure server class that manages incoming TLS/SSL encrypted TCP connections, creating secure_sessio...
Definition ssl.cppm:302
std::unique_ptr< asio::ip::tcp::acceptor > acceptor_
std::function< void(const std::string &)> disconnection_callback_t
Callback type for disconnection.
auto on_accept(std::error_code ec, asio::ip::tcp::socket socket) -> void
Handler called when an asynchronous accept finishes.
auto set_connection_callback(connection_callback_t callback) -> void
Sets the callback for new client connections.
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for session errors.
auto server_id() const -> const std::string &
Returns the server identifier.
auto cleanup_dead_sessions() -> void
Removes stopped sessions from the sessions vector.
auto do_stop_impl() -> VoidResult
Secure TCP-specific implementation of server stop.
auto get_connection_callback() const -> connection_callback_t
Gets a copy of the connection callback.
auto get_disconnection_callback() const -> disconnection_callback_t
Gets a copy of the disconnection callback.
secure_messaging_server(std::string_view server_id, const std::string &cert_file, const std::string &key_file)
Constructs a secure_messaging_server with SSL/TLS support.
auto start_cleanup_timer() -> void
Starts a periodic timer that triggers session cleanup.
std::vector< std::shared_ptr< kcenon::network::session::secure_session > > sessions_
Holds all active secure sessions.
auto invoke_connection_callback(std::shared_ptr< session::secure_session > session) -> void
Invokes the connection callback with the given session.
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
~secure_messaging_server() noexcept
Destructor. If the server is still running, stop_server() is invoked.
auto start_server(unsigned short port) -> VoidResult
Starts the server on the specified port.
auto get_error_callback() const -> error_callback_t
Gets a copy of the error callback.
std::unique_ptr< asio::steady_timer > cleanup_timer_
Timer for periodic cleanup of stopped sessions.
auto stop_server() -> VoidResult
Stops the server and closes all connections.
std::unique_ptr< asio::ssl::context > ssl_context_
std::function< void(std::shared_ptr< session::secure_session >, std::error_code)> error_callback_t
Callback type for errors.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto get_receive_callback() const -> receive_callback_t
Gets a copy of the receive callback.
auto do_accept() -> void
Initiates an asynchronous accept operation (async_accept).
std::function< void(std::shared_ptr< session::secure_session >)> connection_callback_t
Callback type for new connection.
std::function< void(std::shared_ptr< session::secure_session >, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
auto do_start_impl(unsigned short port) -> VoidResult
Secure TCP-specific implementation of server start.
auto is_running() const noexcept -> bool
Check if the server is currently running.
auto set_disconnection_callback(disconnection_callback_t callback) -> void
Sets the callback for client disconnections.
auto wait_for_stop() -> void
Blocks until stop_server() is called.
auto set_receive_callback(receive_callback_t callback) -> void
Sets the callback for received messages.
std::mutex sessions_mutex_
Mutex protecting access to sessions_ vector.
Feature flags for network_system.
Component lifecycle management (start, stop, restart).
Result< std::monostate > VoidResult
tcp_server_callback
Callback indices for messaging_server and secure_messaging_server.
std::mutex mutex
Network-specific error and result type definitions.
Thread system integration interface for network_system.