2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
29 #define WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
31 #include <websocketpp/common/memory.hpp>
35 namespace websocketpp {
36 namespace message_buffer {
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
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.
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
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.
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
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
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
76 /// Custom deleter for use in shared_ptrs to message.
78 * This is used to catch messages about to be deleted and offer the manager the
79 * ability to recycle them instead. Message::recycle will return true if it was
80 * successfully recycled and false otherwise. In the case of exceptions or error
81 * this deleter frees the memory.
84 void message_deleter(T* msg) {
86 if (!msg->recycle()) {
90 // TODO: is there a better way to ensure this function doesn't throw?
95 /// Represents a buffer for a single WebSocket message.
100 template <typename con_msg_manager>
103 typedef lib::shared_ptr<message> ptr;
105 typedef typename con_msg_manager::weak_ptr con_msg_man_ptr;
107 message(con_msg_man_ptr manager, size_t size = 128)
111 frame::opcode::value get_opcode() const {
114 const std::string& get_header() const {
117 const std::string& get_extension_data() const {
118 return m_extension_data;
120 const std::string& get_payload() const {
124 /// Recycle the message
126 * A request to recycle this message was received. Forward that request to
127 * the connection message manager for processing. Errors and exceptions
128 * from the manager's recycle member function should be passed back up the
129 * call chain. The caller to message::recycle will deal with them.
131 * Recycle must *only* be called by the message shared_ptr's destructor.
132 * Once recycled successfully, ownership of the memory has been passed to
133 * another system and must not be accessed again.
135 * @return true if the message was successfully recycled, false otherwise.
138 typename con_msg_manager::ptr shared = m_manager.lock();
141 return shared->(recycle(this));
147 con_msg_man_ptr m_manager;
149 frame::opcode::value m_opcode;
150 std::string m_header;
151 std::string m_extension_data;
152 std::string m_payload;
157 /// A connection message manager that allocates a new message for each
159 template <typename message>
160 class con_msg_manager {
162 typedef lib::shared_ptr<con_msg_manager> ptr;
163 typedef lib::weak_ptr<con_msg_manager> weak_ptr;
165 typedef typename message::ptr message_ptr;
167 /// Get a message buffer with specified size
169 * @param size Minimum size in bytes to request for the message payload.
171 * @return A shared pointer to a new message with specified size.
173 message_ptr get_message(size_t size) const {
174 return lib::make_shared<message>(size);
177 /// Recycle a message
179 * This method shouldn't be called. If it is, return false to indicate an
180 * error. The rest of the method recycle chain should notice this and free
183 * @param msg The message to be recycled.
185 * @return true if the message was successfully recycled, false otherwse.
187 bool recycle(message * msg) {
192 /// An endpoint message manager that allocates a new manager for each
194 template <typename con_msg_manager>
195 class endpoint_msg_manager {
197 typedef typename con_msg_manager::ptr con_msg_man_ptr;
199 /// Get a pointer to a connection message manager
201 * @return A pointer to the requested connection message manager.
203 con_msg_man_ptr get_manager() const {
204 return lib::make_shared<con_msg_manager>();
212 /// A connection messages manager that maintains a pool of messages that is
213 /// used to fulfill get_message requests.
214 class con_msg_manager {
218 /// An endpoint manager that maintains a shared pool of connection managers
219 /// and returns an appropriate one for the requesting connection.
220 class endpoint_msg_manager {
226 } // namespace message_buffer
227 } // namespace websocketpp
229 #endif // WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP