Skip to content
Merged
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
35 changes: 23 additions & 12 deletions include/webcpp/trystero/detail/app_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,15 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
* clears it (dist/shared-peer.mjs:142-149): the rooms hear its end before the new
* connection is adopted, and a room may adopt one to the peer from inside that call, which is
* then the one shared, or ends in its turn if it is stale by then. One whose end is already
* under way, further up the stack, is let go: it ends no second time. */
* under way, further up the stack, is let go: it ends no second time.
*
* `negotiated` is the token of the room whose negotiation opened the connection: an adopted
* connection records it as the remote's advertisement (connection::start), the existing one
* does not, since another remote may have answered the new negotiation in the peer's name. */
std::shared_ptr<connection> adopt(std::string_view peer_view,
std::shared_ptr<backend::peer_connection> pc,
std::shared_ptr<backend::data_channel> channel) {
std::shared_ptr<backend::data_channel> channel,
std::string_view negotiated) {
// `peer_view` may be a view into the stale connection, which its end frees.
const std::pmr::string peer(peer_view, memory_);
for (auto it = connections_.find(peer); it != connections_.end() && !it->second->live();
Expand All @@ -319,14 +324,16 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
connections_.erase(again);
}
}
return share_or_adopt(peer, std::move(pc), std::move(channel));
return share_or_adopt(peer, std::move(pc), std::move(channel), negotiated);
}

private:
/** A second connection to a peer, held unused until the peer shows which of the two it keeps:
* the frames its channel received meanwhile, in their order, and its deadline. */
* the token of the room whose negotiation opened it, the frames its channel received
* meanwhile, in their order, and its deadline. */
struct second_connection {
std::pmr::string peer;
std::pmr::string negotiated;
std::shared_ptr<backend::peer_connection> pc;
std::shared_ptr<backend::data_channel> channel;
std::pmr::deque<backend::bytes> frames;
Expand All @@ -346,10 +353,11 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
// NOLINTBEGIN(performance-unnecessary-value-param): sinks, moved on
std::shared_ptr<connection> share_or_adopt(std::string_view peer,
std::shared_ptr<backend::peer_connection> pc,
std::shared_ptr<backend::data_channel> channel) {
std::shared_ptr<backend::data_channel> channel,
std::string_view negotiated) {
// NOLINTEND(performance-unnecessary-value-param)
if (auto existing = find(peer)) {
hold_or_close(peer, *existing, std::move(pc), std::move(channel));
hold_or_close(peer, *existing, std::move(pc), std::move(channel), negotiated);
return existing;
}
const auto weak = weak_from_this();
Expand Down Expand Up @@ -402,7 +410,7 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
for (const auto& [token, e] : rooms_) {
tokens.emplace_back(token);
}
c->start(tokens);
c->start(tokens, negotiated);
return c;
}

Expand All @@ -412,10 +420,11 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
// NOLINTBEGIN(performance-unnecessary-value-param): sinks, moved on
void hold_or_close(std::string_view peer, const connection& first,
std::shared_ptr<backend::peer_connection> pc,
std::shared_ptr<backend::data_channel> channel) {
std::shared_ptr<backend::data_channel> channel,
std::string_view negotiated) {
// NOLINTEND(performance-unnecessary-value-param)
if (!first.used() && !seconds_.contains(peer)) {
hold_second(peer, std::move(pc), std::move(channel));
hold_second(peer, std::move(pc), std::move(channel), negotiated);
return;
}
channel->set_handlers({});
Expand Down Expand Up @@ -447,10 +456,11 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
* follows its reset at once: Chrome fails a send made in between and closes the channel with
* that failure, which Trystero JS reports as a peer error. */
void hold_second(std::string_view peer, std::shared_ptr<backend::peer_connection> pc,
std::shared_ptr<backend::data_channel> channel) {
std::shared_ptr<backend::data_channel> channel, std::string_view negotiated) {
auto second = std::allocate_shared<second_connection>(
allocator(), second_connection{
.peer = std::pmr::string(peer, memory_),
.negotiated = std::pmr::string(negotiated, memory_),
.pc = std::move(pc),
.channel = std::move(channel),
.frames = std::pmr::deque<backend::bytes>(memory_),
Expand Down Expand Up @@ -539,7 +549,8 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
}
log_("webrtc", "the second connection with ", peer,
" replaces the first: the peer keeps it");
const auto replacement = share_or_adopt(peer, second->pc, second->channel);
const auto replacement =
share_or_adopt(peer, second->pc, second->channel, second->negotiated);
if (first) {
first->end("the peer keeps the second connection");
}
Expand Down Expand Up @@ -598,7 +609,7 @@ class app_impl : public std::enable_shared_from_this<app_impl> {
second->deadline.reset();
frames = std::move(second->frames);
log_("webrtc", "the second connection with ", peer, " replaces the first");
replacement = share_or_adopt(peer, second->pc, second->channel);
replacement = share_or_adopt(peer, second->pc, second->channel, second->negotiated);
} else {
log_("webrtc", "the second connection with ", peer, " closed: the first ended");
close_second(*second);
Expand Down
16 changes: 11 additions & 5 deletions include/webcpp/trystero/detail/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ class binding; // NOLINT(cppcoreguidelines-virtual-class-destructor)
* connection, and its presence for a room comes once per connection, which a page can lose, since
* Chrome drops what a page sends on an answering channel as it opens (its log says "Send failed
* INVALID_STATE"). Without it the room would never bind the remote, whose frames of the room keep
* coming.
* coming. For the same loss, the room whose negotiation made the connection counts as advertised
* from the start (`start`): the remote answered or offered in that room's signaling, and a page
* whose presence and handshake frames were lost sends nothing more of that room once a kick ends
* its binding, so that the room's rebinding over the live connection would wait for an
* advertisement that never comes.
*
* Tip: it holds plain pointers to its bindings, which remove themselves before they go, so that it
* keeps nothing of a room's resource after the room has ended.
Expand Down Expand Up @@ -116,8 +120,9 @@ class connection : public std::enable_shared_from_this<connection> {
connection& operator=(const connection&) = delete;

/** Takes over the channel's and the connection's events, which replaces the handlers a room's
* negotiation set, and advertises `tokens`. */
void start(std::span<const std::string_view> tokens) {
* negotiation set, records `negotiated`, the token of the room whose negotiation made this
* connection, as the remote's advertisement, and advertises `tokens`. */
void start(std::span<const std::string_view> tokens, std::string_view negotiated) {
const auto weak = weak_from_this();
const std::pmr::polymorphic_allocator<> allocator(memory_);
channel_->set_handlers({
Expand Down Expand Up @@ -167,6 +172,7 @@ class connection : public std::enable_shared_from_this<connection> {
},
},
});
remote_tokens_.emplace(negotiated);
for (const auto token : tokens) {
send_presence(token, true);
}
Expand All @@ -185,8 +191,8 @@ class connection : public std::enable_shared_from_this<connection> {
/** How many frames are kept for rooms not bound yet. */
[[nodiscard]] std::size_t kept_count() const { return kept_.size(); }

/** Whether the remote advertises the room of `token`: by its presence, or by a room frame of
* the token that the app kept. */
/** Whether the remote advertises the room of `token`: by its presence, by a room frame of the
* token that the app kept, or by the negotiation of that room that made this connection. */
[[nodiscard]] bool advertised(std::string_view token) const {
return remote_tokens_.contains(token);
}
Expand Down
2 changes: 1 addition & 1 deletion include/webcpp/trystero/room.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ class room_impl final : public std::enable_shared_from_this<room_impl>, public r
return;
}
const std::pmr::string peer(peer_view, memory_);
const auto c = app_->adopt(peer, std::move(pc), std::move(channel));
const auto c = app_->adopt(peer, std::move(pc), std::move(channel), token_);
// Adoption logs synchronously: the sink may leave or destroy this room, or end the
// connection. A farewell still counts as leaving and must not gain a new binding.
if (left_) {
Expand Down
Loading
Loading