Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
connection.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#ifndef WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
29#define WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
30
32
34
35#include <websocketpp/uri.hpp>
36
38
42
43#include <algorithm>
44#include <iostream>
45#include <sstream>
46#include <string>
47#include <vector>
48
49namespace websocketpp {
50namespace transport {
51namespace iostream {
52
55struct timer {
56 void cancel() {}
57};
58
59template <typename config>
60class connection : public lib::enable_shared_from_this< connection<config> > {
61public:
65 typedef lib::shared_ptr<type> ptr;
66
68 typedef typename config::concurrency_type concurrency_type;
70 typedef typename config::alog_type alog_type;
72 typedef typename config::elog_type elog_type;
73
74 // Concurrency policy types
75 typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
76 typedef typename concurrency_type::mutex_type mutex_type;
77
78 typedef lib::shared_ptr<timer> timer_ptr;
79
80 explicit connection(bool is_server, alog_type & alog, elog_type & elog)
81 : m_output_stream(NULL)
82 , m_reading(false)
83 , m_is_server(is_server)
84 , m_is_secure(false)
85 , m_alog(alog)
86 , m_elog(elog)
87 , m_remote_endpoint("iostream transport")
88 {
89 m_alog.write(log::alevel::devel,"iostream con transport constructor");
90 }
91
94 return type::shared_from_this();
95 }
96
98
104 void register_ostream(std::ostream * o) {
105 // TODO: lock transport state?
106 scoped_lock_type lock(m_read_mutex);
107 m_output_stream = o;
108 }
109
111
122
124
142 friend std::istream & operator>> (std::istream & in, type & t) {
143 // this serializes calls to external read.
144 scoped_lock_type lock(t.m_read_mutex);
145
146 t.read(in);
147
148 return in;
149 }
150
152
165 size_t read_some(char const * buf, size_t len) {
166 // this serializes calls to external read.
167 scoped_lock_type lock(m_read_mutex);
168
169 return this->read_some_impl(buf,len);
170 }
171
173
188 size_t read_all(char const * buf, size_t len) {
189 // this serializes calls to external read.
190 scoped_lock_type lock(m_read_mutex);
191
192 size_t total_read = 0;
193 size_t temp_read = 0;
194
195 do {
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);
199
200 return total_read;
201 }
202
204
208 size_t readsome(char const * buf, size_t len) {
209 return this->read_some(buf,len);
210 }
211
213
219 void eof() {
220 // this serializes calls to external read.
221 scoped_lock_type lock(m_read_mutex);
222
223 if (m_reading) {
224 complete_read(make_error_code(transport::error::eof));
225 }
226 }
227
229
235 void fatal_error() {
236 // this serializes calls to external read.
237 scoped_lock_type lock(m_read_mutex);
238
239 if (m_reading) {
240 complete_read(make_error_code(transport::error::pass_through));
241 }
242 }
243
245
257 void set_secure(bool value) {
258 m_is_secure = value;
259 }
260
262
271 bool is_secure() const {
272 return m_is_secure;
273 }
274
276
289 void set_remote_endpoint(std::string value) {
290 m_remote_endpoint = value;
291 }
292
294
305 std::string get_remote_endpoint() const {
306 return m_remote_endpoint;
307 }
308
310
314 return m_connection_hdl;
315 }
316
318
328 return timer_ptr();
329 }
330
332
351 m_write_handler = h;
352 }
353
355
381 m_vector_write_handler = h;
382 }
383
385
401 m_shutdown_handler = h;
402 }
403protected:
405
410 void init(init_handler handler) {
411 m_alog.write(log::alevel::devel,"iostream connection init");
412 handler(lib::error_code());
413 }
414
416
439 void async_read_at_least(size_t num_bytes, char *buf, size_t len,
440 read_handler handler)
441 {
442 std::stringstream s;
443 s << "iostream_con async_read_at_least: " << num_bytes;
444 m_alog.write(log::alevel::devel,s.str());
445
446 if (num_bytes > len) {
447 handler(make_error_code(error::invalid_num_bytes),size_t(0));
448 return;
449 }
450
451 if (m_reading == true) {
452 handler(make_error_code(error::double_read),size_t(0));
453 return;
454 }
455
456 if (num_bytes == 0 || len == 0) {
457 handler(lib::error_code(),size_t(0));
458 return;
459 }
460
461 m_buf = buf;
462 m_len = len;
463 m_bytes_needed = num_bytes;
464 m_read_handler = handler;
465 m_cursor = 0;
466 m_reading = true;
467 }
468
470
487 void async_write(char const * buf, size_t len, transport::write_handler
488 handler)
489 {
490 m_alog.write(log::alevel::devel,"iostream_con async_write");
491 // TODO: lock transport state?
492
493 lib::error_code ec;
494
495 if (m_output_stream) {
496 m_output_stream->write(buf,len);
497
498 if (m_output_stream->bad()) {
499 ec = make_error_code(error::bad_stream);
500 }
501 } else if (m_write_handler) {
502 ec = m_write_handler(m_connection_hdl, buf, len);
503 } else {
504 ec = make_error_code(error::output_stream_required);
505 }
506
507 handler(ec);
508 }
509
511
527 void async_write(std::vector<buffer> const & bufs, transport::write_handler
528 handler)
529 {
530 m_alog.write(log::alevel::devel,"iostream_con async_write buffer list");
531 // TODO: lock transport state?
532
533 lib::error_code ec;
534
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);
539
540 if (m_output_stream->bad()) {
541 ec = make_error_code(error::bad_stream);
542 break;
543 }
544 }
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);
551 if (ec) {break;}
552 }
553
554 } else {
555 ec = make_error_code(error::output_stream_required);
556 }
557
558 handler(ec);
559 }
560
562
566 m_connection_hdl = hdl;
567 }
568
570
580 lib::error_code dispatch(dispatch_handler handler) {
581 handler();
582 return lib::error_code();
583 }
584
586
594 lib::error_code ec;
595
596 if (m_shutdown_handler) {
597 ec = m_shutdown_handler(m_connection_hdl);
598 }
599
600 handler(ec);
601 }
602private:
603 void read(std::istream &in) {
604 m_alog.write(log::alevel::devel,"iostream_con read");
605
606 while (in.good()) {
607 if (!m_reading) {
608 m_elog.write(log::elevel::devel,"write while not reading");
609 break;
610 }
611
612 in.read(m_buf+m_cursor,static_cast<std::streamsize>(m_len-m_cursor));
613
614 if (in.gcount() == 0) {
615 m_elog.write(log::elevel::devel,"read zero bytes");
616 break;
617 }
618
619 m_cursor += static_cast<size_t>(in.gcount());
620
621 // TODO: error handling
622 if (in.bad()) {
623 m_reading = false;
624 complete_read(make_error_code(error::bad_stream));
625 }
626
627 if (m_cursor >= m_bytes_needed) {
628 m_reading = false;
629 complete_read(lib::error_code());
630 }
631 }
632 }
633
634 size_t read_some_impl(char const * buf, size_t len) {
635 m_alog.write(log::alevel::devel,"iostream_con read_some");
636
637 if (!m_reading) {
638 m_elog.write(log::elevel::devel,"write while not reading");
639 return 0;
640 }
641
642 size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
643
644 std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
645
646 m_cursor += bytes_to_copy;
647
648 if (m_cursor >= m_bytes_needed) {
649 complete_read(lib::error_code());
650 }
651
652 return bytes_to_copy;
653 }
654
656
671 void complete_read(lib::error_code const & ec) {
672 m_reading = false;
673
674 read_handler handler = m_read_handler;
675 m_read_handler = read_handler();
676
677 handler(ec,m_cursor);
678 }
679
680 // Read space (Protected by m_read_mutex)
681 char * m_buf;
682 size_t m_len;
683 size_t m_bytes_needed;
684 read_handler m_read_handler;
685 size_t m_cursor;
686
687 // transport resources
688 std::ostream * m_output_stream;
689 connection_hdl m_connection_hdl;
690 write_handler m_write_handler;
691 vector_write_handler m_vector_write_handler;
692 shutdown_handler m_shutdown_handler;
693
694 bool m_reading;
695 bool const m_is_server;
696 bool m_is_secure;
697 alog_type & m_alog;
698 elog_type & m_elog;
699 std::string m_remote_endpoint;
700
701 // This lock ensures that only one thread can edit read data for this
702 // connection. This is a very coarse lock that is basically locked all the
703 // time. The nature of the connection is such that it cannot be
704 // parallelized, the locking is here to prevent intra-connection concurrency
705 // in order to allow inter-connection concurrency.
706 mutex_type m_read_mutex;
707};
708
709
710} // namespace iostream
711} // namespace transport
712} // namespace websocketpp
713
714#endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
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)
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.
concurrency_type::mutex_type mutex_type
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)
connection(bool is_server, alog_type &alog, elog_type &elog)
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.
void set_remote_endpoint(std::string value)
Set human readable remote endpoint address.
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)
concurrency_type::scoped_lock_type scoped_lock_type
ptr get_shared()
Get a shared pointer to this component.
connection< config > type
Type of this connection transport component.
#define elog(FORMAT,...)
Definition logger.hpp:130
@ pass_through
underlying transport pass through
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
Definition base.hpp:71
@ double_read
async_read called while another async_read was in progress
Definition base.hpp:74
lib::function< lib::error_code(connection_hdl, std::vector< transport::buffer > const &bufs)> vector_write_handler
Definition base.hpp:57
lib::function< lib::error_code(connection_hdl)> shutdown_handler
Definition base.hpp:61
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.
Definition base.hpp:48
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 &)> timer_handler
The type and signature of the callback passed to the read 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.
Definition base64.hpp:41
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:351
#define value
Definition pkcs11.h:157
static level const devel
Development messages (warning: very chatty)
Definition levels.hpp:141
static level const devel
Low level debugging information (warning: very chatty)
Definition levels.hpp:63
void lock()
char * s
size_t len
uint8_t buf[2048]