mirror of
https://github.com/socketio/socket.io-client-cpp.git
synced 2026-06-09 19:54:46 +00:00
add branch multiplex
This commit is contained in:
parent
61e44e98a3
commit
3b756510eb
165
src/internal/sio_client_impl.h
Normal file
165
src/internal/sio_client_impl.h
Normal file
@ -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 <websocketpp/client.hpp>
|
||||
#if _DEBUG || DEBUG
|
||||
#include <websocketpp/config/debug_asio_no_tls.hpp>
|
||||
typedef websocketpp::config::debug_asio client_config;
|
||||
#else
|
||||
#include <websocketpp/config/asio_no_tls_client.hpp>
|
||||
typedef websocketpp::config::asio_client client_config;
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <sio_socket.h>
|
||||
|
||||
|
||||
namespace sio
|
||||
{
|
||||
typedef websocketpp::client<client_config> 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<sio::socket> 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 std::string> 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 std::string> 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 string> 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<std::thread> m_network_thread;
|
||||
|
||||
struct message_queue_element
|
||||
{
|
||||
frame::opcode::value opcode;
|
||||
std::shared_ptr<const std::string> payload_ptr;
|
||||
};
|
||||
|
||||
packet_manager m_packet_mgr;
|
||||
|
||||
std::queue<message_queue_element> m_message_queue;
|
||||
|
||||
std::unique_ptr<boost::asio::deadline_timer> m_ping_timer;
|
||||
|
||||
std::unique_ptr<boost::asio::deadline_timer> 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
|
||||
|
||||
@ -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 <websocketpp/client.hpp>
|
||||
#if _DEBUG || DEBUG
|
||||
#include <websocketpp/config/debug_asio_no_tls.hpp>
|
||||
typedef websocketpp::config::debug_asio client_config;
|
||||
#else
|
||||
#include <websocketpp/config/asio_no_tls_client.hpp>
|
||||
typedef websocketpp::config::asio_client client_config;
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <sstream>
|
||||
#include <rapidjson/document.h>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#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_config> 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<void (message::ptr const&)> const& ack);
|
||||
|
||||
void emit(std::string const& name, message::ptr const& args);
|
||||
|
||||
void emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack);
|
||||
|
||||
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr);
|
||||
|
||||
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> 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 std::string> 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 std::string> 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 string> 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<unsigned int, std::function<void (message::ptr const&)> > m_acks;
|
||||
|
||||
std::map<std::string, event_listener> m_event_binding;
|
||||
|
||||
static unsigned int s_global_event_id;
|
||||
|
||||
std::unique_ptr<std::thread> m_network_thread;
|
||||
|
||||
struct message_queue_element
|
||||
{
|
||||
frame::opcode::value opcode;
|
||||
std::shared_ptr<const std::string> payload_ptr;
|
||||
};
|
||||
|
||||
packet_manager m_packet_mgr;
|
||||
|
||||
std::queue<message_queue_element> m_message_queue;
|
||||
|
||||
std::unique_ptr<boost::asio::deadline_timer> m_ping_timer;
|
||||
|
||||
std::unique_ptr<boost::asio::deadline_timer> m_ping_timeout_timer;
|
||||
|
||||
std::unique_ptr<boost::asio::deadline_timer> 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:
|
||||
|
||||
@ -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__
|
||||
#endif // __SIO_CLIENT__H__
|
||||
|
||||
104
src/sio_socket.cpp
Normal file
104
src/sio_socket.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
#include "sio_socket.h"
|
||||
namespace sio {
|
||||
class socket::impl
|
||||
{
|
||||
public:
|
||||
typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::ptr& ack_message)> event_listener;
|
||||
|
||||
typedef std::function<void(message::ptr const& message)> error_listener;
|
||||
|
||||
typedef std::function<void(void)> 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<void (message::ptr const&)> const& ack);
|
||||
|
||||
void emit(std::string const& name, message::ptr const& args);
|
||||
|
||||
void emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack);
|
||||
|
||||
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr);
|
||||
|
||||
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> 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<unsigned int, std::function<void (message::ptr const&)> > m_acks;
|
||||
|
||||
std::map<std::string, event_listener> m_event_binding;
|
||||
|
||||
con_listener m_open_listener;
|
||||
|
||||
std::unique_ptr<boost::asio::deadline_timer> 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<void (message::ptr const&)> 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<void (message::ptr const&)> const& ack);
|
||||
|
||||
void socket::emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr);
|
||||
|
||||
void socket::emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> const& ack);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
45
src/sio_socket.h
Normal file
45
src/sio_socket.h
Normal file
@ -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<void(const std::string& name,message::ptr const& message,bool need_ack, message::ptr& ack_message)> event_listener;
|
||||
|
||||
typedef std::function<void(message::ptr const& message)> error_listener;
|
||||
|
||||
typedef std::function<void(void)> 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<void (message::ptr const&)> const& ack);
|
||||
|
||||
void emit(std::string const& name, message::ptr const& args);
|
||||
|
||||
void emit(std::string const& name, message::ptr const& args, std::function<void (message::ptr const&)> const& ack);
|
||||
|
||||
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr);
|
||||
|
||||
void emit(std::string const& name, std::shared_ptr<const std::string> const& binary_ptr, std::function<void (message::ptr const&)> const& ack);
|
||||
|
||||
private:
|
||||
class impl;
|
||||
impl *m_impl;
|
||||
};
|
||||
}
|
||||
#endif // SIO_SOCKET_H
|
||||
Loading…
x
Reference in New Issue
Block a user