NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
none.hpp
Go to the documentation of this file.
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 
35 
38 
39 #include <sstream>
40 #include <string>
41 
42 namespace websocketpp {
43 namespace transport {
44 namespace asio {
47 namespace basic_socket {
48 
50 typedef lib::function<void(connection_hdl,lib::asio::ip::tcp::socket&)>
52 
54 
58 class connection : public lib::enable_shared_from_this<connection> {
59 public:
61  typedef connection type;
63  typedef lib::shared_ptr<type> ptr;
64 
66  typedef lib::asio::io_service* io_service_ptr;
68  typedef lib::shared_ptr<lib::asio::io_service::strand> strand_ptr;
72  typedef lib::shared_ptr<socket_type> socket_ptr;
73 
74  explicit connection() : m_state(UNINITIALIZED) {
75  //std::cout << "transport::asio::basic_socket::connection constructor"
76  // << std::endl;
77  }
78 
80  ptr get_shared() {
81  return shared_from_this();
82  }
83 
85 
88  bool is_secure() const {
89  return false;
90  }
91 
93 
101  m_socket_init_handler = h;
102  }
103 
105 
109  return *m_socket;
110  }
111 
113 
117  return *m_socket;
118  }
119 
121 
125  return *m_socket;
126  }
127 
129 
138  std::string get_remote_endpoint(lib::error_code & ec) const {
139  std::stringstream s;
140 
141  lib::asio::error_code aec;
142  lib::asio::ip::tcp::endpoint ep = m_socket->remote_endpoint(aec);
143 
144  if (aec) {
146  s << "Error getting remote endpoint: " << aec
147  << " (" << aec.message() << ")";
148  return s.str();
149  } else {
150  ec = lib::error_code();
151  s << ep;
152  return s.str();
153  }
154  }
155 protected:
157 
165  lib::error_code init_asio (io_service_ptr service, strand_ptr, bool)
166  {
167  if (m_state != UNINITIALIZED) {
169  }
170 
171  m_socket = lib::make_shared<lib::asio::ip::tcp::socket>(
172  lib::ref(*service));
173 
174  m_state = READY;
175 
176  return lib::error_code();
177  }
178 
180 
190  void set_uri(uri_ptr) {}
191 
193 
201  void pre_init(init_handler callback) {
202  if (m_state != READY) {
204  return;
205  }
206 
207  if (m_socket_init_handler) {
208  m_socket_init_handler(m_hdl,*m_socket);
209  }
210 
211  m_state = READING;
212 
213  callback(lib::error_code());
214  }
215 
217 
224  void post_init(init_handler callback) {
225  callback(lib::error_code());
226  }
227 
229 
236  m_hdl = hdl;
237  }
238 
240 
248  lib::asio::error_code cancel_socket() {
249  lib::asio::error_code ec;
250  m_socket->cancel(ec);
251  return ec;
252  }
253 
255  lib::asio::error_code ec;
256  m_socket->shutdown(lib::asio::ip::tcp::socket::shutdown_both, ec);
257  h(ec);
258  }
259 
260  lib::error_code get_ec() const {
261  return lib::error_code();
262  }
263 
265 
282  template <typename ErrorCodeType>
283  lib::error_code translate_ec(ErrorCodeType) {
284  // We don't know any more information about this error so pass through
286  }
287 
290  lib::error_code translate_ec(lib::error_code ec) {
291  // We don't know any more information about this error, but the error is
292  // the same type as the one we are translating to, so pass through
293  // untranslated.
294  return ec;
295  }
296 private:
297  enum state {
298  UNINITIALIZED = 0,
299  READY = 1,
300  READING = 2
301  };
302 
303  socket_ptr m_socket;
304  state m_state;
305 
306  connection_hdl m_hdl;
307  socket_init_handler m_socket_init_handler;
308 };
309 
311 
315 class endpoint {
316 public:
318  typedef endpoint type;
319 
325 
326  explicit endpoint() {}
327 
329 
332  bool is_secure() const {
333  return false;
334  }
335 
337 
345  m_socket_init_handler = h;
346  }
347 protected:
349 
357  lib::error_code init(socket_con_ptr scon) {
358  scon->set_socket_init_handler(m_socket_init_handler);
359  return lib::error_code();
360  }
361 private:
362  socket_init_handler m_socket_init_handler;
363 };
364 
365 } // namespace basic_socket
366 } // namespace asio
367 } // namespace transport
368 } // namespace websocketpp
369 
370 #endif // WEBSOCKETPP_TRANSPORT_SECURITY_NONE_HPP
Basic Asio connection socket component.
Definition: none.hpp:58
void pre_init(init_handler callback)
Pre-initialize security policy.
Definition: none.hpp:201
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
void set_socket_init_handler(socket_init_handler h)
Set the socket initialization handler.
Definition: none.hpp:100
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:217
bool is_secure() const
Checks whether the endpoint creates secure connections.
Definition: none.hpp:332
lib::asio::io_service * io_service_ptr
Type of a pointer to the Asio io_service being used.
Definition: none.hpp:66
lib::error_code init_asio(io_service_ptr service, strand_ptr, bool)
Perform one time initializations.
Definition: none.hpp:165
lib::asio::ip::tcp::socket & get_raw_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:124
std::string get_remote_endpoint(lib::error_code &ec) const
Get the remote endpoint address.
Definition: none.hpp:138
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
underlying transport pass through
Definition: connection.hpp:153
void async_shutdown(socket::shutdown_handler h)
Definition: none.hpp:254
lib::asio::ip::tcp::socket & get_socket()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:108
lib::error_code init(socket_con_ptr scon)
Initialize a connection.
Definition: none.hpp:357
lib::error_code translate_ec(lib::error_code ec)
Overload of translate_ec to catch cases where lib::error_code is the same type as lib::asio::error_co...
Definition: none.hpp:290
lib::error_code translate_ec(ErrorCodeType)
Translate any security policy specific information about an error code.
Definition: none.hpp:283
A function was called in a state that it was illegal to do so.
Definition: base.hpp:86
ptr get_shared()
Get a shared pointer to this component.
Definition: none.hpp:80
lib::shared_ptr< socket_type > socket_ptr
Type of a shared pointer to the socket being used.
Definition: none.hpp:72
there was an error in the underlying transport library
Definition: base.hpp:171
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:70
Basic ASIO endpoint socket component.
Definition: none.hpp:315
lib::function< void(lib::asio::error_code const &)> shutdown_handler
Definition: base.hpp:67
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:351
void post_init(init_handler callback)
Post-initialize security policy.
Definition: none.hpp:224
endpoint type
The type of this endpoint socket component.
Definition: none.hpp:318
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection socket component.
Definition: none.hpp:63
void set_handle(connection_hdl hdl)
Sets the connection handle.
Definition: none.hpp:235
lib::asio::error_code cancel_socket()
Cancel all async operations on this socket.
Definition: none.hpp:248
connection socket_con_type
The type of the corresponding connection socket component.
Definition: none.hpp:321
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:68
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:51
lib::asio::ip::tcp::socket & get_next_layer()
Retrieve a pointer to the underlying socket.
Definition: none.hpp:116
connection type
Type of this connection socket component.
Definition: none.hpp:61
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
bool is_secure() const
Check whether or not this connection is secure.
Definition: none.hpp:88
lib::error_code make_error_code(error::value e)
Definition: base.hpp:147
void set_socket_init_handler(socket_init_handler h)
Set socket init handler.
Definition: none.hpp:344
socket_con_type::ptr socket_con_ptr
The type of a shared pointer to the corresponding connection socket component.
Definition: none.hpp:324