From e86b25af9f0fa7150480926cb10f7c6a30b62171 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Fri, 3 Mar 2023 10:32:23 +0800 Subject: [PATCH] =?UTF-8?q?selectedTuple=E4=BD=BF=E7=94=A8=E5=BC=BA?= =?UTF-8?q?=E5=BC=95=E7=94=A8=EF=BC=8C=E6=8F=90=E9=AB=98=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/IceServer.cpp | 33 ++++++++++++++++++--------------- webrtc/IceServer.hpp | 11 +++++------ webrtc/WebRtcTransport.cpp | 4 ++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/webrtc/IceServer.cpp b/webrtc/IceServer.cpp index 64c79a97..7dfac0b7 100644 --- a/webrtc/IceServer.cpp +++ b/webrtc/IceServer.cpp @@ -26,6 +26,7 @@ namespace RTC { /* Static. */ /* Instance methods. */ + IceServer::IceServer(Listener* listener, const std::string& usernameFragment, const std::string& password) : listener(listener), usernameFragment(usernameFragment), password(password) { @@ -281,11 +282,11 @@ namespace RTC this->tuples.erase(it); // If this is not the selected tuple, stop here. - if (removedTuple != this->selectedTuple.lock().get()) + if (removedTuple != this->selectedTuple) return; // Otherwise this was the selected tuple. - // this->selectedTuple = nullptr; + this->selectedTuple = nullptr; // Mark the first tuple as selected tuple (if any). if (!this->tuples.empty()) @@ -306,7 +307,8 @@ namespace RTC { MS_TRACE(); - MS_ASSERT(!this->selectedTuple.expired(), "cannot force the selected tuple if there was not a selected tuple"); + MS_ASSERT( + this->selectedTuple, "cannot force the selected tuple if there was not a selected tuple"); auto* storedTuple = HasTuple(tuple); @@ -331,7 +333,7 @@ namespace RTC this->tuples.empty(), "state is 'new' but there are %zu tuples", this->tuples.size()); // There shouldn't be a selected tuple. - MS_ASSERT(!this->selectedTuple.expired(), "state is 'new' but there is selected tuple"); + MS_ASSERT(!this->selectedTuple, "state is 'new' but there is selected tuple"); if (!hasUseCandidate) { @@ -374,7 +376,7 @@ namespace RTC this->tuples.size()); // There shouldn't be a selected tuple. - MS_ASSERT(!this->selectedTuple.expired(), "state is 'disconnected' but there is selected tuple"); + MS_ASSERT(!this->selectedTuple, "state is 'disconnected' but there is selected tuple"); if (!hasUseCandidate) { @@ -414,7 +416,7 @@ namespace RTC MS_ASSERT(!this->tuples.empty(), "state is 'connected' but there are no tuples"); // There should be a selected tuple. - MS_ASSERT(!this->selectedTuple.expired(), "state is 'connected' but there is not selected tuple"); + MS_ASSERT(this->selectedTuple, "state is 'connected' but there is not selected tuple"); if (!hasUseCandidate) { @@ -449,7 +451,7 @@ namespace RTC MS_ASSERT(!this->tuples.empty(), "state is 'completed' but there are no tuples"); // There should be a selected tuple. - MS_ASSERT(!this->selectedTuple.expired(), "state is 'completed' but there is not selected tuple"); + MS_ASSERT(this->selectedTuple, "state is 'completed' but there is not selected tuple"); if (!hasUseCandidate) { @@ -491,17 +493,17 @@ namespace RTC // If there is no selected tuple yet then we know that the tuples list // is empty. - if (this->selectedTuple.expired()) + if (!this->selectedTuple) return nullptr; // Check the current selected tuple. - if (selectedTuple.lock().get() == tuple) - return this->selectedTuple.lock().get(); + if (selectedTuple == tuple) + return this->selectedTuple; // Otherwise check other stored tuples. - for (auto it : this->tuples) + for (const auto& it : this->tuples) { - auto storedTuple = it; + auto& storedTuple = it; if (storedTuple == tuple) return storedTuple; } @@ -514,12 +516,13 @@ namespace RTC MS_TRACE(); // If already the selected tuple do nothing. - if (storedTuple == this->selectedTuple.lock().get()) + if (storedTuple == this->selectedTuple) return; - this->selectedTuple = storedTuple->shared_from_this(); + this->selectedTuple = storedTuple; + this->lastSelectedTuple = storedTuple->shared_from_this(); // Notify the listener. - this->listener->OnIceServerSelectedTuple(this, storedTuple); + this->listener->OnIceServerSelectedTuple(this, this->selectedTuple); } } // namespace RTC diff --git a/webrtc/IceServer.hpp b/webrtc/IceServer.hpp index 57c3f65a..587fb1e9 100644 --- a/webrtc/IceServer.hpp +++ b/webrtc/IceServer.hpp @@ -28,8 +28,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -//using _TransportTuple = struct sockaddr; - namespace RTC { using TransportTuple = toolkit::Session; @@ -81,10 +79,10 @@ namespace RTC { return this->state; } - RTC::TransportTuple* GetSelectedTuple() const + RTC::TransportTuple* GetSelectedTuple(bool try_last_tuple = false) const { - return this->selectedTuple.lock().get(); - } + return try_last_tuple ? this->lastSelectedTuple.lock().get() : this->selectedTuple; + } void SetUsernameFragment(const std::string& usernameFragment) { this->oldUsernameFragment = this->usernameFragment; @@ -129,7 +127,8 @@ namespace RTC std::string oldPassword; IceState state{ IceState::NEW }; std::list tuples; - std::weak_ptr selectedTuple; + RTC::TransportTuple *selectedTuple; + std::weak_ptr lastSelectedTuple; //最大不超过mtu static constexpr size_t StunSerializeBufferSize{ 1600 }; uint8_t StunSerializeBuffer[StunSerializeBufferSize]; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 3a57e2f3..99f9f55d 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -229,7 +229,7 @@ void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTu } Session::Ptr WebRtcTransport::getSession() const { - auto tuple = _ice_server->GetSelectedTuple(); + auto tuple = _ice_server->GetSelectedTuple(true); return tuple ? tuple->shared_from_this() : nullptr; } @@ -307,7 +307,7 @@ static bool isDtls(char *buf) { } static string getPeerAddress(RTC::TransportTuple *tuple) { - return tuple->get_peer_ip();// SockUtil::inet_ntoa(tuple); + return tuple->get_peer_ip(); } void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tuple) {