/Src/Dependencies/Boost/boost/asio/buffered_write_stream.hpp

http://hadesmem.googlecode.com/ · C++ Header · 353 lines · 245 code · 47 blank · 61 comment · 16 complexity · 12d212c55f58b182124c375f6252043b MD5 · raw file

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