wsClient.cpp 5.7 KB

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