user_manager.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "user_manager.h"
  2. #include "user_handler.h"
  3. #include <functional>
  4. const long long MAX_DELAY_TIME = 3;
  5. void CUserManager::Start()
  6. {
  7. _thread = std::thread(std::bind(&CUserManager::Run, this));
  8. }
  9. void CUserManager::Run()
  10. {
  11. _run = true;
  12. while (_run)
  13. {
  14. long long tick = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  15. {
  16. std::unique_lock<std::mutex> l(_lock);
  17. for (auto it = SocketList.begin(); it != SocketList.end();)
  18. {
  19. CUserHandler* lhs = *it;
  20. if (tick - lhs->GetTimeTick() > MAX_DELAY_TIME)
  21. {
  22. lhs->OnTimeout();
  23. it = SocketList.erase(it);
  24. }
  25. else
  26. {
  27. ++it;
  28. }
  29. }
  30. }
  31. }
  32. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  33. }
  34. CUserManager& CUserManager::GetInstance()
  35. {
  36. static CUserManager m;
  37. return m;
  38. }
  39. void CUserManager::Add(CUserHandler* ptr)
  40. {
  41. std::lock_guard<std::mutex> l(_lock);
  42. SocketList.push_back(ptr);
  43. }
  44. void CUserManager::Remove(CUserHandler* lhs)
  45. {
  46. std::lock_guard<std::mutex> l(_lock);
  47. for (auto it = SocketList.begin(); it != SocketList.end(); ++it)
  48. {
  49. CUserHandler* User = *it;
  50. if (User == lhs)
  51. {
  52. SocketList.erase(it);
  53. break;
  54. }
  55. }
  56. }
  57. void CUserManager::Write(int32_t uid, CIOBuffer* pBuffer)
  58. {
  59. std::lock_guard<std::mutex> l(_lock);
  60. for (auto& a : SocketList)
  61. {
  62. if (a->uid() == uid)
  63. {
  64. a->Write(pBuffer);
  65. break;
  66. }
  67. }
  68. }
  69. void CUserManager::NotifyAnswer(int32_t peer, int32_t uid, int32_t index, const char* type, const char* sdp)
  70. {
  71. std::lock_guard<std::mutex> l(_lock);
  72. for (auto& a : SocketList)
  73. {
  74. if (a->uid() == peer)
  75. {
  76. a->NotifyAnswer(uid, index, type, sdp);
  77. break;
  78. }
  79. }
  80. }
  81. 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)
  82. {
  83. std::lock_guard<std::mutex> l(_lock);
  84. for (auto& a : SocketList)
  85. {
  86. if (a->uid() == peer)
  87. {
  88. a->NotifyCandidate(uid, index, type, candidate, sdp_mline_index, sdp_mid);
  89. break;
  90. }
  91. }
  92. }
  93. void CUserManager::BroadCast(CIOBuffer* pBuffer)
  94. {
  95. std::lock_guard<std::mutex> l(_lock);
  96. for (auto& a : SocketList)
  97. {
  98. if (a->type() == EgoType::Monitor)
  99. {
  100. a->Write(pBuffer);
  101. }
  102. }
  103. }
  104. void CUserManager::GetRobot(std::vector<Benchboard>& ret)
  105. {
  106. std::lock_guard<std::mutex> l(_lock);
  107. for (auto& a : SocketList)
  108. {
  109. if (a->type() == EgoType::Car)
  110. {
  111. Benchboard m;
  112. m.name = a->name();
  113. m.uid = a->uid();
  114. m.type = a->type();
  115. m.state= a->state();
  116. ret.push_back(m);
  117. }
  118. }
  119. }
  120. void CUserManager::NotifyOffer(int32_t peer, int32_t uid, int32_t index, const char* type, const char* sdp)
  121. {
  122. std::lock_guard<std::mutex> l(_lock);
  123. for (auto& a : SocketList)
  124. {
  125. if (a->uid() == peer)
  126. {
  127. a->NotifyOffer(uid, index, type, sdp);
  128. break;
  129. }
  130. }
  131. }
  132. void CUserManager::ReplyPeerVideo(int32_t peer, int32_t uid, remote::VideoDesc ret, int32_t index)
  133. {
  134. std::lock_guard<std::mutex> l(_lock);
  135. for (auto& a : SocketList)
  136. {
  137. if (a->uid() == peer)
  138. {
  139. a->RepVideo(uid, index, ret);
  140. break;
  141. }
  142. }
  143. }
  144. remote::VideoDesc CUserManager::ConnectPeerVideo(int32_t peer, int32_t uid, int32_t index)
  145. {
  146. std::lock_guard<std::mutex> l(_lock);
  147. for (auto& a : SocketList)
  148. {
  149. if (a->uid() == peer)
  150. {
  151. return a->ReqVideo(uid, index);
  152. }
  153. }
  154. return remote::VideoDesc::NoFound;
  155. }
  156. void CUserManager::LeavePeerVideo(int32_t peer, int32_t uid,EgoType type)
  157. {
  158. std::lock_guard<std::mutex> l(_lock);
  159. for (auto& a : SocketList)
  160. {
  161. if (a->uid() == peer)
  162. {
  163. a->LeaveVideo(uid,type);
  164. break;
  165. }
  166. }
  167. }