123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- #include "wsClient.h"
- #include "constdef.h"
- #include <thread>
- #include <chrono>
- #include <boost/locale.hpp>
- namespace YA
- {
- const int _LOGIN_SLEEP_TIME_ = 500;//登录前的等待时间(单位:毫秒)
- const int _OPEN_SOCKET_WAIT_TIME_ = 100;//等待打开socket时间(单位:毫秒)
- wsClient::wsClient()
- {
- __ID = 0;
- _reset();
- }
- wsClient::~wsClient()
- {
- }
-
- void wsClient::_reset()
- {
- __Connected = false;
- __SktOpened = false;
- __Logined = false;
- }
- void wsClient::init( int ID, const std::string & uri, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
- {
- __ID = ID;
- __uri = uri;
- __MsgFuncList = MsgFuncList;
- __wsclient.set_reconnect_attempts( 0 );
- using std::placeholders::_1;
- using std::placeholders::_2;
- using std::placeholders::_3;
- using std::placeholders::_4;
- sio::socket::ptr sock = __wsclient.socket();
- __wsclient.set_open_listener( std::bind( &wsClient::_on_connected, this ) );
- __wsclient.set_close_listener( std::bind( &wsClient::_on_close, this, std::placeholders::_1 ) );
- __wsclient.set_reconnect_listener( std::bind( &wsClient::_on_reconnect, this, std::placeholders::_1, std::placeholders::_2 ) );
- __wsclient.set_fail_listener( std::bind( &wsClient::_on_fail, this ) );
- __wsclient.set_socket_open_listener( std::bind( &wsClient::_on_socket_opened, this ) );
- sock->on( JSON_CMD_VALUE_CALL, sio::socket::event_listener_aux( std::bind( &wsClient::_OnCallMessage, this, _1, _2, _3, _4 ) ) );
- sock->on( "code", sio::socket::event_listener_aux( std::bind( &wsClient::_OnLoginResponse, this, _1, _2, _3, _4 ) ) );
- }
- std::string wsClient::get_uri()
- {
- return __uri;
- }
- int wsClient::connect( int time_out )
- {
- if ( __uri.empty() )
- {
- __LastError = "Error, uri is empty.";
- return -1;
- }
- __wsclient.connect( __uri );
- std::cv_status status = std::cv_status::timeout;
- int nRet = -1;
- __lock.lock();
- if ( !IsConnected() )
- {
- status = __cond.wait_for( __lock, std::chrono::seconds( time_out ) );
- if ( std::cv_status::timeout == status )
- {
- __LastError = "Failed to connect server.";
- nRet = -1;
- }
- else
- {
- nRet = 0;
- }
- }
- else
- {
- nRet = 0;
- }
- __lock.unlock();
- return nRet;
- }
- void wsClient::login()
- {
- if ( !IsConnected() )
- {
- __LastError = "please connect server before login!";
- return;
- }
- //std::cv_status status = std::cv_status::timeout;
- __lock.lock();
- if ( !IsSktOpened() )
- {
- // status =
- __cond.wait_for( __lock, std::chrono::milliseconds( _OPEN_SOCKET_WAIT_TIME_ ) );
- }
- __lock.unlock();
- if ( !IsSktOpened() )
- {
- __LastError = "socket is not opened before login!";
- return;
- }
- std::this_thread::sleep_for( std::chrono::milliseconds( _LOGIN_SLEEP_TIME_ ) );
-
- YA::_JS_LOGIN_ Login;
- Login.user_name = JSON_VALUE_USERNAME;
- Login.user_password = JSON_VALUE_PASSWORD;
- std::string strLogin = __jsBuilder.BuildLogin( Login );
- strLogin += "\n";
- sio::socket::ptr skt_ptr;
- skt_ptr = __wsclient.socket();
- skt_ptr->emit( JSON_CMD_VALUE_USER, strLogin, [this]( sio::message::list const& msglist )
- {
- int nRet = 0;
- char szError[512] = { 0 };
- sio::message::ptr msg_ptr = msglist[0];
- nRet = ( int ) msg_ptr->get_map()["code"]->get_int();
- if ( 0 == nRet )
- {
- __Logined = true;
- }
- else
- {
- __Logined = false;
- sprintf( szError, "Login failed,code=%d", nRet );
- __LastError = szError;
- }
- } );
- }
- void wsClient::_on_connected()
- {
- __lock.lock();
- __cond.notify_all();
- __Connected = true;
- __lock.unlock();
- }
- void wsClient::send( const std::string & Cmd, const std::string & Data )
- {
- std::lock_guard<std::recursive_mutex> lock( __send_lock );
- sio::socket::ptr skt_ptr;
- skt_ptr = __wsclient.socket();
- skt_ptr->emit( Cmd, Data );
- }
- void wsClient::_on_close( sio::client::close_reason const & reason )
- {
- _reset();
- }
- void wsClient::_on_reconnect( unsigned p1, unsigned p2 )
- {
- _reset();
- }
- void wsClient::_on_fail()
- {
- _reset();
- }
- void wsClient::_on_socket_opened()
- {
- __SktOpened = true;
- }
- void wsClient::_OnCallMessage( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
- {
- if ( data->get_flag() == sio::message::flag_object )
- {
- std::string cmd = data->get_map()[JSON_ROOT_KEY_CMD]->get_string();
- std::map<std::string, MSG_HANDLE_FUNC_TYPE>::iterator mit_func;
- mit_func = __MsgFuncList.find( cmd );
- if ( mit_func != __MsgFuncList.end() )
- {
- mit_func->second( GetID(), name, data, need_ack, ack_resp );
- }
- }
- }
- void wsClient::_OnLoginResponse( std::string const & name, sio::message::ptr const & data, bool need_ack, sio::message::list & ack_resp )
- {
- char szError[512] = { 0 };
- int res_code = ( int ) data->get_map()["code"]->get_int();
- switch ( res_code )
- {
- case -1:
- {
- __Logined = false;
- sprintf( szError, "Login failed,code=%d", res_code );
- __LastError = szError;
- break;
- }
- case 0:
- {
- __Logined = true;
- break;
- }
- default:
- break;
- }
- }
- int wsClient::GetID()
- {
- return __ID;
- }
- bool wsClient::IsConnected()
- {
- return __Connected;
- }
- bool wsClient::IsSktOpened()
- {
- return __SktOpened;
- }
- bool wsClient::IsLogined()
- {
- return __Logined;
- }
- void wsClient::close()
- {
- __wsclient.sync_close();
- }
- std::string wsClient::GetLastError()
- {
- return __LastError;
- }
- }
|