rate_policy.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_RATE_POLICY_HPP
  10. #define BOOST_BEAST_CORE_RATE_POLICY_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <cstdint>
  13. #include <limits>
  14. namespace boost {
  15. namespace beast {
  16. /** Helper class to assist implementing a <em>RatePolicy</em>.
  17. This class is used by the implementation to gain access to the
  18. private members of a user-defined object meeting the requirements
  19. of <em>RatePolicy</em>. To use it, simply declare it as a friend
  20. in your class:
  21. @par Example
  22. @code
  23. class custom_rate_policy
  24. {
  25. friend class beast::rate_policy_access;
  26. ...
  27. @endcode
  28. @par Concepts
  29. @li <em>RatePolicy</em>
  30. @see beast::basic_stream
  31. */
  32. class rate_policy_access
  33. {
  34. private:
  35. template<class, class, class>
  36. friend class basic_stream;
  37. template<class Policy>
  38. static
  39. std::size_t
  40. available_read_bytes(Policy& policy)
  41. {
  42. return policy.available_read_bytes();
  43. }
  44. template<class Policy>
  45. static
  46. std::size_t
  47. available_write_bytes(Policy& policy)
  48. {
  49. return policy.available_write_bytes();
  50. }
  51. template<class Policy>
  52. static
  53. void
  54. transfer_read_bytes(
  55. Policy& policy, std::size_t n)
  56. {
  57. return policy.transfer_read_bytes(n);
  58. }
  59. template<class Policy>
  60. static
  61. void
  62. transfer_write_bytes(
  63. Policy& policy, std::size_t n)
  64. {
  65. return policy.transfer_write_bytes(n);
  66. }
  67. template<class Policy>
  68. static
  69. void
  70. on_timer(Policy& policy)
  71. {
  72. return policy.on_timer();
  73. }
  74. };
  75. //------------------------------------------------------------------------------
  76. /** A rate policy with unlimited throughput.
  77. This rate policy object does not apply any rate limit.
  78. @par Concepts
  79. @li <em>RatePolicy</em>
  80. @see beast::basic_stream, beast::tcp_stream
  81. */
  82. class unlimited_rate_policy
  83. {
  84. friend class rate_policy_access;
  85. static std::size_t constexpr all =
  86. (std::numeric_limits<std::size_t>::max)();
  87. std::size_t
  88. available_read_bytes() const noexcept
  89. {
  90. return all;
  91. }
  92. std::size_t
  93. available_write_bytes() const noexcept
  94. {
  95. return all;
  96. }
  97. void
  98. transfer_read_bytes(std::size_t) const noexcept
  99. {
  100. }
  101. void
  102. transfer_write_bytes(std::size_t) const noexcept
  103. {
  104. }
  105. void
  106. on_timer() const noexcept
  107. {
  108. }
  109. };
  110. //------------------------------------------------------------------------------
  111. /** A rate policy with simple, configurable limits on reads and writes.
  112. This rate policy allows for simple individual limits on the amount
  113. of bytes per second allowed for reads and writes.
  114. @par Concepts
  115. @li <em>RatePolicy</em>
  116. @see beast::basic_stream
  117. */
  118. class simple_rate_policy
  119. {
  120. friend class rate_policy_access;
  121. static std::size_t constexpr all =
  122. (std::numeric_limits<std::size_t>::max)();
  123. std::size_t rd_remain_ = all;
  124. std::size_t wr_remain_ = all;
  125. std::size_t rd_limit_ = all;
  126. std::size_t wr_limit_ = all;
  127. std::size_t
  128. available_read_bytes() const noexcept
  129. {
  130. return rd_remain_;
  131. }
  132. std::size_t
  133. available_write_bytes() const noexcept
  134. {
  135. return wr_remain_;
  136. }
  137. void
  138. transfer_read_bytes(std::size_t n) noexcept
  139. {
  140. if( rd_remain_ != all)
  141. rd_remain_ =
  142. (n < rd_remain_) ? rd_remain_ - n : 0;
  143. }
  144. void
  145. transfer_write_bytes(std::size_t n) noexcept
  146. {
  147. if( wr_remain_ != all)
  148. wr_remain_ =
  149. (n < wr_remain_) ? wr_remain_ - n : 0;
  150. }
  151. void
  152. on_timer() noexcept
  153. {
  154. rd_remain_ = rd_limit_;
  155. wr_remain_ = wr_limit_;
  156. }
  157. public:
  158. /// Set the limit of bytes per second to read
  159. void
  160. read_limit(std::size_t bytes_per_second) noexcept
  161. {
  162. rd_limit_ = bytes_per_second;
  163. if( rd_remain_ > bytes_per_second)
  164. rd_remain_ = bytes_per_second;
  165. }
  166. /// Set the limit of bytes per second to write
  167. void
  168. write_limit(std::size_t bytes_per_second) noexcept
  169. {
  170. wr_limit_ = bytes_per_second;
  171. if( wr_remain_ > bytes_per_second)
  172. wr_remain_ = bytes_per_second;
  173. }
  174. };
  175. } // beast
  176. } // boost
  177. #endif