NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
base.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_ASIO_BASE_HPP
29 #define WEBSOCKETPP_TRANSPORT_ASIO_BASE_HPP
30 
36 
37 #include <string>
38 
39 namespace websocketpp {
40 namespace transport {
42 
46 namespace asio {
47 
48 // Class to manage the memory to be used for handler-based custom allocation.
49 // It contains a single block of memory which may be returned for allocation
50 // requests. If the memory is in use when an allocation request is made, the
51 // allocator delegates allocation to the global heap.
53 public:
54  static const size_t size = 1024;
55 
56  handler_allocator() : m_in_use(false) {}
57 
58 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
59  handler_allocator(handler_allocator const & cpy) = delete;
60  handler_allocator & operator =(handler_allocator const &) = delete;
61 #endif
62 
63  void * allocate(std::size_t memsize) {
64  if (!m_in_use && memsize < size) {
65  m_in_use = true;
66  return static_cast<void*>(&m_storage);
67  } else {
68  return ::operator new(memsize);
69  }
70  }
71 
72  void deallocate(void * pointer) {
73  if (pointer == &m_storage) {
74  m_in_use = false;
75  } else {
76  ::operator delete(pointer);
77  }
78  }
79 
80 private:
81  // Storage space used for handler-based custom memory allocation.
82  lib::aligned_storage<size>::type m_storage;
83 
84  // Whether the handler-based custom allocation storage has been used.
85  bool m_in_use;
86 };
87 
88 // Wrapper class template for handler objects to allow handler memory
89 // allocation to be customised. Calls to operator() are forwarded to the
90 // encapsulated handler.
91 template <typename Handler>
93 public:
95  : allocator_(a),
96  handler_(h)
97  {}
98 
99  template <typename Arg1>
100  void operator()(Arg1 arg1) {
101  handler_(arg1);
102  }
103 
104  template <typename Arg1, typename Arg2>
105  void operator()(Arg1 arg1, Arg2 arg2) {
106  handler_(arg1, arg2);
107  }
108 
110  custom_alloc_handler<Handler> * this_handler)
111  {
112  return this_handler->allocator_.allocate(size);
113  }
114 
115  friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/,
116  custom_alloc_handler<Handler> * this_handler)
117  {
118  this_handler->allocator_.deallocate(pointer);
119  }
120 
121 private:
122  handler_allocator & allocator_;
123  Handler handler_;
124 };
125 
126 // Helper function to wrap a handler object to add custom allocation.
127 template <typename Handler>
129  handler_allocator & a, Handler h)
130 {
131  return custom_alloc_handler<Handler>(a, h);
132 }
133 
134 
135 
136 
137 
138 
139 
140 // Forward declaration of class endpoint so that it can be friended/referenced
141 // before being included.
142 template <typename config>
143 class endpoint;
144 
145 typedef lib::function<void (lib::asio::error_code const & ec,
146  size_t bytes_transferred)> async_read_handler;
147 
148 typedef lib::function<void (lib::asio::error_code const & ec,
149  size_t bytes_transferred)> async_write_handler;
150 
151 typedef lib::function<void (lib::error_code const & ec)> pre_init_handler;
152 
153 // handle_timer: dynamic parameters, multiple copies
154 // handle_proxy_write
155 // handle_proxy_read
156 // handle_async_write
157 // handle_pre_init
158 
159 
161 namespace error {
162 enum value {
165  general = 1,
166 
169 
172 
175 
178 
181 };
182 
184 class category : public lib::error_category {
185 public:
186  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
187  return "websocketpp.transport.asio";
188  }
189 
190  std::string message(int value) const {
191  switch(value) {
192  case error::general:
193  return "Generic asio transport policy error";
195  return "async_read_at_least call requested more bytes than buffer can store";
196  case error::pass_through:
197  return "Underlying Transport Error";
198  case error::proxy_failed:
199  return "Proxy connection failed";
201  return "Invalid proxy URI";
203  return "Invalid host or service";
204  default:
205  return "Unknown";
206  }
207  }
208 };
209 
211 inline lib::error_category const & get_category() {
212  static category instance;
213  return instance;
214 }
215 
217 inline lib::error_code make_error_code(error::value e) {
218  return lib::error_code(static_cast<int>(e), get_category());
219 }
220 
221 } // namespace error
222 } // namespace asio
223 } // namespace transport
224 } // namespace websocketpp
225 
227 template<> struct is_error_code_enum<websocketpp::transport::asio::error::value>
228 {
229  static bool const value = true;
230 };
232 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
Asio based endpoint transport component.
Definition: base.hpp:143
lib::error_code make_error_code(error::value e)
Get an error code with the given value and the stub transport category.
Definition: base.hpp:80
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
Definition: cpp11.hpp:113
span_CONFIG_SIZE_TYPE size_t
Definition: span-lite.hpp:565
std::string message(int value) const
Definition: base.hpp:190
async_read_at_least call requested more bytes than buffer can store
Definition: base.hpp:168
pass_through from underlying library
Definition: base.hpp:96
void * allocate(std::size_t memsize)
Definition: base.hpp:63
Asio transport error category.
Definition: base.hpp:184
lib::function< void(lib::error_code const &ec)> pre_init_handler
Definition: base.hpp:151
async_read_at_least call requested more bytes than buffer can store
Definition: base.hpp:71
friend void * asio_handler_allocate(std::size_t size, custom_alloc_handler< Handler > *this_handler)
Definition: base.hpp:109
there was an error in the underlying transport library
Definition: base.hpp:171
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
lib::function< void(lib::asio::error_code const &ec, size_t bytes_transferred)> async_read_handler
Definition: base.hpp:143
friend void asio_handler_deallocate(void *pointer, std::size_t, custom_alloc_handler< Handler > *this_handler)
Definition: base.hpp:115
custom_alloc_handler< Handler > make_custom_alloc_handler(handler_allocator &a, Handler h)
Definition: base.hpp:128
The connection to the requested proxy server failed.
Definition: base.hpp:174
Catch-all error for transport policy errors that don&#39;t fit in other categories.
Definition: base.hpp:46
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
custom_alloc_handler(handler_allocator &a, Handler h)
Definition: base.hpp:94
lib::error_category const & get_category()
Get a reference to a static copy of the stub transport error category.
Definition: base.hpp:74
lib::function< void(lib::asio::error_code const &ec, size_t bytes_transferred)> async_write_handler
Definition: base.hpp:149
Catch-all error for transport policy errors that don&#39;t fit in other categories.
Definition: base.hpp:165
char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
Definition: base.hpp:186