wsClientMgr.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  81. {
  82. if ( !__wsClientList[i]->IsConnected() )
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. bool wsClientMgr::IsLogined()
  90. {
  91. std::lock_guard<std::recursive_mutex> lock( __lock );
  92. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  93. {
  94. if ( !__wsClientList[i]->IsLogined() )
  95. {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. bool wsClientMgr::IsSktOpened()
  102. {
  103. std::lock_guard<std::recursive_mutex> lock( __lock );
  104. for ( size_t i = 0; i < __wsClientList.size() ; i++ )
  105. {
  106. if ( !__wsClientList[i]->IsSktOpened() )
  107. {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. }