wsClient.cpp 5.7 KB

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