|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
A lightweight wrapper around asio::ip::tcp::socket, enabling asynchronous read and write operations.
More...
#include <tcp_socket.h>


Public Types | |
| using | backpressure_callback = std::function<void(bool apply_backpressure)> |
| Callback type for backpressure notifications. | |
Public Member Functions | |
| tcp_socket (asio::ip::tcp::socket socket) | |
Constructs a tcp_socket by taking ownership of a moved socket. | |
| tcp_socket (asio::ip::tcp::socket socket, const socket_config &config) | |
Constructs a tcp_socket with custom configuration. | |
| ~tcp_socket ()=default | |
| Default destructor (no special cleanup needed). | |
| auto | attach_observer (std::shared_ptr< network_core::interfaces::socket_observer > observer) -> void |
| Attaches an observer to receive socket events. | |
| auto | detach_observer (std::shared_ptr< network_core::interfaces::socket_observer > observer) -> void |
| Detaches a previously attached observer. | |
| auto | set_receive_callback (std::function< void(const std::vector< uint8_t > &)> callback) -> void |
| Sets a callback to receive inbound data chunks. | |
| auto | set_receive_callback_view (std::function< void(std::span< const uint8_t >)> callback) -> void |
| Sets a zero-copy callback to receive inbound data as a view. | |
| auto | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets a callback to handle socket errors (e.g., read/write failures). | |
| auto | start_read () -> void |
| Begins the continuous asynchronous read loop. | |
| auto | async_send (std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> void |
Initiates an asynchronous write of the given data buffer. | |
| auto | set_backpressure_callback (backpressure_callback callback) -> void |
| Sets a callback for backpressure notifications. | |
| auto | try_send (std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> bool |
| Attempts to send data without blocking. | |
| auto | pending_bytes () const -> std::size_t |
| Returns current pending bytes count. | |
| auto | is_backpressure_active () const -> bool |
| Checks if backpressure is currently active. | |
| auto | metrics () const -> const socket_metrics & |
| Returns socket metrics for monitoring. | |
| auto | reset_metrics () -> void |
| Resets socket metrics to zero. | |
| auto | config () const -> const socket_config & |
| Returns the current socket configuration. | |
| auto | socket () -> asio::ip::tcp::socket & |
Provides direct access to the underlying asio::ip::tcp::socket in case advanced operations are needed. | |
| auto | stop_read () -> void |
| Stops the read loop to prevent further async operations. | |
| auto | close () -> void |
| Safely closes the socket and stops all async operations. | |
| auto | is_closed () const -> bool |
| Checks if the socket has been closed. | |
Private Types | |
| using | receive_callback_t = std::function<void(const std::vector<uint8_t>&)> |
| Callback type aliases for lock-free storage. | |
| using | receive_callback_view_t = std::function<void(std::span<const uint8_t>)> |
| using | error_callback_t = std::function<void(std::error_code)> |
Private Member Functions | |
| auto | do_read () -> void |
Internal function to handle the read logic with async_read_some(). | |
| auto | notify_observers_receive (std::span< const uint8_t > data) -> void |
| Helper to notify all observers of receive events. | |
| auto | notify_observers_error (std::error_code ec) -> void |
| Helper to notify all observers of error events. | |
| auto | notify_observers_backpressure (bool apply) -> void |
| Helper to notify all observers of backpressure events. | |
Private Attributes | |
| asio::ip::tcp::socket | socket_ |
| std::array< uint8_t, 4096 > | read_buffer_ |
| std::shared_ptr< receive_callback_t > | receive_callback_ |
| std::shared_ptr< receive_callback_view_t > | receive_callback_view_ |
| std::shared_ptr< error_callback_t > | error_callback_ |
| std::mutex | callback_mutex_ |
| socket_config | config_ |
| Backpressure configuration. | |
| socket_metrics | metrics_ |
| Socket runtime metrics. | |
| std::vector< std::weak_ptr< network_core::interfaces::socket_observer > > | observers_ {} |
| List of socket observers (using weak_ptr for automatic cleanup). Protected by callback_mutex_. | |
| std::atomic< bool > | is_reading_ {false} |
| std::atomic< bool > | is_closed_ {false} |
| std::atomic< std::size_t > | pending_bytes_ {0} |
| Current pending bytes in send buffer. | |
| std::atomic< bool > | backpressure_active_ {false} |
| Backpressure state flag. | |
| std::shared_ptr< backpressure_callback > | backpressure_callback_ |
| Backpressure notification callback. | |
A lightweight wrapper around asio::ip::tcp::socket, enabling asynchronous read and write operations.
socket_ (from ASIO) for TCP communication.set_receive_callback() to handle inbound data and set_error_callback() for error handling.start_read() begins an ongoing loop of async_read_some().async_send() performs an async_write of a given data buffer.Definition at line 43 of file tcp_socket.h.
| using kcenon::network::internal::tcp_socket::backpressure_callback = std::function<void(bool apply_backpressure)> |
Callback type for backpressure notifications.
| apply_backpressure | True when backpressure should be applied (high water mark reached), false when it can be released (low water mark reached). |
Definition at line 52 of file tcp_socket.h.
|
private |
Definition at line 305 of file tcp_socket.h.
|
private |
Callback type aliases for lock-free storage.
Definition at line 303 of file tcp_socket.h.
|
private |
Definition at line 304 of file tcp_socket.h.
| kcenon::network::internal::tcp_socket::tcp_socket | ( | asio::ip::tcp::socket | socket | ) |
Constructs a tcp_socket by taking ownership of a moved socket.
| socket | An asio::ip::tcp::socket that must be open/connected or at least valid. |
After construction, you can immediately call start_read() to begin receiving data. For sending, call async_send().
Definition at line 15 of file tcp_socket.cpp.
| kcenon::network::internal::tcp_socket::tcp_socket | ( | asio::ip::tcp::socket | socket, |
| const socket_config & | config ) |
Constructs a tcp_socket with custom configuration.
| socket | An asio::ip::tcp::socket that must be open/connected. |
| config | Socket configuration including backpressure settings. |
Definition at line 21 of file tcp_socket.cpp.
References config.
|
default |
Default destructor (no special cleanup needed).
| auto kcenon::network::internal::tcp_socket::async_send | ( | std::vector< uint8_t > && | data, |
| std::function< void(std::error_code, std::size_t)> | handler ) -> void |
Initiates an asynchronous write of the given data buffer.
| data | The buffer to send over TCP (moved for efficiency). |
| handler | A completion handler with signature void(std::error_code, std::size_t) that is invoked upon success or failure. |
The handler receives:
ec : the std::error_code from the write operation,bytes_transferred : how many bytes were actually written.Definition at line 253 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::attach_observer | ( | std::shared_ptr< network_core::interfaces::socket_observer > | observer | ) | -> void |
Attaches an observer to receive socket events.
| observer | Shared pointer to socket_observer implementation. |
Multiple observers can be attached to the same socket. All attached observers will be notified of socket events.
| auto kcenon::network::internal::tcp_socket::close | ( | ) | -> void |
Safely closes the socket and stops all async operations.
This method atomically sets the closed flag before closing the socket, preventing data races between the close operation and async read operations. Thread-safe with respect to concurrent async operations.
Definition at line 89 of file tcp_socket.cpp.
|
nodiscard |
Returns the current socket configuration.
Definition at line 429 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::detach_observer | ( | std::shared_ptr< network_core::interfaces::socket_observer > | observer | ) | -> void |
Detaches a previously attached observer.
| observer | The observer to detach. |
If the observer was not attached, this method has no effect.
|
private |
Internal function to handle the read logic with async_read_some().
Upon success, it calls receive_callback_ if set, then schedules another read. On error, it calls error_callback_ if available.
Definition at line 132 of file tcp_socket.cpp.
|
nodiscard |
Checks if backpressure is currently active.
Definition at line 414 of file tcp_socket.cpp.
|
nodiscard |
Checks if the socket has been closed.
Definition at line 127 of file tcp_socket.cpp.
|
nodiscard |
Returns socket metrics for monitoring.
Definition at line 419 of file tcp_socket.cpp.
|
private |
Helper to notify all observers of backpressure events.
|
private |
Helper to notify all observers of error events.
|
private |
Helper to notify all observers of receive events.
|
nodiscard |
Returns current pending bytes count.
Definition at line 409 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::reset_metrics | ( | ) | -> void |
| auto kcenon::network::internal::tcp_socket::set_backpressure_callback | ( | backpressure_callback | callback | ) | -> void |
Sets a callback for backpressure notifications.
| callback | Function invoked when backpressure state changes. |
The callback receives true when pending bytes exceed high_water_mark (apply backpressure), and false when they drop below low_water_mark (release backpressure).
Definition at line 382 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets a callback to handle socket errors (e.g., read/write failures).
| callback | A function with signature void(std::error_code), invoked when any asynchronous operation fails. |
If no callback is set, errors are not explicitly handled here (beyond stopping reads).
Definition at line 43 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::set_receive_callback | ( | std::function< void(const std::vector< uint8_t > &)> | callback | ) | -> void |
Sets a callback to receive inbound data chunks.
| callback | A function with signature void(const
std::vector<uint8_t>&), called whenever a chunk of data is successfully read. |
If no callback is set, received data is effectively discarded.
Definition at line 27 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::set_receive_callback_view | ( | std::function< void(std::span< const uint8_t >)> | callback | ) | -> void |
Sets a zero-copy callback to receive inbound data as a view.
| callback | A function with signature void(std::span<const
uint8_t>), called whenever a chunk of data is successfully read. |
Unlike set_receive_callback(), this callback receives data as a non-owning view directly into the internal read buffer, avoiding per-read std::vector allocations and copies.
Definition at line 35 of file tcp_socket.cpp.
|
inline |
Provides direct access to the underlying asio::ip::tcp::socket in case advanced operations are needed.
asio::ip::tcp::socket. Definition at line 269 of file tcp_socket.h.
References socket_.
| auto kcenon::network::internal::tcp_socket::start_read | ( | ) | -> void |
Begins the continuous asynchronous read loop.
Once called, the class repeatedly calls async_read_some(). If an error occurs, on_error() is triggered, stopping further reads.
Definition at line 51 of file tcp_socket.cpp.
| auto kcenon::network::internal::tcp_socket::stop_read | ( | ) | -> void |
Stops the read loop to prevent further async operations.
Definition at line 83 of file tcp_socket.cpp.
|
nodiscard |
Attempts to send data without blocking.
| data | The buffer to send (moved for efficiency). |
| handler | Completion handler for async operation. |
Unlike async_send(), this method checks backpressure limits before initiating the send. Returns false immediately if:
Definition at line 389 of file tcp_socket.cpp.
|
private |
|
private |
Backpressure notification callback.
Definition at line 344 of file tcp_socket.h.
|
private |
Protects callback registration only.
Definition at line 320 of file tcp_socket.h.
|
private |
Backpressure configuration.
Definition at line 323 of file tcp_socket.h.
|
private |
Definition at line 318 of file tcp_socket.h.
|
private |
|
private |
|
mutableprivate |
Socket runtime metrics.
Definition at line 326 of file tcp_socket.h.
|
private |
List of socket observers (using weak_ptr for automatic cleanup). Protected by callback_mutex_.
Definition at line 332 of file tcp_socket.h.
|
private |
|
private |
Buffer for receiving data in do_read().
Definition at line 310 of file tcp_socket.h.
|
private |
Lock-free callback storage using shared_ptr + atomic operations. This eliminates mutex contention on the receive hot path.
Definition at line 316 of file tcp_socket.h.
|
private |
Definition at line 317 of file tcp_socket.h.
|
private |
The underlying ASIO TCP socket.
Definition at line 307 of file tcp_socket.h.
Referenced by socket().