#include "user_manager.h" #include "user_handler.h" #include const long long MAX_DELAY_TIME = 3; void CUserManager::Start() { _thread = std::thread(std::bind(&CUserManager::Run, this)); } void CUserManager::Run() { _run = true; while (_run) { long long tick = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); { std::unique_lock l(_lock); for (auto it = SocketList.begin(); it != SocketList.end();) { CUserHandler* lhs = *it; if (tick - lhs->GetTimeTick() > MAX_DELAY_TIME) { lhs->OnTimeout(); it = SocketList.erase(it); } else { ++it; } } } } std::this_thread::sleep_for(std::chrono::milliseconds(5000)); } CUserManager& CUserManager::GetInstance() { static CUserManager m; return m; } void CUserManager::Add(CUserHandler* ptr) { std::lock_guard l(_lock); SocketList.push_back(ptr); } void CUserManager::Remove(CUserHandler* lhs) { std::lock_guard l(_lock); for (auto it = SocketList.begin(); it != SocketList.end(); ++it) { CUserHandler* User = *it; if (User == lhs) { SocketList.erase(it); break; } } } void CUserManager::Write(int32_t uid, CIOBuffer* pBuffer) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == uid) { a->Write(pBuffer); break; } } } void CUserManager::NotifyAnswer(int32_t peer, int32_t uid, int32_t index, const char* type, const char* sdp) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == peer) { a->NotifyAnswer(uid, index, type, sdp); break; } } } void CUserManager::NotifyCandidate(int32_t peer, int32_t uid, int32_t index, const char* type, const char* candidate, int32_t sdp_mline_index, const char* sdp_mid) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == peer) { a->NotifyCandidate(uid, index, type, candidate, sdp_mline_index, sdp_mid); break; } } } void CUserManager::BroadCast(CIOBuffer* pBuffer) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->type() == EgoType::Monitor) { a->Write(pBuffer); } } } void CUserManager::GetRobot(std::vector& ret) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->type() == EgoType::Car) { Benchboard m; m.name = a->name(); m.uid = a->uid(); m.type = a->type(); m.state= a->state(); ret.push_back(m); } } } void CUserManager::NotifyOffer(int32_t peer, int32_t uid, int32_t index, const char* type, const char* sdp) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == peer) { a->NotifyOffer(uid, index, type, sdp); break; } } } void CUserManager::ReplyPeerVideo(int32_t peer, int32_t uid, remote::VideoDesc ret, int32_t index) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == peer) { a->RepVideo(uid, index, ret); break; } } } remote::VideoDesc CUserManager::ConnectPeerVideo(int32_t peer, int32_t uid, int32_t index) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == peer) { return a->ReqVideo(uid, index); } } return remote::VideoDesc::NoFound; } void CUserManager::LeavePeerVideo(int32_t peer, int32_t uid,EgoType type) { std::lock_guard l(_lock); for (auto& a : SocketList) { if (a->uid() == peer) { a->LeaveVideo(uid,type); break; } } }