buffered_write_stream.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // buffered_write_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_WRITE_STREAM_HPP
  11. #define ASIO_BUFFERED_WRITE_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_write_stream_fwd.hpp"
  18. #include "asio/buffer.hpp"
  19. #include "asio/completion_condition.hpp"
  20. #include "asio/detail/bind_handler.hpp"
  21. #include "asio/detail/buffered_stream_storage.hpp"
  22. #include "asio/detail/noncopyable.hpp"
  23. #include "asio/detail/type_traits.hpp"
  24. #include "asio/error.hpp"
  25. #include "asio/io_service.hpp"
  26. #include "asio/write.hpp"
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. /// Adds buffering to the write-related operations of a stream.
  30. /**
  31. * The buffered_write_stream class template can be used to add buffering to the
  32. * synchronous and asynchronous write operations of a stream.
  33. *
  34. * @par Thread Safety
  35. * @e Distinct @e objects: Safe.@n
  36. * @e Shared @e objects: Unsafe.
  37. *
  38. * @par Concepts:
  39. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  40. */
  41. template <typename Stream>
  42. class buffered_write_stream
  43. : private noncopyable
  44. {
  45. public:
  46. /// The type of the next layer.
  47. typedef typename remove_reference<Stream>::type next_layer_type;
  48. /// The type of the lowest layer.
  49. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  50. #if defined(GENERATING_DOCUMENTATION)
  51. /// The default buffer size.
  52. static const std::size_t default_buffer_size = implementation_defined;
  53. #else
  54. ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
  55. #endif
  56. /// Construct, passing the specified argument to initialise the next layer.
  57. template <typename Arg>
  58. explicit buffered_write_stream(Arg& a)
  59. : next_layer_(a),
  60. storage_(default_buffer_size)
  61. {
  62. }
  63. /// Construct, passing the specified argument to initialise the next layer.
  64. template <typename Arg>
  65. buffered_write_stream(Arg& a, std::size_t buffer_size)
  66. : next_layer_(a),
  67. storage_(buffer_size)
  68. {
  69. }
  70. /// Get a reference to the next layer.
  71. next_layer_type& next_layer()
  72. {
  73. return next_layer_;
  74. }
  75. /// Get a reference to the lowest layer.
  76. lowest_layer_type& lowest_layer()
  77. {
  78. return next_layer_.lowest_layer();
  79. }
  80. /// Get a const reference to the lowest layer.
  81. const lowest_layer_type& lowest_layer() const
  82. {
  83. return next_layer_.lowest_layer();
  84. }
  85. /// Get the io_service associated with the object.
  86. asio::io_service& get_io_service()
  87. {
  88. return next_layer_.get_io_service();
  89. }
  90. /// Close the stream.
  91. void close()
  92. {
  93. next_layer_.close();
  94. }
  95. /// Close the stream.
  96. asio::error_code close(asio::error_code& ec)
  97. {
  98. return next_layer_.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. /// 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. /// Start an asynchronous flush.
  109. template <typename WriteHandler>
  110. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  111. void (asio::error_code, std::size_t))
  112. async_flush(ASIO_MOVE_ARG(WriteHandler) handler);
  113. /// Write the given data to the stream. Returns the number of bytes written.
  114. /// Throws an exception on failure.
  115. template <typename ConstBufferSequence>
  116. std::size_t write_some(const ConstBufferSequence& buffers);
  117. /// Write the given data to the stream. Returns the number of bytes written,
  118. /// or 0 if an error occurred and the error handler did not throw.
  119. template <typename ConstBufferSequence>
  120. std::size_t write_some(const ConstBufferSequence& buffers,
  121. asio::error_code& ec);
  122. /// Start an asynchronous write. The data being written must be valid for the
  123. /// lifetime of the asynchronous operation.
  124. template <typename ConstBufferSequence, typename WriteHandler>
  125. ASIO_INITFN_RESULT_TYPE(WriteHandler,
  126. void (asio::error_code, std::size_t))
  127. async_write_some(const ConstBufferSequence& buffers,
  128. ASIO_MOVE_ARG(WriteHandler) handler);
  129. /// Read some data from the stream. Returns the number of bytes read. Throws
  130. /// an exception on failure.
  131. template <typename MutableBufferSequence>
  132. std::size_t read_some(const MutableBufferSequence& buffers)
  133. {
  134. return next_layer_.read_some(buffers);
  135. }
  136. /// Read some data from the stream. Returns the number of bytes read or 0 if
  137. /// an error occurred.
  138. template <typename MutableBufferSequence>
  139. std::size_t read_some(const MutableBufferSequence& buffers,
  140. asio::error_code& ec)
  141. {
  142. return next_layer_.read_some(buffers, ec);
  143. }
  144. /// Start an asynchronous read. The buffer into which the data will be read
  145. /// must be valid for the lifetime of the asynchronous operation.
  146. template <typename MutableBufferSequence, typename ReadHandler>
  147. ASIO_INITFN_RESULT_TYPE(ReadHandler,
  148. void (asio::error_code, std::size_t))
  149. async_read_some(const MutableBufferSequence& buffers,
  150. ASIO_MOVE_ARG(ReadHandler) handler)
  151. {
  152. detail::async_result_init<
  153. ReadHandler, void (asio::error_code, std::size_t)> init(
  154. ASIO_MOVE_CAST(ReadHandler)(handler));
  155. next_layer_.async_read_some(buffers,
  156. ASIO_MOVE_CAST(ASIO_HANDLER_TYPE(ReadHandler,
  157. void (asio::error_code, std::size_t)))(init.handler));
  158. return init.result.get();
  159. }
  160. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  161. /// Throws an exception on failure.
  162. template <typename MutableBufferSequence>
  163. std::size_t peek(const MutableBufferSequence& buffers)
  164. {
  165. return next_layer_.peek(buffers);
  166. }
  167. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  168. /// or 0 if an error occurred.
  169. template <typename MutableBufferSequence>
  170. std::size_t peek(const MutableBufferSequence& buffers,
  171. asio::error_code& ec)
  172. {
  173. return next_layer_.peek(buffers, ec);
  174. }
  175. /// Determine the amount of data that may be read without blocking.
  176. std::size_t in_avail()
  177. {
  178. return next_layer_.in_avail();
  179. }
  180. /// Determine the amount of data that may be read without blocking.
  181. std::size_t in_avail(asio::error_code& ec)
  182. {
  183. return next_layer_.in_avail(ec);
  184. }
  185. private:
  186. /// Copy data into the internal buffer from the specified source buffer.
  187. /// Returns the number of bytes copied.
  188. template <typename ConstBufferSequence>
  189. std::size_t copy(const ConstBufferSequence& buffers);
  190. /// The next layer.
  191. Stream next_layer_;
  192. // The data in the buffer.
  193. detail::buffered_stream_storage storage_;
  194. };
  195. } // namespace asio
  196. #include "asio/detail/pop_options.hpp"
  197. #include "asio/impl/buffered_write_stream.hpp"
  198. #endif // ASIO_BUFFERED_WRITE_STREAM_HPP