wsClientMgr.cpp 4.2 KB

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