-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathsio_client_impl.h
259 lines (176 loc) · 7.45 KB
/
sio_client_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#ifndef SIO_CLIENT_IMPL_H
#define SIO_CLIENT_IMPL_H
#include <cstdint>
#ifdef _WIN32
#define _WEBSOCKETPP_CPP11_THREAD_
//#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
#if SIO_TLS
#include <websocketpp/config/debug_asio.hpp>
typedef websocketpp::config::debug_asio_tls client_config;
#else
#include <websocketpp/config/debug_asio_no_tls.hpp>
typedef websocketpp::config::debug_asio client_config;
#endif //SIO_TLS
#else
#if SIO_TLS
#include <websocketpp/config/asio_client.hpp>
typedef websocketpp::config::asio_tls_client client_config;
#else
#include <websocketpp/config/asio_no_tls_client.hpp>
typedef websocketpp::config::asio_client client_config;
#endif //SIO_TLS
#endif //DEBUG
#if SIO_TLS
#include <asio/ssl/context.hpp>
#endif
#include <asio/steady_timer.hpp>
#include <asio/error_code.hpp>
#include <asio/io_service.hpp>
#include <atomic>
#include <memory>
#include <map>
#include <thread>
#include "../sio_client.h"
#include "sio_packet.h"
namespace sio
{
using namespace websocketpp;
typedef websocketpp::client<client_config> client_type;
class client_impl {
protected:
enum con_state
{
con_opening,
con_opened,
con_closing,
con_closed
};
client_impl(client_options const& options);
~client_impl();
//set listeners and event bindings.
#define SYNTHESIS_SETTER(__TYPE__,__FIELD__) \
void set_##__FIELD__(__TYPE__ const& l) \
{ m_##__FIELD__ = l;}
SYNTHESIS_SETTER(client::con_listener,open_listener)
SYNTHESIS_SETTER(client::con_listener,fail_listener)
SYNTHESIS_SETTER(client::reconnect_listener,reconnect_listener)
SYNTHESIS_SETTER(client::con_listener,reconnecting_listener)
SYNTHESIS_SETTER(client::close_listener,close_listener)
SYNTHESIS_SETTER(client::socket_listener,socket_open_listener)
SYNTHESIS_SETTER(client::socket_listener,socket_close_listener)
SYNTHESIS_SETTER(client::connection_listener,connection_listener)
#undef SYNTHESIS_SETTER
void clear_con_listeners()
{
m_open_listener = nullptr;
m_close_listener = nullptr;
m_fail_listener = nullptr;
m_reconnect_listener = nullptr;
m_reconnecting_listener = nullptr;
m_connection_listener = nullptr;
}
void clear_socket_listeners()
{
m_socket_open_listener = nullptr;
m_socket_close_listener = nullptr;
}
// Client Functions - such as send, etc.
void connect(const std::string& uri, const std::map<std::string, std::string>& queryString,
const std::map<std::string, std::string>& httpExtraHeaders, const message::ptr& auth);
sio::socket::ptr const& socket(const std::string& nsp);
// Closes the connection
void close();
void sync_close();
bool opened() const { return m_con_state == con_opened; }
std::string const& get_sessionid() const { return m_sid; }
void set_reconnect_attempts(unsigned attempts) {m_reconn_attempts = attempts;}
void set_reconnect_delay(unsigned millis) {m_reconn_delay = millis;if(m_reconn_delay_max<millis) m_reconn_delay_max = millis;}
void set_reconnect_delay_max(unsigned millis) {m_reconn_delay_max = millis;if(m_reconn_delay>millis) m_reconn_delay = millis;}
void set_logs_default();
void set_logs_quiet();
void set_logs_verbose();
void set_proxy_basic_auth(const std::string& uri, const std::string& username, const std::string& password);
protected:
void send(packet& p);
void remove_socket(std::string const& nsp);
asio::io_service& get_io_service();
void on_socket_closed(std::string const& nsp);
void on_socket_opened(std::string const& nsp);
void on_client_disconnect(bool const& wasDisconnected);
private:
void run_loop();
void connect_impl(const std::string& uri, const std::string& query);
void close_impl(close::status::value const& code,std::string const& reason);
void send_impl(std::shared_ptr<const std::string> const& payload_ptr,frame::opcode::value opcode);
void ping(const asio::error_code& ec);
void timeout_ping(const asio::error_code& ec);
void timeout_reconnect(asio::error_code const& ec);
unsigned next_delay() const;
socket::ptr get_socket_locked(std::string const& nsp);
void sockets_invoke_void(void (sio::socket::*fn)(void));
void on_decode(packet const& pack);
void on_encode(bool isBinary,shared_ptr<const string> const& payload);
//websocket 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);
//socketio callbacks
void on_handshake(message::ptr const& message);
void on_ping();
void reset_states();
void clear_timers();
void update_ping_timeout_timer();
#if SIO_TLS
typedef websocketpp::lib::shared_ptr<asio::ssl::context> context_ptr;
context_ptr on_tls_init(connection_hdl con);
#endif
// Percent encode query string
std::string encode_query_string(const std::string &query);
// Connection pointer for client functions.
connection_hdl m_con;
client_type m_client;
// Socket.IO server settings
std::string m_sid;
std::string m_base_url;
std::string m_query_string;
std::map<std::string, std::string> m_http_headers;
message::ptr m_auth;
std::string m_proxy_base_url;
std::string m_proxy_basic_username;
std::string m_proxy_basic_password;
unsigned int m_ping_interval;
unsigned int m_ping_timeout;
std::unique_ptr<std::thread> m_network_thread;
packet_manager m_packet_mgr;
std::unique_ptr<asio::steady_timer> m_ping_timeout_timer;
std::unique_ptr<asio::steady_timer> m_reconn_timer;
con_state m_con_state;
client::con_listener m_open_listener;
client::con_listener m_fail_listener;
client::con_listener m_reconnecting_listener;
client::reconnect_listener m_reconnect_listener;
client::close_listener m_close_listener;
client::connection_listener m_connection_listener;
client::socket_listener m_socket_open_listener;
client::socket_listener m_socket_close_listener;
std::map<const std::string,socket::ptr> m_sockets;
std::mutex m_socket_mutex;
unsigned m_reconn_delay;
unsigned m_reconn_delay_max;
unsigned m_reconn_attempts;
unsigned m_reconn_made;
std::atomic<bool> m_abort_retries { false };
friend class sio::client;
friend class sio::socket;
};
}
#endif // SIO_CLIENT_IMPL_H