buffered_read_stream.hpp 13 KB

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