123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #include "user_manager.h"
- #include "user_handler.h"
- #include <functional>
- 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::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
- {
- std::unique_lock<std::mutex> 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<std::mutex> l(_lock);
- SocketList.push_back(ptr);
- }
- void CUserManager::Remove(CUserHandler* lhs)
- {
- std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> l(_lock);
- for (auto& a : SocketList)
- {
- if (a->type() == EgoType::Monitor)
- {
- a->Write(pBuffer);
- }
- }
- }
- void CUserManager::GetRobot(std::vector<Benchboard>& ret)
- {
- std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> l(_lock);
- for (auto& a : SocketList)
- {
- if (a->uid() == peer)
- {
- a->LeaveVideo(uid,type);
- break;
- }
- }
- }
|