buffered_write_stream.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // buffered_write_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. /// Adds buffering to the write-related operations of a stream.
  30. /**
  31. * The buffered_write_stream class template can be used to add buffering to the
  32. * synchronous and asynchronous write operations of a stream.
  33. *
  34. * @par Thread Safety
  35. * @e Distinct @e objects: Safe.@n
  36. * @e Shared @e objects: Unsafe.
  37. *
  38. * @par Concepts:
  39. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  40. */
  41. template <typename Stream>
  42. class buffered_write_stream
  43. : private noncopyable
  44. {
  45. public:
  46. /// The type of the next layer.
  47. typedef typename remove_reference<Stream>::type next_layer_type;
  48. /// The type of the lowest layer.
  49. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  50. /// The type of the executor associated with the object.
  51. typedef typename lowest_layer_type::executor_type executor_type;
  52. #if defined(GENERATING_DOCUMENTATION)
  53. /// The default buffer size.
  54. static const std::size_t default_buffer_size = implementation_defined;
  55. #else
  56. BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
  57. #endif
  58. /// Construct, passing the specified argument to initialise the next layer.
  59. template <typename Arg>
  60. explicit buffered_write_stream(Arg& a)
  61. : next_layer_(a),
  62. storage_(default_buffer_size)
  63. {
  64. }
  65. /// Construct, passing the specified argument to initialise the next layer.
  66. template <typename Arg>
  67. buffered_write_stream(Arg& a, std::size_t buffer_size)
  68. : next_layer_(a),
  69. storage_(buffer_size)
  70. {
  71. }
  72. /// Get a reference to the next layer.
  73. next_layer_type& next_layer()
  74. {
  75. return next_layer_;
  76. }
  77. /// Get a reference to the lowest layer.
  78. lowest_layer_type& lowest_layer()
  79. {
  80. return next_layer_.lowest_layer();
  81. }
  82. /// Get a const reference to the lowest layer.
  83. const lowest_layer_type& lowest_layer() const
  84. {
  85. return next_layer_.lowest_layer();
  86. }
  87. /// Get the executor associated with the object.
  88. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  89. {
  90. return next_layer_.lowest_layer().get_executor();
  91. }
  92. /// Close the stream.
  93. void close()
  94. {
  95. next_layer_.close();
  96. }
  97. /// Close the stream.
  98. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  99. {
  100. next_layer_.close(ec);
  101. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  102. }
  103. /// Flush all data from the buffer to the next layer. Returns the number of
  104. /// bytes written to the next layer on the last write operation. Throws an
  105. /// exception on failure.
  106. std::size_t flush();
  107. /// Flush all data from the buffer to the next layer. Returns the number of
  108. /// bytes written to the next layer on the last write operation, or 0 if an
  109. /// error occurred.
  110. std::size_t flush(boost::system::error_code& ec);
  111. /// Start an asynchronous flush.
  112. template <
  113. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  114. std::size_t)) WriteHandler
  115. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  116. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  117. void (boost::system::error_code, std::size_t))
  118. async_flush(
  119. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  120. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
  121. /// Write the given data to the stream. Returns the number of bytes written.
  122. /// Throws an exception on failure.
  123. template <typename ConstBufferSequence>
  124. std::size_t write_some(const ConstBufferSequence& buffers);
  125. /// Write the given data to the stream. Returns the number of bytes written,
  126. /// or 0 if an error occurred and the error handler did not throw.
  127. template <typename ConstBufferSequence>
  128. std::size_t write_some(const ConstBufferSequence& buffers,
  129. boost::system::error_code& ec);
  130. /// Start an asynchronous write. The data being written must be valid for the
  131. /// lifetime of the asynchronous operation.
  132. template <typename ConstBufferSequence,
  133. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  134. std::size_t)) WriteHandler
  135. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  136. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  137. void (boost::system::error_code, std::size_t))
  138. async_write_some(const ConstBufferSequence& buffers,
  139. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  140. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
  141. /// Read some data from the stream. Returns the number of bytes read. Throws
  142. /// an exception on failure.
  143. template <typename MutableBufferSequence>
  144. std::size_t read_some(const MutableBufferSequence& buffers)
  145. {
  146. return next_layer_.read_some(buffers);
  147. }
  148. /// Read some data from the stream. Returns the number of bytes read or 0 if
  149. /// an error occurred.
  150. template <typename MutableBufferSequence>
  151. std::size_t read_some(const MutableBufferSequence& buffers,
  152. boost::system::error_code& ec)
  153. {
  154. return next_layer_.read_some(buffers, ec);
  155. }
  156. /// Start an asynchronous read. The buffer into which the data will be read
  157. /// must be valid for the lifetime of the asynchronous operation.
  158. template <typename MutableBufferSequence,
  159. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  160. std::size_t)) ReadHandler
  161. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  162. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  163. void (boost::system::error_code, std::size_t))
  164. async_read_some(const MutableBufferSequence& buffers,
  165. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  166. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  167. {
  168. return next_layer_.async_read_some(buffers,
  169. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  170. }
  171. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  172. /// Throws an exception on failure.
  173. template <typename MutableBufferSequence>
  174. std::size_t peek(const MutableBufferSequence& buffers)
  175. {
  176. return next_layer_.peek(buffers);
  177. }
  178. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  179. /// or 0 if an error occurred.
  180. template <typename MutableBufferSequence>
  181. std::size_t peek(const MutableBufferSequence& buffers,
  182. boost::system::error_code& ec)
  183. {
  184. return next_layer_.peek(buffers, ec);
  185. }
  186. /// Determine the amount of data that may be read without blocking.
  187. std::size_t in_avail()
  188. {
  189. return next_layer_.in_avail();
  190. }
  191. /// Determine the amount of data that may be read without blocking.
  192. std::size_t in_avail(boost::system::error_code& ec)
  193. {
  194. return next_layer_.in_avail(ec);
  195. }
  196. private:
  197. /// Copy data into the internal buffer from the specified source buffer.
  198. /// Returns the number of bytes copied.
  199. template <typename ConstBufferSequence>
  200. std::size_t copy(const ConstBufferSequence& buffers);
  201. /// The next layer.
  202. Stream next_layer_;
  203. // The data in the buffer.
  204. detail::buffered_stream_storage storage_;
  205. };
  206. } // namespace asio
  207. } // namespace boost
  208. #include <boost/asio/detail/pop_options.hpp>
  209. #include <boost/asio/impl/buffered_write_stream.hpp>
  210. #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP