diff --git a/p2p/source/channel.cpp b/p2p/source/channel.cpp index b9eb8fa78..82926821e 100644 --- a/p2p/source/channel.cpp +++ b/p2p/source/channel.cpp @@ -27,6 +27,7 @@ #include "channel.hpp" #include "peer.hpp" +#include "pirate.hpp" #include "tube.hpp" namespace orc { @@ -152,6 +153,9 @@ struct P { } }; +struct SctpDataChannel$network_thread_ { typedef rtc::Thread *const (webrtc::SctpDataChannel::*type); }; +template struct Pirate; + task Channel::Send(const Buffer &data) { Trace("WebRTC", true, false, data); @@ -159,12 +163,21 @@ task Channel::Send(const Buffer &data) { rtc::CopyOnWriteBuffer buffer(size); data.copy(buffer.MutableData(), size); - orc_assert(channel_ != nullptr); - if (channel_->buffered_amount() == 0) - // XXX: consider blocking this fiber on this error result? - channel_->SendAsync({buffer, true}, [](webrtc::RTCError){}); + const auto channel(channel_); + orc_assert(channel != nullptr); + const auto sctp(static_cast(reinterpret_cast(channel.get())[3])); + + Transfer writ; - co_return; + co_await Post([&]() { + if (sctp->buffered_amount() != 0) + writ = webrtc::RTCError(); + else + sctp->SendAsync({buffer, true}, [&](webrtc::RTCError error) { + writ = std::move(error); }); + }, *(sctp->*Loot::pointer)); + + orc_assert((co_await *writ).ok()); } task Description(const S &base, std::vector ice) {