6#ifndef KCENON_NETWORK_INTERNAL_CORE_MESSAGING_SERVER_H_
7#define KCENON_NETWORK_INTERNAL_CORE_MESSAGING_SERVER_H_
35#if KCENON_WITH_COMMON_SYSTEM
36 #include <kcenon/common/interfaces/monitoring_interface.h>
40 class messaging_session;
94 :
public std::enable_shared_from_this<messaging_server>
95 ,
public utils::startable_base<messaging_server>
105 const std::vector<uint8_t>&)>;
158 [[nodiscard]] auto
server_id() const -> const std::
string&;
188#if KCENON_WITH_COMMON_SYSTEM
199 auto set_monitor(kcenon::common::interfaces::IMonitor* monitor) -> void;
205 auto get_monitor() const ->
kcenon::common::interfaces::IMonitor*;
217 [[nodiscard]]
static constexpr auto component_name() noexcept -> std::string_view
301 auto
on_accept(std::error_code ec, asio::ip::
tcp::socket socket)
343 std::shared_ptr<asio::io_context>
345 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
347 std::unique_ptr<asio::ip::
tcp::acceptor>
374#if KCENON_WITH_COMMON_SYSTEM
378 kcenon::common::interfaces::IMonitor* monitor_ =
nullptr;
383 std::atomic<uint64_t> messages_received_{0};
384 std::atomic<uint64_t> messages_sent_{0};
385 std::atomic<uint64_t> connection_errors_{0};
Thread-safe callback registration and invocation manager.
A server class that manages incoming TCP connections, creating messaging_session instances for each a...
auto on_stopped() -> void
Called after stop operation completes. No-op for server (no disconnection callback at server level).
std::future< void > io_context_future_
auto invoke_connection_callback(std::shared_ptr< session::messaging_session > session) -> void
Invokes the connection callback with the given session.
auto set_disconnection_callback(disconnection_callback_t callback) -> void
Sets the callback for client disconnections.
std::mutex sessions_mutex_
Mutex protecting access to sessions_ vector.
~messaging_server() noexcept
Destructor. If the server is still running, stop_server() is invoked.
std::unique_ptr< asio::ip::tcp::acceptor > acceptor_
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.
std::function< void(std::shared_ptr< session::messaging_session >, std::error_code)> error_callback_t
Callback type for errors.
auto cleanup_dead_sessions() -> void
Removes stopped sessions from the sessions vector.
auto do_accept() -> void
Initiates an asynchronous accept operation (async_accept).
std::function< void(std::shared_ptr< session::messaging_session >)> connection_callback_t
Callback type for new connection.
std::shared_ptr< asio::io_context > io_context_
auto get_disconnection_callback() const -> disconnection_callback_t
Gets a copy of the disconnection callback.
std::function< void(std::shared_ptr< session::messaging_session >, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
auto start_cleanup_timer() -> void
Starts a periodic timer that triggers session cleanup.
auto get_receive_callback() const -> receive_callback_t
Gets a copy of the receive callback.
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
auto do_stop_impl() -> VoidResult
TCP-specific implementation of server stop.
std::vector< std::shared_ptr< kcenon::network::session::messaging_session > > sessions_
Holds all active sessions. When stop_server() is invoked, each session's stop_session() is called and...
auto start_server(unsigned short port) -> VoidResult
Starts the server on the specified port.
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for session errors.
auto on_accept(std::error_code ec, asio::ip::tcp::socket socket) -> void
Handler called when an asynchronous accept finishes.
auto stop_server() -> VoidResult
Stops the server and closes all connections.
messaging_server(const std::string &server_id)
Constructs a messaging_server with an optional string server_id.
static constexpr auto component_name() noexcept -> std::string_view
Returns the component name for error messages.
auto set_receive_callback(receive_callback_t callback) -> void
Sets the callback for received messages.
std::function< void(const std::string &)> disconnection_callback_t
Callback type for disconnection.
std::mutex acceptor_mutex_
Mutex protecting access to acceptor_ for thread-safe operations.
auto set_connection_callback(connection_callback_t callback) -> void
Sets the callback for new client connections.
auto do_start_impl(unsigned short port) -> VoidResult
TCP-specific implementation of server start.
auto server_id() const -> const std::string &
Returns the server identifier.
auto get_connection_callback() const -> connection_callback_t
Gets a copy of the connection callback.
Represents a client session on the server.
Feature flags for network_system.
Unified io_context thread management for network components.
Result< std::monostate > VoidResult
tcp_server_callback
Callback indices for messaging_server and secure_messaging_server.
kcenon::network::core::messaging_server messaging_server
Network-specific error and result type definitions.
Base class for components with start/stop lifecycle.