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

Connectionless datagram transport over UDP via standalone Asio. More...

#include <UDPBackend.hpp>

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

Classes

struct  EndpointRecord
 Per-endpoint state. More...
 
struct  SocketState
 Per-bound-port socket state. 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.
 
UDPBackendoperator= (const UDPBackend &)=delete
 
UDPBackendoperator= (UDPBackend &&)=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_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.
 
 UDPBackend (const UDPBackend &)=delete
 
 UDPBackend (const UDPBackendInfo &config, asio::io_context &context)
 Construct with config and a reference to the shared io_context.
 
 UDPBackend (UDPBackend &&)=delete
 
 ~UDPBackend () override
 
- Public Member Functions inherited from MayaFlux::Core::INetworkBackend
virtual ~INetworkBackend ()=default
 

Private Member Functions

SocketStateacquire_socket (uint16_t local_port)
 Get or create a socket bound to local_port.
 
void on_receive (SocketState &state, const asio::error_code &ec, size_t bytes_received)
 Completion handler for async_receive_from.
 
void release_socket (uint16_t local_port)
 Decrement ref_count for a socket.
 
uint64_t resolve_endpoint_for_sender (uint16_t local_port, const asio::ip::udp::endpoint &sender) const
 Resolve which endpoint id a received datagram belongs to.
 
void start_receive_loop (SocketState &state)
 Post the first async_receive_from for a newly bound socket.
 
void transition_state (EndpointRecord &record, EndpointState new_state)
 

Private Attributes

UDPBackendInfo m_config
 
asio::io_context & m_context
 
std::unordered_map< uint64_t, EndpointRecordm_endpoints
 
std::shared_mutex m_endpoints_mutex
 
std::atomic< bool > m_initialized { false }
 
NetworkReceiveCallback m_receive_callback
 
std::atomic< bool > m_running { false }
 
std::unordered_map< uint16_t, std::unique_ptr< SocketState > > m_sockets
 
std::shared_mutex m_sockets_mutex
 
EndpointStateCallback m_state_callback
 

Detailed Description

Connectionless datagram transport over UDP via standalone Asio.

Each endpoint maps to a (socket, optional default remote) pair. Endpoints that share a local port share the underlying socket.

All I/O is async on the io_context provided by NetworkSubsystem. No dedicated threads, no mutexes on the data path.

async_receive_from -> completion fires receive callback -> resubmit async_send_to -> fire-and-forget (UDP is unreliable anyway)

OSC, Art-Net, or any other datagram protocol is a serialisation concern above this layer. This backend knows nothing about message formats: bytes in, bytes out.

// Created and started by NetworkSubsystem. Direct use for testing:
asio::io_context ctx;
UDPBackend udp(config, ctx);
udp.initialize();
udp.set_receive_callback([](uint64_t id, const uint8_t* d, size_t s, std::string_view a) {
// handle datagram
});
udp.start();
ep.local_port = 8000;
ep.remote_address = "127.0.0.1";
ep.remote_port = 9000;
auto id = udp.open_endpoint(ep);
udp.send(id, payload.data(), payload.size());
ctx.run(); // normally driven by NetworkSubsystem's jthread
size_t a
Connectionless datagram transport over UDP via standalone Asio.
Describes one logical send/receive endpoint managed by a backend.

Definition at line 49 of file UDPBackend.hpp.


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