MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkService.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7/**
8 * @brief Backend network transport service interface
9 *
10 * Registered into BackendRegistry by NetworkSubsystem. Follows the same
11 * service pattern as BufferService, ComputeService, DisplayService, and
12 * InputService. Enables any component (nodes, buffer processors,
13 * InputManager) to send and receive data over the network without
14 * coupling to NetworkSubsystem internals.
15 *
16 * Consumption model: callbacks. The receive callback fires on the
17 * backend's receive thread. Consumers that need to integrate with
18 * coroutine-based workflows can bridge via Kriya::ProcessingGate
19 * (poll a flag the callback sets) or, when implemented, via a
20 * dedicated Kriya::NetworkReceive awaiter modelled on EventAwaiter.
21 *
22 * The coroutine surface is deliberately absent from this initial
23 * version. The pattern exists (EventAwaiter), the integration point
24 * is clear (set_endpoint_receive_callback resumes a suspended handle),
25 * but the awaiter should be added when a concrete use case demands it,
26 * not speculatively.
27 *
28 * @code
29 * auto* svc = BackendRegistry::instance().get_service<NetworkService>();
30 * if (!svc) return;
31 *
32 * auto ep = svc->open_endpoint({
33 * .transport = NetworkTransport::UDP,
34 * .role = EndpointRole::BIDIRECTIONAL,
35 * .local_port = 8000,
36 * .remote_address = "127.0.0.1",
37 * .remote_port = 9000
38 * });
39 *
40 * svc->set_endpoint_receive_callback(ep,
41 * [](uint64_t id, const uint8_t* d, size_t s, std::string_view addr) {
42 * // fires on backend receive thread
43 * });
44 *
45 * svc->send(ep, payload.data(), payload.size());
46 * svc->close_endpoint(ep);
47 * @endcode
48 */
49struct MAYAFLUX_API NetworkService {
50
51 // ─────────────────────────────────────────────────────────────────────────
52 // Endpoint lifecycle
53 // ─────────────────────────────────────────────────────────────────────────
54
55 /**
56 * @brief Open a network endpoint on the appropriate backend
57 * @param info Endpoint configuration. transport field selects the backend.
58 * @return Globally unique endpoint id, or 0 on failure.
59 *
60 * For UDP: non-blocking (bind is instant).
61 * For TCP with EndpointRole::RECEIVE (listener): non-blocking.
62 * For TCP with outbound connection: blocks until connected or timeout.
63 */
64 std::function<uint64_t(const Core::EndpointInfo& info)> open_endpoint;
65
66 /**
67 * @brief Close an endpoint
68 * @param endpoint_id Endpoint to close.
69 */
70 std::function<void(uint64_t endpoint_id)> close_endpoint;
71
72 /**
73 * @brief Query endpoint state
74 * @param endpoint_id Endpoint to query.
75 * @return Current state, or CLOSED if unknown.
76 */
77 std::function<Core::EndpointState(uint64_t endpoint_id)> get_endpoint_state;
78
79 /**
80 * @brief List all open endpoints across all backends
81 */
82 std::function<std::vector<Core::EndpointInfo>()> get_all_endpoints;
83
84 // ─────────────────────────────────────────────────────────────────────────
85 // Data transfer
86 // ─────────────────────────────────────────────────────────────────────────
87
88 /**
89 * @brief Send data through an endpoint
90 * @param endpoint_id Target endpoint.
91 * @param data Payload bytes.
92 * @param size Payload size.
93 * @return true if the send was accepted.
94 *
95 * For UDP: non-blocking sendto().
96 * For TCP: may block briefly if kernel send buffer is full.
97 */
98 std::function<bool(uint64_t endpoint_id, const uint8_t* data, size_t size)> send;
99
100 /**
101 * @brief Send data to a specific address through an endpoint
102 * @param endpoint_id Source endpoint.
103 * @param data Payload bytes.
104 * @param size Payload size.
105 * @param address Target address.
106 * @param port Target port.
107 * @return true if the send was accepted.
108 *
109 * Primary use: UDP. TCP ignores address/port, uses connected peer.
110 */
111 std::function<bool(uint64_t endpoint_id, const uint8_t* data, size_t size,
112 const std::string& address, uint16_t port)>
114
115 // ─────────────────────────────────────────────────────────────────────────
116 // Receive callbacks
117 // ─────────────────────────────────────────────────────────────────────────
118
119 /**
120 * @brief Register a per-endpoint receive callback
121 * @param endpoint_id Endpoint to listen on.
122 * @param callback Called on the backend's receive thread.
123 *
124 * Overrides any previously registered callback for this endpoint.
125 * For endpoints opened with EndpointRole::SEND this is a no-op.
126 *
127 * Thread safety: the callback fires on the backend's receive thread.
128 * If the consumer needs to bridge to a different thread (audio callback,
129 * coroutine scheduler), it must handle that in the callback body
130 * (e.g., push into a lock-free queue, set an atomic flag).
131 */
132 std::function<void(uint64_t endpoint_id,
133 std::function<void(uint64_t, const uint8_t*, size_t, std::string_view)> callback)>
135};
136
137} // namespace MayaFlux::Registry::Service
Range size
EndpointState
Observable connection state for an endpoint.
Describes one logical send/receive endpoint managed by a backend.
std::function< Core::EndpointState(uint64_t endpoint_id)> get_endpoint_state
Query endpoint state.
std::function< bool(uint64_t endpoint_id, const uint8_t *data, size_t size)> send
Send data through an endpoint.
std::function< bool(uint64_t endpoint_id, const uint8_t *data, size_t size, const std::string &address, uint16_t port)> send_to
Send data to a specific address through an endpoint.
std::function< uint64_t(const Core::EndpointInfo &info)> open_endpoint
Open a network endpoint on the appropriate backend.
std::function< void(uint64_t endpoint_id, std::function< void(uint64_t, const uint8_t *, size_t, std::string_view)> callback)> set_endpoint_receive_callback
Register a per-endpoint receive callback.
std::function< void(uint64_t endpoint_id)> close_endpoint
Close an endpoint.
std::function< std::vector< Core::EndpointInfo >()> get_all_endpoints
List all open endpoints across all backends.
Backend network transport service interface.