Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
pool.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, 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_MESSAGE_BUFFER_ALLOC_HPP
29#define WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
30
32
33#include <string>
34
35namespace websocketpp {
36namespace message_buffer {
37
38/* # message:
39 * object that stores a message while it is being sent or received. Contains
40 * the message payload itself, the message header, the extension data, and the
41 * opcode.
42 *
43 * # connection_message_manager:
44 * An object that manages all of the message_buffers associated with a given
45 * connection. Implements the get_message_buffer(size) method that returns
46 * a message buffer at least size bytes long.
47 *
48 * Message buffers are reference counted with shared ownership semantics. Once
49 * requested from the manager the requester and it's associated downstream code
50 * may keep a pointer to the message indefinitely at a cost of extra resource
51 * usage. Once the reference count drops to the point where the manager is the
52 * only reference the messages is recycled using whatever method is implemented
53 * in the manager.
54 *
55 * # endpoint_message_manager:
56 * An object that manages connection_message_managers. Implements the
57 * get_message_manager() method. This is used once by each connection to
58 * request the message manager that they are supposed to use to manage message
59 * buffers for their own use.
60 *
61 * TYPES OF CONNECTION_MESSAGE_MANAGERS
62 * - allocate a message with the exact size every time one is requested
63 * - maintain a pool of pre-allocated messages and return one when needed.
64 * Recycle previously used messages back into the pool
65 *
66 * TYPES OF ENDPOINT_MESSAGE_MANAGERS
67 * - allocate a new connection manager for each connection. Message pools
68 * become connection specific. This increases memory usage but improves
69 * concurrency.
70 * - allocate a single connection manager and share a pointer to it with all
71 * connections created by this endpoint. The message pool will be shared
72 * among all connections, improving memory usage and performance at the cost
73 * of reduced concurrency
74 */
75
77
83template <typename T>
84void message_deleter(T* msg) {
85 try {
86 if (!msg->recycle()) {
87 delete msg;
88 }
89 } catch (...) {
90 // TODO: is there a better way to ensure this function doesn't throw?
91 delete msg;
92 }
93}
94
96
100template <typename con_msg_manager>
101class message {
102public:
103 typedef lib::shared_ptr<message> ptr;
104
105 typedef typename con_msg_manager::weak_ptr con_msg_man_ptr;
106
107 message(con_msg_man_ptr manager, size_t size = 128)
108 : m_manager(manager)
109 , m_payload(size) {}
110
112 return m_opcode;
113 }
114 const std::string& get_header() const {
115 return m_header;
116 }
117 const std::string& get_extension_data() const {
118 return m_extension_data;
119 }
120 const std::string& get_payload() const {
121 return m_payload;
122 }
123
125
137 bool recycle() {
138 typename con_msg_manager::ptr shared = m_manager.lock();
139
140 if (shared) {
141 return shared->(recycle(this));
142 } else {
143 return false;
144 }
145 }
146private:
147 con_msg_man_ptr m_manager;
148
149 frame::opcode::value m_opcode;
150 std::string m_header;
151 std::string m_extension_data;
152 std::string m_payload;
153};
154
155namespace alloc {
156
159template <typename message>
161public:
162 typedef lib::shared_ptr<con_msg_manager> ptr;
163 typedef lib::weak_ptr<con_msg_manager> weak_ptr;
164
165 typedef typename message::ptr message_ptr;
166
168
173 message_ptr get_message(size_t size) const {
174 return lib::make_shared<message>(size);
175 }
176
178
187 bool recycle(message * msg) {
188 return false;
189 }
190};
191
194template <typename con_msg_manager>
196public:
197 typedef typename con_msg_manager::ptr con_msg_man_ptr;
198
200
204 return lib::make_shared<con_msg_manager>();
205 }
206};
207
208} // namespace alloc
209
210namespace pool {
211
215
216};
217
221
222};
223
224} // namespace pool
225
226} // namespace message_buffer
227} // namespace websocketpp
228
229#endif // WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
message_ptr get_message(size_t size) const
Get a message buffer with specified size.
Definition pool.hpp:173
bool recycle(message *msg)
Recycle a message.
Definition pool.hpp:187
lib::weak_ptr< con_msg_manager > weak_ptr
Definition alloc.hpp:47
lib::shared_ptr< con_msg_manager > ptr
Definition alloc.hpp:46
con_msg_man_ptr get_manager() const
Get a pointer to a connection message manager.
Definition pool.hpp:203
Represents a buffer for a single WebSocket message.
Definition pool.hpp:101
bool recycle()
Recycle the message.
Definition pool.hpp:137
const std::string & get_header() const
Definition pool.hpp:114
const std::string & get_extension_data() const
Definition pool.hpp:117
con_msg_manager::weak_ptr con_msg_man_ptr
Definition pool.hpp:105
lib::shared_ptr< message > ptr
Definition message.hpp:86
frame::opcode::value get_opcode() const
Definition pool.hpp:111
con_msg_man_type::ptr con_msg_man_ptr
Definition message.hpp:89
const std::string & get_payload() const
Definition pool.hpp:120
message(con_msg_man_ptr manager, size_t size=128)
Definition pool.hpp:107
void message_deleter(T *msg)
Custom deleter for use in shared_ptrs to message.
Definition pool.hpp:84
Namespace for the WebSocket++ project.
Definition base64.hpp:41
#define T(meth, val, expected)