WebSocket++  0.8.3-dev
C++ 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 {
41 /// Transport policy that uses asio
42 /**
43  * This policy uses a single asio io_service to provide transport
44  * services to a WebSocket++ endpoint.
45  */
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.
52 class handler_allocator {
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:
94  custom_alloc_handler(handler_allocator& a, Handler h)
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 & 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 
160 /// Asio transport errors
161 namespace error {
162 enum value {
163  /// Catch-all error for transport policy errors that don't fit in other
164  /// categories
165  general = 1,
166 
167  /// async_read_at_least call requested more bytes than buffer can store
169 
170  /// there was an error in the underlying transport library
172 
173  /// The connection to the requested proxy server failed
175 
176  /// Invalid Proxy URI
178 
179  /// Invalid host or service
181 };
182 
183 /// Asio transport error category
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";
194  case error::invalid_num_bytes:
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";
200  case error::proxy_invalid:
201  return "Invalid proxy URI";
202  case error::invalid_host_service:
203  return "Invalid host or service";
204  default:
205  return "Unknown";
206  }
207  }
208 };
209 
210 /// Get a reference to a static copy of the asio transport error category
211 inline lib::error_category const & get_category() {
212  static category instance;
213  return instance;
214 }
215 
216 /// Create an error code with the given value and the asio transport category
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 
226 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
227 template<> struct is_error_code_enum<websocketpp::transport::asio::error::value>
228 {
229  static bool const value = true;
230 };
231 _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
232 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
websocketpp::transport::asio::error::make_error_code
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
websocketpp::transport::asio::error::proxy_failed
@ proxy_failed
The connection to the requested proxy server failed.
Definition: base.hpp:174
websocketpp::transport::asio::error::pass_through
@ pass_through
there was an error in the underlying transport library
Definition: base.hpp:171
websocketpp::transport::asio::custom_alloc_handler
Definition: base.hpp:92
websocketpp::transport::asio
Transport policy that uses asio.
Definition: base.hpp:46
websocketpp::transport::asio::error
Asio transport errors.
Definition: base.hpp:161
websocketpp::versions_supported
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
websocketpp::transport::asio::error::general
@ general
Definition: base.hpp:165
websocketpp::transport::dispatch_handler
lib::function< void()> dispatch_handler
The type and signature of the callback passed to the dispatch method.
Definition: connection.hpp:135
websocketpp::transport::asio::error::proxy_invalid
@ proxy_invalid
Invalid Proxy URI.
Definition: base.hpp:177
websocketpp::transport::asio::error::get_category
lib::error_category const & get_category()
Get a reference to a static copy of the asio transport error category.
Definition: base.hpp:211
websocketpp::transport::asio::error::invalid_host_service
@ invalid_host_service
Invalid host or service.
Definition: base.hpp:180
websocketpp::transport::asio::error::value
value
Definition: base.hpp:162
websocketpp::transport::asio::error::invalid_num_bytes
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
Definition: base.hpp:168