buffered_write_stream.hpp 7.3 KB

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