websocketpp  0.6.0
C++/Boost Asio based websocket client/server library
none.hpp
1 /*
2  * Copyright (c) 2015, 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_SECURITY_NONE_HPP
29 #define WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
30 
31 #include <websocketpp/uri.hpp>
32 
33 #include <websocketpp/transport/asio/security/base.hpp>
34 
35 #include <websocketpp/common/asio.hpp>
36 #include <websocketpp/common/memory.hpp>
37 
38 #include <sstream>
39 #include <string>
40 
41 namespace websocketpp {
42 namespace transport {
43 namespace asio {
46 namespace basic_socket {
47 
49 typedef lib::function<void(connection_hdl,lib::asio::ip::tcp::socket&)>
51 
53 
57 class connection : public lib::enable_shared_from_this<connection> {
58 public:
60  typedef connection type;
62  typedef lib::shared_ptr<type> ptr;
63 
65  typedef lib::asio::io_service* io_service_ptr;
67  typedef lib::shared_ptr<lib::asio::io_service::strand> strand_ptr;
69  typedef lib::asio::ip::tcp::socket socket_type;
71  typedef lib::shared_ptr<socket_type> socket_ptr;
72 
73  explicit connection() : m_state(UNINITIALIZED) {
74  //std::cout << "transport::asio::basic_socket::connection constructor"
75  // << std::endl;
76  }
77 
79  ptr get_shared() {
80  return shared_from_this();
81  }
82 
84 
87  bool is_secure() const {
88  return false;
89  }
90 
92 
100  m_socket_init_handler = h;
101  }
102 
104 
107  lib::asio::ip::tcp::socket & get_socket() {
108  return *m_socket;
109  }
110 
112 
115  lib::asio::ip::tcp::socket & get_next_layer() {
116  return *m_socket;
117  }
118 
120 
123  lib::asio::ip::tcp::socket & get_raw_socket() {
124  return *m_socket;
125  }
126 
128 
137  std::string get_remote_endpoint(lib::error_code & ec) const {
138  std::stringstream s;
139 
140  lib::asio::error_code aec;
141  lib::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(aec);
142 
143  if (aec) {
145  s << "Error getting remote endpoint: " << aec
146  << " (" << aec.message() << ")";
147  return s.str();
148  } else {
149  ec = lib::error_code();
150  s << ep;
151  return s.str();
152  }
153  }
154 protected:
156 
164  lib::error_code init_asio (io_service_ptr service, strand_ptr, bool)
165  {
166  if (m_state != UNINITIALIZED) {
167  return socket::make_error_code(socket::error::invalid_state);
168  }
169 
170  m_socket = lib::make_shared<lib::asio::ip::tcp::socket>(
171  lib::ref(*service));
172 
173  m_state = READY;
174 
175  return lib::error_code();
176  }
177 
179 
189  void set_uri(uri_ptr) {}
190 
192 
200  void pre_init(init_handler callback) {
201  if (m_state != READY) {
202  callback(socket::make_error_code(socket::error::invalid_state));
203  return;
204  }
205 
206  if (m_socket_init_handler) {
207  m_socket_init_handler(m_hdl,*m_socket);
208  }
209 
210  m_state = READING;
211 
212  callback(lib::error_code());
213  }
214 
216 
223  void post_init(init_handler callback) {
224  callback(lib::error_code());
225  }
226 
228 
235  m_hdl = hdl;
236  }
237 
239  void cancel_socket() {
240  m_socket->cancel();
241  }
242 
243  void async_shutdown(socket_shutdown_handler h) {
244  lib::asio::error_code ec;
245  m_socket->shutdown(lib::asio::ip::tcp::socket::shutdown_both, ec);
246  h(ec);
247  }
248 
249  lib::error_code get_ec() const {
250  return lib::error_code();
251  }
252 
254 
265  lib::error_code translate_ec(lib::asio::error_code) {
266  // We don't know any more information about this error so pass through
267  return make_error_code(transport::error::pass_through);
268  }
269 private:
270  enum state {
271  UNINITIALIZED = 0,
272  READY = 1,
273  READING = 2
274  };
275 
276  socket_ptr m_socket;
277  state m_state;
278 
279  connection_hdl m_hdl;
280  socket_init_handler m_socket_init_handler;
281 };
282 
284 
288 class endpoint {
289 public:
291  typedef endpoint type;
292 
298 
299  explicit endpoint() {}
300 
302 
305  bool is_secure() const {
306  return false;
307  }
308 
310 
318  m_socket_init_handler = h;
319  }
320 protected:
322 
330  lib::error_code init(socket_con_ptr scon) {
331  scon->set_socket_init_handler(m_socket_init_handler);
332  return lib::error_code();
333  }
334 private:
335  socket_init_handler m_socket_init_handler;
336 };
337 
338 } // namespace basic_socket
339 } // namespace asio
340 } // namespace transport
341 } // namespace websocketpp
342 
343 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
Basic Asio connection socket component.
Definition: none.hpp:57
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: none.hpp:200
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: none.hpp:99
lib::error_code make_error_code(error::value e)
Create an error code with the given value and the asio transport category.
Definition: base.hpp:220
bool is_secure() const
Checks whether the endpoint creates secure connections.
Definition: none.hpp:305
lib::asio::io_service * io_service_ptr
Type of a pointer to the Asio io_service being used.
Definition: none.hpp:65
lib::error_code init_asio(io_service_ptr service, strand_ptr, bool)
Perform one time initializations.
Definition: none.hpp:164
lib::asio::ip::tcp::socket & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:123
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: none.hpp:137
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
underlying transport pass through
Definition: connection.hpp:153
lib::asio::ip::tcp::socket & get_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:107
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: none.hpp:330
void cancel_socket()
Cancel all async operations on this socket.
Definition: none.hpp:239
A function was called in a state that it was illegal to do so.
Definition: base.hpp:83
ptr get_shared()
Get a shared pointer to this component.
Definition: none.hpp:79
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the socket being used.
Definition: none.hpp:71
there was an error in the underlying transport library
Definition: base.hpp:174
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
lib::asio::ip::tcp::socket socket_type
Type of the ASIO socket being used.
Definition: none.hpp:69
Basic ASIO endpoint socket component.
Definition: none.hpp:288
lib::error_code translate_ec(lib::asio::error_code)
Translate any security policy specific information about an error code.
Definition: none.hpp:265
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:350
void post_init(init_handler callback)
Post-initialize security policy.
Definition: none.hpp:223
endpoint type
The type of this endpoint socket component.
Definition: none.hpp:291
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: none.hpp:62
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: none.hpp:234
connection socket_con_type
The type of the corresponding connection socket component.
Definition: none.hpp:294
lib::shared_ptr< lib::asio::io_service::strand > strand_ptr
Type of a pointer to the Asio io_service strand being used.
Definition: none.hpp:67
lib::function< void(connection_hdl, lib::asio::ip::tcp::socket &)> socket_init_handler
The signature of the socket init handler for this socket policy.
Definition: none.hpp:50
lib::asio::ip::tcp::socket & get_next_layer()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:115
connection type
Type of this connection socket component.
Definition: none.hpp:60
bool is_secure() const
Check whether or not this connection is secure.
Definition: none.hpp:87
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: none.hpp:317