You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
  28. #define WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP
  29. #include <string>
  30. #include <websocketpp/common/memory.hpp>
  31. namespace websocketpp
  32. {
  33. namespace message_buffer
  34. {
  35. /* # message:
  36. * object that stores a message while it is being sent or received. Contains
  37. * the message payload itself, the message header, the extension data, and the
  38. * opcode.
  39. *
  40. * # connection_message_manager:
  41. * An object that manages all of the message_buffers associated with a given
  42. * connection. Implements the get_message_buffer(size) method that returns
  43. * a message buffer at least size bytes long.
  44. *
  45. * Message buffers are reference counted with shared ownership semantics. Once
  46. * requested from the manager the requester and it's associated downstream code
  47. * may keep a pointer to the message indefinitely at a cost of extra resource
  48. * usage. Once the reference count drops to the point where the manager is the
  49. * only reference the messages is recycled using whatever method is implemented
  50. * in the manager.
  51. *
  52. * # endpoint_message_manager:
  53. * An object that manages connection_message_managers. Implements the
  54. * get_message_manager() method. This is used once by each connection to
  55. * request the message manager that they are supposed to use to manage message
  56. * buffers for their own use.
  57. *
  58. * TYPES OF CONNECTION_MESSAGE_MANAGERS
  59. * - allocate a message with the exact size every time one is requested
  60. * - maintain a pool of pre-allocated messages and return one when needed.
  61. * Recycle previously used messages back into the pool
  62. *
  63. * TYPES OF ENDPOINT_MESSAGE_MANAGERS
  64. * - allocate a new connection manager for each connection. Message pools
  65. * become connection specific. This increases memory usage but improves
  66. * concurrency.
  67. * - allocate a single connection manager and share a pointer to it with all
  68. * connections created by this endpoint. The message pool will be shared
  69. * among all connections, improving memory usage and performance at the cost
  70. * of reduced concurrency
  71. */
  72. /// Custom deleter for use in shared_ptrs to message.
  73. /**
  74. * This is used to catch messages about to be deleted and offer the manager the
  75. * ability to recycle them instead. Message::recycle will return true if it was
  76. * successfully recycled and false otherwise. In the case of exceptions or error
  77. * this deleter frees the memory.
  78. */
  79. template <typename T> void message_deleter(T * msg)
  80. {
  81. try
  82. {
  83. if (! msg->recycle())
  84. {
  85. delete msg;
  86. }
  87. }
  88. catch (...)
  89. {
  90. // TODO: is there a better way to ensure this function doesn't throw?
  91. delete msg;
  92. }
  93. }
  94. /// Represents a buffer for a single WebSocket message.
  95. /**
  96. *
  97. *
  98. */
  99. template <typename con_msg_manager> class message
  100. {
  101. public:
  102. typedef lib::shared_ptr<message> ptr;
  103. typedef typename con_msg_manager::weak_ptr con_msg_man_ptr;
  104. message(con_msg_man_ptr manager, size_t size = 128) :
  105. m_manager(manager),
  106. m_payload(size)
  107. {
  108. }
  109. frame::opcode::value get_opcode() const
  110. {
  111. return m_opcode;
  112. }
  113. const std::string & get_header() const
  114. {
  115. return m_header;
  116. }
  117. const std::string & get_extension_data() const
  118. {
  119. return m_extension_data;
  120. }
  121. const std::string & get_payload() const
  122. {
  123. return m_payload;
  124. }
  125. /// Recycle the message
  126. /**
  127. * A request to recycle this message was received. Forward that request to
  128. * the connection message manager for processing. Errors and exceptions
  129. * from the manager's recycle member function should be passed back up the
  130. * call chain. The caller to message::recycle will deal with them.
  131. *
  132. * Recycle must *only* be called by the message shared_ptr's destructor.
  133. * Once recycled successfully, ownership of the memory has been passed to
  134. * another system and must not be accessed again.
  135. *
  136. * @return true if the message was successfully recycled, false otherwise.
  137. */
  138. bool recycle()
  139. {
  140. typename con_msg_manager::ptr shared = m_manager.lock();
  141. if (shared)
  142. {
  143. return shared->(recycle(this));
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. private:
  151. con_msg_man_ptr m_manager;
  152. frame::opcode::value m_opcode;
  153. std::string m_header;
  154. std::string m_extension_data;
  155. std::string m_payload;
  156. };
  157. namespace alloc
  158. {
  159. /// A connection message manager that allocates a new message for each
  160. /// request.
  161. template <typename message> class con_msg_manager
  162. {
  163. public:
  164. typedef lib::shared_ptr<con_msg_manager> ptr;
  165. typedef lib::weak_ptr<con_msg_manager> weak_ptr;
  166. typedef typename message::ptr message_ptr;
  167. /// Get a message buffer with specified size
  168. /**
  169. * @param size Minimum size in bytes to request for the message payload.
  170. *
  171. * @return A shared pointer to a new message with specified size.
  172. */
  173. message_ptr get_message(size_t size) const
  174. {
  175. return lib::make_shared<message>(size);
  176. }
  177. /// Recycle a message
  178. /**
  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
  181. * the memory.
  182. *
  183. * @param msg The message to be recycled.
  184. *
  185. * @return true if the message was successfully recycled, false otherwse.
  186. */
  187. bool recycle(message * msg)
  188. {
  189. return false;
  190. }
  191. };
  192. /// An endpoint message manager that allocates a new manager for each
  193. /// connection.
  194. template <typename con_msg_manager> class endpoint_msg_manager
  195. {
  196. public:
  197. typedef typename con_msg_manager::ptr con_msg_man_ptr;
  198. /// Get a pointer to a connection message manager
  199. /**
  200. * @return A pointer to the requested connection message manager.
  201. */
  202. con_msg_man_ptr get_manager() const
  203. {
  204. return lib::make_shared<con_msg_manager>();
  205. }
  206. };
  207. } // namespace alloc
  208. namespace pool
  209. {
  210. /// A connection messages manager that maintains a pool of messages that is
  211. /// used to fulfill get_message requests.
  212. class con_msg_manager
  213. {
  214. };
  215. /// An endpoint manager that maintains a shared pool of connection managers
  216. /// and returns an appropriate one for the requesting connection.
  217. class endpoint_msg_manager
  218. {
  219. };
  220. } // namespace pool
  221. } // namespace message_buffer
  222. } // namespace websocketpp
  223. #endif // WEBSOCKETPP_MESSAGE_BUFFER_ALLOC_HPP