buffered_stream.hpp 7.5 KB

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