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.

369 lines
11 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_MESSAGE_HPP
  28. #define WEBSOCKETPP_MESSAGE_BUFFER_MESSAGE_HPP
  29. #include <string>
  30. #include <websocketpp/common/memory.hpp>
  31. #include <websocketpp/frame.hpp>
  32. namespace websocketpp
  33. {
  34. namespace message_buffer
  35. {
  36. /* # message:
  37. * object that stores a message while it is being sent or received. Contains
  38. * the message payload itself, the message header, the extension data, and the
  39. * opcode.
  40. *
  41. * # connection_message_manager:
  42. * An object that manages all of the message_buffers associated with a given
  43. * connection. Implements the get_message_buffer(size) method that returns
  44. * a message buffer at least size bytes long.
  45. *
  46. * Message buffers are reference counted with shared ownership semantics. Once
  47. * requested from the manager the requester and it's associated downstream code
  48. * may keep a pointer to the message indefinitely at a cost of extra resource
  49. * usage. Once the reference count drops to the point where the manager is the
  50. * only reference the messages is recycled using whatever method is implemented
  51. * in the manager.
  52. *
  53. * # endpoint_message_manager:
  54. * An object that manages connection_message_managers. Implements the
  55. * get_message_manager() method. This is used once by each connection to
  56. * request the message manager that they are supposed to use to manage message
  57. * buffers for their own use.
  58. *
  59. * TYPES OF CONNECTION_MESSAGE_MANAGERS
  60. * - allocate a message with the exact size every time one is requested
  61. * - maintain a pool of pre-allocated messages and return one when needed.
  62. * Recycle previously used messages back into the pool
  63. *
  64. * TYPES OF ENDPOINT_MESSAGE_MANAGERS
  65. * - allocate a new connection manager for each connection. Message pools
  66. * become connection specific. This increases memory usage but improves
  67. * concurrency.
  68. * - allocate a single connection manager and share a pointer to it with all
  69. * connections created by this endpoint. The message pool will be shared
  70. * among all connections, improving memory usage and performance at the cost
  71. * of reduced concurrency
  72. */
  73. /// Represents a buffer for a single WebSocket message.
  74. /**
  75. *
  76. *
  77. */
  78. template <template <class> class con_msg_manager> class message
  79. {
  80. public:
  81. typedef lib::shared_ptr<message> ptr;
  82. typedef con_msg_manager<message> con_msg_man_type;
  83. typedef typename con_msg_man_type::ptr con_msg_man_ptr;
  84. typedef typename con_msg_man_type::weak_ptr con_msg_man_weak_ptr;
  85. /// Construct an empty message
  86. /**
  87. * Construct an empty message
  88. */
  89. message(const con_msg_man_ptr manager) :
  90. m_manager(manager),
  91. m_prepared(false),
  92. m_fin(true),
  93. m_terminal(false),
  94. m_compressed(false)
  95. {
  96. }
  97. /// Construct a message and fill in some values
  98. /**
  99. *
  100. */
  101. message(const con_msg_man_ptr manager, frame::opcode::value op, size_t size = 128) :
  102. m_manager(manager),
  103. m_opcode(op),
  104. m_prepared(false),
  105. m_fin(true),
  106. m_terminal(false),
  107. m_compressed(false)
  108. {
  109. m_payload.reserve(size);
  110. }
  111. /// Return whether or not the message has been prepared for sending
  112. /**
  113. * The prepared flag indicates that the message has been prepared by a
  114. * websocket protocol processor and is ready to be written to the wire.
  115. *
  116. * @return whether or not the message has been prepared for sending
  117. */
  118. bool get_prepared() const
  119. {
  120. return m_prepared;
  121. }
  122. /// Set or clear the flag that indicates that the message has been prepared
  123. /**
  124. * This flag should not be set by end user code without a very good reason.
  125. *
  126. * @param value The value to set the prepared flag to
  127. */
  128. void set_prepared(bool value)
  129. {
  130. m_prepared = value;
  131. }
  132. /// Return whether or not the message is flagged as compressed
  133. /**
  134. * @return whether or not the message is/should be compressed
  135. */
  136. bool get_compressed() const
  137. {
  138. return m_compressed;
  139. }
  140. /// Set or clear the compression flag
  141. /**
  142. * Setting the compression flag indicates that the data in this message
  143. * would benefit from compression. If both endpoints negotiate a compression
  144. * extension WebSocket++ will attempt to compress messages with this flag.
  145. * Setting this flag does not guarantee that the message will be compressed.
  146. *
  147. * @param value The value to set the compressed flag to
  148. */
  149. void set_compressed(bool value)
  150. {
  151. m_compressed = value;
  152. }
  153. /// Get whether or not the message is terminal
  154. /**
  155. * Messages can be flagged as terminal, which results in the connection
  156. * being close after they are written rather than the implementation going
  157. * on to the next message in the queue. This is typically used internally
  158. * for close messages only.
  159. *
  160. * @return Whether or not this message is marked terminal
  161. */
  162. bool get_terminal() const
  163. {
  164. return m_terminal;
  165. }
  166. /// Set the terminal flag
  167. /**
  168. * This flag should not be set by end user code without a very good reason.
  169. *
  170. * @see get_terminal()
  171. *
  172. * @param value The value to set the terminal flag to.
  173. */
  174. void set_terminal(bool value)
  175. {
  176. m_terminal = value;
  177. }
  178. /// Read the fin bit
  179. /**
  180. * A message with the fin bit set will be sent as the last message of its
  181. * sequence. A message with the fin bit cleared will require subsequent
  182. * frames of opcode continuation until one of them has the fin bit set.
  183. *
  184. * The remote end likely will not deliver any bytes until the frame with the fin
  185. * bit set has been received.
  186. *
  187. * @return Whether or not the fin bit is set
  188. */
  189. bool get_fin() const
  190. {
  191. return m_fin;
  192. }
  193. /// Set the fin bit
  194. /**
  195. * @see get_fin for a more detailed explaination of the fin bit
  196. *
  197. * @param value The value to set the fin bit to.
  198. */
  199. void set_fin(bool value)
  200. {
  201. m_fin = value;
  202. }
  203. /// Return the message opcode
  204. frame::opcode::value get_opcode() const
  205. {
  206. return m_opcode;
  207. }
  208. /// Set the opcode
  209. void set_opcode(frame::opcode::value op)
  210. {
  211. m_opcode = op;
  212. }
  213. /// Return the prepared frame header
  214. /**
  215. * This value is typically set by a websocket protocol processor
  216. * and shouldn't be tampered with.
  217. */
  218. const std::string & get_header() const
  219. {
  220. return m_header;
  221. }
  222. /// Set prepared frame header
  223. /**
  224. * Under normal circumstances this should not be called by end users
  225. *
  226. * @param header A string to set the header to.
  227. */
  228. void set_header(const std::string & header)
  229. {
  230. m_header = header;
  231. }
  232. const std::string & get_extension_data() const
  233. {
  234. return m_extension_data;
  235. }
  236. /// Get a reference to the payload string
  237. /**
  238. * @return A const reference to the message's payload string
  239. */
  240. const std::string & get_payload() const
  241. {
  242. return m_payload;
  243. }
  244. /// Get a non-const reference to the payload string
  245. /**
  246. * @return A reference to the message's payload string
  247. */
  248. std::string & get_raw_payload()
  249. {
  250. return m_payload;
  251. }
  252. /// Set payload data
  253. /**
  254. * Set the message buffer's payload to the given value.
  255. *
  256. * @param payload A string to set the payload to.
  257. */
  258. void set_payload(const std::string & payload)
  259. {
  260. m_payload = payload;
  261. }
  262. /// Set payload data
  263. /**
  264. * Set the message buffer's payload to the given value.
  265. *
  266. * @param payload A pointer to a data array to set to.
  267. * @param len The length of new payload in bytes.
  268. */
  269. void set_payload(const void * payload, size_t len)
  270. {
  271. m_payload.reserve(len);
  272. const char * pl = static_cast<const char *>(payload);
  273. m_payload.assign(pl, pl + len);
  274. }
  275. /// Append payload data
  276. /**
  277. * Append data to the message buffer's payload.
  278. *
  279. * @param payload A string containing the data array to append.
  280. */
  281. void append_payload(const std::string & payload)
  282. {
  283. m_payload.append(payload);
  284. }
  285. /// Append payload data
  286. /**
  287. * Append data to the message buffer's payload.
  288. *
  289. * @param payload A pointer to a data array to append
  290. * @param len The length of payload in bytes
  291. */
  292. void append_payload(const void * payload, size_t len)
  293. {
  294. m_payload.reserve(m_payload.size() + len);
  295. m_payload.append(static_cast<const char *>(payload), len);
  296. }
  297. /// Recycle the message
  298. /**
  299. * A request to recycle this message was received. Forward that request to
  300. * the connection message manager for processing. Errors and exceptions
  301. * from the manager's recycle member function should be passed back up the
  302. * call chain. The caller to message::recycle will deal with them.
  303. *
  304. * Recycle must *only* be called by the message shared_ptr's destructor.
  305. * Once recycled successfully, ownership of the memory has been passed to
  306. * another system and must not be accessed again.
  307. *
  308. * @return true if the message was successfully recycled, false otherwise.
  309. */
  310. bool recycle()
  311. {
  312. con_msg_man_ptr shared = m_manager.lock();
  313. if (shared)
  314. {
  315. return shared->recycle(this);
  316. }
  317. else
  318. {
  319. return false;
  320. }
  321. }
  322. private:
  323. con_msg_man_weak_ptr m_manager;
  324. std::string m_header;
  325. std::string m_extension_data;
  326. std::string m_payload;
  327. frame::opcode::value m_opcode;
  328. bool m_prepared;
  329. bool m_fin;
  330. bool m_terminal;
  331. bool m_compressed;
  332. };
  333. } // namespace message_buffer
  334. } // namespace websocketpp
  335. #endif // WEBSOCKETPP_MESSAGE_BUFFER_MESSAGE_HPP