Commit 8c1caeff by ziyue

合并pr:#1163

parent 25a1434e
......@@ -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<uint64_t>(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);
......
......@@ -857,10 +857,9 @@ uint64_t WebRtcTransportImp::getDuration() const{
/////////////////////////////////////////////////////////////////////////////////////////////
void WebRtcTransportImp::registerSelf() {
_self = static_pointer_cast<WebRtcTransportImp>(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<mutex> 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<mutex> lck(_mtx);
_map.erase(key);
......
......@@ -226,13 +226,14 @@ private:
};
class WebRtcTransportManager {
mutable mutex _mtx;
unordered_map<string, weak_ptr<WebRtcTransportImp> > _map;
WebRtcTransportManager() = default;
public:
static WebRtcTransportManager& instance();
static WebRtcTransportManager &Instance();
void addItem(string key, const WebRtcTransportImp::Ptr &ptr);
WebRtcTransportImp::Ptr getItem(const string &key);
void removeItem(string key);
WebRtcTransportImp::Ptr getItem(const string &key);
private:
WebRtcTransportManager() = default;
mutable mutex _mtx;
unordered_map<string, weak_ptr<WebRtcTransportImp> > _map;
};
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论