PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/include/boost/asio/buffered_read_stream.hpp

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