1
0

wsClientMgr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "wsClientMgr.h"
  2. #include "log.h"
  3. #include<thread>
  4. namespace sys
  5. {
  6. wsClientMgr::wsClientMgr()
  7. {
  8. }
  9. wsClientMgr::~wsClientMgr()
  10. {
  11. }
  12. void wsClientMgr::Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
  13. {
  14. std::lock_guard<std::recursive_mutex> lock( __lock );
  15. std::vector<std::string>::const_iterator vit_uri;
  16. int ID = 1;
  17. int Index = 0;
  18. for ( vit_uri = uri_list.begin(); vit_uri != uri_list.end(); vit_uri++ )
  19. {
  20. std::shared_ptr<wsClient> pClient = std::make_shared<wsClient>();
  21. pClient->init( ID, *vit_uri, MsgFuncList );
  22. __wsClientList.push_back( pClient );
  23. __uriIndexList.insert( std::make_pair( *vit_uri, Index ) );
  24. ID++;
  25. Index++;
  26. }
  27. }
  28. int wsClientMgr::connect( int time_out )
  29. {
  30. std::lock_guard<std::recursive_mutex> lock( __lock );
  31. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  32. {
  33. if ( __wsClientList[i]->connect( time_out ) != 0 )
  34. {
  35. char szError[1024] = { 0 };
  36. sprintf( szError, "[%d] websocket client failed to connect: %s.", __wsClientList[i]->GetID(), __wsClientList[i]->get_uri().c_str() );
  37. __LastError = szError;
  38. std_error(szError);
  39. return -1;
  40. }
  41. }
  42. return 0;
  43. }
  44. void wsClientMgr::login()
  45. {
  46. std::lock_guard<std::recursive_mutex> lock( __lock );
  47. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  48. {
  49. __wsClientList[i]->login();
  50. }
  51. }
  52. void wsClientMgr::send( const std::string & Cmd, const std::string & Data )
  53. {
  54. log_info("send2web:cmd=%s,data=%s",Cmd.c_str(),Data.c_str());
  55. std::lock_guard<std::recursive_mutex> lock( __lock );
  56. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  57. {
  58. __wsClientList[i]->send( Cmd, Data );
  59. }
  60. }
  61. std::shared_ptr<wsClient> wsClientMgr::GetClientByURI( const std::string & URI )
  62. {
  63. std::lock_guard<std::recursive_mutex> lock( __lock );
  64. std::shared_ptr<wsClient> pClient;
  65. std::map<std::string, int>::iterator mit_uri;
  66. mit_uri = __uriIndexList.find( URI );
  67. if ( mit_uri != __uriIndexList.end() )
  68. {
  69. pClient = __wsClientList[mit_uri->second];
  70. }
  71. return pClient;
  72. }
  73. void wsClientMgr::close()
  74. {
  75. std::lock_guard<std::recursive_mutex> lock( __lock );
  76. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  77. {
  78. __wsClientList[i]->close();
  79. }
  80. }
  81. bool wsClientMgr::IsConnected()
  82. {
  83. std::lock_guard<std::recursive_mutex> lock( __lock );
  84. if ( __wsClientList.empty() )
  85. {
  86. return false;
  87. }
  88. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  89. {
  90. if ( !__wsClientList[i]->IsConnected() )
  91. {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. bool wsClientMgr::IsLogined()
  98. {
  99. std::lock_guard<std::recursive_mutex> lock( __lock );
  100. if ( __wsClientList.empty() )
  101. {
  102. return false;
  103. }
  104. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  105. {
  106. if ( !__wsClientList[i]->IsLogined() )
  107. {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. bool wsClientMgr::IsSktOpened()
  114. {
  115. std::lock_guard<std::recursive_mutex> lock( __lock );
  116. if ( __wsClientList.empty() )
  117. {
  118. return false;
  119. }
  120. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  121. {
  122. if ( !__wsClientList[i]->IsSktOpened() )
  123. {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. void wsClientMgr::SendSpecialAreaProcess( const _BASE_CARD_ & stCard )
  130. {
  131. std::string strjson = __jsBuilder.BuildSpecialAreaProcess( stCard );
  132. send( JSON_CMD_VALUE_PUSH, strjson );
  133. }
  134. void wsClientMgr::Reconnect(std::shared_ptr<wsClient> & ws)
  135. {
  136. if (nullptr == ws)
  137. return;
  138. ws=ws->clone();
  139. log_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());
  140. if (ws->connect() == 0)
  141. {
  142. ws->login();
  143. log_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
  144. // std_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
  145. }
  146. }
  147. void wsClientMgr::Disconnet(std::shared_ptr<wsClient> & ws)
  148. {
  149. if (nullptr != ws)
  150. {
  151. ws->close();
  152. }
  153. }
  154. // @brief 检测是否有连接断开,并重连
  155. void wsClientMgr::CheckClientConnect(unsigned int cur_time)
  156. {
  157. std::lock_guard<std::recursive_mutex> lock( __lock );
  158. for( std::shared_ptr<wsClient>& w : __wsClientList)
  159. {
  160. unsigned int t = w->GetPingTime();
  161. // 间隔小于15秒 认为是正常
  162. // log_info("now=%ud,ping=%ud,now-ping=%d",cur_time,t,cur_time-t);
  163. if (w->IsConnected() && (t == 0 || cur_time - t < 15))
  164. {
  165. continue;
  166. }
  167. //重连
  168. Disconnet(w);
  169. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  170. Reconnect(w);
  171. std::this_thread::sleep_for(std::chrono::seconds(3));
  172. }
  173. }
  174. }