From 8c1caeff501105366ca02623892fa981d822a1cd Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Sat, 16 Oct 2021 10:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6pr:#1163?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/WebRtcSession.cpp | 4 ++-- webrtc/WebRtcTransport.cpp | 10 ++++++---- webrtc/WebRtcTransport.h | 15 ++++++++------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index 856684e3..52478763 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -44,7 +44,7 @@ EventPoller::Ptr QueryPollerByBuffer(const Buffer::Ptr &buffer) { if (user_name.empty()) { return nullptr; } - auto ret = WebRtcTransportManager::instance().getItem(user_name); + auto ret = WebRtcTransportManager::Instance().getItem(user_name); return ret ? ret->getPoller() : nullptr; } @@ -62,7 +62,7 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) { _find_transport = false; auto user_name = getUserName(buffer); _identifier = user_name + '-' + to_string(reinterpret_cast(this)); - auto transport = WebRtcTransportManager::instance().getItem(user_name); + auto transport = WebRtcTransportManager::Instance().getItem(user_name); CHECK(transport && transport->getPoller()->isCurrentThread()); transport->setSession(shared_from_this()); _transport = std::move(transport); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 7000ea48..130ff0f3 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -857,10 +857,9 @@ uint64_t WebRtcTransportImp::getDuration() const{ ///////////////////////////////////////////////////////////////////////////////////////////// - void WebRtcTransportImp::registerSelf() { _self = static_pointer_cast(shared_from_this()); - WebRtcTransportManager::instance().addItem(getIdentifier(), _self); + WebRtcTransportManager::Instance().addItem(getIdentifier(), _self); } void WebRtcTransportImp::unrefSelf() { @@ -869,17 +868,19 @@ void WebRtcTransportImp::unrefSelf() { void WebRtcTransportImp::unregisterSelf() { unrefSelf(); - WebRtcTransportManager::instance().removeItem(getIdentifier()); + WebRtcTransportManager::Instance().removeItem(getIdentifier()); } -WebRtcTransportManager &WebRtcTransportManager::instance() { +WebRtcTransportManager &WebRtcTransportManager::Instance() { static WebRtcTransportManager s_instance; return s_instance; } + void WebRtcTransportManager::addItem(string key, const WebRtcTransportImp::Ptr &ptr) { lock_guard lck(_mtx); _map[key] = ptr; } + WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) { if (key.empty()) { return nullptr; @@ -891,6 +892,7 @@ WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) { } return it->second.lock(); } + void WebRtcTransportManager::removeItem(string key) { lock_guard lck(_mtx); _map.erase(key); diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index eb2a72cc..02c4cb0d 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -226,13 +226,14 @@ private: }; class WebRtcTransportManager { +public: + static WebRtcTransportManager &Instance(); + void addItem(string key, const WebRtcTransportImp::Ptr &ptr); + void removeItem(string key); + WebRtcTransportImp::Ptr getItem(const string &key); + +private: + WebRtcTransportManager() = default; mutable mutex _mtx; unordered_map > _map; - WebRtcTransportManager() = default; - -public: - static WebRtcTransportManager& instance(); - void addItem(string key, const WebRtcTransportImp::Ptr &ptr); - WebRtcTransportImp::Ptr getItem(const string &key); - void removeItem(string key); }; \ No newline at end of file