From 3c4285a0561ff8c9584730b2d53f08363a505bfe Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 15 Sep 2021 11:50:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E9=AB=98webrtc=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=81=A5=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/WebRtcSession.cpp | 30 ++++++++++++++---------------- webrtc/WebRtcSession.h | 4 ++++ webrtc/WebRtcTransport.cpp | 5 ++++- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index 1e1bcf76..4756dc8f 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -50,26 +50,24 @@ EventPoller::Ptr WebRtcSession::getPoller(const Buffer::Ptr &buffer) { } void WebRtcSession::onRecv(const Buffer::Ptr &buffer) { - auto buf = buffer->data(); - auto len = buffer->size(); + try { + onRecv_l(buffer); + } catch (std::exception &ex) { + shutdown(SockException(Err_shutdown, ex.what())); + } +} - if (!_transport) { - auto user_name = getUserName(buffer); - if (user_name.empty()) { - //逻辑分支不太可能走到这里 - WarnL << user_name; - return; - } - _transport = WebRtcTransportImp::getRtcTransport(user_name, true); - if (!_transport) { - //逻辑分支不太可能走到这里 - WarnL << user_name; - return; - } +void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) { + if (_find_transport) { + //只允许寻找一次transport + _find_transport = false; + _transport = WebRtcTransportImp::getRtcTransport(getUserName(buffer), true); + CHECK(_transport && _transport->getPoller()->isCurrentThread()); _transport->setSession(shared_from_this()); } _ticker.resetTime(); - _transport->inputSockData(buf, len, &_peer_addr); + CHECK(_transport); + _transport->inputSockData(buffer->data(), buffer->size(), &_peer_addr); } void WebRtcSession::onError(const SockException &err) { diff --git a/webrtc/WebRtcSession.h b/webrtc/WebRtcSession.h index 9fbe342c..965f4e6c 100644 --- a/webrtc/WebRtcSession.h +++ b/webrtc/WebRtcSession.h @@ -30,6 +30,10 @@ public: void onManager() override; private: + void onRecv_l(const Buffer::Ptr &); + +private: + bool _find_transport = true; Ticker _ticker; struct sockaddr _peer_addr; std::shared_ptr _transport; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 25575bda..8b92d617 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -1018,7 +1018,10 @@ void WebRtcTransportImp::unregisterSelf() { s_rtc_map.erase(getKey()); } -WebRtcTransportImp::Ptr WebRtcTransportImp::getRtcTransport(const string &key, bool unref_self){ +WebRtcTransportImp::Ptr WebRtcTransportImp::getRtcTransport(const string &key, bool unref_self) { + if (key.empty()) { + return nullptr; + } lock_guard lck(s_rtc_mtx); auto it = s_rtc_map.find(key); if (it == s_rtc_map.end()) {