buffered_stream.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // buffered_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_STREAM_HPP
  11. #define BOOST_ASIO_BUFFERED_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/async_result.hpp>
  18. #include <boost/asio/buffered_read_stream.hpp>
  19. #include <boost/asio/buffered_write_stream.hpp>
  20. #include <boost/asio/buffered_stream_fwd.hpp>
  21. #include <boost/asio/detail/noncopyable.hpp>
  22. #include <boost/asio/error.hpp>
  23. #include <boost/asio/io_service.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. /// Adds buffering to the read- and write-related operations of a stream.
  28. /**
  29. * The buffered_stream class template can be used to add buffering to the
  30. * synchronous and asynchronous read and write operations of a stream.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. *
  36. * @par Concepts:
  37. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  38. */
  39. template <typename Stream>
  40. class buffered_stream
  41. : private noncopyable
  42. {
  43. public:
  44. /// The type of the next layer.
  45. typedef typename remove_reference<Stream>::type next_layer_type;
  46. /// The type of the lowest layer.
  47. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  48. /// Construct, passing the specified argument to initialise the next layer.
  49. template <typename Arg>
  50. explicit buffered_stream(Arg& a)
  51. : inner_stream_impl_(a),
  52. stream_impl_(inner_stream_impl_)
  53. {
  54. }
  55. /// Construct, passing the specified argument to initialise the next layer.
  56. template <typename Arg>
  57. explicit buffered_stream(Arg& a, std::size_t read_buffer_size,
  58. std::size_t write_buffer_size)
  59. : inner_stream_impl_(a, write_buffer_size),
  60. stream_impl_(inner_stream_impl_, read_buffer_size)
  61. {
  62. }
  63. /// Get a reference to the next layer.
  64. next_layer_type& next_layer()
  65. {
  66. return stream_impl_.next_layer().next_layer();
  67. }
  68. /// Get a reference to the lowest layer.
  69. lowest_layer_type& lowest_layer()
  70. {
  71. return stream_impl_.lowest_layer();
  72. }
  73. /// Get a const reference to the lowest layer.
  74. const lowest_layer_type& lowest_layer() const
  75. {
  76. return stream_impl_.lowest_layer();
  77. }
  78. /// Get the io_service associated with the object.
  79. boost::asio::io_service& get_io_service()
  80. {
  81. return stream_impl_.get_io_service();
  82. }
  83. /// Close the stream.
  84. void close()
  85. {
  86. stream_impl_.close();
  87. }
  88. /// Close the stream.
  89. boost::system::error_code close(boost::system::error_code& ec)
  90. {
  91. return stream_impl_.close(ec);
  92. }
  93. /// Flush all data from the buffer to the next layer. Returns the number of
  94. /// bytes written to the next layer on the last write operation. Throws an
  95. /// exception on failure.
  96. std::size_t flush()
  97. {
  98. return stream_impl_.next_layer().flush();
  99. }
  100. /// Flush all data from the buffer to the next layer. Returns the number of
  101. /// bytes written to the next layer on the last write operation, or 0 if an
  102. /// error occurred.
  103. std::size_t flush(boost::system::error_code& ec)
  104. {
  105. return stream_impl_.next_layer().flush(ec);
  106. }
  107. /// Start an asynchronous flush.
  108. template <typename WriteHandler>
  109. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  110. void (boost::system::error_code, std::size_t))
  111. async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  112. {
  113. return stream_impl_.next_layer().async_flush(
  114. BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  115. }
  116. /// Write the given data to the stream. Returns the number of bytes written.
  117. /// Throws an exception on failure.
  118. template <typename ConstBufferSequence>
  119. std::size_t write_some(const ConstBufferSequence& buffers)
  120. {
  121. return stream_impl_.write_some(buffers);
  122. }
  123. /// Write the given data to the stream. Returns the number of bytes written,
  124. /// or 0 if an error occurred.
  125. template <typename ConstBufferSequence>
  126. std::size_t write_some(const ConstBufferSequence& buffers,
  127. boost::system::error_code& ec)
  128. {
  129. return stream_impl_.write_some(buffers, ec);
  130. }
  131. /// Start an asynchronous write. The data being written must be valid for the
  132. /// lifetime of the asynchronous operation.
  133. template <typename ConstBufferSequence, typename WriteHandler>
  134. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  135. void (boost::system::error_code, std::size_t))
  136. async_write_some(const ConstBufferSequence& buffers,
  137. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  138. {
  139. return stream_impl_.async_write_some(buffers,
  140. BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
  141. }
  142. /// Fill the buffer with some data. Returns the number of bytes placed in the
  143. /// buffer as a result of the operation. Throws an exception on failure.
  144. std::size_t fill()
  145. {
  146. return stream_impl_.fill();
  147. }
  148. /// Fill the buffer with some data. Returns the number of bytes placed in the
  149. /// buffer as a result of the operation, or 0 if an error occurred.
  150. std::size_t fill(boost::system::error_code& ec)
  151. {
  152. return stream_impl_.fill(ec);
  153. }
  154. /// Start an asynchronous fill.
  155. template <typename ReadHandler>
  156. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  157. void (boost::system::error_code, std::size_t))
  158. async_fill(BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  159. {
  160. return stream_impl_.async_fill(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  161. }
  162. /// Read some data from the stream. Returns the number of bytes read. Throws
  163. /// an exception on failure.
  164. template <typename MutableBufferSequence>
  165. std::size_t read_some(const MutableBufferSequence& buffers)
  166. {
  167. return stream_impl_.read_some(buffers);
  168. }
  169. /// Read some data from the stream. Returns the number of bytes read or 0 if
  170. /// an error occurred.
  171. template <typename MutableBufferSequence>
  172. std::size_t read_some(const MutableBufferSequence& buffers,
  173. boost::system::error_code& ec)
  174. {
  175. return stream_impl_.read_some(buffers, ec);
  176. }
  177. /// Start an asynchronous read. The buffer into which the data will be read
  178. /// must be valid for the lifetime of the asynchronous operation.
  179. template <typename MutableBufferSequence, typename ReadHandler>
  180. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  181. void (boost::system::error_code, std::size_t))
  182. async_read_some(const MutableBufferSequence& buffers,
  183. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  184. {
  185. return stream_impl_.async_read_some(buffers,
  186. BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  187. }
  188. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  189. /// Throws an exception on failure.
  190. template <typename MutableBufferSequence>
  191. std::size_t peek(const MutableBufferSequence& buffers)
  192. {
  193. return stream_impl_.peek(buffers);
  194. }
  195. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  196. /// or 0 if an error occurred.
  197. template <typename MutableBufferSequence>
  198. std::size_t peek(const MutableBufferSequence& buffers,
  199. boost::system::error_code& ec)
  200. {
  201. return stream_impl_.peek(buffers, ec);
  202. }
  203. /// Determine the amount of data that may be read without blocking.
  204. std::size_t in_avail()
  205. {
  206. return stream_impl_.in_avail();
  207. }
  208. /// Determine the amount of data that may be read without blocking.
  209. std::size_t in_avail(boost::system::error_code& ec)
  210. {
  211. return stream_impl_.in_avail(ec);
  212. }
  213. private:
  214. // The buffered write stream.
  215. typedef buffered_write_stream<Stream> write_stream_type;
  216. write_stream_type inner_stream_impl_;
  217. // The buffered read stream.
  218. typedef buffered_read_stream<write_stream_type&> read_stream_type;
  219. read_stream_type stream_impl_;
  220. };
  221. } // namespace asio
  222. } // namespace boost
  223. #include <boost/asio/detail/pop_options.hpp>
  224. #endif // BOOST_ASIO_BUFFERED_STREAM_HPP