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);