Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:28

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
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 /** Helper class to assist implementing a <em>RatePolicy</em>.
0021 
0022     This class is used by the implementation to gain access to the
0023     private members of a user-defined object meeting the requirements
0024     of <em>RatePolicy</em>. To use it, simply declare it as a friend
0025     in your class:
0026     
0027     @par Example
0028     @code
0029     class custom_rate_policy
0030     {
0031         friend class beast::rate_policy_access;
0032         ...
0033     @endcode
0034 
0035     @par Concepts
0036 
0037     @li <em>RatePolicy</em>
0038 
0039     @see beast::basic_stream
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 /** A rate policy with unlimited throughput.
0095 
0096     This rate policy object does not apply any rate limit.
0097 
0098     @par Concepts
0099 
0100     @li <em>RatePolicy</em>
0101 
0102     @see beast::basic_stream, beast::tcp_stream
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 /** A rate policy with simple, configurable limits on reads and writes.
0144 
0145     This rate policy allows for simple individual limits on the amount
0146     of bytes per second allowed for reads and writes.
0147 
0148     @par Concepts
0149 
0150     @li <em>RatePolicy</em>
0151 
0152     @see beast::basic_stream
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     /// Set the limit of bytes per second to read
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     /// Set the limit of bytes per second to write
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 } // beast
0224 } // boost
0225 
0226 #endif