Skip to content

Commit

Permalink
Bring back some of my Channel::Send complexity :(.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurik committed Mar 6, 2024
1 parent d10448f commit 3e3f704
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions p2p/source/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "channel.hpp"
#include "peer.hpp"
#include "pirate.hpp"
#include "tube.hpp"

namespace orc {
Expand Down Expand Up @@ -152,19 +153,31 @@ struct P {
}
};

struct SctpDataChannel$network_thread_ { typedef rtc::Thread *const (webrtc::SctpDataChannel::*type); };
template struct Pirate<SctpDataChannel$network_thread_, &webrtc::SctpDataChannel::network_thread_>;

task<void> Channel::Send(const Buffer &data) {
Trace("WebRTC", true, false, data);

const auto size(data.size());
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<webrtc::SctpDataChannel *>(reinterpret_cast<void **>(channel.get())[3]));

Transfer<webrtc::RTCError> 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<SctpDataChannel$network_thread_>::pointer));

orc_assert((co_await *writ).ok());
}

task<std::string> Description(const S<Base> &base, std::vector<std::string> ice) {
Expand Down

0 comments on commit 3e3f704

Please sign in to comment.