3
Copyright (c) 2007, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_BANDWIDTH_LIMIT_HPP_INCLUDED
34
#define TORRENT_BANDWIDTH_LIMIT_HPP_INCLUDED
36
#include <boost/integer_traits.hpp>
38
#include "libtorrent/assert.hpp"
40
namespace libtorrent {
42
// member of peer_connection
43
struct bandwidth_limit
45
static const int inf = boost::integer_traits<int>::const_max;
53
void throttle(int limit)
55
TORRENT_ASSERT(limit > 0);
56
m_local_limit = limit;
64
void assign(int amount)
66
TORRENT_ASSERT(amount >= 0);
67
m_current_rate += amount;
68
m_quota_left += amount;
71
void use_quota(int amount)
73
TORRENT_ASSERT(amount <= m_quota_left);
74
m_quota_left -= amount;
77
int quota_left() const
79
return (std::max)(m_quota_left, 0);
82
void expire(int amount)
84
TORRENT_ASSERT(amount >= 0);
85
m_current_rate -= amount;
88
int max_assignable() const
90
if (m_local_limit == inf) return inf;
91
if (m_local_limit <= m_current_rate) return 0;
92
return m_local_limit - m_current_rate;
97
// this is the amount of bandwidth we have
98
// been assigned without using yet. i.e.
99
// the bandwidth that we use up every time
100
// we receive or send a message. Once this
101
// hits zero, we need to request more
102
// bandwidth from the torrent which
103
// in turn will request bandwidth from
104
// the bandwidth manager
107
// the local limit is the number of bytes
108
// per window size we are allowed to use.
111
// the current rate is the number of
112
// bytes we have been assigned within