123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #include "wsClientMgr.h"
- #include "log.h"
- #include<thread>
- namespace sys
- {
- wsClientMgr::wsClientMgr()
- {
- }
- wsClientMgr::~wsClientMgr()
- {
- }
- void wsClientMgr::Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- std::vector<std::string>::const_iterator vit_uri;
- int ID = 1;
- int Index = 0;
- for ( vit_uri = uri_list.begin(); vit_uri != uri_list.end(); vit_uri++ )
- {
- std::shared_ptr<wsClient> pClient = std::make_shared<wsClient>();
- pClient->init( ID, *vit_uri, MsgFuncList );
- __wsClientList.push_back( pClient );
- __uriIndexList.insert( std::make_pair( *vit_uri, Index ) );
- ID++;
- Index++;
- }
- }
- int wsClientMgr::connect( int time_out )
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- if ( __wsClientList[i]->connect( time_out ) != 0 )
- {
- char szError[1024] = { 0 };
- sprintf( szError, "[%d] websocket client failed to connect: %s.", __wsClientList[i]->GetID(), __wsClientList[i]->get_uri().c_str() );
- __LastError = szError;
- std_error(szError);
- return -1;
- }
- }
- return 0;
- }
- void wsClientMgr::login()
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- __wsClientList[i]->login();
- }
- }
- void wsClientMgr::send( const std::string & Cmd, const std::string & Data )
- {
- log_info("send2web:cmd=%s,data=%s",Cmd.c_str(),Data.c_str());
- std::lock_guard<std::recursive_mutex> lock( __lock );
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- __wsClientList[i]->send( Cmd, Data );
- }
- }
- std::shared_ptr<wsClient> wsClientMgr::GetClientByURI( const std::string & URI )
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- std::shared_ptr<wsClient> pClient;
- std::map<std::string, int>::iterator mit_uri;
- mit_uri = __uriIndexList.find( URI );
- if ( mit_uri != __uriIndexList.end() )
- {
- pClient = __wsClientList[mit_uri->second];
- }
- return pClient;
- }
- void wsClientMgr::close()
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- __wsClientList[i]->close();
- }
- }
- bool wsClientMgr::IsConnected()
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- if ( __wsClientList.empty() )
- {
- return false;
- }
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- if ( !__wsClientList[i]->IsConnected() )
- {
- return false;
- }
- }
- return true;
- }
- bool wsClientMgr::IsLogined()
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- if ( __wsClientList.empty() )
- {
- return false;
- }
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- if ( !__wsClientList[i]->IsLogined() )
- {
- return false;
- }
- }
- return true;
- }
- bool wsClientMgr::IsSktOpened()
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- if ( __wsClientList.empty() )
- {
- return false;
- }
- for ( size_t i = 0; i < __wsClientList.size() ; i++ )
- {
- if ( !__wsClientList[i]->IsSktOpened() )
- {
- return false;
- }
- }
- return true;
- }
- void wsClientMgr::SendSpecialAreaProcess( const _BASE_CARD_ & stCard )
- {
- std::string strjson = __jsBuilder.BuildSpecialAreaProcess( stCard );
- send( JSON_CMD_VALUE_PUSH, strjson );
- }
- void wsClientMgr::Reconnect(std::shared_ptr<wsClient> & ws)
- {
- if (nullptr == ws)
- return;
- ws=ws->clone();
- log_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());
- if (ws->connect() == 0)
- {
- ws->login();
- log_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
- // std_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
- }
- }
- void wsClientMgr::Disconnet(std::shared_ptr<wsClient> & ws)
- {
- if (nullptr != ws)
- {
- ws->close();
- }
- }
- // @brief 检测是否有连接断开,并重连
- void wsClientMgr::CheckClientConnect(unsigned int cur_time)
- {
- std::lock_guard<std::recursive_mutex> lock( __lock );
- for( std::shared_ptr<wsClient>& w : __wsClientList)
- {
- unsigned int t = w->GetPingTime();
- // 间隔小于15秒 认为是正常
- // log_info("now=%ud,ping=%ud,now-ping=%d",cur_time,t,cur_time-t);
- if (w->IsConnected() && (t == 0 || cur_time - t < 15))
- {
- continue;
- }
- //重连
- Disconnet(w);
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- Reconnect(w);
- std::this_thread::sleep_for(std::chrono::seconds(3));
- }
- }
- }
|