5#include <asio/io_context.hpp>
6#include <asio/ip/tcp.hpp>
7#include <asio/steady_timer.hpp>
86 void start()
override;
88 void shutdown()
override;
90 [[nodiscard]]
bool is_initialized()
const override {
return m_initialized.load(); }
91 [[nodiscard]]
bool is_running()
const override {
return m_running.load(); }
94 [[nodiscard]] std::string
get_name()
const override {
return "TCP (Asio)"; }
95 [[nodiscard]] std::string
get_version()
const override {
return "1.0"; }
99 uint64_t open_endpoint(
const EndpointInfo& info)
override;
100 void close_endpoint(uint64_t endpoint_id)
override;
101 [[nodiscard]]
EndpointState get_endpoint_state(uint64_t endpoint_id)
const override;
102 [[nodiscard]] std::vector<EndpointInfo> get_endpoints()
const override;
106 bool send(uint64_t endpoint_id,
const uint8_t* data,
size_t size)
override;
107 bool send_to(uint64_t endpoint_id,
const uint8_t* data,
size_t size,
108 const std::string& address, uint16_t port)
override;
122 std::array<uint8_t, 4> header_buf {};
143 , pending_socket(ctx)
151 std::atomic<bool> m_initialized {
false };
152 std::atomic<bool> m_running {
false };
155 std::unordered_map<uint64_t, std::unique_ptr<ConnectionState>>
m_connections;
158 std::unordered_map<uint64_t, std::unique_ptr<ListenerState>>
m_listeners;
204 const asio::error_code& ec,
size_t bytes);
210 const asio::error_code& ec,
size_t bytes);
218 void on_connection_error(
ConnectionState& conn,
const asio::error_code& ec);
236 m_allocate_endpoint_id = std::move(allocator);
Abstract interface for network transport backends.
std::string get_name() const override
void set_endpoint_id_allocator(std::function< uint64_t()> allocator)
Set the endpoint id allocator (called by NetworkSubsystem)
std::function< uint64_t()> m_allocate_endpoint_id
Pointer to subsystem's endpoint id allocator.
NetworkReceiveCallback m_receive_callback
TCPBackend & operator=(const TCPBackend &)=delete
std::unordered_map< uint64_t, std::unique_ptr< ConnectionState > > m_connections
std::shared_mutex m_listeners_mutex
TCPBackend(TCPBackend &&)=delete
std::unordered_map< uint64_t, std::unique_ptr< ListenerState > > m_listeners
bool is_initialized() const override
TCPBackend(const TCPBackend &)=delete
std::shared_mutex m_connections_mutex
std::string get_version() const override
bool is_running() const override
TCPBackend & operator=(TCPBackend &&)=delete
asio::io_context & m_context
NetworkTransport get_transport() const override
EndpointStateCallback m_state_callback
Connection-oriented reliable stream transport over TCP via standalone Asio.
EndpointState
Observable connection state for an endpoint.
NetworkTransport
Identifies the transport protocol a backend implements.
std::function< void(uint64_t endpoint_id, const uint8_t *data, size_t size, std::string_view sender_addr)> NetworkReceiveCallback
Callback signature for inbound data on an endpoint.
std::function< void(const EndpointInfo &info, EndpointState previous, EndpointState current)> EndpointStateCallback
Callback signature for endpoint state changes.
Describes one logical send/receive endpoint managed by a backend.
Configuration for the TCP transport backend.
asio::ip::tcp::socket socket
ConnectionState(asio::io_context &ctx)
std::unique_ptr< asio::steady_timer > reconnect_timer
std::vector< uint8_t > payload_buf
State for a connected TCP peer (inbound or outbound)
ListenerState(asio::io_context &ctx)
asio::ip::tcp::acceptor acceptor
asio::ip::tcp::socket pending_socket
State for a listening acceptor.