MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
MayaFlux::Core::TCPBackend Class Reference

Connection-oriented reliable stream transport over TCP via standalone Asio. More...

#include <TCPBackend.hpp>

+ Inheritance diagram for MayaFlux::Core::TCPBackend:
+ Collaboration diagram for MayaFlux::Core::TCPBackend:

Classes

struct  ConnectionState
 State for a connected TCP peer (inbound or outbound) More...
 
struct  ListenerState
 State for a listening acceptor. More...
 

Public Member Functions

void close_endpoint (uint64_t endpoint_id) override
 Close an endpoint and release its resources.
 
EndpointState get_endpoint_state (uint64_t endpoint_id) const override
 Query the current state of an endpoint.
 
std::vector< EndpointInfoget_endpoints () const override
 List all endpoints currently managed by this backend.
 
std::string get_name () const override
 
NetworkTransport get_transport () const override
 
std::string get_version () const override
 
bool initialize () override
 Initialise backend resources (sockets, SHM segments, etc.)
 
bool is_initialized () const override
 
bool is_running () const override
 
uint64_t open_endpoint (const EndpointInfo &info) override
 Open a new endpoint.
 
TCPBackendoperator= (const TCPBackend &)=delete
 
TCPBackendoperator= (TCPBackend &&)=delete
 
bool send (uint64_t endpoint_id, const uint8_t *data, size_t size) override
 Send data through an endpoint.
 
bool send_to (uint64_t endpoint_id, const uint8_t *data, size_t size, const std::string &address, uint16_t port) override
 Send data to a specific address through an endpoint.
 
void set_endpoint_id_allocator (std::function< uint64_t()> allocator)
 Set the endpoint id allocator (called by NetworkSubsystem)
 
void set_receive_callback (NetworkReceiveCallback callback) override
 Register the receive callback.
 
void set_state_callback (EndpointStateCallback callback) override
 Register the endpoint state change callback.
 
void shutdown () override
 Release all resources, close all endpoints.
 
void start () override
 Start receive threads and accept connections.
 
void stop () override
 Stop receive threads without releasing resources.
 
 TCPBackend (const TCPBackend &)=delete
 
 TCPBackend (const TCPBackendInfo &config, asio::io_context &context)
 Construct with config and a reference to the shared io_context.
 
 TCPBackend (TCPBackend &&)=delete
 
 ~TCPBackend () override
 
- Public Member Functions inherited from MayaFlux::Core::INetworkBackend
virtual ~INetworkBackend ()=default
 

Private Member Functions

void on_connection_error (ConnectionState &conn, const asio::error_code &ec)
 Handle a connection error (read/write failure).
 
void on_header_received (ConnectionState &conn, const asio::error_code &ec, size_t bytes)
 Header read completion handler.
 
void on_payload_received (ConnectionState &conn, const asio::error_code &ec, size_t bytes)
 Payload read completion handler.
 
void schedule_reconnect (ConnectionState &conn)
 Schedule a reconnect attempt after the configured interval.
 
void start_accept (ListenerState &listener)
 Initiate async_accept loop for a listener.
 
void start_connect (ConnectionState &conn)
 Initiate async_connect for an outbound endpoint.
 
void start_receive_chain (ConnectionState &conn)
 Start the framed message receive chain on a connection.
 
void transition_state (EndpointInfo &info, EndpointState new_state)
 

Private Attributes

std::function< uint64_t()> m_allocate_endpoint_id
 Pointer to subsystem's endpoint id allocator.
 
TCPBackendInfo m_config
 
std::unordered_map< uint64_t, std::unique_ptr< ConnectionState > > m_connections
 
std::shared_mutex m_connections_mutex
 
asio::io_context & m_context
 
std::atomic< bool > m_initialized { false }
 
std::unordered_map< uint64_t, std::unique_ptr< ListenerState > > m_listeners
 
std::shared_mutex m_listeners_mutex
 
NetworkReceiveCallback m_receive_callback
 
std::atomic< bool > m_running { false }
 
EndpointStateCallback m_state_callback
 

Detailed Description

Connection-oriented reliable stream transport over TCP via standalone Asio.

Two endpoint modes:

  • Outbound (remote_address set): async_connect to a remote host.
  • Listener (local_port set, no remote_address): async_accept on a port. Accepted connections become new endpoints, announced via state callback.

Framing: every message on the wire is [uint32_t network-order length][payload]. The backend handles framing transparently. Callers send and receive complete messages, never partial streams.

Reconnection: if auto_reconnect is enabled and a connection drops, the backend transitions to RECONNECTING and schedules async_connect retries via asio::steady_timer.

All I/O is async on the shared io_context. No dedicated threads per connection. No mutexes on the data path. One io_context thread handles all sockets: accept, connect, read, write, reconnect timers.

Receive chain per connection: async_read(4 bytes header) -> parse length -> async_read(length bytes payload) -> fire callback -> resubmit header read

Send: async_write(4 bytes header + payload). Serialised per-connection via Asio strand (implicit: all operations on one io_context thread).

asio::io_context ctx;
TCPBackend tcp(config, ctx);
tcp.initialize();
tcp.set_receive_callback([](uint64_t id, const uint8_t* d, size_t s, std::string_view a) {
// handle complete framed message
});
tcp.set_state_callback([](const EndpointInfo& ep, EndpointState prev, EndpointState cur) {
// track connection/disconnection
});
tcp.start();
// Outbound connection
EndpointInfo client_ep;
client_ep.role = EndpointRole::BIDIRECTIONAL;
client_ep.remote_address = "192.168.1.50";
client_ep.remote_port = 7000;
auto ep_id = tcp.open_endpoint(client_ep);
// Listener
EndpointInfo server_ep;
server_ep.role = EndpointRole::RECEIVE;
server_ep.local_port = 7000;
auto listen_id = tcp.open_endpoint(server_ep);
ctx.run();
size_t a
Connection-oriented reliable stream transport over TCP via standalone Asio.
EndpointState
Observable connection state for an endpoint.
Describes one logical send/receive endpoint managed by a backend.

Definition at line 68 of file TCPBackend.hpp.


The documentation for this class was generated from the following files: