buffered_write_stream.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //
  2. // buffered_write_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_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/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <cstring>
  19. #include <boost/config.hpp>
  20. #include <boost/type_traits/remove_reference.hpp>
  21. #include "asio/detail/pop_options.hpp"
  22. #include "asio/buffered_write_stream_fwd.hpp"
  23. #include "asio/buffer.hpp"
  24. #include "asio/completion_condition.hpp"
  25. #include "asio/error.hpp"
  26. #include "asio/io_service.hpp"
  27. #include "asio/write.hpp"
  28. #include "asio/detail/bind_handler.hpp"
  29. #include "asio/detail/buffered_stream_storage.hpp"
  30. #include "asio/detail/noncopyable.hpp"
  31. namespace asio {
  32. /// Adds buffering to the write-related operations of a stream.
  33. /**
  34. * The buffered_write_stream class template can be used to add buffering to the
  35. * synchronous and asynchronous write operations of a stream.
  36. *
  37. * @par Thread Safety
  38. * @e Distinct @e objects: Safe.@n
  39. * @e Shared @e objects: Unsafe.
  40. *
  41. * @par Concepts:
  42. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  43. */
  44. template <typename Stream>
  45. class buffered_write_stream
  46. : private noncopyable
  47. {
  48. public:
  49. /// The type of the next layer.
  50. typedef typename boost::remove_reference<Stream>::type next_layer_type;
  51. /// The type of the lowest layer.
  52. typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
  53. #if defined(GENERATING_DOCUMENTATION)
  54. /// The default buffer size.
  55. static const std::size_t default_buffer_size = implementation_defined;
  56. #else
  57. BOOST_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
  58. #endif
  59. /// Construct, passing the specified argument to initialise the next layer.
  60. template <typename Arg>
  61. explicit buffered_write_stream(Arg& a)
  62. : next_layer_(a),
  63. storage_(default_buffer_size)
  64. {
  65. }
  66. /// Construct, passing the specified argument to initialise the next layer.
  67. template <typename Arg>
  68. buffered_write_stream(Arg& a, std::size_t buffer_size)
  69. : next_layer_(a),
  70. storage_(buffer_size)
  71. {
  72. }
  73. /// Get a reference to the next layer.
  74. next_layer_type& next_layer()
  75. {
  76. return next_layer_;
  77. }
  78. /// Get a reference to the lowest layer.
  79. lowest_layer_type& lowest_layer()
  80. {
  81. return next_layer_.lowest_layer();
  82. }
  83. /// Get a const reference to the lowest layer.
  84. const lowest_layer_type& lowest_layer() const
  85. {
  86. return next_layer_.lowest_layer();
  87. }
  88. /// (Deprecated: use get_io_service().) Get the io_service associated with
  89. /// the object.
  90. asio::io_service& io_service()
  91. {
  92. return next_layer_.get_io_service();
  93. }
  94. /// Get the io_service associated with the object.
  95. asio::io_service& get_io_service()
  96. {
  97. return next_layer_.get_io_service();
  98. }
  99. /// Close the stream.
  100. void close()
  101. {
  102. next_layer_.close();
  103. }
  104. /// Close the stream.
  105. asio::error_code close(asio::error_code& ec)
  106. {
  107. return next_layer_.close(ec);
  108. }
  109. /// Flush all data from the buffer to the next layer. Returns the number of
  110. /// bytes written to the next layer on the last write operation. Throws an
  111. /// exception on failure.
  112. std::size_t flush()
  113. {
  114. std::size_t bytes_written = write(next_layer_,
  115. buffer(storage_.data(), storage_.size()));
  116. storage_.consume(bytes_written);
  117. return bytes_written;
  118. }
  119. /// Flush all data from the buffer to the next layer. Returns the number of
  120. /// bytes written to the next layer on the last write operation, or 0 if an
  121. /// error occurred.
  122. std::size_t flush(asio::error_code& ec)
  123. {
  124. std::size_t bytes_written = write(next_layer_,
  125. buffer(storage_.data(), storage_.size()),
  126. transfer_all(), ec);
  127. storage_.consume(bytes_written);
  128. return bytes_written;
  129. }
  130. template <typename WriteHandler>
  131. class flush_handler
  132. {
  133. public:
  134. flush_handler(asio::io_service& io_service,
  135. detail::buffered_stream_storage& storage, WriteHandler handler)
  136. : io_service_(io_service),
  137. storage_(storage),
  138. handler_(handler)
  139. {
  140. }
  141. void operator()(const asio::error_code& ec,
  142. std::size_t bytes_written)
  143. {
  144. storage_.consume(bytes_written);
  145. io_service_.dispatch(detail::bind_handler(handler_, ec, bytes_written));
  146. }
  147. private:
  148. asio::io_service& io_service_;
  149. detail::buffered_stream_storage& storage_;
  150. WriteHandler handler_;
  151. };
  152. /// Start an asynchronous flush.
  153. template <typename WriteHandler>
  154. void async_flush(WriteHandler handler)
  155. {
  156. async_write(next_layer_, buffer(storage_.data(), storage_.size()),
  157. flush_handler<WriteHandler>(get_io_service(), storage_, handler));
  158. }
  159. /// Write the given data to the stream. Returns the number of bytes written.
  160. /// Throws an exception on failure.
  161. template <typename ConstBufferSequence>
  162. std::size_t write_some(const ConstBufferSequence& buffers)
  163. {
  164. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  165. typename ConstBufferSequence::const_iterator end = buffers.end();
  166. size_t total_buffer_size = 0;
  167. for (; iter != end; ++iter)
  168. {
  169. asio::const_buffer buffer(*iter);
  170. total_buffer_size += asio::buffer_size(buffer);
  171. }
  172. if (total_buffer_size == 0)
  173. return 0;
  174. if (storage_.size() == storage_.capacity())
  175. flush();
  176. return copy(buffers);
  177. }
  178. /// Write the given data to the stream. Returns the number of bytes written,
  179. /// or 0 if an error occurred and the error handler did not throw.
  180. template <typename ConstBufferSequence>
  181. std::size_t write_some(const ConstBufferSequence& buffers,
  182. asio::error_code& ec)
  183. {
  184. ec = asio::error_code();
  185. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  186. typename ConstBufferSequence::const_iterator end = buffers.end();
  187. size_t total_buffer_size = 0;
  188. for (; iter != end; ++iter)
  189. {
  190. asio::const_buffer buffer(*iter);
  191. total_buffer_size += asio::buffer_size(buffer);
  192. }
  193. if (total_buffer_size == 0)
  194. return 0;
  195. if (storage_.size() == storage_.capacity() && !flush(ec))
  196. return 0;
  197. return copy(buffers);
  198. }
  199. template <typename ConstBufferSequence, typename WriteHandler>
  200. class write_some_handler
  201. {
  202. public:
  203. write_some_handler(asio::io_service& io_service,
  204. detail::buffered_stream_storage& storage,
  205. const ConstBufferSequence& buffers, WriteHandler handler)
  206. : io_service_(io_service),
  207. storage_(storage),
  208. buffers_(buffers),
  209. handler_(handler)
  210. {
  211. }
  212. void operator()(const asio::error_code& ec, std::size_t)
  213. {
  214. if (ec)
  215. {
  216. std::size_t length = 0;
  217. io_service_.dispatch(detail::bind_handler(handler_, ec, length));
  218. }
  219. else
  220. {
  221. using namespace std; // For memcpy.
  222. std::size_t orig_size = storage_.size();
  223. std::size_t space_avail = storage_.capacity() - orig_size;
  224. std::size_t bytes_copied = 0;
  225. typename ConstBufferSequence::const_iterator iter = buffers_.begin();
  226. typename ConstBufferSequence::const_iterator end = buffers_.end();
  227. for (; iter != end && space_avail > 0; ++iter)
  228. {
  229. std::size_t bytes_avail = buffer_size(*iter);
  230. std::size_t length = (bytes_avail < space_avail)
  231. ? bytes_avail : space_avail;
  232. storage_.resize(orig_size + bytes_copied + length);
  233. memcpy(storage_.data() + orig_size + bytes_copied,
  234. buffer_cast<const void*>(*iter), length);
  235. bytes_copied += length;
  236. space_avail -= length;
  237. }
  238. io_service_.dispatch(detail::bind_handler(handler_, ec, bytes_copied));
  239. }
  240. }
  241. private:
  242. asio::io_service& io_service_;
  243. detail::buffered_stream_storage& storage_;
  244. ConstBufferSequence buffers_;
  245. WriteHandler handler_;
  246. };
  247. /// Start an asynchronous write. The data being written must be valid for the
  248. /// lifetime of the asynchronous operation.
  249. template <typename ConstBufferSequence, typename WriteHandler>
  250. void async_write_some(const ConstBufferSequence& buffers,
  251. WriteHandler handler)
  252. {
  253. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  254. typename ConstBufferSequence::const_iterator end = buffers.end();
  255. size_t total_buffer_size = 0;
  256. for (; iter != end; ++iter)
  257. {
  258. asio::const_buffer buffer(*iter);
  259. total_buffer_size += asio::buffer_size(buffer);
  260. }
  261. if (total_buffer_size == 0)
  262. {
  263. get_io_service().post(detail::bind_handler(
  264. handler, asio::error_code(), 0));
  265. }
  266. else if (storage_.size() == storage_.capacity())
  267. {
  268. async_flush(write_some_handler<ConstBufferSequence, WriteHandler>(
  269. get_io_service(), storage_, buffers, handler));
  270. }
  271. else
  272. {
  273. std::size_t bytes_copied = copy(buffers);
  274. get_io_service().post(detail::bind_handler(
  275. handler, asio::error_code(), bytes_copied));
  276. }
  277. }
  278. /// Read some data from the stream. Returns the number of bytes read. Throws
  279. /// an exception on failure.
  280. template <typename MutableBufferSequence>
  281. std::size_t read_some(const MutableBufferSequence& buffers)
  282. {
  283. return next_layer_.read_some(buffers);
  284. }
  285. /// Read some data from the stream. Returns the number of bytes read or 0 if
  286. /// an error occurred.
  287. template <typename MutableBufferSequence>
  288. std::size_t read_some(const MutableBufferSequence& buffers,
  289. asio::error_code& ec)
  290. {
  291. return next_layer_.read_some(buffers, ec);
  292. }
  293. /// Start an asynchronous read. The buffer into which the data will be read
  294. /// must be valid for the lifetime of the asynchronous operation.
  295. template <typename MutableBufferSequence, typename ReadHandler>
  296. void async_read_some(const MutableBufferSequence& buffers,
  297. ReadHandler handler)
  298. {
  299. next_layer_.async_read_some(buffers, handler);
  300. }
  301. /// Peek at the incoming data on the stream. Returns the number of bytes read.
  302. /// Throws an exception on failure.
  303. template <typename MutableBufferSequence>
  304. std::size_t peek(const MutableBufferSequence& buffers)
  305. {
  306. return next_layer_.peek(buffers);
  307. }
  308. /// Peek at the incoming data on the stream. Returns the number of bytes read,
  309. /// or 0 if an error occurred.
  310. template <typename MutableBufferSequence>
  311. std::size_t peek(const MutableBufferSequence& buffers,
  312. asio::error_code& ec)
  313. {
  314. return next_layer_.peek(buffers, ec);
  315. }
  316. /// Determine the amount of data that may be read without blocking.
  317. std::size_t in_avail()
  318. {
  319. return next_layer_.in_avail();
  320. }
  321. /// Determine the amount of data that may be read without blocking.
  322. std::size_t in_avail(asio::error_code& ec)
  323. {
  324. return next_layer_.in_avail(ec);
  325. }
  326. private:
  327. /// Copy data into the internal buffer from the specified source buffer.
  328. /// Returns the number of bytes copied.
  329. template <typename ConstBufferSequence>
  330. std::size_t copy(const ConstBufferSequence& buffers)
  331. {
  332. using namespace std; // For memcpy.
  333. std::size_t orig_size = storage_.size();
  334. std::size_t space_avail = storage_.capacity() - orig_size;
  335. std::size_t bytes_copied = 0;
  336. typename ConstBufferSequence::const_iterator iter = buffers.begin();
  337. typename ConstBufferSequence::const_iterator end = buffers.end();
  338. for (; iter != end && space_avail > 0; ++iter)
  339. {
  340. std::size_t bytes_avail = buffer_size(*iter);
  341. std::size_t length = (bytes_avail < space_avail)
  342. ? bytes_avail : space_avail;
  343. storage_.resize(orig_size + bytes_copied + length);
  344. memcpy(storage_.data() + orig_size + bytes_copied,
  345. buffer_cast<const void*>(*iter), length);
  346. bytes_copied += length;
  347. space_avail -= length;
  348. }
  349. return bytes_copied;
  350. }
  351. /// The next layer.
  352. Stream next_layer_;
  353. // The data in the buffer.
  354. detail::buffered_stream_storage storage_;
  355. };
  356. } // namespace asio
  357. #include "asio/detail/pop_options.hpp"
  358. #endif // ASIO_BUFFERED_WRITE_STREAM_HPP