1
0

sio_client_impl.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef SIO_CLIENT_IMPL_H
  2. #define SIO_CLIENT_IMPL_H
  3. #include <cstdint>
  4. #ifdef _WIN32
  5. #define _WEBSOCKETPP_CPP11_THREAD_
  6. #define BOOST_ALL_NO_LIB
  7. //#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_
  8. #define _WEBSOCKETPP_NO_CPP11_FUNCTIONAL_
  9. #define INTIALIZER(__TYPE__)
  10. #else
  11. #define _WEBSOCKETPP_CPP11_STL_ 1
  12. #define INTIALIZER(__TYPE__) (__TYPE__)
  13. #endif
  14. #include <websocketpp/client.hpp>
  15. #if _DEBUG || DEBUG
  16. #if SIO_TLS
  17. #include <websocketpp/config/debug_asio.hpp>
  18. typedef websocketpp::config::debug_asio_tls client_config;
  19. #else
  20. #include <websocketpp/config/debug_asio_no_tls.hpp>
  21. typedef websocketpp::config::debug_asio client_config;
  22. #endif //SIO_TLS
  23. #else
  24. #if SIO_TLS
  25. #include <websocketpp/config/asio_client.hpp>
  26. typedef websocketpp::config::asio_tls_client client_config;
  27. #else
  28. #include <websocketpp/config/asio_no_tls_client.hpp>
  29. typedef websocketpp::config::asio_client client_config;
  30. #endif //SIO_TLS
  31. #endif //DEBUG
  32. #include <boost/asio/deadline_timer.hpp>
  33. #include <memory>
  34. #include <map>
  35. #include <thread>
  36. #include "../sio_client.h"
  37. #include "sio_packet.h"
  38. namespace sio
  39. {
  40. using namespace websocketpp;
  41. typedef websocketpp::client<client_config> client_type;
  42. class client_impl {
  43. protected:
  44. enum con_state
  45. {
  46. con_opening,
  47. con_opened,
  48. con_closing,
  49. con_closed
  50. };
  51. client_impl();
  52. ~client_impl();
  53. //set listeners and event bindings.
  54. #define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \
  55. void set_##__FIELD__(__TYPE__ const& l) \
  56. { m_##__FIELD__ = l;}
  57. SYNTHESIS_SETTER(client::con_listener,open_listener)
  58. SYNTHESIS_SETTER(client::con_listener,fail_listener)
  59. SYNTHESIS_SETTER(client::reconnect_listener,reconnect_listener)
  60. SYNTHESIS_SETTER(client::con_listener,reconnecting_listener)
  61. SYNTHESIS_SETTER(client::close_listener,close_listener)
  62. SYNTHESIS_SETTER(client::socket_listener,socket_open_listener)
  63. SYNTHESIS_SETTER(client::socket_listener,socket_close_listener)
  64. #undef SYNTHESIS_SETTER
  65. void clear_con_listeners()
  66. {
  67. m_open_listener = nullptr;
  68. m_close_listener = nullptr;
  69. m_fail_listener = nullptr;
  70. m_reconnect_listener = nullptr;
  71. m_reconnecting_listener = nullptr;
  72. }
  73. void clear_socket_listeners()
  74. {
  75. m_socket_open_listener = nullptr;
  76. m_socket_close_listener = nullptr;
  77. }
  78. // Client Functions - such as send, etc.
  79. void connect(const std::string& uri, const std::map<std::string, std::string>& queryString,
  80. const std::map<std::string, std::string>& httpExtraHeaders);
  81. sio::socket::ptr const& socket(const std::string& nsp);
  82. // Closes the connection
  83. void close();
  84. void sync_close();
  85. bool opened() const { return m_con_state == con_opened; }
  86. std::string const& get_sessionid() const { return m_sid; }
  87. void set_reconnect_attempts(unsigned attempts) {m_reconn_attempts = attempts;}
  88. void set_reconnect_delay(unsigned millis) {m_reconn_delay = millis;if(m_reconn_delay_max<millis) m_reconn_delay_max = millis;}
  89. void set_reconnect_delay_max(unsigned millis) {m_reconn_delay_max = millis;if(m_reconn_delay>millis) m_reconn_delay = millis;}
  90. protected:
  91. void send(packet& p);
  92. void remove_socket(std::string const& nsp);
  93. boost::asio::io_service& get_io_service();
  94. void on_socket_closed(std::string const& nsp);
  95. void on_socket_opened(std::string const& nsp);
  96. private:
  97. void run_loop();
  98. void connect_impl(const std::string& uri, const std::string& query);
  99. void close_impl(close::status::value const& code,std::string const& reason);
  100. void send_impl(std::shared_ptr<const std::string> const& payload_ptr,frame::opcode::value opcode);
  101. void ping(const boost::system::error_code& ec);
  102. void timeout_pong(const boost::system::error_code& ec);
  103. void timeout_reconnect(boost::system::error_code const& ec);
  104. unsigned next_delay() const;
  105. socket::ptr get_socket_locked(std::string const& nsp);
  106. void sockets_invoke_void(void (sio::socket::*fn)(void));
  107. void on_decode(packet const& pack);
  108. void on_encode(bool isBinary,shared_ptr<const string> const& payload);
  109. //websocket callbacks
  110. void on_fail(connection_hdl con);
  111. void on_open(connection_hdl con);
  112. void on_close(connection_hdl con);
  113. void on_message(connection_hdl con, client_type::message_ptr msg);
  114. //socketio callbacks
  115. void on_handshake(message::ptr const& message);
  116. void on_pong();
  117. void reset_states();
  118. void clear_timers();
  119. #if SIO_TLS
  120. typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
  121. context_ptr on_tls_init(connection_hdl con);
  122. #endif
  123. // Percent encode query string
  124. std::string encode_query_string(const std::string &query);
  125. // Connection pointer for client functions.
  126. connection_hdl m_con;
  127. client_type m_client;
  128. // Socket.IO server settings
  129. std::string m_sid;
  130. std::string m_base_url;
  131. std::string m_query_string;
  132. std::map<std::string, std::string> m_http_headers;
  133. unsigned int m_ping_interval;
  134. unsigned int m_ping_timeout;
  135. std::unique_ptr<std::thread> m_network_thread;
  136. packet_manager m_packet_mgr;
  137. std::unique_ptr<boost::asio::deadline_timer> m_ping_timer;
  138. std::unique_ptr<boost::asio::deadline_timer> m_ping_timeout_timer;
  139. std::unique_ptr<boost::asio::deadline_timer> m_reconn_timer;
  140. con_state m_con_state;
  141. client::con_listener m_open_listener;
  142. client::con_listener m_fail_listener;
  143. client::con_listener m_reconnecting_listener;
  144. client::reconnect_listener m_reconnect_listener;
  145. client::close_listener m_close_listener;
  146. client::socket_listener m_socket_open_listener;
  147. client::socket_listener m_socket_close_listener;
  148. std::map<const std::string,socket::ptr> m_sockets;
  149. std::mutex m_socket_mutex;
  150. unsigned m_reconn_delay;
  151. unsigned m_reconn_delay_max;
  152. unsigned m_reconn_attempts;
  153. unsigned m_reconn_made;
  154. friend class sio::client;
  155. friend class sio::socket;
  156. };
  157. }
  158. #endif // SIO_CLIENT_IMPL_H