wsClient.cpp 5.4 KB

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