From 2ec3b0ccea42aed2d61e555b5752f34dc49d4102 Mon Sep 17 00:00:00 2001 From: johzzy Date: Wed, 13 Oct 2021 14:15:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20WebRtcTransportManager=20(?= =?UTF-8?q?#1156)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加 WebRtcTransportManager --- webrtc/WebRtcSession.cpp | 4 +-- webrtc/WebRtcTransport.cpp | 58 ++++++++++++++++++++++++++------------ webrtc/WebRtcTransport.h | 3 +- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index aceac77e..859aba65 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -45,7 +45,7 @@ EventPoller::Ptr WebRtcSession::getPoller(const Buffer::Ptr &buffer) { if (user_name.empty()) { return nullptr; } - auto ret = WebRtcTransportImp::getRtcTransport(user_name, false); + auto ret = WebRtcTransportImp::get(user_name); return ret ? ret->getPoller() : nullptr; } @@ -61,7 +61,7 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) { if (_find_transport) { //只允许寻找一次transport _find_transport = false; - _transport = WebRtcTransportImp::getRtcTransport(getUserName(buffer), true); + _transport = WebRtcTransportImp::move(getUserName(buffer)); CHECK(_transport && _transport->getPoller()->isCurrentThread()); _transport->setSession(shared_from_this()); } diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index f6b3a3df..f6187691 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -1010,13 +1010,40 @@ void WebRtcTransportImp::setSession(Session::Ptr session) { _session = std::move(session); } -static mutex s_rtc_mtx; -static unordered_map > s_rtc_map; +class WebRtcTransportManager { + mutable mutex _mtx; + unordered_map > _map; + WebRtcTransportManager() = default; + +public: + static WebRtcTransportManager& instance() { + static WebRtcTransportManager s_instance; + return s_instance; + } + void addItem(string key, const WebRtcTransportImp::Ptr &ptr) { + lock_guard lck(_mtx); + _map[key] = ptr; + } + WebRtcTransportImp::Ptr getItem(const string &key) { + if (key.empty()) { + return nullptr; + } + lock_guard lck(_mtx); + auto it = _map.find(key); + if (it == _map.end()) { + return nullptr; + } + return it->second.lock(); + } + void removeItem(string key) { + lock_guard lck(_mtx); + _map.erase(key); + } +}; void WebRtcTransportImp::registerSelf() { _self = static_pointer_cast(shared_from_this()); - lock_guard lck(s_rtc_mtx); - s_rtc_map[getKey()] = static_pointer_cast(_self); + WebRtcTransportManager::instance().addItem(getKey(), _self); } void WebRtcTransportImp::unrefSelf() { @@ -1025,23 +1052,18 @@ void WebRtcTransportImp::unrefSelf() { void WebRtcTransportImp::unregisterSelf() { unrefSelf(); - lock_guard lck(s_rtc_mtx); - s_rtc_map.erase(getKey()); + WebRtcTransportManager::instance().removeItem(getKey()); } -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()) { - return nullptr; - } - auto ret = it->second.lock(); - if (unref_self) { +WebRtcTransportImp::Ptr WebRtcTransportImp::get(const string &key) { + return WebRtcTransportManager::instance().getItem(key); +} + +WebRtcTransportImp::Ptr WebRtcTransportImp::move(const string &key) { + auto ret = WebRtcTransportManager::instance().getItem(key); + if (ret) { //此对象不再强引用自己,因为自己将被WebRtcSession对象持有 ret->unrefSelf(); } return ret; -} \ No newline at end of file +} diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 2ce8866b..29ce2550 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -170,7 +170,8 @@ public: * @return 对象 */ static Ptr create(const EventPoller::Ptr &poller); - static Ptr getRtcTransport(const string &key, bool unref_self); + static Ptr get(const string &key); // 借用 + static Ptr move(const string &key); // 所有权转移 void setSession(Session::Ptr session);