Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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
39namespace websocketpp {
40namespace transport {
42
46namespace 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.
53public:
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
80private:
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.
91template <typename Handler>
93public:
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
121private:
122 handler_allocator & allocator_;
123 Handler handler_;
124};
125
126// Helper function to wrap a handler object to add custom allocation.
127template <typename Handler>
133
134
135
136
137
138
139
140// Forward declaration of class endpoint so that it can be friended/referenced
141// before being included.
142template <typename config>
143class endpoint;
144
145typedef lib::function<void (lib::asio::error_code const & ec,
146 size_t bytes_transferred)> async_read_handler;
147
148typedef lib::function<void (lib::asio::error_code const & ec,
149 size_t bytes_transferred)> async_write_handler;
150
151typedef 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
161namespace error {
182
184class category : public lib::error_category {
185public:
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";
197 return "Underlying Transport Error";
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
211inline lib::error_category const & get_category() {
212 static category instance;
213 return instance;
214}
215
217inline 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
227template<> struct is_error_code_enum<websocketpp::transport::asio::error::value>
228{
229 static bool const value = true;
230};
232#endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
Creates and manages connections associated with a WebSocket endpoint.
Definition endpoint.hpp:42
friend void asio_handler_deallocate(void *pointer, std::size_t, custom_alloc_handler< Handler > *this_handler)
Definition base.hpp:115
friend void * asio_handler_allocate(std::size_t size, custom_alloc_handler< Handler > *this_handler)
Definition base.hpp:109
custom_alloc_handler(handler_allocator &a, Handler h)
Definition base.hpp:94
Asio transport error category.
Definition base.hpp:184
char const * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
Definition base.hpp:186
std::string message(int value) const
Definition base.hpp:190
void * allocate(std::size_t memsize)
Definition base.hpp:63
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
Definition cpp11.hpp:113
lib::error_category const & get_category()
Get a reference to a static copy of the asio transport error category.
Definition base.hpp:211
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
@ proxy_invalid
Invalid Proxy URI.
Definition base.hpp:177
@ pass_through
there was an error in the underlying transport library
Definition base.hpp:171
@ proxy_failed
The connection to the requested proxy server failed.
Definition base.hpp:174
@ invalid_num_bytes
async_read_at_least call requested more bytes than buffer can store
Definition base.hpp:168
@ invalid_host_service
Invalid host or service.
Definition base.hpp:180
lib::function< void(lib::asio::error_code const &ec, size_t bytes_transferred)> async_write_handler
Definition base.hpp:149
lib::function< void(lib::asio::error_code const &ec, size_t bytes_transferred)> async_read_handler
Definition base.hpp:146
lib::function< void(lib::error_code const &ec)> pre_init_handler
Definition base.hpp:151
custom_alloc_handler< Handler > make_custom_alloc_handler(handler_allocator &a, Handler h)
Definition base.hpp:128
Namespace for the WebSocket++ project.
Definition base64.hpp:41
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > & pointer
Definition pointer.h:1181
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
#define _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_