completion_condition.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // completion_condition.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_COMPLETION_CONDITION_HPP
  11. #define BOOST_ASIO_COMPLETION_CONDITION_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/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. // The default maximum number of bytes to transfer in a single operation.
  22. enum default_max_transfer_size_t { default_max_transfer_size = 65536 };
  23. // Adapt result of old-style completion conditions (which had a bool result
  24. // where true indicated that the operation was complete).
  25. inline std::size_t adapt_completion_condition_result(bool result)
  26. {
  27. return result ? 0 : default_max_transfer_size;
  28. }
  29. // Adapt result of current completion conditions (which have a size_t result
  30. // where 0 means the operation is complete, and otherwise the result is the
  31. // maximum number of bytes to transfer on the next underlying operation).
  32. inline std::size_t adapt_completion_condition_result(std::size_t result)
  33. {
  34. return result;
  35. }
  36. class transfer_all_t
  37. {
  38. public:
  39. typedef std::size_t result_type;
  40. template <typename Error>
  41. std::size_t operator()(const Error& err, std::size_t)
  42. {
  43. return !!err ? 0 : default_max_transfer_size;
  44. }
  45. };
  46. class transfer_at_least_t
  47. {
  48. public:
  49. typedef std::size_t result_type;
  50. explicit transfer_at_least_t(std::size_t minimum)
  51. : minimum_(minimum)
  52. {
  53. }
  54. template <typename Error>
  55. std::size_t operator()(const Error& err, std::size_t bytes_transferred)
  56. {
  57. return (!!err || bytes_transferred >= minimum_)
  58. ? 0 : default_max_transfer_size;
  59. }
  60. private:
  61. std::size_t minimum_;
  62. };
  63. class transfer_exactly_t
  64. {
  65. public:
  66. typedef std::size_t result_type;
  67. explicit transfer_exactly_t(std::size_t size)
  68. : size_(size)
  69. {
  70. }
  71. template <typename Error>
  72. std::size_t operator()(const Error& err, std::size_t bytes_transferred)
  73. {
  74. return (!!err || bytes_transferred >= size_) ? 0 :
  75. (size_ - bytes_transferred < default_max_transfer_size
  76. ? size_ - bytes_transferred : std::size_t(default_max_transfer_size));
  77. }
  78. private:
  79. std::size_t size_;
  80. };
  81. } // namespace detail
  82. /**
  83. * @defgroup completion_condition Completion Condition Function Objects
  84. *
  85. * Function objects used for determining when a read or write operation should
  86. * complete.
  87. */
  88. /*@{*/
  89. /// Return a completion condition function object that indicates that a read or
  90. /// write operation should continue until all of the data has been transferred,
  91. /// or until an error occurs.
  92. /**
  93. * This function is used to create an object, of unspecified type, that meets
  94. * CompletionCondition requirements.
  95. *
  96. * @par Example
  97. * Reading until a buffer is full:
  98. * @code
  99. * boost::array<char, 128> buf;
  100. * boost::system::error_code ec;
  101. * std::size_t n = boost::asio::read(
  102. * sock, boost::asio::buffer(buf),
  103. * boost::asio::transfer_all(), ec);
  104. * if (ec)
  105. * {
  106. * // An error occurred.
  107. * }
  108. * else
  109. * {
  110. * // n == 128
  111. * }
  112. * @endcode
  113. */
  114. #if defined(GENERATING_DOCUMENTATION)
  115. unspecified transfer_all();
  116. #else
  117. inline detail::transfer_all_t transfer_all()
  118. {
  119. return detail::transfer_all_t();
  120. }
  121. #endif
  122. /// Return a completion condition function object that indicates that a read or
  123. /// write operation should continue until a minimum number of bytes has been
  124. /// transferred, or until an error occurs.
  125. /**
  126. * This function is used to create an object, of unspecified type, that meets
  127. * CompletionCondition requirements.
  128. *
  129. * @par Example
  130. * Reading until a buffer is full or contains at least 64 bytes:
  131. * @code
  132. * boost::array<char, 128> buf;
  133. * boost::system::error_code ec;
  134. * std::size_t n = boost::asio::read(
  135. * sock, boost::asio::buffer(buf),
  136. * boost::asio::transfer_at_least(64), ec);
  137. * if (ec)
  138. * {
  139. * // An error occurred.
  140. * }
  141. * else
  142. * {
  143. * // n >= 64 && n <= 128
  144. * }
  145. * @endcode
  146. */
  147. #if defined(GENERATING_DOCUMENTATION)
  148. unspecified transfer_at_least(std::size_t minimum);
  149. #else
  150. inline detail::transfer_at_least_t transfer_at_least(std::size_t minimum)
  151. {
  152. return detail::transfer_at_least_t(minimum);
  153. }
  154. #endif
  155. /// Return a completion condition function object that indicates that a read or
  156. /// write operation should continue until an exact number of bytes has been
  157. /// transferred, or until an error occurs.
  158. /**
  159. * This function is used to create an object, of unspecified type, that meets
  160. * CompletionCondition requirements.
  161. *
  162. * @par Example
  163. * Reading until a buffer is full or contains exactly 64 bytes:
  164. * @code
  165. * boost::array<char, 128> buf;
  166. * boost::system::error_code ec;
  167. * std::size_t n = boost::asio::read(
  168. * sock, boost::asio::buffer(buf),
  169. * boost::asio::transfer_exactly(64), ec);
  170. * if (ec)
  171. * {
  172. * // An error occurred.
  173. * }
  174. * else
  175. * {
  176. * // n == 64
  177. * }
  178. * @endcode
  179. */
  180. #if defined(GENERATING_DOCUMENTATION)
  181. unspecified transfer_exactly(std::size_t size);
  182. #else
  183. inline detail::transfer_exactly_t transfer_exactly(std::size_t size)
  184. {
  185. return detail::transfer_exactly_t(size);
  186. }
  187. #endif
  188. /*@}*/
  189. } // namespace asio
  190. } // namespace boost
  191. #include <boost/asio/detail/pop_options.hpp>
  192. #endif // BOOST_ASIO_COMPLETION_CONDITION_HPP