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
28#ifndef WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
29#define WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
31#include <websocketpp/transport/iostream/base.hpp>
33#include <websocketpp/transport/base/connection.hpp>
35#include <websocketpp/uri.hpp>
37#include <websocketpp/logger/levels.hpp>
39#include <websocketpp/common/connection_hdl.hpp>
40#include <websocketpp/common/memory.hpp>
41#include <websocketpp/common/platforms.hpp>
59template <
typename config>
78 typedef lib::shared_ptr<
timer> timer_ptr;
80 explicit connection(
bool is_server,
const lib::shared_ptr<
alog_type> & alog,
const lib::shared_ptr<
elog_type> & elog)
81 : m_output_stream(NULL)
83 , m_is_server(is_server)
87 , m_remote_endpoint(
"iostream transport")
94 return type::shared_from_this();
99
100
101
102
103
106 scoped_lock_type lock(m_read_mutex);
112
113
114
115
116
117
118
119
120
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
144 scoped_lock_type lock(t.m_read_mutex);
153
154
155
156
157
158
159
160
161
162
163
164
167 scoped_lock_type lock(m_read_mutex);
169 return this->read_some_impl(buf,len);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
190 scoped_lock_type lock(m_read_mutex);
192 size_t total_read = 0;
193 size_t temp_read = 0;
196 temp_read =
this->read_some_impl(buf+total_read,len-total_read);
197 total_read += temp_read;
198 }
while (temp_read != 0 && total_read < len);
205
206
207
209 return this->read_some(buf,len);
214
215
216
217
218
221 scoped_lock_type lock(m_read_mutex);
230
231
232
233
234
237 scoped_lock_type lock(m_read_mutex);
246
247
248
249
250
251
252
253
254
255
256
263
264
265
266
267
268
269
270
277
278
279
280
281
282
283
284
285
286
287
288
289 void set_remote_endpoint(std::string value) {
290 m_remote_endpoint = value;
295
296
297
298
299
300
301
302
303
304
306 return m_remote_endpoint;
311
312
314 return m_connection_hdl;
319
320
321
322
323
324
325
326
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
381 m_vector_write_handler = h;
386
387
388
389
390
391
392
393
394
395
396
397
398
399
401 m_shutdown_handler = h;
406
407
408
409
412 handler(lib::error_code());
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
443 s <<
"iostream_con async_read_at_least: " << num_bytes;
446 if (num_bytes > len) {
451 if (m_reading ==
true) {
456 if (num_bytes == 0 || len == 0) {
457 handler(lib::error_code(),size_t(0));
463 m_bytes_needed = num_bytes;
464 m_read_handler = handler;
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
495 if (m_output_stream) {
496 m_output_stream->write(buf,len);
498 if (m_output_stream->bad()) {
501 }
else if (m_write_handler) {
502 ec = m_write_handler(m_connection_hdl, buf, len);
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
530 m_alog->write(log
::alevel::devel,
"iostream_con async_write buffer list");
535 if (m_output_stream) {
536 std::vector<buffer>::const_iterator it;
537 for (it = bufs.begin(); it != bufs.end(); it++) {
538 m_output_stream->write((*it).buf,(*it).len);
540 if (m_output_stream->bad()) {
545 }
else if (m_vector_write_handler) {
546 ec = m_vector_write_handler(m_connection_hdl, bufs);
547 }
else if (m_write_handler) {
548 std::vector<buffer>::const_iterator it;
549 for (it = bufs.begin(); it != bufs.end(); it++) {
550 ec = m_write_handler(m_connection_hdl, (*it).buf, (*it).len);
563
564
566 m_connection_hdl = hdl;
571
572
573
574
575
576
577
578
579
582 return lib::error_code();
587
588
589
590
591
592
596 if (m_shutdown_handler) {
597 ec = m_shutdown_handler(m_connection_hdl);
603 void read(std::istream &in) {
612 in.read(m_buf+m_cursor,
static_cast<std::streamsize>(m_len-m_cursor));
614 if (in.gcount() == 0) {
619 m_cursor +=
static_cast<size_t>(in.gcount());
627 if (m_cursor >= m_bytes_needed) {
629 complete_read(lib::error_code());
634 size_t read_some_impl(
char const * buf, size_t len) {
642 size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
644 std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
646 m_cursor += bytes_to_copy;
648 if (m_cursor >= m_bytes_needed) {
649 complete_read(lib::error_code());
652 return bytes_to_copy;
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671 void complete_read(lib::error_code
const & ec) {
675 m_read_handler = read_handler();
677 handler(ec,m_cursor);
683 size_t m_bytes_needed;
688 std::ostream * m_output_stream;
695 bool const m_is_server;
699 std::string m_remote_endpoint;
706 mutex_type m_read_mutex;
#define _WEBSOCKETPP_CPP11_FUNCTIONAL_
#define _WEBSOCKETPP_CPP11_THREAD_
#define _WEBSOCKETPP_CPP11_SYSTEM_ERROR_
Concurrency policy that uses std::mutex / boost::mutex.
Stub for user supplied base class.
Stub for user supplied base class.
Stub class for use when disabling permessage_deflate extension.
Stores, parses, and manipulates HTTP requests.
Stores, parses, and manipulates HTTP responses.
Basic logger that outputs to an ostream.
Thread safe stub "random" integer generator.
Server endpoint role based on the given config.
Basic ASIO endpoint socket component.
Asio based endpoint transport component.
connection_hdl get_handle() const
Get the connection handle.
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present)
void set_uri(uri_ptr)
Set uri hook.
config::concurrency_type concurrency_type
transport concurrency policy
void async_shutdown(transport::shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
void set_write_handler(write_handler h)
Sets the write handler.
void set_secure(bool value)
Set whether or not this connection is secure.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
void fatal_error()
Signal transport error.
size_t read_some(char const *buf, size_t len)
Manual input supply (read some)
size_t read_all(char const *buf, size_t len)
Manual input supply (read all)
config::elog_type elog_type
Type of this transport's error logging policy.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
config::alog_type alog_type
Type of this transport's access logging policy.
void async_write(char const *buf, size_t len, transport::write_handler handler)
Asyncronous Transport Write.
size_t readsome(char const *buf, size_t len)
Manual input supply (DEPRECATED)
void init(init_handler handler)
Initialize the connection transport.
timer_ptr set_timer(long, timer_handler)
Call back a function after a period of time.
friend std::istream & operator>>(std::istream &in, type &t)
Overloaded stream input operator.
void set_vector_write_handler(vector_write_handler h)
Sets the vectored write handler.
bool is_secure() const
Tests whether or not the underlying transport is secure.
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
void set_handle(connection_hdl hdl)
Set Connection Handle.
void register_ostream(std::ostream *o)
Register a std::ostream with the transport for writing output.
void async_read_at_least(size_t num_bytes, char *buf, size_t len, read_handler handler)
Initiate an async_read for at least num_bytes bytes into buf.
void async_write(std::vector< buffer > const &bufs, transport::write_handler handler)
Asyncronous Transport Write (scatter-gather)
ptr get_shared()
Get a shared pointer to this component.
connection< config > type
Type of this connection transport component.
transport_con_type::ptr transport_con_ptr
void set_write_handler(write_handler h)
Sets the write handler.
config::concurrency_type concurrency_type
Type of this endpoint's concurrency policy.
void set_shutdown_handler(shutdown_handler h)
Sets the shutdown handler.
bool is_secure() const
Tests whether or not the underlying transport is secure.
void async_connect(transport_con_ptr, uri_ptr, connect_handler cb)
Initiate a new connection.
config::alog_type alog_type
Type of this endpoint's access logging policy.
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
void init_logging(lib::shared_ptr< alog_type > a, lib::shared_ptr< elog_type > e)
Initialize logging.
endpoint type
Type of this endpoint transport component.
iostream::connection< config > transport_con_type
void register_ostream(std::ostream *o)
Register a default output stream.
void set_secure(bool value)
Set whether or not endpoint can create secure connections.
config::elog_type elog_type
Type of this endpoint's error logging policy.
lib::shared_ptr< type > ptr
Type of a pointer to this endpoint transport component.
Concurrency handling support.
Implementation of RFC 7692, the permessage-deflate WebSocket extension.
Stub RNG policy that always returns 0.
Random number generation policies.
Transport policy that uses asio.
Generic transport related errors.
@ pass_through
underlying transport pass through
iostream transport errors
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
@ double_read
async_read called while another async_read was in progress
lib::error_code make_error_code(error::value e)
Get an error code with the given value and the iostream transport category.
Transport policy that uses STL iostream for I/O and does not support timers.
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
lib::function< lib::error_code(connection_hdl)> shutdown_handler
lib::function< lib::error_code(connection_hdl, char const *, size_t)> write_handler
The type and signature of the callback used by iostream transport to write.
Transport policies provide network connectivity and timers.
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
Namespace for the WebSocket++ project.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Server config with asio transport and TLS disabled.
Extension specific settings:
static const uint8_t minimum_outgoing_window_bits
static const bool allow_disabling_context_takeover
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
static const long timeout_connect
Length of time to wait for TCP connect.
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
static const long timeout_socket_pre_init
Default timer values (in ms)
static bool const enable_multithreading
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Server config with iostream transport.
websocketpp::random::none::int_generator< uint32_t > rng_type
RNG policies.
static const websocketpp::log::level elog_level
Default static error logging channels.
websocketpp::transport::iostream::endpoint< transport_config > transport_type
Transport Endpoint Component.
static const size_t max_http_body_size
Default maximum http body size.
static const long timeout_open_handshake
Default timer values (in ms)
static const size_t max_message_size
Default maximum message size.
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
static const websocketpp::log::level alog_level
Default static access logging channels.
static const long timeout_pong
Length of time to wait for a pong after a ping.
static const bool silent_close
Suppresses the return of detailed connection close information.
static bool const enable_multithreading
static const size_t connection_read_buffer_size
Size of the per-connection read buffer.
static const bool enable_extensions
Global flag for enabling/disabling extensions.
static const int client_version
WebSocket Protocol version to use as a client.
Package of log levels for logging access events.
static level const devel
Development messages (warning: very chatty)
static level const all
Special aggregate value representing "all levels".
Package of log levels for logging errors.
static level const devel
Low level debugging information (warning: very chatty)
static level const all
Special aggregate value representing "all levels".