websocketpp  0.5.1
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/transport/asio/security/base.hpp>
32 
33 #include <websocketpp/common/memory.hpp>
34 
35 #include <boost/asio.hpp>
36 
37 #include <sstream>
38 #include <string>
39 
40 namespace websocketpp {
41 namespace transport {
42 namespace asio {
45 namespace basic_socket {
46 
48 typedef lib::function<void(connection_hdl,boost::asio::ip::tcp::socket&)>
50 
52 
56 class connection : public lib::enable_shared_from_this<connection> {
57 public:
59  typedef connection type;
61  typedef lib::shared_ptr<type> ptr;
62 
64  typedef boost::asio::io_service* io_service_ptr;
66  typedef lib::shared_ptr<boost::asio::io_service::strand> strand_ptr;
68  typedef boost::asio::ip::tcp::socket socket_type;
70  typedef lib::shared_ptr<socket_type> socket_ptr;
71 
72  explicit connection() : m_state(UNINITIALIZED) {
73  //std::cout << "transport::asio::basic_socket::connection constructor"
74  // << std::endl;
75  }
76 
78  ptr get_shared() {
79  return shared_from_this();
80  }
81 
83 
86  bool is_secure() const {
87  return false;
88  }
89 
91 
99  m_socket_init_handler = h;
100  }
101 
103 
106  boost::asio::ip::tcp::socket& get_socket() {
107  return *m_socket;
108  }
109 
111 
114  boost::asio::ip::tcp::socket& get_next_layer() {
115  return *m_socket;
116  }
117 
119 
122  boost::asio::ip::tcp::socket& get_raw_socket() {
123  return *m_socket;
124  }
125 
127 
136  std::string get_remote_endpoint(lib::error_code &ec) const {
137  std::stringstream s;
138 
139  boost::system::error_code bec;
140  boost::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(bec);
141 
142  if (bec) {
144  s << "Error getting remote endpoint: " << bec
145  << " (" << bec.message() << ")";
146  return s.str();
147  } else {
148  ec = lib::error_code();
149  s << ep;
150  return s.str();
151  }
152  }
153 protected:
155 
163  lib::error_code init_asio (io_service_ptr service, strand_ptr, bool)
164  {
165  if (m_state != UNINITIALIZED) {
166  return socket::make_error_code(socket::error::invalid_state);
167  }
168 
169  m_socket = lib::make_shared<boost::asio::ip::tcp::socket>(
170  lib::ref(*service));
171 
172  m_state = READY;
173 
174  return lib::error_code();
175  }
176 
178 
186  void pre_init(init_handler callback) {
187  if (m_state != READY) {
188  callback(socket::make_error_code(socket::error::invalid_state));
189  return;
190  }
191 
192  if (m_socket_init_handler) {
193  m_socket_init_handler(m_hdl,*m_socket);
194  }
195 
196  m_state = READING;
197 
198  callback(lib::error_code());
199  }
200 
202 
209  void post_init(init_handler callback) {
210  callback(lib::error_code());
211  }
212 
214 
221  m_hdl = hdl;
222  }
223 
225  void cancel_socket() {
226  m_socket->cancel();
227  }
228 
229  void async_shutdown(socket_shutdown_handler h) {
230  boost::system::error_code ec;
231  m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both,ec);
232  h(ec);
233  }
234 
235  lib::error_code get_ec() const {
236  return lib::error_code();
237  }
238 
240 
251  lib::error_code translate_ec(boost::system::error_code) {
252  // We don't know any more information about this error so pass through
253  return make_error_code(transport::error::pass_through);
254  }
255 private:
256  enum state {
257  UNINITIALIZED = 0,
258  READY = 1,
259  READING = 2
260  };
261 
262  socket_ptr m_socket;
263  state m_state;
264 
265  connection_hdl m_hdl;
266  socket_init_handler m_socket_init_handler;
267 };
268 
270 
274 class endpoint {
275 public:
277  typedef endpoint type;
278 
284 
285  explicit endpoint() {}
286 
288 
291  bool is_secure() const {
292  return false;
293  }
294 
296 
304  m_socket_init_handler = h;
305  }
306 protected:
308 
316  lib::error_code init(socket_con_ptr scon) {
317  scon->set_socket_init_handler(m_socket_init_handler);
318  return lib::error_code();
319  }
320 private:
321  socket_init_handler m_socket_init_handler;
322 };
323 
324 } // namespace basic_socket
325 } // namespace asio
326 } // namespace transport
327 } // namespace websocketpp
328 
329 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
Basic Boost ASIO connection socket component.
Definition: none.hpp:56
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: none.hpp:186
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: none.hpp:98
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:291
lib::error_code init_asio(io_service_ptr service, strand_ptr, bool)
Perform one time initializations.
Definition: none.hpp:163
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: none.hpp:136
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
underlying transport pass through
Definition: connection.hpp:153
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:66
boost::asio::ip::tcp::socket & get_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:106
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: none.hpp:316
lib::error_code translate_ec(boost::system::error_code)
Translate any security policy specific information about an error code.
Definition: none.hpp:251
void cancel_socket()
Cancel all async operations on this socket.
Definition: none.hpp:225
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:78
boost::asio::ip::tcp::socket & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:122
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the socket being used.
Definition: none.hpp:70
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:117
Basic ASIO endpoint socket component.
Definition: none.hpp:274
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: none.hpp:64
void post_init(init_handler callback)
Post-initialize security policy.
Definition: none.hpp:209
boost::asio::ip::tcp::socket socket_type
Type of the ASIO socket being used.
Definition: none.hpp:68
endpoint type
The type of this endpoint socket component.
Definition: none.hpp:277
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: none.hpp:61
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: none.hpp:220
connection socket_con_type
The type of the corresponding connection socket component.
Definition: none.hpp:280
connection type
Type of this connection socket component.
Definition: none.hpp:59
bool is_secure() const
Check whether or not this connection is secure.
Definition: none.hpp:86
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:49
boost::asio::ip::tcp::socket & get_next_layer()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:114
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: none.hpp:303