websocketpp  0.5.1
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
connection.hpp
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_STUB_CON_HPP
29 #define WEBSOCKETPP_TRANSPORT_STUB_CON_HPP
30 
31 #include <websocketpp/transport/stub/base.hpp>
32 
33 #include <websocketpp/transport/base/connection.hpp>
34 
35 #include <websocketpp/logger/levels.hpp>
36 
37 #include <websocketpp/common/connection_hdl.hpp>
38 #include <websocketpp/common/memory.hpp>
39 #include <websocketpp/common/platforms.hpp>
40 
41 #include <string>
42 #include <vector>
43 
44 namespace websocketpp {
45 namespace transport {
46 namespace stub {
47 
50 struct timer {
51  void cancel() {}
52 };
53 
54 template <typename config>
55 class connection : public lib::enable_shared_from_this< connection<config> > {
56 public:
60  typedef lib::shared_ptr<type> ptr;
61 
63  typedef typename config::concurrency_type concurrency_type;
65  typedef typename config::alog_type alog_type;
67  typedef typename config::elog_type elog_type;
68 
69  // Concurrency policy types
70  typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
71  typedef typename concurrency_type::mutex_type mutex_type;
72 
73  typedef lib::shared_ptr<timer> timer_ptr;
74 
75  explicit connection(bool is_server, alog_type & alog, elog_type & elog)
76  : m_alog(alog), m_elog(elog)
77  {
78  m_alog.write(log::alevel::devel,"stub con transport constructor");
79  }
80 
82  ptr get_shared() {
83  return type::shared_from_this();
84  }
85 
87 
94  void set_secure(bool value) {}
95 
97 
102  bool is_secure() const {
103  return false;
104  }
105 
107 
120  void set_remote_endpoint(std::string value) {}
121 
123 
131  std::string get_remote_endpoint() const {
132  return "unknown (stub transport)";
133  }
134 
136 
140  return connection_hdl();
141  }
142 
144 
153  timer_ptr set_timer(long duration, timer_handler handler) {
154  return timer_ptr();
155  }
156 protected:
158 
163  void init(init_handler handler) {
164  m_alog.write(log::alevel::devel,"stub connection init");
165  handler(make_error_code(error::not_implemented));
166  }
167 
169 
192  void async_read_at_least(size_t num_bytes, char * buf, size_t len,
193  read_handler handler)
194  {
195  m_alog.write(log::alevel::devel, "stub_con async_read_at_least");
196  handler(make_error_code(error::not_implemented), 0);
197  }
198 
200 
211  void async_write(char const * buf, size_t len, write_handler handler) {
212  m_alog.write(log::alevel::devel,"stub_con async_write");
213  handler(make_error_code(error::not_implemented));
214  }
215 
217 
227  void async_write(std::vector<buffer> const & bufs, write_handler handler) {
228  m_alog.write(log::alevel::devel,"stub_con async_write buffer list");
229  handler(make_error_code(error::not_implemented));
230  }
231 
233 
237 
239 
249  lib::error_code dispatch(dispatch_handler handler) {
250  handler();
251  return lib::error_code();
252  }
253 
255 
259  handler(lib::error_code());
260  }
261 private:
262  // member variables!
263  alog_type & m_alog;
264  elog_type & m_elog;
265 };
266 
267 
268 } // namespace stub
269 } // namespace transport
270 } // namespace websocketpp
271 
272 #endif // WEBSOCKETPP_TRANSPORT_STUB_CON_HPP
config::concurrency_type concurrency_type
transport concurrency policy
Definition: connection.hpp:63
uint16_t value
The type of a close code value.
Definition: close.hpp:49
connection_hdl get_handle() const
Get the connection handle.
Definition: connection.hpp:139
void async_write(std::vector< buffer > const &bufs, write_handler handler)
Asyncronous Transport Write (scatter-gather)
Definition: connection.hpp:227
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.
Definition: connection.hpp:192
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present)
Definition: connection.hpp:249
bool is_secure() const
Tests whether or not the underlying transport is secure.
Definition: connection.hpp:102
lib::function< void(lib::error_code const &)> write_handler
The type and signature of the callback passed to the write method.
Definition: connection.hpp:123
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::function< void(lib::error_code const &, size_t)> read_handler
The type and signature of the callback passed to the read method.
Definition: connection.hpp:120
void set_secure(bool value)
Set whether or not this connection is secure.
Definition: connection.hpp:94
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
void set_handle(connection_hdl hdl)
Set Connection Handle.
Definition: connection.hpp:236
void async_write(char const *buf, size_t len, write_handler handler)
Asyncronous Transport Write.
Definition: connection.hpp:211
void set_remote_endpoint(std::string value)
Set human readable remote endpoint address.
Definition: connection.hpp:120
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
Definition: connection.hpp:135
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
Definition: connection.hpp:60
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
Definition: connection.hpp:126
lib::function< void(lib::error_code const &)> shutdown_handler
The type and signature of the callback passed to the shutdown method.
Definition: connection.hpp:129
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
lib::function< void(lib::error_code const &)> init_handler
The type and signature of the callback passed to the init hook.
Definition: connection.hpp:117
ptr get_shared()
Get a shared pointer to this component.
Definition: connection.hpp:82
connection< config > type
Type of this connection transport component.
Definition: connection.hpp:58
void init(init_handler handler)
Initialize the connection transport.
Definition: connection.hpp:163
timer_ptr set_timer(long duration, timer_handler handler)
Call back a function after a period of time.
Definition: connection.hpp:153
config::elog_type elog_type
Type of this transport's error logging policy.
Definition: connection.hpp:67
config::alog_type alog_type
Type of this transport's access logging policy.
Definition: connection.hpp:65
std::string get_remote_endpoint() const
Get human readable remote endpoint address.
Definition: connection.hpp:131
void async_shutdown(shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
Definition: connection.hpp:258