13#include <system_error>
19#include "kcenon/network-core/interfaces/socket_observer.h"
43 class tcp_socket :
public std::enable_shared_from_this<tcp_socket>
87 auto attach_observer(std::shared_ptr<network_core::interfaces::socket_observer> observer)
96 auto detach_observer(std::shared_ptr<network_core::interfaces::socket_observer> observer)
111 std::function<
void(
const std::vector<uint8_t>&)> callback) -> void;
146 std::function<
void(std::span<const uint8_t>)> callback) -> void;
199 std::vector<uint8_t>&& data,
200 std::function<
void(std::error_code, std::size_t)> handler) -> void;
232 std::vector<uint8_t>&& data,
233 std::function<
void(std::error_code, std::size_t)> handler) -> bool;
283 auto close() -> void;
289 [[nodiscard]]
auto is_closed() const ->
bool;
309 std::array<uint8_t, 4096>
332 std::vector<std::weak_ptr<network_core::interfaces::socket_observer>>
observers_{};
A lightweight wrapper around asio::ip::tcp::socket, enabling asynchronous read and write operations.
std::shared_ptr< receive_callback_t > receive_callback_
std::function< void(std::error_code)> error_callback_t
auto socket() -> asio::ip::tcp::socket &
Provides direct access to the underlying asio::ip::tcp::socket in case advanced operations are needed...
tcp_socket(asio::ip::tcp::socket socket)
Constructs a tcp_socket by taking ownership of a moved socket.
std::function< void(std::span< const uint8_t >)> receive_callback_view_t
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
Sets a callback to receive inbound data chunks.
socket_config config_
Backpressure configuration.
auto notify_observers_receive(std::span< const uint8_t > data) -> void
Helper to notify all observers of receive events.
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 notify_observers_backpressure(bool apply) -> void
Helper to notify all observers of backpressure events.
std::atomic< bool > is_closed_
std::array< uint8_t, 4096 > read_buffer_
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_.
asio::ip::tcp::socket socket_
auto stop_read() -> void
Stops the read loop to prevent further async operations.
auto detach_observer(std::shared_ptr< network_core::interfaces::socket_observer > observer) -> void
Detaches a previously attached observer.
auto is_backpressure_active() const -> bool
Checks if backpressure is currently active.
std::atomic< bool > backpressure_active_
Backpressure state flag.
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.
std::atomic< std::size_t > pending_bytes_
Current pending bytes in send buffer.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type aliases for lock-free storage.
std::mutex callback_mutex_
auto attach_observer(std::shared_ptr< network_core::interfaces::socket_observer > observer) -> void
Attaches an observer to receive socket events.
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 notify_observers_error(std::error_code ec) -> void
Helper to notify all observers of error events.
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 pending_bytes() const -> std::size_t
Returns current pending bytes count.
std::shared_ptr< backpressure_callback > backpressure_callback_
Backpressure notification callback.
auto reset_metrics() -> void
Resets socket metrics to zero.
std::function< void(bool apply_backpressure)> backpressure_callback
Callback type for backpressure notifications.
auto metrics() const -> const socket_metrics &
Returns socket metrics for monitoring.
std::shared_ptr< error_callback_t > error_callback_
std::shared_ptr< receive_callback_view_t > receive_callback_view_
auto do_read() -> void
Internal function to handle the read logic with async_read_some().
auto config() const -> const socket_config &
Returns the current socket configuration.
~tcp_socket()=default
Default destructor (no special cleanup needed).
auto is_closed() const -> bool
Checks if the socket has been closed.
std::atomic< bool > is_reading_
auto close() -> void
Safely closes the socket and stops all async operations.
Configuration for TCP socket backpressure control.
Runtime metrics for socket monitoring.