buffered_stream.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // buffered_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #include "asio/buffered_read_stream.hpp"
  21. #include "asio/buffered_write_stream.hpp"
  22. #include "asio/buffered_stream_fwd.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/io_service.hpp"
  25. #include "asio/detail/noncopyable.hpp"
  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 boost::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. /// (Deprecated: use get_io_service().) Get the io_service associated with
  79. /// the object.
  80. asio::io_service& io_service()
  81. {
  82. return stream_impl_.get_io_service();
  83. }
  84. /// Get the io_service associated with the object.
  85. asio::io_service& get_io_service()
  86. {
  87. return stream_impl_.get_io_service();
  88. }
  89. /// Close the stream.
  90. void close()
  91. {
  92. stream_impl_.close();
  93. }
  94. /// Close the stream.
  95. asio::error_code close(asio::error_code& ec)
  96. {
  97. return stream_impl_.close(ec);
  98. }
  99. /// Flush all data from the buffer to the next layer. Returns the number of
  100. /// bytes written to the next layer on the last write operation. Throws an
  101. /// exception on failure.
  102. std::size_t flush()
  103. {
  104. return stream_impl_.next_layer().flush();
  105. }
  106. /// Flush all data from the buffer to the next layer. Returns the number of
  107. /// bytes written to the next layer on the last write operation, or 0 if an
  108. /// error occurred.
  109. std::size_t flush(asio::error_code& ec)
  110. {
  111. return stream_impl_.next_layer().flush(ec);
  112. }
  113. /// Start an asynchronous flush.
  114. template <typename WriteHandler>
  115. void async_flush(WriteHandler handler)
  116. {
  117. return stream_impl_.next_layer().async_flush(handler);
  118. }
  119. /// Write the given data to the stream. Returns the number of bytes written.
  120. /// Throws an exception on failure.
  121. template <typename ConstBufferSequence>
  122. std::size_t write_some(const ConstBufferSequence& buffers)
  123. {
  124. return stream_impl_.write_some(buffers);
  125. }
  126. /// Write the given data to the stream. Returns the number of bytes written,
  127. /// or 0 if an error occurred.
  128. template <typename ConstBufferSequence>
  129. std::size_t write_some(const ConstBufferSequence& buffers,
  130. asio::error_code& ec)
  131. {
  132. return stream_impl_.write_some(buffers, ec);
  133. }
  134. /// Start an asynchronous write. The data being written must be valid for the
  135. /// lifetime of the asynchronous operation.
  136. template <typename ConstBufferSequence, typename WriteHandler>
  137. void async_write_some(const ConstBufferSequence& buffers,
  138. WriteHandler handler)
  139. {
  140. stream_impl_.async_write_some(buffers, 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(asio::error_code& ec)
  151. {
  152. return stream_impl_.fill(ec);
  153. }
  154. /// Start an asynchronous fill.
  155. template <typename ReadHandler>
  156. void async_fill(ReadHandler handler)
  157. {
  158. stream_impl_.async_fill(handler);
  159. }
  160. /// Read some data from the stream. Returns the number of bytes read. Throws
  161. /// an exception on failure.
  162. template <typename MutableBufferSequence>
  163. std::size_t read_some(const MutableBufferSequence& buffers)
  164. {
  165. return stream_impl_.read_some(buffers);
  166. }
  167. /// Read some data from the stream. Returns the number of bytes read or 0 if
  168. /// an error occurred.
  169. template <typename MutableBufferSequence>
  170. std::size_t read_some(const MutableBufferSequence& buffers,
  171. asio::error_code& ec)
  172. {
  173. return stream_impl_.read_some(buffers, ec);
  174. }
  175. /// Start an asynchronous read. The buffer into which the data will be read
  176. /// must be valid for the lifetime of the asynchronous operation.
  177. template <typename MutableBufferSequence, typename ReadHandler>
  178. void async_read_some(const MutableBufferSequence& buffers,
  179. ReadHandler handler)
  180. {
  181. stream_impl_.async_read_some(buffers, handler);
  182. }
  183. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  184. /// Throws an exception on failure.
  185. template <typename MutableBufferSequence>
  186. std::size_t peek(const MutableBufferSequence& buffers)
  187. {
  188. return stream_impl_.peek(buffers);
  189. }
  190. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  191. /// or 0 if an error occurred.
  192. template <typename MutableBufferSequence>
  193. std::size_t peek(const MutableBufferSequence& buffers,
  194. asio::error_code& ec)
  195. {
  196. return stream_impl_.peek(buffers, ec);
  197. }
  198. /// Determine the amount of data that may be read without blocking.
  199. std::size_t in_avail()
  200. {
  201. return stream_impl_.in_avail();
  202. }
  203. /// Determine the amount of data that may be read without blocking.
  204. std::size_t in_avail(asio::error_code& ec)
  205. {
  206. return stream_impl_.in_avail(ec);
  207. }
  208. private:
  209. // The buffered write stream.
  210. typedef buffered_write_stream<Stream> write_stream_type;
  211. write_stream_type inner_stream_impl_;
  212. // The buffered read stream.
  213. typedef buffered_read_stream<write_stream_type&> read_stream_type;
  214. read_stream_type stream_impl_;
  215. };
  216. } // namespace asio
  217. #include "asio/detail/pop_options.hpp"
  218. #endif // ASIO_BUFFERED_STREAM_HPP