Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
core.cppm
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
22module;
23
24// =============================================================================
25// Global Module Fragment - Standard Library Headers
26// =============================================================================
27#include <atomic>
28#include <chrono>
29#include <concepts>
30#include <functional>
31#include <future>
32#include <memory>
33#include <mutex>
34#include <optional>
35#include <shared_mutex>
36#include <span>
37#include <string>
38#include <string_view>
39#include <thread>
40#include <type_traits>
41#include <vector>
42
43// Third-party headers (must be in global module fragment)
44#include <asio.hpp>
45
46export module kcenon.network:core;
47
48export import kcenon.common;
49export import kcenon.thread;
50
51// =============================================================================
52// Forward Declarations
53// =============================================================================
54
55export namespace kcenon::network::core {
56
57class network_context;
58class connection_pool;
59
60} // namespace kcenon::network::core
61
62export namespace kcenon::network::integration {
63
64class thread_pool_interface;
65class logger_interface;
66class monitoring_interface;
67class io_context_thread_manager;
68
69} // namespace kcenon::network::integration
70
71// =============================================================================
72// Thread Pool Interface
73// =============================================================================
74
75export namespace kcenon::network::integration {
76
84public:
85 virtual ~thread_pool_interface() = default;
86
91 virtual void submit(std::function<void()> task) = 0;
92
97 virtual size_t worker_count() const = 0;
98
103 virtual bool is_running() const = 0;
104};
105
113public:
114 virtual ~logger_interface() = default;
115
120 virtual void info(std::string_view message) = 0;
121
126 virtual void warning(std::string_view message) = 0;
127
132 virtual void error(std::string_view message) = 0;
133
138 virtual void debug(std::string_view message) = 0;
139};
140
148public:
149 virtual ~monitoring_interface() = default;
150
156 virtual void record_counter(std::string_view name, int64_t value) = 0;
157
163 virtual void record_gauge(std::string_view name, double value) = 0;
164
170 virtual void record_histogram(std::string_view name, double value) = 0;
171};
172
181public:
187
192
193 // Non-copyable, movable
198
203 bool start();
204
208 void stop();
209
214 bool is_running() const noexcept;
215
220 asio::io_context& get_io_context() noexcept;
221
226 size_t worker_count() const noexcept;
227
228private:
229 std::unique_ptr<asio::io_context> io_context_;
230 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>> work_guard_;
231 std::vector<std::thread> workers_;
232 std::atomic<bool> running_{false};
234 mutable std::mutex mutex_;
235};
236
237} // namespace kcenon::network::integration
238
239// =============================================================================
240// Network Context
241// =============================================================================
242
243export namespace kcenon::network::core {
244
253public:
259
264 void set_thread_pool(std::shared_ptr<integration::thread_pool_interface> pool);
265
270 std::shared_ptr<integration::thread_pool_interface> get_thread_pool();
271
276 void set_logger(std::shared_ptr<integration::logger_interface> logger);
277
282 std::shared_ptr<integration::logger_interface> get_logger();
283
288 void set_monitoring(std::shared_ptr<integration::monitoring_interface> monitoring);
289
294 std::shared_ptr<integration::monitoring_interface> get_monitoring();
295
301
305 void reset();
306
307private:
309 ~network_context() = default;
310
311 // Non-copyable
314
315 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
316 std::shared_ptr<integration::logger_interface> logger_;
317 std::shared_ptr<integration::monitoring_interface> monitoring_;
318 std::unique_ptr<integration::io_context_thread_manager> io_manager_;
319 mutable std::shared_mutex mutex_;
320};
321
332public:
339 size_t max_connections = 10,
340 std::chrono::seconds idle_timeout = std::chrono::seconds(60));
341
346
347 // Non-copyable
350
355 size_t size() const noexcept;
356
361 size_t max_size() const noexcept;
362
366 void clear();
367
368private:
370 std::chrono::seconds idle_timeout_;
371 mutable std::mutex mutex_;
372};
373
374} // namespace kcenon::network::core
375
376// =============================================================================
377// Base Classes for Messaging
378// =============================================================================
379
380export namespace kcenon::network::core {
381
388 connected,
390};
391
397constexpr std::string_view to_string(connection_state state) noexcept {
398 switch (state) {
399 case connection_state::disconnected: return "disconnected";
400 case connection_state::connecting: return "connecting";
401 case connection_state::connected: return "connected";
402 case connection_state::disconnecting: return "disconnecting";
403 default: return "unknown";
404 }
405}
406
410using connection_callback = std::function<void()>;
411using disconnection_callback = std::function<void()>;
412using error_callback = std::function<void(const std::string&)>;
413using data_callback = std::function<void(std::span<const uint8_t>)>;
414
415} // namespace kcenon::network::core
Manages a pool of reusable client connections to reduce connection overhead and improve performance.
Definition core.cppm:331
void clear()
Clear all pooled connections.
connection_pool & operator=(const connection_pool &)=delete
size_t size() const noexcept
Get current pool size.
size_t max_size() const noexcept
Get maximum pool size.
connection_pool(const connection_pool &)=delete
connection_pool(size_t max_connections=10, std::chrono::seconds idle_timeout=std::chrono::seconds(60))
Construct a connection pool.
std::chrono::seconds idle_timeout_
Definition core.cppm:370
~connection_pool()
Destructor - closes all pooled connections.
Global context for shared network system resources.
Definition core.cppm:252
std::unique_ptr< integration::io_context_thread_manager > io_manager_
Definition core.cppm:318
void set_thread_pool(std::shared_ptr< integration::thread_pool_interface > pool)
Set custom thread pool.
integration::io_context_thread_manager & get_io_context_manager()
Get the io_context thread manager.
static network_context & instance()
Get the singleton instance.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
Definition core.cppm:315
std::shared_ptr< integration::monitoring_interface > get_monitoring()
Get current monitoring system.
void set_monitoring(std::shared_ptr< integration::monitoring_interface > monitoring)
Set custom monitoring system.
std::shared_ptr< integration::logger_interface > get_logger()
Get current logger.
void reset()
Reset all resources to defaults.
network_context & operator=(const network_context &)=delete
void set_logger(std::shared_ptr< integration::logger_interface > logger)
Set custom logger.
std::shared_ptr< integration::monitoring_interface > monitoring_
Definition core.cppm:317
std::shared_ptr< integration::thread_pool_interface > get_thread_pool()
Get current thread pool.
std::shared_ptr< integration::logger_interface > logger_
Definition core.cppm:316
network_context(const network_context &)=delete
Manages io_context execution on shared thread pools.
Definition core.cppm:180
io_context_thread_manager(size_t worker_count=0)
Construct a manager with specified worker count.
void stop()
Stop the io_context and join worker threads.
io_context_thread_manager(const io_context_thread_manager &)=delete
asio::io_context & get_io_context() noexcept
Get the managed io_context.
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
Definition core.cppm:230
bool is_running() const noexcept
Check if the manager is running.
std::unique_ptr< asio::io_context > io_context_
Definition core.cppm:229
size_t worker_count() const noexcept
Get the number of worker threads.
bool start()
Start the io_context worker threads.
io_context_thread_manager(io_context_thread_manager &&) noexcept
~io_context_thread_manager()
Destructor - stops the manager if running.
io_context_thread_manager & operator=(const io_context_thread_manager &)=delete
Interface for logger integration.
Definition core.cppm:112
virtual void warning(std::string_view message)=0
Log a warning message.
virtual void info(std::string_view message)=0
Log an informational message.
virtual void error(std::string_view message)=0
Log an error message.
virtual void debug(std::string_view message)=0
Log a debug message.
Interface for monitoring integration.
Definition core.cppm:147
virtual void record_gauge(std::string_view name, double value)=0
Record a gauge metric.
virtual void record_counter(std::string_view name, int64_t value)=0
Record a counter metric.
virtual void record_histogram(std::string_view name, double value)=0
Record a histogram metric.
Interface for thread pool integration.
Definition core.cppm:83
virtual size_t worker_count() const =0
Get the number of worker threads.
virtual void submit(std::function< void()> task)=0
Submit a task to the thread pool.
virtual bool is_running() const =0
Check if the thread pool is running.
std::function< void()> disconnection_callback
Definition core.cppm:411
std::function< void()> connection_callback
Callback type aliases for messaging.
Definition core.cppm:410
constexpr std::string_view to_string(connection_state state) noexcept
Convert connection state to string.
Definition core.cppm:397
std::function< void(const std::string &)> error_callback
Definition core.cppm:412
connection_state
Connection state enumeration.
Definition core.cppm:385
std::function< void(std::span< const uint8_t >)> data_callback
Definition core.cppm:413
std::mutex mutex