Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_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#ifndef KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_SERVER_H_
7#define KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_SERVER_H_
8
9#include <atomic>
10#include <concepts>
11#include <functional>
12#include <future>
13#include <memory>
14#include <mutex>
15#include <string>
16#include <string_view>
17#include <type_traits>
18#include <vector>
19
20#include <asio.hpp>
21
29
30#ifdef BUILD_TLS_SUPPORT
31#include <asio/ssl.hpp>
32#endif
33
34// Optional monitoring support via common_system
35#if KCENON_WITH_COMMON_SYSTEM
36#include <kcenon/common/interfaces/monitoring_interface.h>
37#endif
38
40{
41class messaging_session;
42#ifdef BUILD_TLS_SUPPORT
43class secure_session;
44#endif
45} // namespace kcenon::network::session
46
48{
49
91template <protocol::Protocol Protocol, policy::TlsPolicy TlsPolicy = policy::no_tls>
92 requires std::same_as<Protocol, protocol::tcp_protocol>
94 : public std::enable_shared_from_this<unified_messaging_server<Protocol, TlsPolicy>>
95{
96public:
99
101#ifdef BUILD_TLS_SUPPORT
102 using session_type = std::conditional_t<
103 is_secure,
106#else
108#endif
109
111 using session_ptr = std::shared_ptr<session_type>;
112
114 using connection_callback_t = std::function<void(session_ptr)>;
116 using disconnection_callback_t = std::function<void(const std::string&)>;
118 using receive_callback_t = std::function<void(session_ptr, const std::vector<uint8_t>&)>;
120 using error_callback_t = std::function<void(session_ptr, std::error_code)>;
121
128 explicit unified_messaging_server(std::string_view server_id)
130
138 unified_messaging_server(std::string_view server_id, const TlsPolicy& tls_config)
140
145
146 // Non-copyable, non-movable
151
152 // =====================================================================
153 // Lifecycle Management
154 // =====================================================================
155
164 [[nodiscard]] auto start_server(uint16_t port) -> VoidResult;
165
172 [[nodiscard]] auto stop_server() -> VoidResult;
173
177 auto wait_for_stop() -> void;
178
183 [[nodiscard]] auto is_running() const noexcept -> bool;
184
189 [[nodiscard]] auto server_id() const -> const std::string&;
190
191 // =====================================================================
192 // Callback Setters
193 // =====================================================================
194
200
206
212
217 auto set_error_callback(error_callback_t callback) -> void;
218
219#if KCENON_WITH_COMMON_SYSTEM
224 auto set_monitor(kcenon::common::interfaces::IMonitor* monitor) -> void;
225
230 auto get_monitor() const -> kcenon::common::interfaces::IMonitor*;
231#endif
232
233private:
234 // =====================================================================
235 // Internal Implementation Methods
236 // =====================================================================
237
238 auto do_start_impl(uint16_t port) -> VoidResult;
240
241 // =====================================================================
242 // Internal Callback Helpers
243 // =====================================================================
244
245 [[nodiscard]] auto get_connection_callback() const -> connection_callback_t;
247 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
248 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
249
251
252 // =====================================================================
253 // Internal Connection Handlers
254 // =====================================================================
255
256 auto do_accept() -> void;
257 auto on_accept(std::error_code ec, asio::ip::tcp::socket socket) -> void;
258 auto cleanup_dead_sessions() -> void;
259 auto start_cleanup_timer() -> void;
260
261#ifdef BUILD_TLS_SUPPORT
262 auto on_handshake_complete(session_ptr session, std::error_code ec) -> void
263 requires is_secure;
264#endif
265
266private:
269
276
277 // =====================================================================
278 // Member Variables
279 // =====================================================================
280
281 std::string server_id_;
284 std::atomic<bool> stop_initiated_{false};
286 std::shared_ptr<asio::io_context> io_context_;
287 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
289 std::unique_ptr<asio::ip::tcp::acceptor> acceptor_;
290 std::future<void> io_context_future_;
291
292#ifdef BUILD_TLS_SUPPORT
293 std::unique_ptr<asio::ssl::context> ssl_context_;
294 [[no_unique_address]] TlsPolicy tls_config_;
295#endif
296
297 std::vector<session_ptr> sessions_;
298 mutable std::mutex sessions_mutex_;
299 mutable std::mutex acceptor_mutex_;
300 std::unique_ptr<asio::steady_timer> cleanup_timer_;
301
302#if KCENON_WITH_COMMON_SYSTEM
303 kcenon::common::interfaces::IMonitor* monitor_ = nullptr;
304 std::atomic<uint64_t> messages_received_{0};
305 std::atomic<uint64_t> messages_sent_{0};
306 std::atomic<uint64_t> connection_errors_{0};
307#endif
308};
309
310// =====================================================================
311// Type Aliases for Convenience
312// =====================================================================
313
320
321#ifdef BUILD_TLS_SUPPORT
328#endif
329
330} // namespace kcenon::network::core
331
332// Include template implementation
333#include "unified_messaging_server.inl"
334
335#endif // KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_SERVER_H_
Thread-safe callback registration and invocation manager.
Unified TCP server template parameterized by protocol and TLS policy.
auto set_disconnection_callback(disconnection_callback_t callback) -> void
Sets the callback for client disconnections.
auto set_connection_callback(connection_callback_t callback) -> void
Sets the callback for new client connections.
~unified_messaging_server() noexcept
Destructor; automatically calls stop_server() if still running.
auto server_id() const -> const std::string &
Returns the server identifier.
std::function< void(const std::string &)> disconnection_callback_t
Callback type for disconnection.
auto is_running() const noexcept -> bool
Checks if the server is currently running.
auto set_receive_callback(receive_callback_t callback) -> void
Sets the callback for received messages.
std::function< void(session_ptr, std::error_code)> error_callback_t
Callback type for errors.
auto do_start_impl(uint16_t port) -> VoidResult
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
static constexpr bool is_secure
Indicates whether TLS is enabled for this server.
auto wait_for_stop() -> void
Blocks until stop_server() is called.
auto start_server(uint16_t port) -> VoidResult
Starts the server on the specified port.
auto get_disconnection_callback() const -> disconnection_callback_t
auto get_receive_callback() const -> receive_callback_t
auto get_error_callback() const -> error_callback_t
std::unique_ptr< asio::steady_timer > cleanup_timer_
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for session errors.
unified_messaging_server(std::string_view server_id, const TlsPolicy &tls_config)
Constructs a secure server with TLS configuration.
std::unique_ptr< asio::ip::tcp::acceptor > acceptor_
auto get_connection_callback() const -> connection_callback_t
auto stop_server() -> VoidResult
Stops the server and closes all connections.
auto invoke_connection_callback(session_ptr session) -> void
unified_messaging_server(std::string_view server_id)
Constructs a plain server with a given identifier.
std::shared_ptr< session_type > session_ptr
Session pointer type.
std::function< void(session_ptr, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
auto on_accept(std::error_code ec, asio::ip::tcp::socket socket) -> void
std::function< void(session_ptr)> connection_callback_t
Callback type for new connection.
Manages a single connected client session on the server side, providing asynchronous read/write opera...
Manages a single connected secure (TLS/SSL) client session on the server side, providing asynchronous...
Thread-safe lifecycle state management for network components.
Feature flags for network_system.
Component lifecycle management (start, stop, restart).
constexpr bool is_tls_enabled_v
Helper variable template to check if TLS is enabled at compile time.
Definition tls_policy.h:96
Result< std::monostate > VoidResult
tcp_server_callback
Callback indices for messaging_server and secure_messaging_server.
Protocol tag types for compile-time protocol selection.
Network-specific error and result type definitions.
Policy-based TLS configuration (no_tls, require_tls, optional_tls).