Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ exceptions on and off.
* Added a runtime that holds libdatachannel's threads and caps connections.
* Added errors as values, in the category `webcpp.webrtc` and Asio's codes,
and `+[[nodiscard]]+` on every function that returns a `boost::system::result`.
* Added a peer connection's `max_message_size()`, the largest message a
channel sends.
* Added the library's page, with its examples, guarantees and differences.
// end::unreleased[]
6 changes: 5 additions & 1 deletion doc/signalling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ negotiation. The W3C's `createOffer()`, `createAnswer()` and

`local_description()` is the local description as it is now, with the
candidates gathered so far; `has_remote_description()` says whether a remote
one was applied.
one was applied. `max_message_size()` is the largest message a channel of the
connection sends, the smaller of `options::max_message_size` and the limit
the peer announced, as the W3C's `sctp.maxMessageSize`; it is nothing before
that limit is known: natively until a remote description is applied, in the
browser until the connection has its SCTP transport.

[#webrtc-signalling-candidates]
=== Candidates
Expand Down
19 changes: 19 additions & 0 deletions include/webcpp/webrtc/detail/browser/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ class connection_state final
return !pc_.isUndefined() && webcpp_webrtc_has_remote(pc_.as_handle());
}

/**
The largest message a channel sends: the smaller of `options::max_message_size` and the
SCTP transport's `maxMessageSize` (`Infinity` for no limit), the limit each channel's sends
check; nothing while the connection has no SCTP transport (`sctp` is null).
*/
[[nodiscard]] std::optional<std::size_t> max_message_size() const {
if (pc_.isUndefined()) {
return std::nullopt;
}
const double peer = webcpp_webrtc_peer_max_message_size(pc_.as_handle());
if (!(peer > 0)) {
return std::nullopt;
}
if (peer >= static_cast<double>(options_.max_message_size)) {
return options_.max_message_size;
}
return static_cast<std::size_t>(peer);
}

/** The connection's state. */
[[nodiscard]] peer_connection_state state() const noexcept { return state_.current(); }

Expand Down
18 changes: 18 additions & 0 deletions include/webcpp/webrtc/detail/native/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <boost/system/result.hpp>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
Expand Down Expand Up @@ -313,6 +314,23 @@ class connection_state
return pc_ >= 0 && rtcGetRemoteDescription(pc_, nullptr, 0) > 0;
}

/**
The largest message a channel sends: the smaller of `options::max_message_size` and the
peer's limit libdatachannel read from the remote description (`rtcGetRemoteMaxMessageSize`,
the limit `rtcMaxMessageSize` gives each channel's sends); nothing before a remote
description is applied.
*/
[[nodiscard]] std::optional<std::size_t> max_message_size() const {
if (!has_remote_description()) {
return std::nullopt;
}
const int peer = rtcGetRemoteMaxMessageSize(pc_);
if (peer <= 0) {
return std::nullopt;
}
return std::min(options_.max_message_size, static_cast<std::size_t>(peer));
}

/** The connection's state. */
[[nodiscard]] peer_connection_state state() const noexcept { return state_.current(); }

Expand Down
16 changes: 16 additions & 0 deletions include/webcpp/webrtc/peer_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,22 @@ class basic_peer_connection {
*/
[[nodiscard]] bool has_remote_description() const { return state_->has_remote_description(); }

/**
The largest message a channel of this connection sends, the most `data_channel::send` lets
through: the smaller of `options::max_message_size` and the peer's limit, which the peer
announces in its description (`a=max-message-size`, RFC 8841; 65,536 when it announces none
and no limit when it announces 0), as the W3C's `sctp.maxMessageSize` reports it.

It is nothing before the peer's limit is known: natively until a remote description is
applied (libdatachannel's `rtcGetRemoteMaxMessageSize`), in the browser while the connection
has no SCTP transport (`sctp` is null until an offer and its answer negotiated data channels).

@return The size in bytes, or nothing before the peer's limit is known.
*/
[[nodiscard]] std::optional<std::size_t> max_message_size() const {
return state_->max_message_size();
}

/**
The connection's state, the W3C's `connectionState`.

Expand Down
34 changes: 32 additions & 2 deletions test/browser_backend_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// closes the channels. The channel: its attributes, as created and as announced, its refusals to
// send leave it open, the buffered-amount wait, messages kept before the first receive, a message
// past max_message_size and the receive budget close it, its owner's close, the peer's close, and
// a channel accepted after its messages arrived, past the backlog closed at once.
// a channel accepted after its messages arrived, past the backlog closed at once. The connection's
// max_message_size() follows its SCTP transport's maxMessageSize.

#include <webcpp/webrtc/data_channel.hpp>
#include <webcpp/webrtc/peer_connection.hpp>
Expand Down Expand Up @@ -382,7 +383,7 @@ void a_close_while_a_notification_is_in_flight() {
std::optional<error_code> candidate;
pc.async_wait_ice_candidate(
[&](error_code ec, const connection::ice_candidate&) { candidate = ec; });
const val fake = last_connection();
val fake = last_connection();
set_state(fake, "connectionState", "connected", "onconnectionstatechange");
val end = val::object();
end.set("candidate", val::null());
Expand Down Expand Up @@ -505,6 +506,33 @@ void a_channels_attributes() {
BOOST_TEST(!accepted->negotiated());
}

// max_message_size() is the smaller of options::max_message_size and the SCTP transport's
// maxMessageSize, the limit a send checks; nothing while the connection has no SCTP transport or
// one whose size cannot be read, and options::max_message_size for Infinity.
void the_max_message_size_follows_the_sctp_transport() {
asio::io_context io;
webrtc::runtime rt;
auto made = connection::make(rt, io.get_executor(), no_servers);
if (!BOOST_TEST(made.has_value())) {
return;
}
val fake = last_connection();
fake.set("sctp", val::null());
BOOST_TEST(!made->max_message_size());
fake.set("sctp", val::object());
BOOST_TEST(!made->max_message_size());
fake["sctp"].set("maxMessageSize", val(0));
BOOST_TEST(!made->max_message_size());
fake["sctp"].set("maxMessageSize", val(16384));
BOOST_TEST(made->max_message_size() == std::optional<std::size_t>(16384));
fake["sctp"].set("maxMessageSize", val(65536));
BOOST_TEST(made->max_message_size() == std::optional<std::size_t>(65536));
fake["sctp"].set("maxMessageSize", val(1073741823));
BOOST_TEST(made->max_message_size() == std::optional<std::size_t>(262144));
fake["sctp"].set("maxMessageSize", val::global("Infinity"));
BOOST_TEST(made->max_message_size() == std::optional<std::size_t>(262144));
}

// A send is refused, and the channel stays open, when it is not open, past max_message_size or
// the peer's limit, and past max_buffered_amount; the buffered-amount wait completes with the
// browser's event once the amount is at the threshold.
Expand Down Expand Up @@ -548,6 +576,7 @@ void sends_and_their_refusals() {
const std::string seventeen(17, 'x');
BOOST_TEST(dc.send_text(seventeen) == webrtc::error::message_too_large);
last_connection()["sctp"].set("maxMessageSize", val(6));
BOOST_TEST(made->max_message_size() == std::optional<std::size_t>(6));
BOOST_TEST(dc.send_text("seven!!") == webrtc::error::message_too_large);
last_connection()["sctp"].set("maxMessageSize", val(65536));
// 60 queued and 4 sent make the bound exactly; one byte more passes it.
Expand Down Expand Up @@ -935,6 +964,7 @@ int main() {
a_close_while_a_notification_is_in_flight();
cancelled_promises();
a_channels_attributes();
the_max_message_size_follows_the_sctp_transport();
sends_and_their_refusals();
messages_and_the_buffer();
the_budget_or_the_size(false);
Expand Down
36 changes: 35 additions & 1 deletion test/native_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

// Two native peers in one process over the loopback, through libdatachannel's C API: the
// exchange, the channel's messages both ways, the cases the semantics name, a channel's attributes,
// and the errors. The Jamfile builds it as the build asks and without exceptions.
// the errors, and the largest message a connection's channels send. The Jamfile builds it as the
// build asks and without exceptions.

#include <webcpp/webrtc.hpp>

Expand Down Expand Up @@ -381,6 +382,38 @@ void bounds() {
BOOST_TEST(later == std::optional<std::size_t>(16));
}

// max_message_size() is nothing before a remote description, then the smaller of each end's own
// options::max_message_size and the limit the peer announced, the limit its sends meet: b, which
// announces 256 KiB, sends at most a's 32 KiB, and a sends at most its own 32 KiB.
void the_max_message_size() {
webrtc::options small;
small.max_message_size = std::size_t{32} * 1024;
pair p(small, webrtc::options());
BOOST_TEST(!p.a->max_message_size());
BOOST_TEST(!p.b->max_message_size());
std::optional<webrtc::data_channel> ca;
std::optional<webrtc::data_channel> cb;
bool checked = false;
webcpp::test::open_channel(p, "size", [&](webrtc::data_channel a, webrtc::data_channel b) {
ca.emplace(std::move(a));
cb.emplace(std::move(b));
const std::optional<std::size_t> limit = small.max_message_size;
BOOST_TEST(p.a->max_message_size() == limit);
BOOST_TEST(p.b->max_message_size() == limit);
const std::vector<char> fits(small.max_message_size, 'x');
const std::vector<char> over(small.max_message_size + 1, 'x');
BOOST_TEST(!ca->send(asio::buffer(fits)));
BOOST_TEST(ca->send(asio::buffer(over)) == webrtc::error::message_too_large);
BOOST_TEST(!cb->send(asio::buffer(fits)));
BOOST_TEST(cb->send(asio::buffer(over)) == webrtc::error::message_too_large);
checked = true;
p.a->close();
p.b->close();
});
p.run();
BOOST_TEST(checked);
}

// close() fires no state change: the pending wait ends with operation_aborted, the state reads
// closed, the channels close with error::closed, the peer's channel sees its close, and a wait
// started afterwards completes with error::closed.
Expand Down Expand Up @@ -519,6 +552,7 @@ int main() {
a_negotiated_channel();
errors_as_values();
bounds();
the_max_message_size();
close_fires_no_state_change();
a_channel_closed_by_its_owner();
destruction_and_moves();
Expand Down
9 changes: 7 additions & 2 deletions test/pair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,16 @@ void negotiate(PeerConnection& a, PeerConnection& b, trickle<PeerConnection>& ab
struct pair {
explicit pair(const webrtc::options& opts = webrtc::options(),
const webrtc::runtime_options& rt_opts = webrtc::runtime_options())
: pair(opts, opts, rt_opts) {}

/** Two peers made with options of their own, `a_opts` for `a` and `b_opts` for `b`. */
pair(const webrtc::options& a_opts, const webrtc::options& b_opts,
const webrtc::runtime_options& rt_opts = webrtc::runtime_options())
: rt(rt_opts) {
auto first =
webrtc::peer_connection::make(rt, io.get_executor(), loopback_configuration(), opts);
webrtc::peer_connection::make(rt, io.get_executor(), loopback_configuration(), a_opts);
auto second =
webrtc::peer_connection::make(rt, io.get_executor(), loopback_configuration(), opts);
webrtc::peer_connection::make(rt, io.get_executor(), loopback_configuration(), b_opts);
require(BOOST_TEST(first.has_value()));
require(BOOST_TEST(second.has_value()));
a.emplace(std::move(*first));
Expand Down
Loading