Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
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_MESSAGING_SERVER_H_
7#define KCENON_NETWORK_INTERNAL_CORE_MESSAGING_SERVER_H_
8
17
18#include <atomic>
19#include <functional>
20#include <future>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <vector>
25
26#include <asio.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 messaging_session;
41}
42
43namespace kcenon::network::core {
44
94 : public std::enable_shared_from_this<messaging_server>
95 , public utils::startable_base<messaging_server>
96 {
97 friend class utils::startable_base<messaging_server>;
98 public:
100 using connection_callback_t = std::function<void(std::shared_ptr<session::messaging_session>)>;
102 using disconnection_callback_t = std::function<void(const std::string&)>;
104 using receive_callback_t = std::function<void(std::shared_ptr<session::messaging_session>,
105 const std::vector<uint8_t>&)>;
107 using error_callback_t = std::function<void(std::shared_ptr<session::messaging_session>,
108 std::error_code)>;
109
116 explicit messaging_server(const std::string& server_id);
117
122 ~messaging_server() noexcept;
123
124 // Non-copyable, non-movable
126 messaging_server& operator=(const messaging_server&) = delete;
128 messaging_server& operator=(messaging_server&&) = delete;
129
130 // =====================================================================
131 // Lifecycle Management
132 // =====================================================================
133
142 [[nodiscard]] auto start_server(unsigned short port) -> VoidResult;
143
150 [[nodiscard]] auto stop_server() -> VoidResult;
151
152 // Note: wait_for_stop() and is_running() are inherited from startable_base
153
158 [[nodiscard]] auto server_id() const -> const std::string&;
159
160 // =====================================================================
161 // Callback Setters
162 // =====================================================================
163
168 auto set_connection_callback(connection_callback_t callback) -> void;
169
175
180 auto set_receive_callback(receive_callback_t callback) -> void;
181
186 auto set_error_callback(error_callback_t callback) -> void;
187
188#if KCENON_WITH_COMMON_SYSTEM
199 auto set_monitor(kcenon::common::interfaces::IMonitor* monitor) -> void;
200
205 auto get_monitor() const -> kcenon::common::interfaces::IMonitor*;
206#endif // KCENON_WITH_COMMON_SYSTEM
207
208 private:
209 // =====================================================================
210 // startable_base CRTP interface
211 // =====================================================================
212
217 [[nodiscard]] static constexpr auto component_name() noexcept -> std::string_view
218 {
219 return "Server";
220 }
221
226 auto on_stopped() -> void;
227
228 // =====================================================================
229 // Internal Implementation Methods
230 // =====================================================================
231
237 auto do_start_impl(unsigned short port) -> VoidResult;
238
243 auto do_stop_impl() -> VoidResult;
244
245 // =====================================================================
246 // Internal Callback Helpers
247 // =====================================================================
248
253 [[nodiscard]] auto get_connection_callback() const -> connection_callback_t;
254
259 [[nodiscard]] auto get_disconnection_callback() const -> disconnection_callback_t;
260
265 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
266
271 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
272
277 auto invoke_connection_callback(std::shared_ptr<session::messaging_session> session) -> void;
278
279 // =====================================================================
280 // Internal Connection Handlers
281 // =====================================================================
282
289 auto do_accept() -> void;
290
301 auto on_accept(std::error_code ec, asio::ip::tcp::socket socket)
302 -> void;
303
313 auto cleanup_dead_sessions() -> void;
314
321 auto start_cleanup_timer() -> void;
322
323 private:
326
328 using callbacks_t = utils::callback_manager<
333 >;
334
335 // =====================================================================
336 // Member Variables
337 // =====================================================================
338
339 std::string server_id_;
340 // Note: lifecycle_ and stop_initiated_ are managed by startable_base
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>
349 std::future<void>
357 std::vector<std::shared_ptr<kcenon::network::session::messaging_session>> sessions_;
358
363
367 mutable std::mutex acceptor_mutex_;
368
372 std::unique_ptr<asio::steady_timer> cleanup_timer_;
373
374#if KCENON_WITH_COMMON_SYSTEM
378 kcenon::common::interfaces::IMonitor* monitor_ = nullptr;
379
383 std::atomic<uint64_t> messages_received_{0};
384 std::atomic<uint64_t> messages_sent_{0};
385 std::atomic<uint64_t> connection_errors_{0};
386#endif // KCENON_WITH_COMMON_SYSTEM
387 };
388
389} // namespace kcenon::network::core
390
391#endif // KCENON_NETWORK_INTERNAL_CORE_MESSAGING_SERVER_H_
Thread-safe callback registration and invocation manager.
A server class that manages incoming TCP connections, creating messaging_session instances for each a...
Definition tcp.cppm:237
auto on_stopped() -> void
Called after stop operation completes. No-op for server (no disconnection callback at server level).
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.
Definition tcp.cppm:394
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
std::mutex mutex
Network-specific error and result type definitions.
Base class for components with start/stop lifecycle.