websocketpp  0.6.0
C++/Boost Asio based websocket client/server library
base.hpp
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 
31 #include <websocketpp/common/asio.hpp>
32 #include <websocketpp/common/cpp11.hpp>
33 #include <websocketpp/common/functional.hpp>
34 #include <websocketpp/common/system_error.hpp>
35 #include <websocketpp/common/type_traits.hpp>
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 
109  friend void* asio_handler_allocate(std::size_t size,
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>
128 inline custom_alloc_handler<Handler> make_custom_alloc_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 &)>
146  socket_shutdown_handler;
147 
148 typedef lib::function<void (lib::asio::error_code const & ec,
149  size_t bytes_transferred)> async_read_handler;
150 
151 typedef lib::function<void (lib::asio::error_code const & ec,
152  size_t bytes_transferred)> async_write_handler;
153 
154 typedef lib::function<void (lib::error_code const & ec)> pre_init_handler;
155 
156 // handle_timer: dynamic parameters, multiple copies
157 // handle_proxy_write
158 // handle_proxy_read
159 // handle_async_write
160 // handle_pre_init
161 
162 
164 namespace error {
165 enum value {
168  general = 1,
169 
172 
175 
178 
181 
184 };
185 
187 class category : public lib::error_category {
188 public:
189  char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_ {
190  return "websocketpp.transport.asio";
191  }
192 
193  std::string message(int value) const {
194  switch(value) {
195  case error::general:
196  return "Generic asio transport policy error";
198  return "async_read_at_least call requested more bytes than buffer can store";
199  case error::pass_through:
200  return "Underlying Transport Error";
201  case error::proxy_failed:
202  return "Proxy connection failed";
204  return "Invalid proxy URI";
206  return "Invalid host or service";
207  default:
208  return "Unknown";
209  }
210  }
211 };
212 
214 inline lib::error_category const & get_category() {
215  static category instance;
216  return instance;
217 }
218 
220 inline lib::error_code make_error_code(error::value e) {
221  return lib::error_code(static_cast<int>(e), get_category());
222 }
223 
224 } // namespace error
225 } // namespace asio
226 } // namespace transport
227 } // namespace websocketpp
228 
229 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
230 template<> struct is_error_code_enum<websocketpp::transport::asio::error::value>
231 {
232  static bool const value = true;
233 };
234 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
235 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
Asio based endpoint transport component.
Definition: base.hpp:143
uint16_t value
The type of a close code value.
Definition: close.hpp:49
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:220
async_read_at_least call requested more bytes than buffer can store
Definition: base.hpp:171
Asio transport error category.
Definition: base.hpp:187
there was an error in the underlying transport library
Definition: base.hpp:174
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
The connection to the requested proxy server failed.
Definition: base.hpp:177
lib::error_category const & get_category()
Get a reference to a static copy of the asio transport error category.
Definition: base.hpp:214