websocketpp  0.3.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) 2013, 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 <iostream>
37 #include <string>
38 
39 namespace websocketpp {
40 namespace transport {
41 namespace asio {
44 namespace basic_socket {
45 
47 typedef lib::function<void(connection_hdl,boost::asio::ip::tcp::socket&)>
49 
51 
55 class connection : public lib::enable_shared_from_this<connection> {
56 public:
58  typedef connection type;
60  typedef lib::shared_ptr<type> ptr;
61 
63  typedef boost::asio::io_service* io_service_ptr;
65  typedef lib::shared_ptr<boost::asio::io_service::strand> strand_ptr;
67  typedef boost::asio::ip::tcp::socket socket_type;
69  typedef lib::shared_ptr<socket_type> socket_ptr;
70 
71  explicit connection() : m_state(UNINITIALIZED) {
72  //std::cout << "transport::asio::basic_socket::connection constructor"
73  // << std::endl;
74  }
75 
77  ptr get_shared() {
78  return shared_from_this();
79  }
80 
82 
85  bool is_secure() const {
86  return false;
87  }
88 
90 
98  m_socket_init_handler = h;
99  }
100 
102 
105  boost::asio::ip::tcp::socket& get_socket() {
106  return *m_socket;
107  }
108 
110 
113  boost::asio::ip::tcp::socket& get_next_layer() {
114  return *m_socket;
115  }
116 
118 
121  boost::asio::ip::tcp::socket& get_raw_socket() {
122  return *m_socket;
123  }
124 
126 
135  std::string get_remote_endpoint(lib::error_code &ec) const {
136  std::stringstream s;
137 
138  boost::system::error_code bec;
139  boost::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(bec);
140 
141  if (bec) {
143  s << "Error getting remote endpoint: " << bec
144  << " (" << bec.message() << ")";
145  return s.str();
146  } else {
147  ec = lib::error_code();
148  s << ep;
149  return s.str();
150  }
151  }
152 protected:
154 
162  lib::error_code init_asio (io_service_ptr service, strand_ptr strand,
163  bool is_server)
164  {
165  if (m_state != UNINITIALIZED) {
166  return socket::make_error_code(socket::error::invalid_state);
167  }
168 
169  m_socket.reset(new boost::asio::ip::tcp::socket(*service));
170 
171  m_state = READY;
172 
173  return lib::error_code();
174  }
175 
177 
185  void pre_init(init_handler callback) {
186  if (m_state != READY) {
187  callback(socket::make_error_code(socket::error::invalid_state));
188  return;
189  }
190 
191  if (m_socket_init_handler) {
192  m_socket_init_handler(m_hdl,*m_socket);
193  }
194 
195  m_state = READING;
196 
197  callback(lib::error_code());
198  }
199 
201 
208  void post_init(init_handler callback) {
209  callback(lib::error_code());
210  }
211 
213 
220  m_hdl = hdl;
221  }
222 
224  void cancel_socket() {
225  m_socket->cancel();
226  }
227 
228  void async_shutdown(socket_shutdown_handler h) {
229  boost::system::error_code ec;
230  m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both,ec);
231  h(ec);
232  }
233 
234  lib::error_code get_ec() const {
235  return lib::error_code();
236  }
237 
239 
250  lib::error_code translate_ec(boost::system::error_code ec) {
251  // We don't know any more information about this error so pass through
252  return make_error_code(transport::error::pass_through);
253  }
254 private:
255  enum state {
256  UNINITIALIZED = 0,
257  READY = 1,
258  READING = 2
259  };
260 
261  socket_ptr m_socket;
262  state m_state;
263 
264  connection_hdl m_hdl;
265  socket_init_handler m_socket_init_handler;
266 };
267 
269 
273 class endpoint {
274 public:
276  typedef endpoint type;
277 
283 
284  explicit endpoint() {}
285 
287 
290  bool is_secure() const {
291  return false;
292  }
293 
295 
303  m_socket_init_handler = h;
304  }
305 protected:
307 
315  lib::error_code init(socket_con_ptr scon) {
316  scon->set_socket_init_handler(m_socket_init_handler);
317  return lib::error_code();
318  }
319 private:
320  socket_init_handler m_socket_init_handler;
321 };
322 
323 } // namespace basic_socket
324 } // namespace asio
325 } // namespace transport
326 } // namespace websocketpp
327 
328 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
Basic Boost ASIO connection socket component.
Definition: none.hpp:55
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: none.hpp:185
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: none.hpp:97
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:290
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: none.hpp:135
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
underlying transport pass through
Definition: connection.hpp:152
lib::error_code translate_ec(boost::system::error_code ec)
Translate any security policy specific information about an error code.
Definition: none.hpp:250
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:65
boost::asio::ip::tcp::socket & get_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:105
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: none.hpp:315
void cancel_socket()
Cancel all async operations on this socket.
Definition: none.hpp:224
A function was called in a state that it was illegal to do so.
Definition: base.hpp:85
ptr get_shared()
Get a shared pointer to this component.
Definition: none.hpp:77
boost::asio::ip::tcp::socket & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:121
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the socket being used.
Definition: none.hpp:69
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:273
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: none.hpp:63
void post_init(init_handler callback)
Post-initialize security policy.
Definition: none.hpp:208
boost::asio::ip::tcp::socket socket_type
Type of the ASIO socket being used.
Definition: none.hpp:67
endpoint type
The type of this endpoint socket component.
Definition: none.hpp:276
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: none.hpp:60
lib::error_code init_asio(io_service_ptr service, strand_ptr strand, bool is_server)
Perform one time initializations.
Definition: none.hpp:162
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: none.hpp:219
connection socket_con_type
The type of the corresponding connection socket component.
Definition: none.hpp:279
connection type
Type of this connection socket component.
Definition: none.hpp:58
bool is_secure() const
Check whether or not this connection is secure.
Definition: none.hpp:85
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:48
boost::asio::ip::tcp::socket & get_next_layer()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:113
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: none.hpp:302