Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon.network Module Reference

Exported Modules

module  kcenon.common
 
module  kcenon.thread
 

Classes

struct  kcenon::network::module_version
 Version information for network_system module. More...
 
class  kcenon::network::core::messaging_client
 A basic TCP client that connects to a remote host, sends/receives data using asynchronous operations, and can apply a pipeline for transformations. More...
 
class  kcenon::network::core::messaging_server
 A server class that manages incoming TCP connections, creating messaging_session instances for each accepted socket. More...
 
class  kcenon::network::core::messaging_session
 Represents a client session on the server. More...
 
class  kcenon::network::core::messaging_udp_client
 A UDP client that sends datagrams to a target endpoint and can receive responses. More...
 
class  kcenon::network::core::messaging_udp_server
 A UDP server that receives datagrams and routes them based on sender endpoint. More...
 
class  kcenon::network::core::reliable_udp_client
 A UDP client with optional reliability layer for configurable delivery guarantees. More...
 
class  kcenon::network::integration::thread_pool_interface
 Interface for thread pool integration. More...
 
class  kcenon::network::integration::logger_interface
 Interface for logger integration. More...
 
class  kcenon::network::integration::monitoring_interface
 Interface for monitoring integration. More...
 
class  kcenon::network::integration::io_context_thread_manager
 Manages io_context execution on shared thread pools. More...
 
class  kcenon::network::core::network_context
 Global context for shared network system resources. More...
 
class  kcenon::network::core::connection_pool
 Manages a pool of reusable client connections to reduce connection overhead and improve performance. More...
 
struct  kcenon::network::core::ssl_config
 SSL/TLS configuration structure. More...
 
class  kcenon::network::core::secure_messaging_client
 A secure client for establishing TLS/SSL encrypted TCP connections to a server. More...
 
class  kcenon::network::core::secure_messaging_server
 A secure server class that manages incoming TLS/SSL encrypted TCP connections, creating secure_session instances for each accepted socket. More...
 
class  kcenon::network::core::secure_messaging_udp_client
 A secure UDP client using DTLS (Datagram TLS) for encrypted communication. More...
 
class  kcenon::network::core::secure_messaging_udp_server
 A secure UDP server using DTLS (Datagram TLS) for encrypted communication. More...
 

Enumerations

enum class  kcenon::network::core::connection_state { disconnected , connecting , connected , disconnecting }
 Connection state enumeration. More...
 
enum class  kcenon::network::core::ssl_protocol { tls_1_2 , tls_1_3 , dtls_1_2 , dtls_1_3 }
 SSL/TLS protocol version enumeration. More...
 
enum class  kcenon::network::core::ssl_verify_mode { none , peer , fail_if_no_peer_cert , client_once }
 SSL verification mode. More...
 

Typedefs

using client_connected_callback = std::function<void(const std::string& session_id)>
 Callback type for client connection events.
 
using client_disconnected_callback = std::function<void(const std::string& session_id)>
 Callback type for client disconnection events.
 
using client_data_callback
 Callback type for client data events.
 
using udp_data_callback
 Callback type for UDP data reception.
 
using udp_data_callback
 Callback type for UDP data reception.
 
using kcenon::network::core::connection_callback = std::function<void()>
 Callback type aliases for messaging.
 
using kcenon::network::core::disconnection_callback = std::function<void()>
 
using kcenon::network::core::error_callback = std::function<void(const std::string&)>
 
using kcenon::network::core::data_callback = std::function<void(std::span<const uint8_t>)>
 
using client_connected_callback = std::function<void(const std::string& session_id)>
 Callback type for secure client connection events.
 
using client_disconnected_callback = std::function<void(const std::string& session_id)>
 Callback type for client disconnection events.
 
using client_data_callback
 Callback type for client data events.
 
using dtls_data_callback
 Callback type for DTLS data reception.
 

Functions

VoidResult kcenon::network::initialize ()
 Initialize the network system with default production configuration.
 
VoidResult kcenon::network::shutdown ()
 Shutdown the network system.
 
bool kcenon::network::is_initialized ()
 Check if network system is initialized.
 
constexpr const char * kcenon::network::version () noexcept
 Get the network system version string.
 
 messaging_client (std::string_view client_id)
 Construct a client with a given client_id.
 
virtual ~messaging_client () noexcept
 Destructor - automatically stops the client if running.
 
 messaging_client (const messaging_client &)=delete
 
messaging_client & operator= (const messaging_client &)=delete
 
 messaging_client (messaging_client &&) noexcept
 
messaging_client & operator= (messaging_client &&) noexcept
 
bool start_client (std::string_view host, uint16_t port)
 Start the client and connect to a remote host.
 
void stop_client ()
 Stop the client and disconnect.
 
bool wait_for_stop (std::chrono::milliseconds timeout=std::chrono::milliseconds(5000))
 Wait for the client to stop.
 
bool send_packet (std::span< const uint8_t > data)
 Send data to the connected server.
 
bool send_packet (std::string_view data)
 Send string data to the connected server.
 
bool is_connected () const noexcept
 Check if the client is connected.
 
bool is_running () const noexcept
 Check if the client is running.
 
connection_state get_state () const noexcept
 Get the current connection state.
 
std::string_view client_id () const noexcept
 Get the client identifier.
 
void set_connection_callback (connection_callback callback)
 Set connection callback.
 
void set_disconnection_callback (disconnection_callback callback)
 Set disconnection callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
void set_data_callback (data_callback callback)
 Set data received callback.
 
void set_auto_reconnect (bool enable, std::chrono::seconds delay=std::chrono::seconds(5))
 Enable automatic reconnection.
 
void set_heartbeat (bool enable, std::chrono::seconds interval=std::chrono::seconds(30))
 Enable heartbeat mechanism.
 
 messaging_server (std::string_view server_id)
 Construct a server with a given server_id.
 
virtual ~messaging_server () noexcept
 Destructor - automatically stops the server if running.
 
 messaging_server (const messaging_server &)=delete
 
messaging_server & operator= (const messaging_server &)=delete
 
 messaging_server (messaging_server &&) noexcept
 
messaging_server & operator= (messaging_server &&) noexcept
 
bool start_server (uint16_t port, int backlog=128)
 Start the server on a specified port.
 
bool start_server (std::string_view address, uint16_t port, int backlog=128)
 Start the server on a specific interface.
 
void stop_server ()
 Stop the server and disconnect all clients.
 
bool wait_for_stop (std::chrono::milliseconds timeout=std::chrono::milliseconds(5000))
 Wait for the server to stop.
 
bool send_to (std::string_view session_id, std::span< const uint8_t > data)
 Send data to a specific client.
 
bool send_to (std::string_view session_id, std::string_view data)
 Send data to a specific client.
 
void broadcast (std::span< const uint8_t > data)
 Broadcast data to all connected clients.
 
void broadcast (std::string_view data)
 Broadcast string data to all connected clients.
 
void disconnect (std::string_view session_id)
 Disconnect a specific client.
 
bool is_running () const noexcept
 Check if the server is running.
 
size_t client_count () const noexcept
 Get the number of connected clients.
 
std::string_view server_id () const noexcept
 Get the server identifier.
 
void set_client_connected_callback (client_connected_callback callback)
 Set client connected callback.
 
void set_client_disconnected_callback (client_disconnected_callback callback)
 Set client disconnected callback.
 
void set_client_data_callback (client_data_callback callback)
 Set client data callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
 messaging_session (std::string session_id)
 Construct a session with a unique ID.
 
virtual ~messaging_session () noexcept
 Destructor.
 
const std::string & session_id () const noexcept
 Get the session ID.
 
bool is_active () const noexcept
 Check if the session is active.
 
std::string remote_address () const
 Get the remote endpoint address.
 
uint16_t remote_port () const
 Get the remote endpoint port.
 
bool send (std::span< const uint8_t > data)
 Send data to the client.
 
void close ()
 Close the session.
 
 messaging_udp_client (std::string_view client_id)
 Construct a UDP client with a given client_id.
 
virtual ~messaging_udp_client () noexcept
 Destructor - automatically stops the client if running.
 
 messaging_udp_client (const messaging_udp_client &)=delete
 
messaging_udp_client & operator= (const messaging_udp_client &)=delete
 
 messaging_udp_client (messaging_udp_client &&) noexcept
 
messaging_udp_client & operator= (messaging_udp_client &&) noexcept
 
bool start_client (std::string_view remote_host, uint16_t remote_port, uint16_t local_port=0)
 Start the client and bind to an endpoint.
 
void stop_client ()
 Stop the client.
 
bool send_packet (std::span< const uint8_t > data)
 Send a datagram to the configured remote endpoint.
 
bool send_packet (std::string_view data)
 Send a string datagram to the configured remote endpoint.
 
bool send_to (std::span< const uint8_t > data, std::string_view host, uint16_t port)
 Send a datagram to a specific endpoint.
 
bool is_running () const noexcept
 Check if the client is running.
 
std::string_view client_id () const noexcept
 Get the client identifier.
 
void set_data_callback (udp_data_callback callback)
 Set data received callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
bool join_multicast_group (std::string_view multicast_address)
 Enable multicast reception.
 
bool leave_multicast_group (std::string_view multicast_address)
 Leave multicast group.
 
 messaging_udp_server (std::string_view server_id)
 Construct a UDP server with a given server_id.
 
virtual ~messaging_udp_server () noexcept
 Destructor - automatically stops the server if running.
 
 messaging_udp_server (const messaging_udp_server &)=delete
 
messaging_udp_server & operator= (const messaging_udp_server &)=delete
 
 messaging_udp_server (messaging_udp_server &&) noexcept
 
messaging_udp_server & operator= (messaging_udp_server &&) noexcept
 
bool start_server (uint16_t port)
 Start the server on a specified port.
 
bool start_server (std::string_view address, uint16_t port)
 Start the server on a specific interface.
 
void stop_server ()
 Stop the server.
 
bool send_to (std::span< const uint8_t > data, std::string_view host, uint16_t port)
 Send a datagram to a specific endpoint.
 
bool send_to (std::string_view data, std::string_view host, uint16_t port)
 Send a string datagram to a specific endpoint.
 
bool broadcast (std::span< const uint8_t > data, uint16_t port)
 Broadcast data to all endpoints on the network.
 
bool is_running () const noexcept
 Check if the server is running.
 
std::string_view server_id () const noexcept
 Get the server identifier.
 
void set_data_callback (udp_data_callback callback)
 Set data received callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
bool join_multicast_group (std::string_view multicast_address)
 Enable multicast reception.
 
 reliable_udp_client (std::string_view client_id)
 Construct a reliable UDP client.
 
virtual ~reliable_udp_client () noexcept
 Destructor.
 
 reliable_udp_client (const reliable_udp_client &)=delete
 
reliable_udp_client & operator= (const reliable_udp_client &)=delete
 
 reliable_udp_client (reliable_udp_client &&) noexcept
 
reliable_udp_client & operator= (reliable_udp_client &&) noexcept
 
bool start_client (std::string_view remote_host, uint16_t remote_port)
 Start the client.
 
void stop_client ()
 Stop the client.
 
bool send_reliable (std::span< const uint8_t > data)
 Send data with reliable delivery.
 
bool send_unreliable (std::span< const uint8_t > data)
 Send data unreliably (fire and forget)
 
bool is_connected () const noexcept
 Check if the client is connected.
 
bool is_running () const noexcept
 Check if the client is running.
 
std::string_view client_id () const noexcept
 Get the client identifier.
 
void set_connection_callback (connection_callback callback)
 Set connection callback.
 
void set_disconnection_callback (disconnection_callback callback)
 Set disconnection callback.
 
void set_data_callback (data_callback callback)
 Set data received callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
void set_retransmit_timeout (std::chrono::milliseconds timeout)
 Set retransmission timeout.
 
void set_max_retransmit_attempts (size_t max_attempts)
 Set maximum retransmission attempts.
 
std::chrono::milliseconds get_rtt () const noexcept
 Get current round-trip time estimate.
 
double get_packet_loss_rate () const noexcept
 Get packet loss rate.
 
virtual ~thread_pool_interface ()=default
 
virtual void submit (std::function< void()> task)=0
 Submit a task to the thread pool.
 
virtual size_t worker_count () const =0
 Get the number of worker threads.
 
virtual bool is_running () const =0
 Check if the thread pool is running.
 
virtual ~logger_interface ()=default
 
virtual void info (std::string_view message)=0
 Log an informational message.
 
virtual void warning (std::string_view message)=0
 Log a warning 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.
 
virtual ~monitoring_interface ()=default
 
virtual void record_counter (std::string_view name, int64_t value)=0
 Record a counter metric.
 
virtual void record_gauge (std::string_view name, double value)=0
 Record a gauge metric.
 
virtual void record_histogram (std::string_view name, double value)=0
 Record a histogram metric.
 
 io_context_thread_manager (size_t worker_count=0)
 Construct a manager with specified worker count.
 
 ~io_context_thread_manager ()
 Destructor - stops the manager if running.
 
 io_context_thread_manager (const io_context_thread_manager &)=delete
 
io_context_thread_manager & operator= (const io_context_thread_manager &)=delete
 
 io_context_thread_manager (io_context_thread_manager &&) noexcept
 
io_context_thread_manager & operator= (io_context_thread_manager &&) noexcept
 
bool start ()
 Start the io_context worker threads.
 
void stop ()
 Stop the io_context and join worker threads.
 
bool is_running () const noexcept
 Check if the manager is running.
 
asio::io_context & get_io_context () noexcept
 Get the managed io_context.
 
size_t worker_count () const noexcept
 Get the number of worker threads.
 
static network_context & instance ()
 Get the singleton instance.
 
void set_thread_pool (std::shared_ptr< integration::thread_pool_interface > pool)
 Set custom thread pool.
 
std::shared_ptr< integration::thread_pool_interfaceget_thread_pool ()
 Get current thread pool.
 
void set_logger (std::shared_ptr< integration::logger_interface > logger)
 Set custom logger.
 
std::shared_ptr< integration::logger_interfaceget_logger ()
 Get current logger.
 
void set_monitoring (std::shared_ptr< integration::monitoring_interface > monitoring)
 Set custom monitoring system.
 
std::shared_ptr< integration::monitoring_interfaceget_monitoring ()
 Get current monitoring system.
 
integration::io_context_thread_managerget_io_context_manager ()
 Get the io_context thread manager.
 
void reset ()
 Reset all resources to defaults.
 
 network_context ()
 
 ~network_context ()=default
 
 network_context (const network_context &)=delete
 
network_context & operator= (const network_context &)=delete
 
 connection_pool (size_t max_connections=10, std::chrono::seconds idle_timeout=std::chrono::seconds(60))
 Construct a connection pool.
 
 ~connection_pool ()
 Destructor - closes all pooled connections.
 
 connection_pool (const connection_pool &)=delete
 
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.
 
void clear ()
 Clear all pooled connections.
 
constexpr std::string_view kcenon::network::core::to_string (connection_state state) noexcept
 Convert connection state to string.
 
 secure_messaging_client (std::string_view client_id, const ssl_config &config={})
 Construct a secure client with SSL configuration.
 
virtual ~secure_messaging_client () noexcept
 Destructor.
 
 secure_messaging_client (const secure_messaging_client &)=delete
 
secure_messaging_client & operator= (const secure_messaging_client &)=delete
 
 secure_messaging_client (secure_messaging_client &&) noexcept
 
secure_messaging_client & operator= (secure_messaging_client &&) noexcept
 
bool start_client (std::string_view host, uint16_t port)
 Start the client and connect securely.
 
void stop_client ()
 Stop the client.
 
bool wait_for_stop (std::chrono::milliseconds timeout=std::chrono::milliseconds(5000))
 Wait for the client to stop.
 
bool send_packet (std::span< const uint8_t > data)
 Send encrypted data.
 
bool send_packet (std::string_view data)
 Send encrypted string data.
 
bool is_connected () const noexcept
 Check if the client is connected and TLS handshake completed.
 
bool is_running () const noexcept
 Check if the client is running.
 
connection_state get_state () const noexcept
 Get the current connection state.
 
std::string_view client_id () const noexcept
 Get the client identifier.
 
void set_connection_callback (connection_callback callback)
 Set connection callback (called after TLS handshake)
 
void set_disconnection_callback (disconnection_callback callback)
 Set disconnection callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
void set_data_callback (data_callback callback)
 Set data received callback.
 
std::optional< std::string > get_peer_certificate_subject () const
 Get peer certificate information.
 
std::string get_protocol_version () const
 Get the negotiated TLS protocol version.
 
std::string get_cipher_suite () const
 Get the negotiated cipher suite.
 
 secure_messaging_server (std::string_view server_id, const ssl_config &config)
 Construct a secure server with SSL configuration.
 
virtual ~secure_messaging_server () noexcept
 Destructor.
 
 secure_messaging_server (const secure_messaging_server &)=delete
 
secure_messaging_server & operator= (const secure_messaging_server &)=delete
 
 secure_messaging_server (secure_messaging_server &&) noexcept
 
secure_messaging_server & operator= (secure_messaging_server &&) noexcept
 
bool start_server (uint16_t port, int backlog=128)
 Start the secure server.
 
bool start_server (std::string_view address, uint16_t port, int backlog=128)
 Start the secure server on a specific interface.
 
void stop_server ()
 Stop the server.
 
bool wait_for_stop (std::chrono::milliseconds timeout=std::chrono::milliseconds(5000))
 Wait for the server to stop.
 
bool send_to (std::string_view session_id, std::span< const uint8_t > data)
 Send encrypted data to a specific client.
 
void broadcast (std::span< const uint8_t > data)
 Broadcast encrypted data to all connected clients.
 
void disconnect (std::string_view session_id)
 Disconnect a specific client.
 
bool is_running () const noexcept
 Check if the server is running.
 
size_t client_count () const noexcept
 Get the number of connected clients.
 
std::string_view server_id () const noexcept
 Get the server identifier.
 
void set_client_connected_callback (client_connected_callback callback)
 Set client connected callback.
 
void set_client_disconnected_callback (client_disconnected_callback callback)
 Set client disconnected callback.
 
void set_client_data_callback (client_data_callback callback)
 Set client data callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
std::optional< std::string > get_peer_certificate_subject (std::string_view session_id) const
 Get peer certificate for a session.
 
 secure_messaging_udp_client (std::string_view client_id, const ssl_config &config={})
 Construct a secure UDP client with DTLS configuration.
 
virtual ~secure_messaging_udp_client () noexcept
 Destructor.
 
 secure_messaging_udp_client (const secure_messaging_udp_client &)=delete
 
secure_messaging_udp_client & operator= (const secure_messaging_udp_client &)=delete
 
 secure_messaging_udp_client (secure_messaging_udp_client &&) noexcept
 
secure_messaging_udp_client & operator= (secure_messaging_udp_client &&) noexcept
 
bool start_client (std::string_view remote_host, uint16_t remote_port)
 Start the client and perform DTLS handshake.
 
void stop_client ()
 Stop the client.
 
bool send_packet (std::span< const uint8_t > data)
 Send encrypted datagram.
 
bool is_connected () const noexcept
 Check if the client is connected and DTLS handshake completed.
 
bool is_running () const noexcept
 Check if the client is running.
 
std::string_view client_id () const noexcept
 Get the client identifier.
 
void set_connection_callback (connection_callback callback)
 Set connection callback (called after DTLS handshake)
 
void set_disconnection_callback (disconnection_callback callback)
 Set disconnection callback.
 
void set_data_callback (data_callback callback)
 Set data received callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 
 secure_messaging_udp_server (std::string_view server_id, const ssl_config &config)
 Construct a secure UDP server with DTLS configuration.
 
virtual ~secure_messaging_udp_server () noexcept
 Destructor.
 
 secure_messaging_udp_server (const secure_messaging_udp_server &)=delete
 
secure_messaging_udp_server & operator= (const secure_messaging_udp_server &)=delete
 
 secure_messaging_udp_server (secure_messaging_udp_server &&) noexcept
 
secure_messaging_udp_server & operator= (secure_messaging_udp_server &&) noexcept
 
bool start_server (uint16_t port)
 Start the secure UDP server.
 
bool start_server (std::string_view address, uint16_t port)
 Start the secure UDP server on a specific interface.
 
void stop_server ()
 Stop the server.
 
bool send_to (std::string_view session_id, std::span< const uint8_t > data)
 Send encrypted datagram to a specific session.
 
bool is_running () const noexcept
 Check if the server is running.
 
size_t session_count () const noexcept
 Get the number of active DTLS sessions.
 
std::string_view server_id () const noexcept
 Get the server identifier.
 
void set_session_connected_callback (std::function< void(const std::string &)> callback)
 Set session connected callback.
 
void set_session_disconnected_callback (std::function< void(const std::string &)> callback)
 Set session disconnected callback.
 
void set_data_callback (dtls_data_callback callback)
 Set data received callback.
 
void set_error_callback (error_callback callback)
 Set error callback.
 

Variables

static constexpr int major = 0
 
static constexpr int minor = 1
 
static constexpr int patch = 0
 
static constexpr int tweak = 0
 
static constexpr const char * string = "0.1.0.0"
 
static constexpr const char * module_name = "kcenon.network"
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< asio::io_context > io_context_
 
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
 
std::vector< std::thread > workers_
 
std::atomic< bool > running_ {false}
 
size_t worker_count_
 
std::mutex mutex_
 
std::shared_ptr< integration::thread_pool_interfacethread_pool_
 
std::shared_ptr< integration::logger_interfacelogger_
 
std::shared_ptr< integration::monitoring_interfacemonitoring_
 
std::unique_ptr< integration::io_context_thread_managerio_manager_
 
std::shared_mutex mutex_
 
size_t max_connections_
 
std::chrono::seconds idle_timeout_
 
std::mutex mutex_
 
std::filesystem::path certificate_path
 Path to certificate file (PEM format)
 
std::filesystem::path private_key_path
 Path to private key file (PEM format)
 
std::filesystem::path ca_certificate_path
 Path to CA certificate file for verification (PEM format)
 
std::string private_key_password
 Password for encrypted private key (empty if not encrypted)
 
ssl_protocol protocol = ssl_protocol::tls_1_3
 SSL protocol version.
 
ssl_verify_mode verify_mode = ssl_verify_mode::peer
 Verification mode.
 
std::string server_name
 Server name for SNI (Server Name Indication)
 
std::string cipher_list
 Cipher list (OpenSSL format, empty for default)
 
bool enable_session_resumption = true
 Enable session resumption.
 
bool enable_ocsp_stapling = false
 Enable OCSP stapling.
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 
std::unique_ptr< impl > pimpl_
 

Files

file  src/modules/network.cppm
 Primary C++20 module for network_system.
 
file  src/modules/core.cppm
 Core partition for network_system module.
 
file  src/modules/ssl.cppm
 SSL/TLS partition for network_system module.
 
file  src/modules/tcp.cppm
 TCP partition for network_system module.
 
file  src/modules/udp.cppm
 UDP partition for network_system module.