MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NetworkAwaiter.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <coroutine>
6
7namespace MayaFlux::Kriya {
8
9/**
10 * @class NetworkAwaiter
11 * @brief Awaiter for suspending a coroutine until a network message arrives
12 *
13 * Follows the same structural pattern as EventAwaiter. Works with any
14 * coroutine type: SoundRoutine, GraphicsRoutine, Event, ComplexRoutine.
15 *
16 * Multiple awaiters may register against the same NetworkSource
17 * simultaneously. signal() delivers a copy of the message directly to
18 * each awaiter — no shared queue contention between waiters.
19 *
20 * The queue on NetworkSource is used only by await_ready() for the
21 * no-suspend fast path. Once suspended, the awaiter receives its
22 * message via deliver() called directly from NetworkSource::signal().
23 *
24 * The awaiter does not own the NetworkSource. The source must outlive
25 * any suspended awaiter.
26 *
27 * @code
28 * Vruta::NetworkSource source({ .transport = NetworkTransport::UDP, .local_port = 8000 });
29 *
30 * auto routine = [&source]() -> Vruta::Event {
31 * while (true) {
32 * auto msg = co_await source.next_message();
33 * process(msg.data);
34 * }
35 * };
36 * @endcode
37 */
38class MAYAFLUX_API NetworkAwaiter {
39public:
41 : m_source(source)
42 {
43 }
44
46
49 NetworkAwaiter(NetworkAwaiter&&) noexcept = default;
50 NetworkAwaiter& operator=(NetworkAwaiter&&) noexcept = delete;
51
52 /**
53 * @brief Check if a message is already queued
54 * @return true if a message is available (no suspend needed)
55 */
56 bool await_ready();
57
58 /**
59 * @brief Suspend the coroutine and register with the source
60 * @param handle Type-erased coroutine handle
61 */
62 void await_suspend(std::coroutine_handle<> handle);
63
64 /**
65 * @brief Return the received message on resume
66 * @return The NetworkMessage that caused resumption
67 */
68 Core::NetworkMessage await_resume();
69
70 /**
71 * @brief Called by NetworkSource::signal() with the incoming message
72 *
73 * Stores the message directly, unregisters from the waiter list,
74 * and resumes the suspended coroutine. Each awaiter gets its own
75 * copy — no queue pop, no contention with other waiters.
76 */
77 void deliver(const Core::NetworkMessage& message);
78
79private:
80 Vruta::NetworkSource& m_source;
81 Core::NetworkMessage m_result;
82 std::coroutine_handle<> m_handle;
83 bool m_is_suspended {};
84
85 /**
86 * @brief Intrusive list link for lock-free waiter broadcast.
87 */
88 std::atomic<NetworkAwaiter*> m_next { nullptr };
89
91};
92
93} // namespace MayaFlux::Kriya
NetworkAwaiter(const NetworkAwaiter &)=delete
NetworkAwaiter & operator=(const NetworkAwaiter &)=delete
NetworkAwaiter(NetworkAwaiter &&) noexcept=default
NetworkAwaiter(Vruta::NetworkSource &source)
Awaiter for suspending a coroutine until a network message arrives.
Awaitable broadcast message stream for a network endpoint.