buffered_stream.hpp 7.6 KB

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