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_DEBUG_CON_HPP
29#define WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
30
32
34
35#include <websocketpp/uri.hpp>
37
41
42#include <string>
43#include <vector>
44
45namespace websocketpp {
46namespace transport {
47namespace debug {
48
51struct timer {
52 void cancel() {}
53};
54
55template <typename config>
56class connection : public lib::enable_shared_from_this< connection<config> > {
57public:
61 typedef lib::shared_ptr<type> ptr;
62
64 typedef typename config::concurrency_type concurrency_type;
66 typedef typename config::alog_type alog_type;
68 typedef typename config::elog_type elog_type;
69
70 // Concurrency policy types
71 typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
72 typedef typename concurrency_type::mutex_type mutex_type;
73
74 typedef lib::shared_ptr<timer> timer_ptr;
75
76 explicit connection(bool is_server, alog_type & alog, elog_type & elog)
77 : m_reading(false), m_is_server(is_server), m_alog(alog), m_elog(elog)
78 {
79 m_alog.write(log::alevel::devel,"debug con transport constructor");
80 }
81
84 return type::shared_from_this();
85 }
86
88
95 void set_secure(bool) {}
96
98
103 bool is_secure() const {
104 return false;
105 }
106
108
120
122
135 void set_remote_endpoint(std::string) {}
136
138
146 std::string get_remote_endpoint() const {
147 return "unknown (debug transport)";
148 }
149
151
155 return connection_hdl();
156 }
157
159
169 m_alog.write(log::alevel::devel,"debug connection set timer");
170 m_timer_handler = handler;
171 return timer_ptr();
172 }
173
175
190 size_t read_all(char const * buf, size_t len) {
191 size_t total_read = 0;
192 size_t temp_read = 0;
193
194 do {
195 temp_read = this->read_some_impl(buf+total_read,len-total_read);
196 total_read += temp_read;
197 } while (temp_read != 0 && total_read < len);
198
199 return total_read;
200 }
201
202 // debug stuff to invoke the async handlers
203 void expire_timer(lib::error_code const & ec) {
204 m_timer_handler(ec);
205 }
206
208 m_write_handler(lib::error_code());
209 }
210protected:
212
217 void init(init_handler handler) {
218 m_alog.write(log::alevel::devel,"debug connection init");
219 handler(lib::error_code());
220 }
221
223
246 void async_read_at_least(size_t num_bytes, char * buf, size_t len,
247 read_handler handler)
248 {
249 std::stringstream s;
250 s << "debug_con async_read_at_least: " << num_bytes;
251 m_alog.write(log::alevel::devel,s.str());
252
253 if (num_bytes > len) {
254 handler(make_error_code(error::invalid_num_bytes),size_t(0));
255 return;
256 }
257
258 if (m_reading == true) {
259 handler(make_error_code(error::double_read),size_t(0));
260 return;
261 }
262
263 if (num_bytes == 0 || len == 0) {
264 handler(lib::error_code(),size_t(0));
265 return;
266 }
267
268 m_buf = buf;
269 m_len = len;
270 m_bytes_needed = num_bytes;
271 m_read_handler = handler;
272 m_cursor = 0;
273 m_reading = true;
274 }
275
277
288 void async_write(char const *, size_t, write_handler handler) {
289 m_alog.write(log::alevel::devel,"debug_con async_write");
290 m_write_handler = handler;
291 }
292
294
304 void async_write(std::vector<buffer> const &, write_handler handler) {
305 m_alog.write(log::alevel::devel,"debug_con async_write buffer list");
306 m_write_handler = handler;
307 }
308
310
314
316
326 lib::error_code dispatch(dispatch_handler handler) {
327 handler();
328 return lib::error_code();
329 }
330
332
336 handler(lib::error_code());
337 }
338
339 size_t read_some_impl(char const * buf, size_t len) {
340 m_alog.write(log::alevel::devel,"debug_con read_some");
341
342 if (!m_reading) {
343 m_elog.write(log::elevel::devel,"write while not reading");
344 return 0;
345 }
346
347 size_t bytes_to_copy = (std::min)(len,m_len-m_cursor);
348
349 std::copy(buf,buf+bytes_to_copy,m_buf+m_cursor);
350
351 m_cursor += bytes_to_copy;
352
353 if (m_cursor >= m_bytes_needed) {
354 complete_read(lib::error_code());
355 }
356
357 return bytes_to_copy;
358 }
359
361
376 void complete_read(lib::error_code const & ec) {
377 m_reading = false;
378
379 read_handler handler = m_read_handler;
380 m_read_handler = read_handler();
381
382 handler(ec,m_cursor);
383 }
384private:
385 timer_handler m_timer_handler;
386
387 // Read space (Protected by m_read_mutex)
388 char * m_buf;
389 size_t m_len;
390 size_t m_bytes_needed;
391 read_handler m_read_handler;
392 size_t m_cursor;
393
394 // transport resources
395 connection_hdl m_connection_hdl;
396 write_handler m_write_handler;
397 shutdown_handler m_shutdown_handler;
398
399 bool m_reading;
400 bool const m_is_server;
401 bool m_is_secure;
402 alog_type & m_alog;
403 elog_type & m_elog;
404 std::string m_remote_endpoint;
405};
406
407
408} // namespace debug
409} // namespace transport
410} // namespace websocketpp
411
412#endif // WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
void expire_timer(lib::error_code const &ec)
ptr get_shared()
Get a shared pointer to this component.
void complete_read(lib::error_code const &ec)
Signal that a requested read is complete.
void init(init_handler handler)
Initialize the connection transport.
concurrency_type::mutex_type mutex_type
connection_hdl get_handle() const
Get the connection handle.
size_t read_all(char const *buf, size_t len)
Manual input supply (read all)
void set_remote_endpoint(std::string)
Set human readable remote endpoint address.
config::concurrency_type concurrency_type
transport concurrency policy
bool is_secure() const
Tests whether or not the underlying transport is secure.
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present)
timer_ptr set_timer(long, timer_handler handler)
Call back a function after a period of time.
connection< config > type
Type of this connection transport component.
void set_secure(bool)
Set whether or not this connection is secure.
size_t read_some_impl(char const *buf, size_t len)
config::elog_type elog_type
Type of this transport's error logging policy.
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
concurrency_type::scoped_lock_type scoped_lock_type
config::alog_type alog_type
Type of this transport's access logging policy.
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_shutdown(shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
connection(bool is_server, alog_type &alog, elog_type &elog)
void async_write(char const *, size_t, write_handler handler)
Asyncronous Transport Write.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
void async_write(std::vector< buffer > const &, write_handler handler)
Asyncronous Transport Write (scatter-gather)
void set_handle(connection_hdl)
Set Connection Handle.
bool debug
#define elog(FORMAT,...)
Definition logger.hpp:130
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
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
char * s
size_t len
uint8_t buf[2048]