MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkBackend.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6
7/**
8 * @brief Callback signature for inbound data on an endpoint
9 *
10 * @param endpoint_id The endpoint that received the data.
11 * @param data Pointer to received bytes. Valid only for the
12 * duration of the callback.
13 * @param size Number of bytes received.
14 * @param sender_addr Sender address string (IP:port for UDP, peer
15 * address for TCP, segment name for SHM).
16 */
17using NetworkReceiveCallback = std::function<void(
18 uint64_t endpoint_id,
19 const uint8_t* data,
20 size_t size,
21 std::string_view sender_addr)>;
22
23/**
24 * @brief Callback signature for endpoint state changes
25 */
26using EndpointStateCallback = std::function<void(
27 const EndpointInfo& info,
28 EndpointState previous,
29 EndpointState current)>;
30
31/**
32 * @class INetworkBackend
33 * @brief Abstract interface for network transport backends
34 *
35 * Follows the same lifecycle and structural pattern as IInputBackend:
36 * initialize() -> start() -> [running] -> stop() -> shutdown()
37 *
38 * Each concrete implementation (UDP, TCP, SharedMemory) owns sockets
39 * or shared segments, manages background receive threads, and exposes
40 * a uniform endpoint model. The NetworkSubsystem owns backends and
41 * routes high-level operations through this interface.
42 *
43 * Bidirectional: sends and receives, manages persistent endpoints
44 * rather than devices. This is the core structural difference from
45 * IInputBackend.
46 *
47 * Backends are deliberately synchronous and callback-based. They know
48 * nothing about coroutines. The coroutine surface lives in NetworkService
49 * and is implemented by NetworkSubsystem using callbacks internally to
50 * resume suspended coroutines. This keeps backends simple (pure OS socket
51 * wrappers) and avoids coupling transport code to the Vruta/Kriya stack.
52 *
53 * Endpoint lifecycle:
54 * open_endpoint(info) -> endpoint_id
55 * send(endpoint_id, data, size)
56 * [receive via callback]
57 * close_endpoint(endpoint_id)
58 *
59 * Future transport backends (WebRTC, GStreamer, etc.) implement this same
60 * interface. Protocol-level orchestration above raw transport (codec
61 * pipelines, session management, jitter buffering) belongs in a future
62 * Portal::Network layer, not here.
63 */
64class MAYAFLUX_API INetworkBackend {
65public:
66 virtual ~INetworkBackend() = default;
67
68 // ─────────────────────────────────────────────────────────────────────────
69 // Lifecycle
70 // ─────────────────────────────────────────────────────────────────────────
71
72 /**
73 * @brief Initialise backend resources (sockets, SHM segments, etc.)
74 * @return true if initialisation succeeded
75 *
76 * Allocates platform resources but does not start receive threads.
77 */
78 virtual bool initialize() = 0;
79
80 /**
81 * @brief Start receive threads and accept connections
82 *
83 * After this call the backend actively delivers received data via
84 * the registered callback and accepts new inbound connections (TCP).
85 */
86 virtual void start() = 0;
87
88 /**
89 * @brief Stop receive threads without releasing resources
90 *
91 * Endpoints remain valid. Pending sends complete. Receive callbacks
92 * cease until start() is called again.
93 */
94 virtual void stop() = 0;
95
96 /**
97 * @brief Release all resources, close all endpoints
98 *
99 * After this call initialize() must be called again to reuse the backend.
100 */
101 virtual void shutdown() = 0;
102
103 [[nodiscard]] virtual bool is_initialized() const = 0;
104 [[nodiscard]] virtual bool is_running() const = 0;
105
106 // ─────────────────────────────────────────────────────────────────────────
107 // Transport identity
108 // ─────────────────────────────────────────────────────────────────────────
109
110 [[nodiscard]] virtual NetworkTransport get_transport() const = 0;
111 [[nodiscard]] virtual std::string get_name() const = 0;
112 [[nodiscard]] virtual std::string get_version() const = 0;
113
114 // ─────────────────────────────────────────────────────────────────────────
115 // Endpoint management
116 // ─────────────────────────────────────────────────────────────────────────
117
118 /**
119 * @brief Open a new endpoint
120 * @param info Endpoint configuration with id pre-assigned by subsystem.
121 * @return The endpoint id on success, or 0 on failure.
122 *
123 * The subsystem assigns the id before calling this. The backend stores
124 * and uses it for subsequent operations.
125 *
126 * For UDP: binds a local port and/or stores a default remote target.
127 * For TCP: connects to a remote host, or begins listening on a port.
128 * Outbound connect may block. Use co_open_endpoint on the
129 * service for non-blocking connect.
130 * For SHM: opens or creates a named shared memory segment.
131 */
132 virtual uint64_t open_endpoint(const EndpointInfo& info) = 0;
133
134 /**
135 * @brief Close an endpoint and release its resources
136 * @param endpoint_id Endpoint to close. No-op if already closed.
137 */
138 virtual void close_endpoint(uint64_t endpoint_id) = 0;
139
140 /**
141 * @brief Query the current state of an endpoint
142 */
143 [[nodiscard]] virtual EndpointState get_endpoint_state(uint64_t endpoint_id) const = 0;
144
145 /**
146 * @brief List all endpoints currently managed by this backend
147 */
148 [[nodiscard]] virtual std::vector<EndpointInfo> get_endpoints() const = 0;
149
150 // ─────────────────────────────────────────────────────────────────────────
151 // Data transfer
152 // ─────────────────────────────────────────────────────────────────────────
153
154 /**
155 * @brief Send data through an endpoint
156 * @param endpoint_id Target endpoint.
157 * @param data Pointer to payload bytes.
158 * @param size Payload size in bytes.
159 * @return true if the send was accepted (queued or completed).
160 *
161 * For UDP: sends a single datagram via sendto(). Non-blocking.
162 * For TCP: writes framed message. May block briefly if kernel
163 * send buffer is full.
164 * For SHM: writes into the shared segment.
165 */
166 virtual bool send(uint64_t endpoint_id, const uint8_t* data, size_t size) = 0;
167
168 /**
169 * @brief Send data to a specific address through an endpoint
170 * @param endpoint_id Source endpoint (must be open).
171 * @param data Pointer to payload bytes.
172 * @param size Payload size in bytes.
173 * @param address Target address string.
174 * @param port Target port.
175 * @return true if the send was accepted.
176 *
177 * Primary use: UDP (send to arbitrary peer via bound socket).
178 * TCP: ignored, uses connected peer.
179 * SHM: ignored.
180 */
181 virtual bool send_to(uint64_t endpoint_id, const uint8_t* data, size_t size,
182 const std::string& address, uint16_t port) = 0;
183
184 // ─────────────────────────────────────────────────────────────────────────
185 // Callbacks
186 // ─────────────────────────────────────────────────────────────────────────
187
188 /**
189 * @brief Register the receive callback
190 *
191 * Called from the backend's receive thread. The subsystem wires this
192 * to route data to per-endpoint callbacks and to resume suspended
193 * coroutines. Backends fire this for every received datagram (UDP)
194 * or framed message (TCP).
195 */
197
198 /**
199 * @brief Register the endpoint state change callback
200 *
201 * Fired when an endpoint transitions state (OPENING -> OPEN,
202 * OPEN -> ERROR, ERROR -> RECONNECTING, etc.). The subsystem uses
203 * this to resume co_open_endpoint awaitables and to notify consumers.
204 */
205 virtual void set_state_callback(EndpointStateCallback callback) = 0;
206};
207
208} // namespace MayaFlux::Core
Range size
virtual std::string get_name() const =0
virtual bool send(uint64_t endpoint_id, const uint8_t *data, size_t size)=0
Send data through an endpoint.
virtual bool initialize()=0
Initialise backend resources (sockets, SHM segments, etc.)
virtual bool is_initialized() const =0
virtual std::string get_version() const =0
virtual ~INetworkBackend()=default
virtual void shutdown()=0
Release all resources, close all endpoints.
virtual uint64_t open_endpoint(const EndpointInfo &info)=0
Open a new endpoint.
virtual void close_endpoint(uint64_t endpoint_id)=0
Close an endpoint and release its resources.
virtual EndpointState get_endpoint_state(uint64_t endpoint_id) const =0
Query the current state of an endpoint.
virtual void start()=0
Start receive threads and accept connections.
virtual void stop()=0
Stop receive threads without releasing resources.
virtual bool is_running() const =0
virtual bool send_to(uint64_t endpoint_id, const uint8_t *data, size_t size, const std::string &address, uint16_t port)=0
Send data to a specific address through an endpoint.
virtual void set_state_callback(EndpointStateCallback callback)=0
Register the endpoint state change callback.
virtual std::vector< EndpointInfo > get_endpoints() const =0
List all endpoints currently managed by this backend.
virtual void set_receive_callback(NetworkReceiveCallback callback)=0
Register the receive callback.
virtual NetworkTransport get_transport() const =0
Abstract interface for network transport backends.
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.