wsClient.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if ( !IsConnected() )
  81. {
  82. __LastError = "please connect server before login!";
  83. return;
  84. }
  85. //std::cv_status status = std::cv_status::timeout;
  86. __lock.lock();
  87. if ( !IsSktOpened() )
  88. {
  89. // status =
  90. __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. YA::_JS_LOGIN_ Login;
  100. Login.user_name = JSON_VALUE_USERNAME;
  101. Login.user_password = JSON_VALUE_PASSWORD;
  102. std::string strLogin = __jsBuilder.BuildLogin( Login );
  103. strLogin += "\n";
  104. sio::socket::ptr skt_ptr;
  105. skt_ptr = __wsclient.socket();
  106. skt_ptr->emit( JSON_CMD_VALUE_USER, strLogin, [this]( sio::message::list const& msglist )
  107. {
  108. int nRet = 0;
  109. char szError[512] = { 0 };
  110. sio::message::ptr msg_ptr = msglist[0];
  111. nRet = ( int ) msg_ptr->get_map()["code"]->get_int();
  112. if ( 0 == nRet )
  113. {
  114. __Logined = true;
  115. }
  116. else
  117. {
  118. __Logined = false;
  119. sprintf( szError, "Login failed,code=%d", nRet );
  120. __LastError = szError;
  121. }
  122. } );
  123. }
  124. void wsClient::_on_connected()
  125. {
  126. __lock.lock();
  127. __cond.notify_all();
  128. __Connected = true;
  129. __lock.unlock();
  130. }
  131. void wsClient::send( const std::string & Cmd, const std::string & Data )
  132. {
  133. std::lock_guard<std::recursive_mutex> lock( __send_lock );
  134. sio::socket::ptr skt_ptr;
  135. skt_ptr = __wsclient.socket();
  136. skt_ptr->emit( Cmd, Data );
  137. }
  138. void wsClient::_on_close( sio::client::close_reason const & reason )
  139. {
  140. _reset();
  141. }
  142. void wsClient::_on_reconnect( unsigned p1, unsigned p2 )
  143. {
  144. _reset();
  145. }
  146. void wsClient::_on_fail()
  147. {
  148. _reset();
  149. }
  150. void wsClient::_on_socket_opened()
  151. {
  152. __SktOpened = true;
  153. }
  154. void wsClient::_OnCallMessage( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
  155. {
  156. if ( data->get_flag() == sio::message::flag_object )
  157. {
  158. std::string cmd = data->get_map()[JSON_ROOT_KEY_CMD]->get_string();
  159. std::map<std::string, MSG_HANDLE_FUNC_TYPE>::iterator mit_func;
  160. mit_func = __MsgFuncList.find( cmd );
  161. if ( mit_func != __MsgFuncList.end() )
  162. {
  163. mit_func->second( GetID(), name, data, need_ack, ack_resp );
  164. }
  165. }
  166. }
  167. void wsClient::_OnLoginResponse( std::string const & name, sio::message::ptr const & data, bool need_ack, sio::message::list & ack_resp )
  168. {
  169. char szError[512] = { 0 };
  170. int res_code = ( int ) data->get_map()["code"]->get_int();
  171. switch ( res_code )
  172. {
  173. case -1:
  174. {
  175. __Logined = false;
  176. sprintf( szError, "Login failed,code=%d", res_code );
  177. __LastError = szError;
  178. break;
  179. }
  180. case 0:
  181. {
  182. __Logined = true;
  183. break;
  184. }
  185. default:
  186. break;
  187. }
  188. }
  189. int wsClient::GetID()
  190. {
  191. return __ID;
  192. }
  193. bool wsClient::IsConnected()
  194. {
  195. return __Connected;
  196. }
  197. bool wsClient::IsSktOpened()
  198. {
  199. return __SktOpened;
  200. }
  201. bool wsClient::IsLogined()
  202. {
  203. return __Logined;
  204. }
  205. void wsClient::close()
  206. {
  207. __wsclient.sync_close();
  208. }
  209. std::string wsClient::GetLastError()
  210. {
  211. return __LastError;
  212. }
  213. }