buffered_read_stream.hpp 14 KB

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