From 9b8db7c63a88eb3d82a9155a10fe9d9faee0fd14 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Wed, 15 Dec 2021 11:20:17 +0100 Subject: [PATCH] [Net] Fix WebRTC returning packets from peers too early. Due to the async nature of WebRTC implementations, the multiplayer peer could end up having queued packets from a given connection before it is able to emit the "peer_added" signal. This commit ensures that packets from peers which are not notified yet are skipped by `get_packet` and `get_available_packet_count`. --- modules/webrtc/webrtc_multiplayer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/modules/webrtc/webrtc_multiplayer.cpp b/modules/webrtc/webrtc_multiplayer.cpp index 64cd7c96c64..667fbfae464 100644 --- a/modules/webrtc/webrtc_multiplayer.cpp +++ b/modules/webrtc/webrtc_multiplayer.cpp @@ -154,6 +154,10 @@ void WebRTCMultiplayer::_find_next_peer() { } // After last. while (E) { + if (!E->get()->connected) { + E = E->next(); + continue; + } for (List>::Element *F = E->get()->channels.front(); F; F = F->next()) { if (F->get()->get_available_packet_count()) { next_packet_peer = E->key(); @@ -165,6 +169,10 @@ void WebRTCMultiplayer::_find_next_peer() { E = peer_map.front(); // Before last while (E) { + if (!E->get()->connected) { + E = E->next(); + continue; + } for (List>::Element *F = E->get()->channels.front(); F; F = F->next()) { if (F->get()->get_available_packet_count()) { next_packet_peer = E->key(); @@ -357,6 +365,9 @@ int WebRTCMultiplayer::get_available_packet_count() const { } int size = 0; for (Map>::Element *E = peer_map.front(); E; E = E->next()) { + if (!E->get()->connected) { + continue; + } for (List>::Element *F = E->get()->channels.front(); F; F = F->next()) { size += F->get()->get_available_packet_count(); }