File indexing completed on 2025-01-18 09:29:28
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_BEAST_CORE_RATE_POLICY_HPP
0011 #define BOOST_BEAST_CORE_RATE_POLICY_HPP
0012
0013 #include <boost/beast/core/detail/config.hpp>
0014 #include <cstdint>
0015 #include <limits>
0016
0017 namespace boost {
0018 namespace beast {
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 class rate_policy_access
0042 {
0043 private:
0044 #ifndef BOOST_BEAST_DOXYGEN
0045 template<class, class, class>
0046 friend class basic_stream;
0047 #endif
0048
0049 template<class Policy>
0050 static
0051 std::size_t
0052 available_read_bytes(Policy& policy)
0053 {
0054 return policy.available_read_bytes();
0055 }
0056
0057 template<class Policy>
0058 static
0059 std::size_t
0060 available_write_bytes(Policy& policy)
0061 {
0062 return policy.available_write_bytes();
0063 }
0064
0065 template<class Policy>
0066 static
0067 void
0068 transfer_read_bytes(
0069 Policy& policy, std::size_t n)
0070 {
0071 return policy.transfer_read_bytes(n);
0072 }
0073
0074 template<class Policy>
0075 static
0076 void
0077 transfer_write_bytes(
0078 Policy& policy, std::size_t n)
0079 {
0080 return policy.transfer_write_bytes(n);
0081 }
0082
0083 template<class Policy>
0084 static
0085 void
0086 on_timer(Policy& policy)
0087 {
0088 return policy.on_timer();
0089 }
0090 };
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 class unlimited_rate_policy
0105 {
0106 #ifndef BOOST_BEAST_DOXYGEN
0107 friend class rate_policy_access;
0108 #endif
0109
0110 static std::size_t constexpr all =
0111 (std::numeric_limits<std::size_t>::max)();
0112
0113 std::size_t
0114 available_read_bytes() const noexcept
0115 {
0116 return all;
0117 }
0118
0119 std::size_t
0120 available_write_bytes() const noexcept
0121 {
0122 return all;
0123 }
0124
0125 void
0126 transfer_read_bytes(std::size_t) const noexcept
0127 {
0128 }
0129
0130 void
0131 transfer_write_bytes(std::size_t) const noexcept
0132 {
0133 }
0134
0135 void
0136 on_timer() const noexcept
0137 {
0138 }
0139 };
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 class simple_rate_policy
0155 {
0156 #ifndef BOOST_BEAST_DOXYGEN
0157 friend class rate_policy_access;
0158 #endif
0159
0160 static std::size_t constexpr all =
0161 (std::numeric_limits<std::size_t>::max)();
0162
0163 std::size_t rd_remain_ = all;
0164 std::size_t wr_remain_ = all;
0165 std::size_t rd_limit_ = all;
0166 std::size_t wr_limit_ = all;
0167
0168 std::size_t
0169 available_read_bytes() const noexcept
0170 {
0171 return rd_remain_;
0172 }
0173
0174 std::size_t
0175 available_write_bytes() const noexcept
0176 {
0177 return wr_remain_;
0178 }
0179
0180 void
0181 transfer_read_bytes(std::size_t n) noexcept
0182 {
0183 if( rd_remain_ != all)
0184 rd_remain_ =
0185 (n < rd_remain_) ? rd_remain_ - n : 0;
0186 }
0187
0188 void
0189 transfer_write_bytes(std::size_t n) noexcept
0190 {
0191 if( wr_remain_ != all)
0192 wr_remain_ =
0193 (n < wr_remain_) ? wr_remain_ - n : 0;
0194 }
0195
0196 void
0197 on_timer() noexcept
0198 {
0199 rd_remain_ = rd_limit_;
0200 wr_remain_ = wr_limit_;
0201 }
0202
0203 public:
0204
0205 void
0206 read_limit(std::size_t bytes_per_second) noexcept
0207 {
0208 rd_limit_ = bytes_per_second;
0209 if( rd_remain_ > bytes_per_second)
0210 rd_remain_ = bytes_per_second;
0211 }
0212
0213
0214 void
0215 write_limit(std::size_t bytes_per_second) noexcept
0216 {
0217 wr_limit_ = bytes_per_second;
0218 if( wr_remain_ > bytes_per_second)
0219 wr_remain_ = bytes_per_second;
0220 }
0221 };
0222
0223 }
0224 }
0225
0226 #endif