diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h new file mode 100644 index 0000000..9e1da9b --- /dev/null +++ b/src/internal/sio_client_impl.h @@ -0,0 +1,165 @@ +#ifndef SIO_CLIENT_IMPL_H +#define SIO_CLIENT_IMPL_H + +#ifdef WIN32 +#define _WEBSOCKETPP_CPP11_THREAD_ +#define BOOST_ALL_NO_LIB +//#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_ +#define _WEBSOCKETPP_NO_CPP11_FUNCTIONAL_ +#define INTIALIZER(__TYPE__) +#else +#define _WEBSOCKETPP_CPP11_STL_ 1 +#define INTIALIZER(__TYPE__) (__TYPE__) +#endif +#include +#if _DEBUG || DEBUG +#include +typedef websocketpp::config::debug_asio client_config; +#else +#include +typedef websocketpp::config::asio_client client_config; +#endif + +#include +#include +#include +#include +#include + + +namespace sio +{ + typedef websocketpp::client client_type; + + class client::impl { + protected: + impl(); + + ~impl(); + + //set listeners and event bindings. +#define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \ +void set_##__FIELD__(__TYPE__ const& l) \ +{ m_##__FIELD__ = l;} + + SYNTHESIS_SETTER(con_listener,open_listener) + + SYNTHESIS_SETTER(con_listener,fail_listener) + + SYNTHESIS_SETTER(close_listener,close_listener) + + SYNTHESIS_SETTER(event_listener, default_event_listener) +#undef SYNTHESIS_SETTER + + void clear_socketio_listeners() + { + m_default_event_listener = nullptr; + } + + void clear_con_listeners() + { + m_open_listener = nullptr; + m_close_listener = nullptr; + m_fail_listener = nullptr; + } + + // Client Functions - such as send, etc. + + std::shared_ptr socket(const std::string& namespace); + + void connect(const std::string& uri); + + void reconnect(const std::string& uri); + + // Closes the connection + void close(); + + void sync_close(); + + bool connected() const { return m_connected; } + + std::string const& get_sessionid() const { return m_sid; } + + std::string const& get_namespace() const { return m_nsp; } + + friend class client; + protected: + void send(packet const& packet); + + private: + void send(std::shared_ptr const& payload_ptr,frame::opcode::value opcode); + + void __close(close::status::value const& code,std::string const& reason); + + void __connect(const std::string& uri); + + void __send(std::shared_ptr const& payload_ptr,frame::opcode::value opcode); + + + void __ping(const boost::system::error_code& ec); + + void __timeout_pong(const boost::system::error_code& ec); + + void __timeout_connection(const boost::system::error_code& ec); + + void run_loop(); + + void on_decode(packet const& pack); + void on_encode(bool isBinary,shared_ptr const& payload); + + // Callbacks + void on_fail(connection_hdl con); + void on_open(connection_hdl con); + void on_close(connection_hdl con); + void on_message(connection_hdl con, client_type::message_ptr msg); + void on_handshake(message::ptr const& message); + + void on_pong(); + + void on_pong_timeout(); + + // Message Parsing callbacks. + void on_socketio_event(const std::string& nsp, int msgId,const std::string& name, message::ptr const& message); + void on_socketio_ack(int msgId, message::ptr const& message); + + void on_socketio_error(message::ptr const& err_message); + + void reset_states(); + + void clear_timers(); + + // Connection pointer for client functions. + connection_hdl m_con; + client_type m_client; + // Socket.IO server settings + std::string m_sid; + unsigned int m_ping_interval; + unsigned int m_ping_timeout; + + std::unique_ptr m_network_thread; + + struct message_queue_element + { + frame::opcode::value opcode; + std::shared_ptr payload_ptr; + }; + + packet_manager m_packet_mgr; + + std::queue m_message_queue; + + std::unique_ptr m_ping_timer; + + std::unique_ptr m_ping_timeout_timer; + + + con_listener m_open_listener; + con_listener m_fail_listener; + close_listener m_close_listener; + + event_listener m_default_event_listener; + friend class sio::socket; + }; +} +#endif // SIO_CLIENT_IMPL_H + diff --git a/src/sio_client.cpp b/src/sio_client.cpp index a17a25a..48503a3 100755 --- a/src/sio_client.cpp +++ b/src/sio_client.cpp @@ -5,32 +5,9 @@ // #include "sio_client.h" +#include "internal/sio_client_impl.h" -#ifdef WIN32 -#define _WEBSOCKETPP_CPP11_THREAD_ -#define BOOST_ALL_NO_LIB -//#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_ -#define _WEBSOCKETPP_NO_CPP11_FUNCTIONAL_ -#define INTIALIZER(__TYPE__) -#else -#define _WEBSOCKETPP_CPP11_STL_ 1 -#define INTIALIZER(__TYPE__) (__TYPE__) -#endif -#include -#if _DEBUG || DEBUG -#include -typedef websocketpp::config::debug_asio client_config; -#else -#include -typedef websocketpp::config::asio_client client_config; -#endif - -#include -#include -#include -#include #include -#include #include #include "sio_packet.h" // Comment this out to disable handshake logging to stdout @@ -47,183 +24,6 @@ using std::stringstream; namespace sio { - typedef websocketpp::client client_type; - - class client::impl { - protected: - impl(); - - ~impl(); - - //set listeners and event bindings. -#define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \ -void set_##__FIELD__(__TYPE__ const& l) \ -{ m_##__FIELD__ = l;} - - SYNTHESIS_SETTER(con_listener,open_listener) - - SYNTHESIS_SETTER(con_listener,fail_listener) - - SYNTHESIS_SETTER(con_listener,connect_listener) - - SYNTHESIS_SETTER(close_listener,close_listener) - - SYNTHESIS_SETTER(event_listener, default_event_listener) - - SYNTHESIS_SETTER(error_listener, error_listener) //socket io errors - - void bind_event(std::string const& event_name,event_listener const& func) - { - m_event_binding[event_name] = func; - } - - void unbind_event(std::string const& event_name) - { - auto it = m_event_binding.find(event_name); - if(it!=m_event_binding.end()) - { - m_event_binding.erase(it); - } - } - - void clear_socketio_listeners() - { - m_default_event_listener = nullptr; - m_error_listener = nullptr; - } - - void clear_con_listeners() - { - m_open_listener = nullptr; - m_close_listener = nullptr; - m_fail_listener = nullptr; - m_connect_listener = nullptr; - } - - void clear_event_bindings() - { - m_event_binding.clear(); - } - // Client Functions - such as send, etc. - - //event emit functions, for plain message,json and binary - void emit(std::string const& name, std::string const& message); - - void emit(std::string const& name, std::string const& message, std::function const& ack); - - void emit(std::string const& name, message::ptr const& args); - - void emit(std::string const& name, message::ptr const& args, std::function const& ack); - - void emit(std::string const& name, std::shared_ptr const& binary_ptr); - - void emit(std::string const& name, std::shared_ptr const& binary_ptr, std::function const& ack); - - - void connect(const std::string& uri); - - void reconnect(const std::string& uri); - - // Closes the connection - void close(); - - void sync_close(); - - bool connected() const { return m_connected; } - - std::string const& get_sessionid() const { return m_sid; } - - std::string const& get_namespace() const { return m_nsp; } - - friend class client; - private: - void send(std::shared_ptr const& payload_ptr,frame::opcode::value opcode); - - void __close(close::status::value const& code,std::string const& reason); - - void __connect(const std::string& uri); - - void __send(std::shared_ptr const& payload_ptr,frame::opcode::value opcode); - - void __ack(int msgId,string const& name,message::ptr const& ack_message); - - void __ping(const boost::system::error_code& ec); - - void __timeout_pong(const boost::system::error_code& ec); - - void __timeout_connection(const boost::system::error_code& ec); - - void run_loop(); - - void on_decode(packet const& pack); - void on_encode(bool isBinary,shared_ptr const& payload); - - // Callbacks - void on_fail(connection_hdl con); - void on_open(connection_hdl con); - void on_close(connection_hdl con); - void on_message(connection_hdl con, client_type::message_ptr msg); - void on_handshake(message::ptr const& message); - void on_connected(); - void on_pong(); - - void on_pong_timeout(); - - // Message Parsing callbacks. - void on_socketio_event(const std::string& nsp, int msgId,const std::string& name, message::ptr const& message); - void on_socketio_ack(int msgId, message::ptr const& message); - - void on_socketio_error(message::ptr const& err_message); - - void reset_states(); - - void clear_timers(); - - // Connection pointer for client functions. - connection_hdl m_con; - client_type m_client; - // Socket.IO server settings - std::string m_sid; - unsigned int m_ping_interval; - unsigned int m_ping_timeout; - - bool m_connected; - std::string m_nsp; - - // Currently we assume websocket as the transport, though you can find others in this string - std::map > m_acks; - - std::map m_event_binding; - - static unsigned int s_global_event_id; - - std::unique_ptr m_network_thread; - - struct message_queue_element - { - frame::opcode::value opcode; - std::shared_ptr payload_ptr; - }; - - packet_manager m_packet_mgr; - - std::queue m_message_queue; - - std::unique_ptr m_ping_timer; - - std::unique_ptr m_ping_timeout_timer; - - std::unique_ptr m_connection_timer; - - con_listener m_open_listener; - con_listener m_connect_listener; - con_listener m_fail_listener; - close_listener m_close_listener; - - event_listener m_default_event_listener; - error_listener m_error_listener; - }; - class event_adapter { public: diff --git a/src/sio_client.h b/src/sio_client.h index 7d4412d..faf3cbe 100755 --- a/src/sio_client.h +++ b/src/sio_client.h @@ -85,12 +85,14 @@ namespace sio { void bind_event(std::string const& event_name,event_listener_aux const& func); void unbind_event(std::string const& event_name); - + + void clear_event_bindings(); + void clear_socketio_listeners(); void clear_con_listeners(); - void clear_event_bindings(); + // Client Functions - such as send, etc. //event emit functions, for plain message,json and binary @@ -130,4 +132,4 @@ namespace sio { } -#endif // __SIO_CLIENT__H__ \ No newline at end of file +#endif // __SIO_CLIENT__H__ diff --git a/src/sio_socket.cpp b/src/sio_socket.cpp new file mode 100644 index 0000000..6a4f742 --- /dev/null +++ b/src/sio_socket.cpp @@ -0,0 +1,104 @@ +#include "sio_socket.h" +namespace sio { +class socket::impl +{ +public: + typedef std::function event_listener; + + typedef std::function error_listener; + + typedef std::function con_listener; + + impl(); + ~impl(); + + void bind_event(std::string const& event_name,event_listener const& func) + { + m_event_binding[event_name] = func; + } + + void unbind_event(std::string const& event_name) + { + auto it = m_event_binding.find(event_name); + if(it!=m_event_binding.end()) + { + m_event_binding.erase(it); + } + } + + void clear_event_bindings() + { + m_event_binding.clear(); + } + +#define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \ + void set_##__FIELD__(__TYPE__ const& l) \ + { m_##__FIELD__ = l;} + SYNTHESIS_SETTER(con_listener,connect_listener) + + SYNTHESIS_SETTER(error_listener, error_listener) //socket io errors +#undef SYNTHESIS_SETTER + + void emit(std::string const& name, std::string const& message); + + void emit(std::string const& name, std::string const& message, std::function const& ack); + + void emit(std::string const& name, message::ptr const& args); + + void emit(std::string const& name, message::ptr const& args, std::function const& ack); + + void emit(std::string const& name, std::shared_ptr const& binary_ptr); + + void emit(std::string const& name, std::shared_ptr const& binary_ptr, std::function const& ack); + +protected: + void on_connected(); +private: + + void __ack(int msgId,string const& name,message::ptr const& ack_message); + + static unsigned int s_global_event_id; + + client::impl *m_client; + + bool m_connected; + std::string m_nsp; + + // Currently we assume websocket as the transport, though you can find others in this string + std::map > m_acks; + + std::map m_event_binding; + + con_listener m_open_listener; + + std::unique_ptr m_connection_timer; +}; + +socket::impl() +{ + +} + +socket::~impl() +{ + +} + + +void socket::emit(std::string const& name, std::string const& message); + +void socket::emit(std::string const& name, std::string const& message, std::function const& ack); + +void socket::emit(std::string const& name, message::ptr const& args); + +void socket::emit(std::string const& name, message::ptr const& args, std::function const& ack); + +void socket::emit(std::string const& name, std::shared_ptr const& binary_ptr); + +void socket::emit(std::string const& name, std::shared_ptr const& binary_ptr, std::function const& ack); + +} + + + + diff --git a/src/sio_socket.h b/src/sio_socket.h new file mode 100644 index 0000000..baf89ee --- /dev/null +++ b/src/sio_socket.h @@ -0,0 +1,45 @@ +#ifndef SIO_SOCKET_H +#define SIO_SOCKET_H +#include "internal/sio_client_impl.h" +namespace sio +{ +class socket +{ +public: + typedef std::function event_listener; + + typedef std::function error_listener; + + typedef std::function con_listener; + + socket(); + ~socket(); + + void bind_event(std::string const& event_name,event_listener const& func); + + void unbind_event(std::string const& event_name); + + void clear_event_bindings(); + + + void set_connect_listener(con_listener const& l) ; + void set_error_listener(error_listener const& l); + + void emit(std::string const& name, std::string const& message); + + void emit(std::string const& name, std::string const& message, std::function const& ack); + + void emit(std::string const& name, message::ptr const& args); + + void emit(std::string const& name, message::ptr const& args, std::function const& ack); + + void emit(std::string const& name, std::shared_ptr const& binary_ptr); + + void emit(std::string const& name, std::shared_ptr const& binary_ptr, std::function const& ack); + +private: + class impl; + impl *m_impl; +}; +} +#endif // SIO_SOCKET_H