wsClientMgr.cpp 4.5 KB

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