buffered_write_stream.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // buffered_write_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP
  11. #define BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <boost/asio/buffered_write_stream_fwd.hpp>
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/completion_condition.hpp>
  20. #include <boost/asio/detail/bind_handler.hpp>
  21. #include <boost/asio/detail/buffered_stream_storage.hpp>
  22. #include <boost/asio/detail/noncopyable.hpp>
  23. #include <boost/asio/detail/type_traits.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/write.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. template <typename> class initiate_async_buffered_flush;
  31. template <typename> class initiate_async_buffered_write_some;
  32. } // namespace detail
  33. /// Adds buffering to the write-related operations of a stream.
  34. /**
  35. * The buffered_write_stream class template can be used to add buffering to the
  36. * synchronous and asynchronous write operations of a stream.
  37. *
  38. * @par Thread Safety
  39. * @e Distinct @e objects: Safe.@n
  40. * @e Shared @e objects: Unsafe.
  41. *
  42. * @par Concepts:
  43. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  44. */
  45. template <typename Stream>
  46. class buffered_write_stream
  47. : private noncopyable
  48. {
  49. public:
  50. /// The type of the next layer.
  51. typedef typename remove_reference<Stream>::type next_layer_type;
  52. /// The type of the lowest layer.
  53. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  54. /// The type of the executor associated with the object.
  55. typedef typename lowest_layer_type::executor_type executor_type;
  56. #if defined(GENERATING_DOCUMENTATION)
  57. /// The default buffer size.
  58. static const std::size_t default_buffer_size = implementation_defined;
  59. #else
  60. BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
  61. #endif
  62. /// Construct, passing the specified argument to initialise the next layer.
  63. template <typename Arg>
  64. explicit buffered_write_stream(BOOST_ASIO_MOVE_OR_LVALUE_ARG(Arg) a)
  65. : next_layer_(BOOST_ASIO_MOVE_OR_LVALUE(Arg)(a)),
  66. storage_(default_buffer_size)
  67. {
  68. }
  69. /// Construct, passing the specified argument to initialise the next layer.
  70. template <typename Arg>
  71. buffered_write_stream(BOOST_ASIO_MOVE_OR_LVALUE_ARG(Arg) a,
  72. std::size_t buffer_size)
  73. : next_layer_(BOOST_ASIO_MOVE_OR_LVALUE(Arg)(a)),
  74. storage_(buffer_size)
  75. {
  76. }
  77. /// Get a reference to the next layer.
  78. next_layer_type& next_layer()
  79. {
  80. return next_layer_;
  81. }
  82. /// Get a reference to the lowest layer.
  83. lowest_layer_type& lowest_layer()
  84. {
  85. return next_layer_.lowest_layer();
  86. }
  87. /// Get a const reference to the lowest layer.
  88. const lowest_layer_type& lowest_layer() const
  89. {
  90. return next_layer_.lowest_layer();
  91. }
  92. /// Get the executor associated with the object.
  93. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  94. {
  95. return next_layer_.lowest_layer().get_executor();
  96. }
  97. /// Close the stream.
  98. void close()
  99. {
  100. next_layer_.close();
  101. }
  102. /// Close the stream.
  103. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  104. {
  105. next_layer_.close(ec);
  106. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  107. }
  108. /// Flush all data from the buffer to the next layer. Returns the number of
  109. /// bytes written to the next layer on the last write operation. Throws an
  110. /// exception on failure.
  111. std::size_t flush();
  112. /// Flush all data from the buffer to the next layer. Returns the number of
  113. /// bytes written to the next layer on the last write operation, or 0 if an
  114. /// error occurred.
  115. std::size_t flush(boost::system::error_code& ec);
  116. /// Start an asynchronous flush.
  117. /**
  118. * @par Completion Signature
  119. * @code void(boost::system::error_code, std::size_t) @endcode
  120. */
  121. template <
  122. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  123. std::size_t)) WriteHandler
  124. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  125. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WriteHandler,
  126. void (boost::system::error_code, std::size_t))
  127. async_flush(
  128. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  129. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  130. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  131. async_initiate<WriteHandler,
  132. void (boost::system::error_code, std::size_t)>(
  133. declval<detail::initiate_async_buffered_flush<Stream> >(),
  134. handler, declval<detail::buffered_stream_storage*>())));
  135. /// Write the given data to the stream. Returns the number of bytes written.
  136. /// Throws an exception on failure.
  137. template <typename ConstBufferSequence>
  138. std::size_t write_some(const ConstBufferSequence& buffers);
  139. /// Write the given data to the stream. Returns the number of bytes written,
  140. /// or 0 if an error occurred and the error handler did not throw.
  141. template <typename ConstBufferSequence>
  142. std::size_t write_some(const ConstBufferSequence& buffers,
  143. boost::system::error_code& ec);
  144. /// Start an asynchronous write. The data being written must be valid for the
  145. /// lifetime of the asynchronous operation.
  146. /**
  147. * @par Completion Signature
  148. * @code void(boost::system::error_code, std::size_t) @endcode
  149. */
  150. template <typename ConstBufferSequence,
  151. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  152. std::size_t)) WriteHandler
  153. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  154. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WriteHandler,
  155. void (boost::system::error_code, std::size_t))
  156. async_write_some(const ConstBufferSequence& buffers,
  157. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  158. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  159. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  160. async_initiate<WriteHandler,
  161. void (boost::system::error_code, std::size_t)>(
  162. declval<detail::initiate_async_buffered_write_some<Stream> >(),
  163. handler, declval<detail::buffered_stream_storage*>(), buffers)));
  164. /// Read some data from the stream. Returns the number of bytes read. Throws
  165. /// an exception on failure.
  166. template <typename MutableBufferSequence>
  167. std::size_t read_some(const MutableBufferSequence& buffers)
  168. {
  169. return next_layer_.read_some(buffers);
  170. }
  171. /// Read some data from the stream. Returns the number of bytes read or 0 if
  172. /// an error occurred.
  173. template <typename MutableBufferSequence>
  174. std::size_t read_some(const MutableBufferSequence& buffers,
  175. boost::system::error_code& ec)
  176. {
  177. return next_layer_.read_some(buffers, ec);
  178. }
  179. /// Start an asynchronous read. The buffer into which the data will be read
  180. /// must be valid for the lifetime of the asynchronous operation.
  181. /**
  182. * @par Completion Signature
  183. * @code void(boost::system::error_code, std::size_t) @endcode
  184. */
  185. template <typename MutableBufferSequence,
  186. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  187. std::size_t)) ReadHandler
  188. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  189. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(ReadHandler,
  190. void (boost::system::error_code, std::size_t))
  191. async_read_some(const MutableBufferSequence& buffers,
  192. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  193. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  194. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  195. declval<typename conditional<true, Stream&, ReadHandler>::type>()
  196. .async_read_some(buffers,
  197. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))))
  198. {
  199. return next_layer_.async_read_some(buffers,
  200. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  201. }
  202. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  203. /// Throws an exception on failure.
  204. template <typename MutableBufferSequence>
  205. std::size_t peek(const MutableBufferSequence& buffers)
  206. {
  207. return next_layer_.peek(buffers);
  208. }
  209. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  210. /// or 0 if an error occurred.
  211. template <typename MutableBufferSequence>
  212. std::size_t peek(const MutableBufferSequence& buffers,
  213. boost::system::error_code& ec)
  214. {
  215. return next_layer_.peek(buffers, ec);
  216. }
  217. /// Determine the amount of data that may be read without blocking.
  218. std::size_t in_avail()
  219. {
  220. return next_layer_.in_avail();
  221. }
  222. /// Determine the amount of data that may be read without blocking.
  223. std::size_t in_avail(boost::system::error_code& ec)
  224. {
  225. return next_layer_.in_avail(ec);
  226. }
  227. private:
  228. /// Copy data into the internal buffer from the specified source buffer.
  229. /// Returns the number of bytes copied.
  230. template <typename ConstBufferSequence>
  231. std::size_t copy(const ConstBufferSequence& buffers);
  232. /// The next layer.
  233. Stream next_layer_;
  234. // The data in the buffer.
  235. detail::buffered_stream_storage storage_;
  236. };
  237. } // namespace asio
  238. } // namespace boost
  239. #include <boost/asio/detail/pop_options.hpp>
  240. #include <boost/asio/impl/buffered_write_stream.hpp>
  241. #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP