WebSocket++  0.8.0-dev
C++ 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 
48 /// Empty timer class to stub out for timer functionality that stub
49 /// transport doesn't support
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:
57  /// Type of this connection transport component
58  typedef connection<config> type;
59  /// Type of a shared pointer to this connection transport component
60  typedef lib::shared_ptr<type> ptr;
61 
62  /// transport concurrency policy
63  typedef typename config::concurrency_type concurrency_type;
64  /// Type of this transport's access logging policy
65  typedef typename config::alog_type alog_type;
66  /// Type of this transport's error logging policy
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, const lib::shared_ptr<alog_type> & alog, const lib::shared_ptr<elog_type> & elog)
76  : m_alog(alog), m_elog(elog)
77  {
78  m_alog->write(log::alevel::devel,"stub con transport constructor");
79  }
80 
81  /// Get a shared pointer to this component
83  return type::shared_from_this();
84  }
85 
86  /// Set whether or not this connection is secure
87  /**
88  * Todo: docs
89  *
90  * @since 0.3.0-alpha4
91  *
92  * @param value Whether or not this connection is secure.
93  */
94  void set_secure(bool value) {}
95 
96  /// Tests whether or not the underlying transport is secure
97  /**
98  * TODO: docs
99  *
100  * @return Whether or not the underlying transport is secure
101  */
102  bool is_secure() const {
103  return false;
104  }
105 
106  /// Set uri hook
107  /**
108  * Called by the endpoint as a connection is being established to provide
109  * the uri being connected to to the transport layer.
110  *
111  * Implementation is optional and can be ignored if the transport has no
112  * need for this information.
113  *
114  * @since 0.6.0
115  *
116  * @param u The uri to set
117  */
118  void set_uri(uri_ptr) {}
119 
120  /// Set human readable remote endpoint address
121  /**
122  * Sets the remote endpoint address returned by `get_remote_endpoint`. This
123  * value should be a human readable string that describes the remote
124  * endpoint. Typically an IP address or hostname, perhaps with a port. But
125  * may be something else depending on the nature of the underlying
126  * transport.
127  *
128  * If none is set a default is returned.
129  *
130  * @since 0.3.0-alpha4
131  *
132  * @param value The remote endpoint address to set.
133  */
134  void set_remote_endpoint(std::string value) {}
135 
136  /// Get human readable remote endpoint address
137  /**
138  * TODO: docs
139  *
140  * This value is used in access and error logs and is available to the end
141  * application for including in user facing interfaces and messages.
142  *
143  * @return A string identifying the address of the remote endpoint
144  */
145  std::string get_remote_endpoint() const {
146  return "unknown (stub transport)";
147  }
148 
149  /// Get the connection handle
150  /**
151  * @return The handle for this connection.
152  */
154  return connection_hdl();
155  }
156 
157  /// Call back a function after a period of time.
158  /**
159  * Timers are not implemented in this transport. The timer pointer will
160  * always be empty. The handler will never be called.
161  *
162  * @param duration Length of time to wait in milliseconds
163  * @param callback The function to call back when the timer has expired
164  * @return A handle that can be used to cancel the timer if it is no longer
165  * needed.
166  */
168  return timer_ptr();
169  }
170 protected:
171  /// Initialize the connection transport
172  /**
173  * Initialize the connection's transport component.
174  *
175  * @param handler The `init_handler` to call when initialization is done
176  */
177  void init(init_handler handler) {
178  m_alog->write(log::alevel::devel,"stub connection init");
179  handler(make_error_code(error::not_implemented));
180  }
181 
182  /// Initiate an async_read for at least num_bytes bytes into buf
183  /**
184  * Initiates an async_read request for at least num_bytes bytes. The input
185  * will be read into buf. A maximum of len bytes will be input. When the
186  * operation is complete, handler will be called with the status and number
187  * of bytes read.
188  *
189  * This method may or may not call handler from within the initial call. The
190  * application should be prepared to accept either.
191  *
192  * The application should never call this method a second time before it has
193  * been called back for the first read. If this is done, the second read
194  * will be called back immediately with a double_read error.
195  *
196  * If num_bytes or len are zero handler will be called back immediately
197  * indicating success.
198  *
199  * @param num_bytes Don't call handler until at least this many bytes have
200  * been read.
201  * @param buf The buffer to read bytes into
202  * @param len The size of buf. At maximum, this many bytes will be read.
203  * @param handler The callback to invoke when the operation is complete or
204  * ends in an error
205  */
206  void async_read_at_least(size_t num_bytes, char * buf, size_t len,
207  read_handler handler)
208  {
209  m_alog->write(log::alevel::devel, "stub_con async_read_at_least");
210  handler(make_error_code(error::not_implemented), 0);
211  }
212 
213  /// Asyncronous Transport Write
214  /**
215  * Write len bytes in buf to the output stream. Call handler to report
216  * success or failure. handler may or may not be called during async_write,
217  * but it must be safe for this to happen.
218  *
219  * Will return 0 on success.
220  *
221  * @param buf buffer to read bytes from
222  * @param len number of bytes to write
223  * @param handler Callback to invoke with operation status.
224  */
225  void async_write(char const * buf, size_t len, write_handler handler) {
226  m_alog->write(log::alevel::devel,"stub_con async_write");
227  handler(make_error_code(error::not_implemented));
228  }
229 
230  /// Asyncronous Transport Write (scatter-gather)
231  /**
232  * Write a sequence of buffers to the output stream. Call handler to report
233  * success or failure. handler may or may not be called during async_write,
234  * but it must be safe for this to happen.
235  *
236  * Will return 0 on success.
237  *
238  * @param bufs vector of buffers to write
239  * @param handler Callback to invoke with operation status.
240  */
241  void async_write(std::vector<buffer> const & bufs, write_handler handler) {
242  m_alog->write(log::alevel::devel,"stub_con async_write buffer list");
243  handler(make_error_code(error::not_implemented));
244  }
245 
246  /// Set Connection Handle
247  /**
248  * @param hdl The new handle
249  */
250  void set_handle(connection_hdl hdl) {}
251 
252  /// Call given handler back within the transport's event system (if present)
253  /**
254  * Invoke a callback within the transport's event system if it has one. If
255  * it doesn't, the handler will be invoked immediately before this function
256  * returns.
257  *
258  * @param handler The callback to invoke
259  *
260  * @return Whether or not the transport was able to register the handler for
261  * callback.
262  */
264  handler();
265  return lib::error_code();
266  }
267 
268  /// Perform cleanup on socket shutdown_handler
269  /**
270  * @param h The `shutdown_handler` to call back when complete
271  */
272  void async_shutdown(shutdown_handler handler) {
273  handler(lib::error_code());
274  }
275 private:
276  // member variables!
277  lib::shared_ptr<alog_type> m_alog;
278  lib::shared_ptr<elog_type> m_elog;
279 };
280 
281 
282 } // namespace stub
283 } // namespace transport
284 } // namespace websocketpp
285 
286 #endif // WEBSOCKETPP_TRANSPORT_STUB_CON_HPP
config::concurrency_type concurrency_type
transport concurrency policy
Definition: connection.hpp:63
connection_hdl get_handle() const
Get the connection handle.
Definition: connection.hpp:153
void set_uri(uri_ptr)
Set uri hook.
Definition: connection.hpp:118
void async_write(std::vector< buffer > const &bufs, write_handler handler)
Asyncronous Transport Write (scatter-gather)
Definition: connection.hpp:241
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:206
lib::error_code dispatch(dispatch_handler handler)
Call given handler back within the transport's event system (if present)
Definition: connection.hpp:263
bool is_secure() const
Tests whether or not the underlying transport is secure.
Definition: connection.hpp:102
void set_secure(bool value)
Set whether or not this connection is secure.
Definition: connection.hpp:94
void set_handle(connection_hdl hdl)
Set Connection Handle.
Definition: connection.hpp:250
void async_write(char const *buf, size_t len, write_handler handler)
Asyncronous Transport Write.
Definition: connection.hpp:225
void set_remote_endpoint(std::string value)
Set human readable remote endpoint address.
Definition: connection.hpp:134
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.
Stub transport policy that has no input or output.
Definition: base.hpp:39
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:177
timer_ptr set_timer(long duration, timer_handler handler)
Call back a function after a period of time.
Definition: connection.hpp:167
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:145
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
Definition: endpoint.hpp:72
void async_shutdown(shutdown_handler handler)
Perform cleanup on socket shutdown_handler.
Definition: connection.hpp:272