合并pr:#1163

This commit is contained in:
ziyue 2021-10-16 10:25:23 +08:00
parent 25a1434e00
commit 8c1caeff50
3 changed files with 16 additions and 13 deletions

View File

@ -44,7 +44,7 @@ EventPoller::Ptr QueryPollerByBuffer(const Buffer::Ptr &buffer) {
if (user_name.empty()) { if (user_name.empty()) {
return nullptr; return nullptr;
} }
auto ret = WebRtcTransportManager::instance().getItem(user_name); auto ret = WebRtcTransportManager::Instance().getItem(user_name);
return ret ? ret->getPoller() : nullptr; return ret ? ret->getPoller() : nullptr;
} }
@ -62,7 +62,7 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) {
_find_transport = false; _find_transport = false;
auto user_name = getUserName(buffer); auto user_name = getUserName(buffer);
_identifier = user_name + '-' + to_string(reinterpret_cast<uint64_t>(this)); _identifier = user_name + '-' + to_string(reinterpret_cast<uint64_t>(this));
auto transport = WebRtcTransportManager::instance().getItem(user_name); auto transport = WebRtcTransportManager::Instance().getItem(user_name);
CHECK(transport && transport->getPoller()->isCurrentThread()); CHECK(transport && transport->getPoller()->isCurrentThread());
transport->setSession(shared_from_this()); transport->setSession(shared_from_this());
_transport = std::move(transport); _transport = std::move(transport);

View File

@ -857,10 +857,9 @@ uint64_t WebRtcTransportImp::getDuration() const{
///////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransportImp::registerSelf() { void WebRtcTransportImp::registerSelf() {
_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this()); _self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
WebRtcTransportManager::instance().addItem(getIdentifier(), _self); WebRtcTransportManager::Instance().addItem(getIdentifier(), _self);
} }
void WebRtcTransportImp::unrefSelf() { void WebRtcTransportImp::unrefSelf() {
@ -869,17 +868,19 @@ void WebRtcTransportImp::unrefSelf() {
void WebRtcTransportImp::unregisterSelf() { void WebRtcTransportImp::unregisterSelf() {
unrefSelf(); unrefSelf();
WebRtcTransportManager::instance().removeItem(getIdentifier()); WebRtcTransportManager::Instance().removeItem(getIdentifier());
} }
WebRtcTransportManager &WebRtcTransportManager::instance() { WebRtcTransportManager &WebRtcTransportManager::Instance() {
static WebRtcTransportManager s_instance; static WebRtcTransportManager s_instance;
return s_instance; return s_instance;
} }
void WebRtcTransportManager::addItem(string key, const WebRtcTransportImp::Ptr &ptr) { void WebRtcTransportManager::addItem(string key, const WebRtcTransportImp::Ptr &ptr) {
lock_guard<mutex> lck(_mtx); lock_guard<mutex> lck(_mtx);
_map[key] = ptr; _map[key] = ptr;
} }
WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) { WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) {
if (key.empty()) { if (key.empty()) {
return nullptr; return nullptr;
@ -891,6 +892,7 @@ WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) {
} }
return it->second.lock(); return it->second.lock();
} }
void WebRtcTransportManager::removeItem(string key) { void WebRtcTransportManager::removeItem(string key) {
lock_guard<mutex> lck(_mtx); lock_guard<mutex> lck(_mtx);
_map.erase(key); _map.erase(key);

View File

@ -226,13 +226,14 @@ private:
}; };
class WebRtcTransportManager { 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; mutable mutex _mtx;
unordered_map<string, weak_ptr<WebRtcTransportImp> > _map; unordered_map<string, weak_ptr<WebRtcTransportImp> > _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);
}; };