MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkSink.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "NetworkUtils.hpp"
4
6struct NetworkService;
7}
8
10
11/**
12 * @class NetworkSink
13 * @brief Owned handle for sending data through a network stream.
14 *
15 * NetworkSink is the send-side counterpart to Vruta::NetworkSource.
16 * It owns one endpoint opened via NetworkService and exposes send
17 * and send_to as the only public interface.
18 *
19 * Lifetime matches the stream: the endpoint is opened on construction
20 * and closed on destruction. Move-only.
21 *
22 * Consumers hold a shared_ptr<NetworkSink>. A metro coroutine,
23 * a node output callback, or a future Portal::Network::PacketFlow
24 * all hold the same handle. The underlying transport is hidden behind
25 * NetworkFoundry — callers never touch NetworkService directly.
26 *
27 * Today NetworkFoundry is a thin wrapper over NetworkService. When
28 * WebRTC or QUIC arrives, only NetworkFoundry changes.
29 *
30 * @code
31 * auto sink = std::make_shared<Portal::Network::NetworkSink>(
32 * StreamConfig{
33 * .name = "osc_out",
34 * .endpoint = { .address = "127.0.0.1", .port = 9000 },
35 * .profile = StreamProfile::CONTROL,
36 * .transport = NetworkTransportHint::UDP,
37 * });
38 *
39 * sink->send(payload);
40 * sink->send_to(payload, "192.168.1.5", 9001);
41 * @endcode
42 */
43class MAYAFLUX_API NetworkSink {
44public:
45 /**
46 * @brief Open a send endpoint from a StreamConfig.
47 * @param config Stream configuration.
48 *
49 * Resolves the transport hint, opens the endpoint via NetworkService,
50 * and logs a warning if the service is unavailable.
51 */
52 explicit NetworkSink(const StreamConfig& config);
53
54 /**
55 * @brief Close the endpoint and release resources.
56 */
58
59 NetworkSink(const NetworkSink&) = delete;
61 NetworkSink(NetworkSink&&) noexcept;
62 NetworkSink& operator=(NetworkSink&&) noexcept;
63
64 /**
65 * @brief Send bytes through this sink's endpoint.
66 * @param data Read-only byte view of the payload.
67 * @return true if the send was accepted by the backend.
68 *
69 * For UDP: non-blocking sendto() to the address in StreamConfig::endpoint.
70 * For TCP: writes a framed message; may block briefly on backpressure.
71 */
72 [[nodiscard]] bool send(ByteView data) const;
73
74 /**
75 * @brief Send bytes to an explicit address, overriding the config target.
76 * @param data Read-only byte view of the payload.
77 * @param address Target IP address string.
78 * @param port Target port.
79 * @return true if the send was accepted.
80 *
81 * Primary use: UDP fan-out to a different peer than the default target.
82 * TCP ignores address/port and uses the connected peer.
83 */
84 [[nodiscard]] bool send_to(ByteView data, const std::string& address, uint16_t port) const;
85
86 /**
87 * @brief Return true if the endpoint was opened successfully.
88 */
89 [[nodiscard]] bool is_open() const { return m_endpoint_id != 0; }
90
91 /**
92 * @brief Return the stream name from the config.
93 */
94 [[nodiscard]] const std::string& name() const { return m_name; }
95
96private:
97 std::string m_name;
98 uint64_t m_endpoint_id { 0 };
99
100 Registry::Service::NetworkService* m_service { nullptr };
101};
102
103} // namespace MayaFlux::Portal::Network
NetworkSink(const NetworkSink &)=delete
const std::string & name() const
Return the stream name from the config.
NetworkSink & operator=(const NetworkSink &)=delete
Owned handle for sending data through a network stream.
std::span< const uint8_t > ByteView
Convenience alias for a read-only byte view.
Full configuration for an outbound or bidirectional stream.
Backend network transport service interface.