wsClientMgr.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "wsClientMgr.h"
  2. namespace YA
  3. {
  4. wsClientMgr::wsClientMgr()
  5. {
  6. }
  7. wsClientMgr::~wsClientMgr()
  8. {
  9. }
  10. void wsClientMgr::Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
  11. {
  12. std::lock_guard<std::recursive_mutex> lock( __lock );
  13. std::vector<std::string>::const_iterator vit_uri;
  14. int ID = 1;
  15. int Index = 0;
  16. for ( vit_uri = uri_list.begin(); vit_uri != uri_list.end(); vit_uri++ )
  17. {
  18. std::shared_ptr<wsClient> pClient = std::make_shared<wsClient>();
  19. pClient->init( ID, *vit_uri, MsgFuncList );
  20. __wsClientList.push_back( pClient );
  21. __uriIndexList.insert( std::make_pair( *vit_uri, Index ) );
  22. ID++;
  23. Index++;
  24. }
  25. }
  26. int wsClientMgr::connect( int time_out )
  27. {
  28. std::lock_guard<std::recursive_mutex> lock( __lock );
  29. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  30. {
  31. if ( __wsClientList[i]->connect( time_out ) != 0 )
  32. {
  33. char szError[1024] = { 0 };
  34. sprintf( szError, "[%d] websocket client failed to connect: %s.", __wsClientList[i]->GetID(), __wsClientList[i]->get_uri().c_str() );
  35. __LastError = szError;
  36. return -1;
  37. }
  38. }
  39. return 0;
  40. }
  41. void wsClientMgr::login()
  42. {
  43. std::lock_guard<std::recursive_mutex> lock( __lock );
  44. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  45. {
  46. __wsClientList[i]->login();
  47. }
  48. }
  49. void wsClientMgr::send( const std::string & Cmd, const std::string & Data )
  50. {
  51. std::lock_guard<std::recursive_mutex> lock( __lock );
  52. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  53. {
  54. __wsClientList[i]->send( Cmd, Data );
  55. }
  56. }
  57. std::shared_ptr<wsClient> wsClientMgr::GetClientByURI( const std::string & URI )
  58. {
  59. std::lock_guard<std::recursive_mutex> lock( __lock );
  60. std::shared_ptr<wsClient> pClient;
  61. std::map<std::string, int>::iterator mit_uri;
  62. mit_uri = __uriIndexList.find( URI );
  63. if ( mit_uri != __uriIndexList.end() )
  64. {
  65. pClient = __wsClientList[mit_uri->second];
  66. }
  67. return pClient;
  68. }
  69. void wsClientMgr::close()
  70. {
  71. std::lock_guard<std::recursive_mutex> lock( __lock );
  72. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  73. {
  74. __wsClientList[i]->close();
  75. }
  76. }
  77. bool wsClientMgr::IsConnected()
  78. {
  79. std::lock_guard<std::recursive_mutex> lock( __lock );
  80. if ( __wsClientList.empty() )
  81. {
  82. return false;
  83. }
  84. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  85. {
  86. if ( !__wsClientList[i]->IsConnected() )
  87. {
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. bool wsClientMgr::IsLogined()
  94. {
  95. std::lock_guard<std::recursive_mutex> lock( __lock );
  96. if ( __wsClientList.empty() )
  97. {
  98. return false;
  99. }
  100. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  101. {
  102. if ( !__wsClientList[i]->IsLogined() )
  103. {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. bool wsClientMgr::IsSktOpened()
  110. {
  111. std::lock_guard<std::recursive_mutex> lock( __lock );
  112. if ( __wsClientList.empty() )
  113. {
  114. return false;
  115. }
  116. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  117. {
  118. if ( !__wsClientList[i]->IsSktOpened() )
  119. {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. }