rate_policy.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef BOOST_BEAST_DOXYGEN
  36. template<class, class, class>
  37. friend class basic_stream;
  38. #endif
  39. template<class Policy>
  40. static
  41. std::size_t
  42. available_read_bytes(Policy& policy)
  43. {
  44. return policy.available_read_bytes();
  45. }
  46. template<class Policy>
  47. static
  48. std::size_t
  49. available_write_bytes(Policy& policy)
  50. {
  51. return policy.available_write_bytes();
  52. }
  53. template<class Policy>
  54. static
  55. void
  56. transfer_read_bytes(
  57. Policy& policy, std::size_t n)
  58. {
  59. return policy.transfer_read_bytes(n);
  60. }
  61. template<class Policy>
  62. static
  63. void
  64. transfer_write_bytes(
  65. Policy& policy, std::size_t n)
  66. {
  67. return policy.transfer_write_bytes(n);
  68. }
  69. template<class Policy>
  70. static
  71. void
  72. on_timer(Policy& policy)
  73. {
  74. return policy.on_timer();
  75. }
  76. };
  77. //------------------------------------------------------------------------------
  78. /** A rate policy with unlimited throughput.
  79. This rate policy object does not apply any rate limit.
  80. @par Concepts
  81. @li <em>RatePolicy</em>
  82. @see beast::basic_stream, beast::tcp_stream
  83. */
  84. class unlimited_rate_policy
  85. {
  86. #ifndef BOOST_BEAST_DOXYGEN
  87. friend class rate_policy_access;
  88. #endif
  89. static std::size_t constexpr all =
  90. (std::numeric_limits<std::size_t>::max)();
  91. std::size_t
  92. available_read_bytes() const noexcept
  93. {
  94. return all;
  95. }
  96. std::size_t
  97. available_write_bytes() const noexcept
  98. {
  99. return all;
  100. }
  101. void
  102. transfer_read_bytes(std::size_t) const noexcept
  103. {
  104. }
  105. void
  106. transfer_write_bytes(std::size_t) const noexcept
  107. {
  108. }
  109. void
  110. on_timer() const noexcept
  111. {
  112. }
  113. };
  114. //------------------------------------------------------------------------------
  115. /** A rate policy with simple, configurable limits on reads and writes.
  116. This rate policy allows for simple individual limits on the amount
  117. of bytes per second allowed for reads and writes.
  118. @par Concepts
  119. @li <em>RatePolicy</em>
  120. @see beast::basic_stream
  121. */
  122. class simple_rate_policy
  123. {
  124. #ifndef BOOST_BEAST_DOXYGEN
  125. friend class rate_policy_access;
  126. #endif
  127. static std::size_t constexpr all =
  128. (std::numeric_limits<std::size_t>::max)();
  129. std::size_t rd_remain_ = all;
  130. std::size_t wr_remain_ = all;
  131. std::size_t rd_limit_ = all;
  132. std::size_t wr_limit_ = all;
  133. std::size_t
  134. available_read_bytes() const noexcept
  135. {
  136. return rd_remain_;
  137. }
  138. std::size_t
  139. available_write_bytes() const noexcept
  140. {
  141. return wr_remain_;
  142. }
  143. void
  144. transfer_read_bytes(std::size_t n) noexcept
  145. {
  146. if( rd_remain_ != all)
  147. rd_remain_ =
  148. (n < rd_remain_) ? rd_remain_ - n : 0;
  149. }
  150. void
  151. transfer_write_bytes(std::size_t n) noexcept
  152. {
  153. if( wr_remain_ != all)
  154. wr_remain_ =
  155. (n < wr_remain_) ? wr_remain_ - n : 0;
  156. }
  157. void
  158. on_timer() noexcept
  159. {
  160. rd_remain_ = rd_limit_;
  161. wr_remain_ = wr_limit_;
  162. }
  163. public:
  164. /// Set the limit of bytes per second to read
  165. void
  166. read_limit(std::size_t bytes_per_second) noexcept
  167. {
  168. rd_limit_ = bytes_per_second;
  169. if( rd_remain_ > bytes_per_second)
  170. rd_remain_ = bytes_per_second;
  171. }
  172. /// Set the limit of bytes per second to write
  173. void
  174. write_limit(std::size_t bytes_per_second) noexcept
  175. {
  176. wr_limit_ = bytes_per_second;
  177. if( wr_remain_ > bytes_per_second)
  178. wr_remain_ = bytes_per_second;
  179. }
  180. };
  181. } // beast
  182. } // boost
  183. #endif