websocketpp  0.4.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
none.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_SECURITY_NONE_HPP
29 #define WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
30 
31 #include <websocketpp/common/memory.hpp>
32 #include <websocketpp/transport/asio/security/base.hpp>
33 
34 #include <boost/asio.hpp>
35 
36 #include <string>
37 
38 namespace websocketpp {
39 namespace transport {
40 namespace asio {
43 namespace basic_socket {
44 
46 typedef lib::function<void(connection_hdl,boost::asio::ip::tcp::socket&)>
48 
50 
54 class connection : public lib::enable_shared_from_this<connection> {
55 public:
57  typedef connection type;
59  typedef lib::shared_ptr<type> ptr;
60 
62  typedef boost::asio::io_service* io_service_ptr;
64  typedef lib::shared_ptr<boost::asio::io_service::strand> strand_ptr;
66  typedef boost::asio::ip::tcp::socket socket_type;
68  typedef lib::shared_ptr<socket_type> socket_ptr;
69 
70  explicit connection() : m_state(UNINITIALIZED) {
71  //std::cout << "transport::asio::basic_socket::connection constructor"
72  // << std::endl;
73  }
74 
76  ptr get_shared() {
77  return shared_from_this();
78  }
79 
81 
84  bool is_secure() const {
85  return false;
86  }
87 
89 
97  m_socket_init_handler = h;
98  }
99 
101 
104  boost::asio::ip::tcp::socket& get_socket() {
105  return *m_socket;
106  }
107 
109 
112  boost::asio::ip::tcp::socket& get_next_layer() {
113  return *m_socket;
114  }
115 
117 
120  boost::asio::ip::tcp::socket& get_raw_socket() {
121  return *m_socket;
122  }
123 
125 
134  std::string get_remote_endpoint(lib::error_code &ec) const {
135  std::stringstream s;
136 
137  boost::system::error_code bec;
138  boost::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(bec);
139 
140  if (bec) {
142  s << "Error getting remote endpoint: " << bec
143  << " (" << bec.message() << ")";
144  return s.str();
145  } else {
146  ec = lib::error_code();
147  s << ep;
148  return s.str();
149  }
150  }
151 protected:
153 
161  lib::error_code init_asio (io_service_ptr service, strand_ptr, bool)
162  {
163  if (m_state != UNINITIALIZED) {
164  return socket::make_error_code(socket::error::invalid_state);
165  }
166 
167  m_socket = lib::make_shared<boost::asio::ip::tcp::socket>(
168  lib::ref(*service));
169 
170  m_state = READY;
171 
172  return lib::error_code();
173  }
174 
176 
184  void pre_init(init_handler callback) {
185  if (m_state != READY) {
186  callback(socket::make_error_code(socket::error::invalid_state));
187  return;
188  }
189 
190  if (m_socket_init_handler) {
191  m_socket_init_handler(m_hdl,*m_socket);
192  }
193 
194  m_state = READING;
195 
196  callback(lib::error_code());
197  }
198 
200 
207  void post_init(init_handler callback) {
208  callback(lib::error_code());
209  }
210 
212 
219  m_hdl = hdl;
220  }
221 
223  void cancel_socket() {
224  m_socket->cancel();
225  }
226 
227  void async_shutdown(socket_shutdown_handler h) {
228  boost::system::error_code ec;
229  m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both,ec);
230  h(ec);
231  }
232 
233  lib::error_code get_ec() const {
234  return lib::error_code();
235  }
236 
238 
249  lib::error_code translate_ec(boost::system::error_code) {
250  // We don't know any more information about this error so pass through
251  return make_error_code(transport::error::pass_through);
252  }
253 private:
254  enum state {
255  UNINITIALIZED = 0,
256  READY = 1,
257  READING = 2
258  };
259 
260  socket_ptr m_socket;
261  state m_state;
262 
263  connection_hdl m_hdl;
264  socket_init_handler m_socket_init_handler;
265 };
266 
268 
272 class endpoint {
273 public:
275  typedef endpoint type;
276 
282 
283  explicit endpoint() {}
284 
286 
289  bool is_secure() const {
290  return false;
291  }
292 
294 
302  m_socket_init_handler = h;
303  }
304 protected:
306 
314  lib::error_code init(socket_con_ptr scon) {
315  scon->set_socket_init_handler(m_socket_init_handler);
316  return lib::error_code();
317  }
318 private:
319  socket_init_handler m_socket_init_handler;
320 };
321 
322 } // namespace basic_socket
323 } // namespace asio
324 } // namespace transport
325 } // namespace websocketpp
326 
327 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
Basic Boost ASIO connection socket component.
Definition: none.hpp:54
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: none.hpp:184
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: none.hpp:96
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:236
bool is_secure() const
Checks whether the endpoint creates secure connections.
Definition: none.hpp:289
lib::error_code init_asio(io_service_ptr service, strand_ptr, bool)
Perform one time initializations.
Definition: none.hpp:161
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: none.hpp:134
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
underlying transport pass through
Definition: connection.hpp:152
lib::shared_ptr< boost::asio::io_service::strand > strand_ptr
Type of a pointer to the ASIO io_service strand being used.
Definition: none.hpp:64
boost::asio::ip::tcp::socket & get_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:104
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: none.hpp:314
lib::error_code translate_ec(boost::system::error_code)
Translate any security policy specific information about an error code.
Definition: none.hpp:249
void cancel_socket()
Cancel all async operations on this socket.
Definition: none.hpp:223
A function was called in a state that it was illegal to do so.
Definition: base.hpp:84
ptr get_shared()
Get a shared pointer to this component.
Definition: none.hpp:76
boost::asio::ip::tcp::socket & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:120
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the socket being used.
Definition: none.hpp:68
there was an error in the underlying transport library
Definition: base.hpp:190
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:116
Basic ASIO endpoint socket component.
Definition: none.hpp:272
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: none.hpp:62
void post_init(init_handler callback)
Post-initialize security policy.
Definition: none.hpp:207
boost::asio::ip::tcp::socket socket_type
Type of the ASIO socket being used.
Definition: none.hpp:66
endpoint type
The type of this endpoint socket component.
Definition: none.hpp:275
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: none.hpp:59
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: none.hpp:218
connection socket_con_type
The type of the corresponding connection socket component.
Definition: none.hpp:278
connection type
Type of this connection socket component.
Definition: none.hpp:57
bool is_secure() const
Check whether or not this connection is secure.
Definition: none.hpp:84
lib::function< void(connection_hdl, boost::asio::ip::tcp::socket &)> socket_init_handler
The signature of the socket init handler for this socket policy.
Definition: none.hpp:47
boost::asio::ip::tcp::socket & get_next_layer()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:112
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: none.hpp:301