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