增加 WebRtcTransportManager (#1156)

增加 WebRtcTransportManager
This commit is contained in:
johzzy 2021-10-13 14:15:29 +08:00 committed by GitHub
parent acbe99cb43
commit 2ec3b0ccea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 21 deletions

View File

@ -45,7 +45,7 @@ EventPoller::Ptr WebRtcSession::getPoller(const Buffer::Ptr &buffer) {
if (user_name.empty()) { if (user_name.empty()) {
return nullptr; return nullptr;
} }
auto ret = WebRtcTransportImp::getRtcTransport(user_name, false); auto ret = WebRtcTransportImp::get(user_name);
return ret ? ret->getPoller() : nullptr; return ret ? ret->getPoller() : nullptr;
} }
@ -61,7 +61,7 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) {
if (_find_transport) { if (_find_transport) {
//只允许寻找一次transport //只允许寻找一次transport
_find_transport = false; _find_transport = false;
_transport = WebRtcTransportImp::getRtcTransport(getUserName(buffer), true); _transport = WebRtcTransportImp::move(getUserName(buffer));
CHECK(_transport && _transport->getPoller()->isCurrentThread()); CHECK(_transport && _transport->getPoller()->isCurrentThread());
_transport->setSession(shared_from_this()); _transport->setSession(shared_from_this());
} }

View File

@ -1010,13 +1010,40 @@ void WebRtcTransportImp::setSession(Session::Ptr session) {
_session = std::move(session); _session = std::move(session);
} }
static mutex s_rtc_mtx; class WebRtcTransportManager {
static unordered_map<string, weak_ptr<WebRtcTransportImp> > s_rtc_map; mutable mutex _mtx;
unordered_map<string, weak_ptr<WebRtcTransportImp> > _map;
WebRtcTransportManager() = default;
public:
static WebRtcTransportManager& instance() {
static WebRtcTransportManager s_instance;
return s_instance;
}
void addItem(string key, const WebRtcTransportImp::Ptr &ptr) {
lock_guard<mutex> lck(_mtx);
_map[key] = ptr;
}
WebRtcTransportImp::Ptr getItem(const string &key) {
if (key.empty()) {
return nullptr;
}
lock_guard<mutex> lck(_mtx);
auto it = _map.find(key);
if (it == _map.end()) {
return nullptr;
}
return it->second.lock();
}
void removeItem(string key) {
lock_guard<mutex> lck(_mtx);
_map.erase(key);
}
};
void WebRtcTransportImp::registerSelf() { void WebRtcTransportImp::registerSelf() {
_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this()); _self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
lock_guard<mutex> lck(s_rtc_mtx); WebRtcTransportManager::instance().addItem(getKey(), _self);
s_rtc_map[getKey()] = static_pointer_cast<WebRtcTransportImp>(_self);
} }
void WebRtcTransportImp::unrefSelf() { void WebRtcTransportImp::unrefSelf() {
@ -1025,21 +1052,16 @@ void WebRtcTransportImp::unrefSelf() {
void WebRtcTransportImp::unregisterSelf() { void WebRtcTransportImp::unregisterSelf() {
unrefSelf(); unrefSelf();
lock_guard<mutex> lck(s_rtc_mtx); WebRtcTransportManager::instance().removeItem(getKey());
s_rtc_map.erase(getKey());
} }
WebRtcTransportImp::Ptr WebRtcTransportImp::getRtcTransport(const string &key, bool unref_self) { WebRtcTransportImp::Ptr WebRtcTransportImp::get(const string &key) {
if (key.empty()) { return WebRtcTransportManager::instance().getItem(key);
return nullptr; }
}
lock_guard<mutex> lck(s_rtc_mtx); WebRtcTransportImp::Ptr WebRtcTransportImp::move(const string &key) {
auto it = s_rtc_map.find(key); auto ret = WebRtcTransportManager::instance().getItem(key);
if (it == s_rtc_map.end()) { if (ret) {
return nullptr;
}
auto ret = it->second.lock();
if (unref_self) {
//此对象不再强引用自己因为自己将被WebRtcSession对象持有 //此对象不再强引用自己因为自己将被WebRtcSession对象持有
ret->unrefSelf(); ret->unrefSelf();
} }

View File

@ -170,7 +170,8 @@ public:
* @return * @return
*/ */
static Ptr create(const EventPoller::Ptr &poller); 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); void setSession(Session::Ptr session);