buffered_stream.hpp 7.2 KB

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