From 8983e3b40625dec80a2aeffb9cec1809eb0d5623 Mon Sep 17 00:00:00 2001 From: R Date: Sun, 22 Nov 2026 15:56:06 -0300 Subject: [PATCH] webrtc: a peer connection reports the largest message its channels send, as max_message_size() basic_peer_connection::max_message_size() is the smaller of options::max_message_size and the peer's announced limit, the limit each channel's send checks: natively libdatachannel's rtcGetRemoteMaxMessageSize once a remote description is applied, in the browser the SCTP transport's maxMessageSize (Infinity for none) once the connection has one; nothing before. native_test checks it before negotiation and against real sends between peers of different limits; browser_backend_test against the fake transport's sctp, including a refused send at the same limit. --- doc/changelog.adoc | 2 ++ doc/signalling.adoc | 6 +++- .../webrtc/detail/browser/connection.hpp | 19 ++++++++++ .../webrtc/detail/native/connection.hpp | 18 ++++++++++ include/webcpp/webrtc/peer_connection.hpp | 16 +++++++++ test/browser_backend_test.cpp | 34 ++++++++++++++++-- test/native_test.cpp | 36 ++++++++++++++++++- test/pair.hpp | 9 +++-- 8 files changed, 134 insertions(+), 6 deletions(-) diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 83bb360..601064d 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -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[] diff --git a/doc/signalling.adoc b/doc/signalling.adoc index b038bfc..5fb780e 100644 --- a/doc/signalling.adoc +++ b/doc/signalling.adoc @@ -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 diff --git a/include/webcpp/webrtc/detail/browser/connection.hpp b/include/webcpp/webrtc/detail/browser/connection.hpp index ebfec5b..4715373 100644 --- a/include/webcpp/webrtc/detail/browser/connection.hpp +++ b/include/webcpp/webrtc/detail/browser/connection.hpp @@ -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 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(options_.max_message_size)) { + return options_.max_message_size; + } + return static_cast(peer); + } + /** The connection's state. */ [[nodiscard]] peer_connection_state state() const noexcept { return state_.current(); } diff --git a/include/webcpp/webrtc/detail/native/connection.hpp b/include/webcpp/webrtc/detail/native/connection.hpp index f1ebac2..ffe88f3 100644 --- a/include/webcpp/webrtc/detail/native/connection.hpp +++ b/include/webcpp/webrtc/detail/native/connection.hpp @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -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 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(peer)); + } + /** The connection's state. */ [[nodiscard]] peer_connection_state state() const noexcept { return state_.current(); } diff --git a/include/webcpp/webrtc/peer_connection.hpp b/include/webcpp/webrtc/peer_connection.hpp index 4c8bb70..a836467 100644 --- a/include/webcpp/webrtc/peer_connection.hpp +++ b/include/webcpp/webrtc/peer_connection.hpp @@ -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 max_message_size() const { + return state_->max_message_size(); + } + /** The connection's state, the W3C's `connectionState`. diff --git a/test/browser_backend_test.cpp b/test/browser_backend_test.cpp index 24b4743..8299209 100644 --- a/test/browser_backend_test.cpp +++ b/test/browser_backend_test.cpp @@ -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 #include @@ -382,7 +383,7 @@ void a_close_while_a_notification_is_in_flight() { std::optional 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()); @@ -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(16384)); + fake["sctp"].set("maxMessageSize", val(65536)); + BOOST_TEST(made->max_message_size() == std::optional(65536)); + fake["sctp"].set("maxMessageSize", val(1073741823)); + BOOST_TEST(made->max_message_size() == std::optional(262144)); + fake["sctp"].set("maxMessageSize", val::global("Infinity")); + BOOST_TEST(made->max_message_size() == std::optional(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. @@ -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(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. @@ -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); diff --git a/test/native_test.cpp b/test/native_test.cpp index a8041bf..be532d4 100644 --- a/test/native_test.cpp +++ b/test/native_test.cpp @@ -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 @@ -381,6 +382,38 @@ void bounds() { BOOST_TEST(later == std::optional(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 ca; + std::optional 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 limit = small.max_message_size; + BOOST_TEST(p.a->max_message_size() == limit); + BOOST_TEST(p.b->max_message_size() == limit); + const std::vector fits(small.max_message_size, 'x'); + const std::vector 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. @@ -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(); diff --git a/test/pair.hpp b/test/pair.hpp index 44a3a2b..7f9c257 100644 --- a/test/pair.hpp +++ b/test/pair.hpp @@ -158,11 +158,16 @@ void negotiate(PeerConnection& a, PeerConnection& b, trickle& 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));