wsClient.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "wsClient.h"
  2. #include "constdef.h"
  3. #include <thread>
  4. #include <chrono>
  5. #include <boost/locale.hpp>
  6. namespace YA
  7. {
  8. const int _LOGIN_SLEEP_TIME_ = 500;//登录前的等待时间(单位:毫秒)
  9. const int _OPEN_SOCKET_WAIT_TIME_ = 100;//等待打开socket时间(单位:毫秒)
  10. wsClient::wsClient()
  11. {
  12. __ID = 0;
  13. _reset();
  14. }
  15. wsClient::~wsClient()
  16. {
  17. }
  18. void wsClient::_reset()
  19. {
  20. __Connected = false;
  21. __SktOpened = false;
  22. __Logined = false;
  23. }
  24. void wsClient::init( int ID, const std::string & uri, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
  25. {
  26. __ID = ID;
  27. __uri = uri;
  28. __MsgFuncList = MsgFuncList;
  29. __wsclient.set_reconnect_attempts( 0 );
  30. using std::placeholders::_1;
  31. using std::placeholders::_2;
  32. using std::placeholders::_3;
  33. using std::placeholders::_4;
  34. sio::socket::ptr sock = __wsclient.socket();
  35. __wsclient.set_open_listener( std::bind( &wsClient::_on_connected, this ) );
  36. __wsclient.set_close_listener( std::bind( &wsClient::_on_close, this, std::placeholders::_1 ) );
  37. __wsclient.set_reconnect_listener( std::bind( &wsClient::_on_reconnect, this, std::placeholders::_1, std::placeholders::_2 ) );
  38. __wsclient.set_fail_listener( std::bind( &wsClient::_on_fail, this ) );
  39. __wsclient.set_socket_open_listener( std::bind( &wsClient::_on_socket_opened, this ) );
  40. sock->on( JSON_CMD_VALUE_CALL, sio::socket::event_listener_aux( std::bind( &wsClient::_OnCallMessage, this, _1, _2, _3, _4 ) ) );
  41. sock->on( "code", sio::socket::event_listener_aux( std::bind( &wsClient::_OnLoginResponse, this, _1, _2, _3, _4 ) ) );
  42. }
  43. std::string wsClient::get_uri()
  44. {
  45. return __uri;
  46. }
  47. int wsClient::connect( int time_out )
  48. {
  49. if ( __uri.empty() )
  50. {
  51. __LastError = "Error, uri is empty.";
  52. return -1;
  53. }
  54. __wsclient.connect( __uri );
  55. std::cv_status status = std::cv_status::timeout;
  56. int nRet = -1;
  57. __lock.lock();
  58. if ( !IsConnected() )
  59. {
  60. status = __cond.wait_for( __lock, std::chrono::seconds( time_out ) );
  61. if ( std::cv_status::timeout == status )
  62. {
  63. __LastError = "Failed to connect server.";
  64. nRet = -1;
  65. }
  66. else
  67. {
  68. nRet = 0;
  69. }
  70. }
  71. else
  72. {
  73. nRet = 0;
  74. }
  75. __lock.unlock();
  76. return nRet;
  77. }
  78. void wsClient::login()
  79. {
  80. char szError[512] = { 0 };
  81. if ( !IsConnected() )
  82. {
  83. __LastError = "please connect server before login!";
  84. return;
  85. }
  86. std::cv_status status = std::cv_status::timeout;
  87. __lock.lock();
  88. if ( !IsSktOpened() )
  89. {
  90. status = __cond.wait_for( __lock, std::chrono::milliseconds( _OPEN_SOCKET_WAIT_TIME_ ) );
  91. }
  92. __lock.unlock();
  93. if ( !IsSktOpened() )
  94. {
  95. __LastError = "socket is not opened before login!";
  96. return;
  97. }
  98. std::this_thread::sleep_for( std::chrono::milliseconds( _LOGIN_SLEEP_TIME_ ) );
  99. int nRet = 0;
  100. YA::_JS_LOGIN_ Login;
  101. Login.user_name = JSON_VALUE_USERNAME;
  102. Login.user_password = JSON_VALUE_PASSWORD;
  103. std::string strLogin = __jsBuilder.BuildLogin( Login );
  104. strLogin += "\n";
  105. sio::socket::ptr skt_ptr;
  106. skt_ptr = __wsclient.socket();
  107. skt_ptr->emit( JSON_CMD_VALUE_USER, strLogin, [&]( sio::message::list const& msglist )
  108. {
  109. sio::message::ptr msg_ptr = msglist[0];
  110. nRet = ( int ) msg_ptr->get_map()["code"]->get_int();
  111. if ( 0 == nRet )
  112. {
  113. __Logined = true;
  114. }
  115. else
  116. {
  117. __Logined = false;
  118. sprintf( szError, "Login failed,code=%d", nRet );
  119. __LastError = szError;
  120. }
  121. } );
  122. }
  123. void wsClient::_on_connected()
  124. {
  125. __lock.lock();
  126. __cond.notify_all();
  127. __Connected = true;
  128. __lock.unlock();
  129. }
  130. void wsClient::send( const std::string & Cmd, const std::string & Data )
  131. {
  132. std::lock_guard<std::recursive_mutex> lock( __send_lock );
  133. sio::socket::ptr skt_ptr;
  134. skt_ptr = __wsclient.socket();
  135. skt_ptr->emit( Cmd, Data );
  136. }
  137. void wsClient::_on_close( sio::client::close_reason const & reason )
  138. {
  139. _reset();
  140. }
  141. void wsClient::_on_reconnect( unsigned p1, unsigned p2 )
  142. {
  143. _reset();
  144. }
  145. void wsClient::_on_fail()
  146. {
  147. _reset();
  148. }
  149. void wsClient::_on_socket_opened()
  150. {
  151. __SktOpened = true;
  152. }
  153. void wsClient::_OnCallMessage( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
  154. {
  155. if ( data->get_flag() == sio::message::flag_object )
  156. {
  157. std::string cmd = data->get_map()[JSON_ROOT_KEY_CMD]->get_string();
  158. std::map<std::string, MSG_HANDLE_FUNC_TYPE>::iterator mit_func;
  159. mit_func = __MsgFuncList.find( cmd );
  160. if ( mit_func != __MsgFuncList.end() )
  161. {
  162. mit_func->second( GetID(), name, data, need_ack, ack_resp );
  163. }
  164. }
  165. }
  166. void wsClient::_OnLoginResponse( std::string const & name, sio::message::ptr const & data, bool need_ack, sio::message::list & ack_resp )
  167. {
  168. char szError[512] = { 0 };
  169. int res_code = ( int ) data->get_map()["code"]->get_int();
  170. switch ( res_code )
  171. {
  172. case -1:
  173. {
  174. __Logined = false;
  175. sprintf( szError, "Login failed,code=%d", res_code );
  176. __LastError = szError;
  177. break;
  178. }
  179. case 0:
  180. {
  181. __Logined = true;
  182. break;
  183. }
  184. default:
  185. break;
  186. }
  187. }
  188. int wsClient::GetID()
  189. {
  190. return __ID;
  191. }
  192. bool wsClient::IsConnected()
  193. {
  194. return __Connected;
  195. }
  196. bool wsClient::IsSktOpened()
  197. {
  198. return __SktOpened;
  199. }
  200. bool wsClient::IsLogined()
  201. {
  202. return __Logined;
  203. }
  204. void wsClient::close()
  205. {
  206. __wsclient.sync_close();
  207. }
  208. std::string wsClient::GetLastError()
  209. {
  210. return __LastError;
  211. }
  212. }