6#ifndef KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_SERVER_H_
7#define KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_SERVER_H_
30#ifdef BUILD_TLS_SUPPORT
31#include <asio/ssl.hpp>
35#if KCENON_WITH_COMMON_SYSTEM
36#include <kcenon/common/interfaces/monitoring_interface.h>
41class messaging_session;
42#ifdef BUILD_TLS_SUPPORT
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>>
101#ifdef BUILD_TLS_SUPPORT
189 [[nodiscard]] auto
server_id() const -> const std::
string&;
219#if KCENON_WITH_COMMON_SYSTEM
224 auto set_monitor(kcenon::common::interfaces::IMonitor* monitor) -> void;
230 auto get_monitor() const ->
kcenon::common::interfaces::IMonitor*;
257 auto
on_accept(std::error_code ec, asio::ip::
tcp::socket socket) ->
void;
261#ifdef BUILD_TLS_SUPPORT
262 auto on_handshake_complete(
session_ptr session, std::error_code ec) ->
void
287 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
292#ifdef BUILD_TLS_SUPPORT
293 std::unique_ptr<asio::ssl::context> ssl_context_;
294 [[no_unique_address]] TlsPolicy tls_config_;
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};
321#ifdef BUILD_TLS_SUPPORT
333#include "unified_messaging_server.inl"
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.
utils::lifecycle_manager lifecycle_
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.
std::vector< session_ptr > sessions_
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::future< void > io_context_future_
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.
std::shared_ptr< asio::io_context > io_context_
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 start_cleanup_timer() -> void
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_
std::mutex acceptor_mutex_
std::atomic< bool > stop_initiated_
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
auto cleanup_dead_sessions() -> void
std::function< void(session_ptr)> connection_callback_t
Callback type for new connection.
auto do_stop_impl() -> VoidResult
std::mutex sessions_mutex_
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.
Result< std::monostate > VoidResult
tcp_server_callback
Callback indices for messaging_server and secure_messaging_server.
Network-specific error and result type definitions.
Policy-based TLS configuration (no_tls, require_tls, optional_tls).