flat_stream.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_FLAT_STREAM_HPP
  10. #define BOOST_BEAST_CORE_FLAT_STREAM_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/error.hpp>
  13. #include <boost/beast/core/flat_buffer.hpp>
  14. #include <boost/beast/core/stream_traits.hpp>
  15. #include <boost/beast/core/detail/flat_stream.hpp>
  16. #include <boost/asio/async_result.hpp>
  17. #include <cstdlib>
  18. #include <utility>
  19. namespace boost {
  20. namespace beast {
  21. /** (Deprecated: This wrapper is no longer needed; Asio linearizes
  22. scatter/gather I/O in ssl::stream.) Stream wrapper to improve write
  23. performance.
  24. This wrapper flattens writes for buffer sequences having length
  25. greater than 1 and total size below a predefined amount, using
  26. a dynamic memory allocation. It is primarily designed to overcome
  27. a performance limitation of the current version of `net::ssl::stream`,
  28. which does not use OpenSSL's scatter/gather interface for its
  29. low-level read some and write some operations.
  30. It is normally not necessary to use this class directly if you
  31. are already using @ref ssl_stream. The following examples shows
  32. how to use this class with the ssl stream that comes with
  33. networking:
  34. @par Example
  35. To use the @ref flat_stream template with SSL streams, declare
  36. a variable of the correct type. Parameters passed to the constructor
  37. will be forwarded to the next layer's constructor:
  38. @code
  39. flat_stream<net::ssl::stream<ip::tcp::socket>> fs{ioc, ctx};
  40. @endcode
  41. Alternatively you can write
  42. @code
  43. ssl::stream<ip::tcp::socket> ss{ioc, ctx};
  44. flat_stream<net::ssl::stream<ip::tcp::socket>&> fs{ss};
  45. @endcode
  46. The resulting stream may be passed to any stream algorithms which
  47. operate on synchronous or asynchronous read or write streams,
  48. examples include:
  49. @li `net::read`, `net::async_read`
  50. @li `net::write`, `net::async_write`
  51. @li `net::read_until`, `net::async_read_until`
  52. The stream may also be used as a template parameter in other
  53. stream wrappers, such as for websocket:
  54. @code
  55. websocket::stream<flat_stream<net::ssl::stream<ip::tcp::socket>>> ws{ioc, ctx};
  56. @endcode
  57. @tparam NextLayer The type representing the next layer, to which
  58. data will be read and written during operations. For synchronous
  59. operations, the type must support the @b SyncStream concept. For
  60. asynchronous operations, the type must support the @b AsyncStream
  61. concept. This type will usually be some variation of
  62. `net::ssl::stream`.
  63. @par Concepts
  64. @li SyncStream
  65. @li AsyncStream
  66. @see
  67. @li https://github.com/boostorg/asio/issues/100
  68. @li https://github.com/boostorg/beast/issues/1108
  69. @li https://stackoverflow.com/questions/38198638/openssl-ssl-write-from-multiple-buffers-ssl-writev
  70. @li https://stackoverflow.com/questions/50026167/performance-drop-on-port-from-beast-1-0-0-b66-to-boost-1-67-0-beast
  71. */
  72. template<class NextLayer>
  73. class flat_stream
  74. #if ! BOOST_BEAST_DOXYGEN
  75. : private detail::flat_stream_base
  76. #endif
  77. {
  78. NextLayer stream_;
  79. flat_buffer buffer_;
  80. BOOST_STATIC_ASSERT(has_get_executor<NextLayer>::value);
  81. struct ops;
  82. template<class ConstBufferSequence>
  83. std::size_t
  84. stack_write_some(
  85. std::size_t size,
  86. ConstBufferSequence const& buffers,
  87. error_code& ec);
  88. public:
  89. /// The type of the next layer.
  90. using next_layer_type =
  91. typename std::remove_reference<NextLayer>::type;
  92. /// The type of the executor associated with the object.
  93. using executor_type = beast::executor_type<next_layer_type>;
  94. flat_stream(flat_stream&&) = default;
  95. flat_stream(flat_stream const&) = default;
  96. flat_stream& operator=(flat_stream&&) = default;
  97. flat_stream& operator=(flat_stream const&) = default;
  98. /** Destructor
  99. The treatment of pending operations will be the same as that
  100. of the next layer.
  101. */
  102. ~flat_stream() = default;
  103. /** Constructor
  104. Arguments, if any, are forwarded to the next layer's constructor.
  105. */
  106. template<class... Args>
  107. explicit
  108. flat_stream(Args&&... args);
  109. //--------------------------------------------------------------------------
  110. /** Get the executor associated with the object.
  111. This function may be used to obtain the executor object that the
  112. stream uses to dispatch handlers for asynchronous operations.
  113. @return A copy of the executor that stream will use to dispatch handlers.
  114. */
  115. executor_type
  116. get_executor() noexcept
  117. {
  118. return stream_.get_executor();
  119. }
  120. /** Get a reference to the next layer
  121. This function returns a reference to the next layer
  122. in a stack of stream layers.
  123. @return A reference to the next layer in the stack of
  124. stream layers.
  125. */
  126. next_layer_type&
  127. next_layer() noexcept
  128. {
  129. return stream_;
  130. }
  131. /** Get a reference to the next layer
  132. This function returns a reference to the next layer in a
  133. stack of stream layers.
  134. @return A reference to the next layer in the stack of
  135. stream layers.
  136. */
  137. next_layer_type const&
  138. next_layer() const noexcept
  139. {
  140. return stream_;
  141. }
  142. //--------------------------------------------------------------------------
  143. /** Read some data from the stream.
  144. This function is used to read data from the stream. The function call will
  145. block until one or more bytes of data has been read successfully, or until
  146. an error occurs.
  147. @param buffers The buffers into which the data will be read.
  148. @returns The number of bytes read.
  149. @throws boost::system::system_error Thrown on failure.
  150. @note The `read_some` operation may not read all of the requested number of
  151. bytes. Consider using the function `net::read` if you need to ensure
  152. that the requested amount of data is read before the blocking operation
  153. completes.
  154. */
  155. template<class MutableBufferSequence>
  156. std::size_t
  157. read_some(MutableBufferSequence const& buffers);
  158. /** Read some data from the stream.
  159. This function is used to read data from the stream. The function call will
  160. block until one or more bytes of data has been read successfully, or until
  161. an error occurs.
  162. @param buffers The buffers into which the data will be read.
  163. @param ec Set to indicate what error occurred, if any.
  164. @returns The number of bytes read.
  165. @note The `read_some` operation may not read all of the requested number of
  166. bytes. Consider using the function `net::read` if you need to ensure
  167. that the requested amount of data is read before the blocking operation
  168. completes.
  169. */
  170. template<class MutableBufferSequence>
  171. std::size_t
  172. read_some(
  173. MutableBufferSequence const& buffers,
  174. error_code& ec);
  175. /** Start an asynchronous read.
  176. This function is used to asynchronously read one or more bytes of data from
  177. the stream. The function call always returns immediately.
  178. @param buffers The buffers into which the data will be read. Although the
  179. buffers object may be copied as necessary, ownership of the underlying
  180. buffers is retained by the caller, which must guarantee that they remain
  181. valid until the handler is called.
  182. @param handler The completion handler to invoke when the operation
  183. completes. The implementation takes ownership of the handler by
  184. performing a decay-copy. The equivalent function signature of
  185. the handler must be:
  186. @code
  187. void handler(
  188. error_code const& error, // Result of operation.
  189. std::size_t bytes_transferred // Number of bytes read.
  190. );
  191. @endcode
  192. If the handler has an associated immediate executor,
  193. an immediate completion will be dispatched to it.
  194. Otherwise, the handler will not be invoked from within
  195. this function. Invocation of the handler will be performed
  196. by dispatching to the immediate executor. If no
  197. immediate executor is specified, this is equivalent
  198. to using `net::post`.
  199. @note The `read_some` operation may not read all of the requested number of
  200. bytes. Consider using the function `net::async_read` if you need
  201. to ensure that the requested amount of data is read before the asynchronous
  202. operation completes.
  203. */
  204. template<
  205. class MutableBufferSequence,
  206. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  207. net::default_completion_token_t<executor_type>>
  208. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  209. async_read_some(
  210. MutableBufferSequence const& buffers,
  211. ReadHandler&& handler =
  212. net::default_completion_token_t<executor_type>{});
  213. /** Write some data to the stream.
  214. This function is used to write data on the stream. The function call will
  215. block until one or more bytes of data has been written successfully, or
  216. until an error occurs.
  217. @param buffers The data to be written.
  218. @returns The number of bytes written.
  219. @throws boost::system::system_error Thrown on failure.
  220. @note The `write_some` operation may not transmit all of the data to the
  221. peer. Consider using the function `net::write` if you need to
  222. ensure that all data is written before the blocking operation completes.
  223. */
  224. template<class ConstBufferSequence>
  225. std::size_t
  226. write_some(ConstBufferSequence const& buffers);
  227. /** Write some data to the stream.
  228. This function is used to write data on the stream. The function call will
  229. block until one or more bytes of data has been written successfully, or
  230. until an error occurs.
  231. @param buffers The data to be written.
  232. @param ec Set to indicate what error occurred, if any.
  233. @returns The number of bytes written.
  234. @note The `write_some` operation may not transmit all of the data to the
  235. peer. Consider using the function `net::write` if you need to
  236. ensure that all data is written before the blocking operation completes.
  237. */
  238. template<class ConstBufferSequence>
  239. std::size_t
  240. write_some(
  241. ConstBufferSequence const& buffers,
  242. error_code& ec);
  243. /** Start an asynchronous write.
  244. This function is used to asynchronously write one or more bytes of data to
  245. the stream. The function call always returns immediately.
  246. @param buffers The data to be written to the stream. Although the buffers
  247. object may be copied as necessary, ownership of the underlying buffers is
  248. retained by the caller, which must guarantee that they remain valid until
  249. the handler is called.
  250. @param handler The completion handler to invoke when the operation
  251. completes. The implementation takes ownership of the handler by
  252. performing a decay-copy. The equivalent function signature of
  253. the handler must be:
  254. @code
  255. void handler(
  256. error_code const& ec, // Result of operation.
  257. std::size_t bytes_transferred // Number of bytes written.
  258. );
  259. @endcode
  260. If the handler has an associated immediate executor,
  261. an immediate completion will be dispatched to it.
  262. Otherwise, the handler will not be invoked from within
  263. this function. Invocation of the handler will be performed
  264. by dispatching to the immediate executor. If no
  265. immediate executor is specified, this is equivalent
  266. to using `net::post`.
  267. @note The `async_write_some` operation may not transmit all of the data to
  268. the peer. Consider using the function `net::async_write` if you need
  269. to ensure that all data is written before the asynchronous operation completes.
  270. */
  271. template<
  272. class ConstBufferSequence,
  273. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  274. net::default_completion_token_t<executor_type>>
  275. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  276. async_write_some(
  277. ConstBufferSequence const& buffers,
  278. WriteHandler&& handler =
  279. net::default_completion_token_t<executor_type>{});
  280. };
  281. } // beast
  282. } // boost
  283. #include <boost/beast/core/impl/flat_stream.hpp>
  284. #endif