2007-01-10 18:16:55 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_BANDWIDTH_MANAGER_HPP_INCLUDED
|
|
|
|
#define TORRENT_BANDWIDTH_MANAGER_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#include <boost/intrusive_ptr.hpp>
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/integer_traits.hpp>
|
2007-01-29 08:39:33 +01:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2007-01-10 18:16:55 +01:00
|
|
|
#include <deque>
|
|
|
|
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
|
2007-01-10 18:16:55 +01:00
|
|
|
using boost::weak_ptr;
|
|
|
|
using boost::shared_ptr;
|
|
|
|
using boost::intrusive_ptr;
|
|
|
|
using boost::bind;
|
|
|
|
|
2007-08-27 12:20:23 +02:00
|
|
|
//#define TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
// the maximum block of bandwidth quota to
|
|
|
|
// hand out is 33kB. The block size may
|
|
|
|
// be smaller on lower limits
|
|
|
|
enum
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
2007-07-03 01:48:06 +02:00
|
|
|
max_bandwidth_block_size = 33000,
|
|
|
|
min_bandwidth_block_size = 400
|
|
|
|
};
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
const time_duration bw_window_size = seconds(1);
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
template<class PeerConnection, class Torrent>
|
2007-01-10 18:16:55 +01:00
|
|
|
struct history_entry
|
|
|
|
{
|
2007-07-03 01:48:06 +02:00
|
|
|
history_entry(intrusive_ptr<PeerConnection> p, weak_ptr<Torrent> t
|
|
|
|
, int a, ptime exp)
|
|
|
|
: expires_at(exp), amount(a), peer(p), tor(t) {}
|
2007-04-05 00:27:36 +02:00
|
|
|
ptime expires_at;
|
2007-01-10 18:16:55 +01:00
|
|
|
int amount;
|
2007-07-03 01:48:06 +02:00
|
|
|
intrusive_ptr<PeerConnection> peer;
|
|
|
|
weak_ptr<Torrent> tor;
|
2007-01-10 18:16:55 +01:00
|
|
|
};
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
template<class PeerConnection>
|
2007-02-01 08:33:04 +01:00
|
|
|
struct bw_queue_entry
|
|
|
|
{
|
2007-07-03 01:48:06 +02:00
|
|
|
bw_queue_entry(boost::intrusive_ptr<PeerConnection> const& pe
|
|
|
|
, int blk, bool no_prio)
|
|
|
|
: peer(pe), max_block_size(blk), non_prioritized(no_prio) {}
|
|
|
|
boost::intrusive_ptr<PeerConnection> peer;
|
|
|
|
int max_block_size;
|
2007-02-01 08:33:04 +01:00
|
|
|
bool non_prioritized;
|
|
|
|
};
|
|
|
|
|
2007-01-10 18:16:55 +01:00
|
|
|
// member of peer_connection
|
|
|
|
struct bandwidth_limit
|
|
|
|
{
|
|
|
|
static const int inf = boost::integer_traits<int>::const_max;
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
bandwidth_limit() throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
: m_quota_left(0)
|
|
|
|
, m_local_limit(inf)
|
|
|
|
, m_current_rate(0)
|
|
|
|
{}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void throttle(int limit) throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
|
|
|
m_local_limit = limit;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
int throttle() const throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
|
|
|
return m_local_limit;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void assign(int amount) throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
|
|
|
assert(amount > 0);
|
|
|
|
m_current_rate += amount;
|
|
|
|
m_quota_left += amount;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void use_quota(int amount) throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
|
|
|
assert(amount <= m_quota_left);
|
|
|
|
m_quota_left -= amount;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
int quota_left() const throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
2007-02-19 01:01:39 +01:00
|
|
|
return (std::max)(m_quota_left, 0);
|
2007-01-10 18:16:55 +01:00
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void expire(int amount) throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
|
|
|
assert(amount >= 0);
|
|
|
|
m_current_rate -= amount;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
int max_assignable() const throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
|
|
|
if (m_local_limit == inf) return inf;
|
|
|
|
if (m_local_limit <= m_current_rate) return 0;
|
|
|
|
return m_local_limit - m_current_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// this is the amount of bandwidth we have
|
|
|
|
// been assigned without using yet. i.e.
|
|
|
|
// the bandwidth that we use up every time
|
|
|
|
// we receive or send a message. Once this
|
|
|
|
// hits zero, we need to request more
|
|
|
|
// bandwidth from the torrent which
|
|
|
|
// in turn will request bandwidth from
|
|
|
|
// the bandwidth manager
|
|
|
|
int m_quota_left;
|
|
|
|
|
|
|
|
// the local limit is the number of bytes
|
|
|
|
// per window size we are allowed to use.
|
|
|
|
int m_local_limit;
|
|
|
|
|
|
|
|
// the current rate is the number of
|
|
|
|
// bytes we have been assigned within
|
|
|
|
// the window size.
|
|
|
|
int m_current_rate;
|
|
|
|
};
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
template<class T>
|
2007-08-08 23:22:59 +02:00
|
|
|
T clamp(T val, T ceiling, T floor) throw()
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
|
|
|
assert(ceiling >= floor);
|
|
|
|
if (val >= ceiling) return ceiling;
|
|
|
|
else if (val <= floor) return floor;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class PeerConnection, class Torrent>
|
2007-01-10 18:16:55 +01:00
|
|
|
struct bandwidth_manager
|
|
|
|
{
|
2007-08-08 23:22:59 +02:00
|
|
|
bandwidth_manager(io_service& ios, int channel) throw()
|
2007-07-03 01:48:06 +02:00
|
|
|
: m_ios(ios)
|
|
|
|
, m_history_timer(m_ios)
|
|
|
|
, m_limit(bandwidth_limit::inf)
|
|
|
|
, m_current_quota(0)
|
|
|
|
, m_channel(channel)
|
2007-09-19 03:08:50 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
m_in_hand_out_bandwidth = false;
|
|
|
|
#endif
|
|
|
|
}
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void throttle(int limit) throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2007-01-10 18:16:55 +01:00
|
|
|
assert(limit >= 0);
|
|
|
|
m_limit = limit;
|
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
int throttle() const throw()
|
2007-01-10 18:16:55 +01:00
|
|
|
{
|
2007-01-29 08:39:33 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2007-01-10 18:16:55 +01:00
|
|
|
return m_limit;
|
|
|
|
}
|
|
|
|
|
2007-02-01 08:33:04 +01:00
|
|
|
// non prioritized means that, if there's a line for bandwidth,
|
|
|
|
// others will cut in front of the non-prioritized peers.
|
|
|
|
// this is used by web seeds
|
2007-07-03 01:48:06 +02:00
|
|
|
void request_bandwidth(intrusive_ptr<PeerConnection> peer
|
|
|
|
, int blk
|
2007-08-08 23:22:59 +02:00
|
|
|
, bool non_prioritized) throw()
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
assert(blk > 0);
|
|
|
|
|
|
|
|
assert(!peer->ignore_bandwidth_limits());
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
// make sure this peer isn't already in line
|
|
|
|
// waiting for bandwidth
|
2007-01-10 18:16:55 +01:00
|
|
|
#ifndef NDEBUG
|
2007-07-03 01:48:06 +02:00
|
|
|
for (typename queue_t::iterator i = m_queue.begin()
|
|
|
|
, end(m_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
assert(i->peer < peer || peer < i->peer);
|
|
|
|
}
|
2007-01-10 18:16:55 +01:00
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
assert(peer->max_assignable_bandwidth(m_channel) > 0);
|
|
|
|
boost::shared_ptr<Torrent> t = peer->associated_torrent().lock();
|
|
|
|
m_queue.push_back(bw_queue_entry<PeerConnection>(peer, blk, non_prioritized));
|
|
|
|
if (!non_prioritized)
|
|
|
|
{
|
|
|
|
typename queue_t::reverse_iterator i = m_queue.rbegin();
|
|
|
|
typename queue_t::reverse_iterator j(i);
|
|
|
|
for (++j; j != m_queue.rend(); ++j)
|
|
|
|
{
|
|
|
|
// if the peer's torrent is not the same one
|
|
|
|
// continue looking for a peer from the same torrent
|
|
|
|
if (j->peer->associated_torrent().lock() != t)
|
|
|
|
continue;
|
|
|
|
// if we found a peer from the same torrent that
|
|
|
|
// is prioritized, there is no point looking
|
|
|
|
// any further.
|
|
|
|
if (!j->non_prioritized) break;
|
|
|
|
|
|
|
|
using std::swap;
|
|
|
|
swap(*i, *j);
|
|
|
|
i = j;
|
|
|
|
}
|
|
|
|
}
|
2007-08-27 12:20:23 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
std::cerr << " req_bandwidht. m_queue.size() = " << m_queue.size() << std::endl;
|
|
|
|
#endif
|
|
|
|
if (!m_queue.empty()) hand_out_bandwidth();
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void check_invariant() const
|
|
|
|
{
|
|
|
|
int current_quota = 0;
|
|
|
|
for (typename history_t::const_iterator i
|
|
|
|
= m_history.begin(), end(m_history.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
current_quota += i->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(current_quota == m_current_quota);
|
|
|
|
}
|
2007-01-10 18:16:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void add_history_entry(history_entry<PeerConnection, Torrent> const& e) throw()
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-08-08 23:22:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
try {
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
m_history.push_front(e);
|
|
|
|
m_current_quota += e.amount;
|
|
|
|
// in case the size > 1 there is already a timer
|
|
|
|
// active that will be invoked, no need to set one up
|
|
|
|
if (m_history.size() > 1) return;
|
|
|
|
|
|
|
|
m_history_timer.expires_at(e.expires_at);
|
|
|
|
m_history_timer.async_wait(bind(&bandwidth_manager::on_history_expire, this, _1));
|
2007-08-08 23:22:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
}
|
|
|
|
catch (std::exception&) { assert(false); }
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void on_history_expire(asio::error_code const& e) throw()
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-08-08 23:22:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
try {
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
if (e) return;
|
|
|
|
|
|
|
|
assert(!m_history.empty());
|
|
|
|
|
|
|
|
ptime now(time_now());
|
|
|
|
while (!m_history.empty() && m_history.back().expires_at <= now)
|
|
|
|
{
|
|
|
|
history_entry<PeerConnection, Torrent> e = m_history.back();
|
|
|
|
m_history.pop_back();
|
|
|
|
m_current_quota -= e.amount;
|
|
|
|
assert(m_current_quota >= 0);
|
|
|
|
intrusive_ptr<PeerConnection> c = e.peer;
|
|
|
|
shared_ptr<Torrent> t = e.tor.lock();
|
|
|
|
if (!c->is_disconnecting()) c->expire_bandwidth(m_channel, e.amount);
|
|
|
|
if (t) t->expire_bandwidth(m_channel, e.amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now, wait for the next chunk to expire
|
|
|
|
if (!m_history.empty())
|
|
|
|
{
|
|
|
|
m_history_timer.expires_at(m_history.back().expires_at);
|
|
|
|
m_history_timer.async_wait(bind(&bandwidth_manager::on_history_expire, this, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// since some bandwidth just expired, it
|
|
|
|
// means we can hand out more (in case there
|
|
|
|
// are still consumers in line)
|
|
|
|
if (!m_queue.empty()) hand_out_bandwidth();
|
2007-08-08 23:22:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
}
|
|
|
|
catch (std::exception&)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2007-08-08 23:22:59 +02:00
|
|
|
void hand_out_bandwidth() throw()
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-08-08 23:22:59 +02:00
|
|
|
#ifndef NDEBUG
|
2007-09-19 03:08:50 +02:00
|
|
|
assert(m_in_hand_out_bandwidth == false);
|
|
|
|
|
|
|
|
m_in_hand_out_bandwidth = true;
|
2007-08-08 23:22:59 +02:00
|
|
|
try {
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
ptime now(time_now());
|
|
|
|
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
int limit = m_limit;
|
|
|
|
l.unlock();
|
|
|
|
|
|
|
|
// available bandwidth to hand out
|
|
|
|
int amount = limit - m_current_quota;
|
|
|
|
|
2007-08-27 12:20:23 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
std::cerr << " hand_out_bandwidht. m_queue.size() = " << m_queue.size()
|
|
|
|
<< " amount = " << amount
|
|
|
|
<< " limit = " << limit
|
|
|
|
<< " m_current_quota = " << m_current_quota << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
while (!m_queue.empty() && amount > 0)
|
|
|
|
{
|
|
|
|
assert(amount == limit - m_current_quota);
|
|
|
|
bw_queue_entry<PeerConnection> qe = m_queue.front();
|
2007-08-25 07:09:25 +02:00
|
|
|
assert(qe.max_block_size > 0);
|
2007-07-03 01:48:06 +02:00
|
|
|
m_queue.pop_front();
|
|
|
|
|
|
|
|
shared_ptr<Torrent> t = qe.peer->associated_torrent().lock();
|
|
|
|
if (!t) continue;
|
|
|
|
if (qe.peer->is_disconnecting())
|
|
|
|
{
|
|
|
|
t->expire_bandwidth(m_channel, qe.max_block_size);
|
2007-09-19 03:08:50 +02:00
|
|
|
assert(amount == limit - m_current_quota);
|
2007-07-03 01:48:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point, max_assignable may actually be zero. Since
|
|
|
|
// the bandwidth quota is subtracted once the data has been
|
|
|
|
// sent. If the peer was added to the queue while the data was
|
|
|
|
// still being sent, max_assignable may have been > 0 at that time.
|
|
|
|
int max_assignable = (std::min)(
|
|
|
|
qe.peer->max_assignable_bandwidth(m_channel)
|
|
|
|
, t->max_assignable_bandwidth(m_channel));
|
|
|
|
if (max_assignable == 0)
|
|
|
|
{
|
|
|
|
t->expire_bandwidth(m_channel, qe.max_block_size);
|
2007-09-19 03:08:50 +02:00
|
|
|
assert(amount == limit - m_current_quota);
|
2007-07-03 01:48:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is the limit of the block size. It depends on the throttle
|
|
|
|
// so that it can be closer to optimal. Larger block sizes will give lower
|
|
|
|
// granularity to the rate but will be more efficient. At high rates
|
|
|
|
// the block sizes are bigger and at low rates, the granularity
|
|
|
|
// is more important and block sizes are smaller
|
|
|
|
|
|
|
|
// the minimum rate that can be given is the block size, so, the
|
|
|
|
// block size must be smaller for lower rates. This is because
|
|
|
|
// the history window is one second, and the block will be forgotten
|
|
|
|
// after one second.
|
2007-08-23 18:49:59 +02:00
|
|
|
int block_size = (std::min)(qe.peer->bandwidth_throttle(m_channel)
|
2007-09-19 03:08:50 +02:00
|
|
|
, limit / 10);
|
2007-07-03 01:48:06 +02:00
|
|
|
|
|
|
|
if (block_size < min_bandwidth_block_size)
|
|
|
|
{
|
2007-09-19 03:08:50 +02:00
|
|
|
block_size = (std::min)(int(min_bandwidth_block_size), limit);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
else if (block_size > max_bandwidth_block_size)
|
|
|
|
{
|
2007-09-19 03:08:50 +02:00
|
|
|
if (limit == bandwidth_limit::inf)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
|
|
|
block_size = max_bandwidth_block_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// try to make the block_size a divisor of
|
|
|
|
// m_limit to make the distributions as fair
|
|
|
|
// as possible
|
|
|
|
// TODO: move this calculcation to where the limit
|
|
|
|
// is changed
|
2007-09-19 03:08:50 +02:00
|
|
|
block_size = limit
|
|
|
|
/ (limit / max_bandwidth_block_size);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-23 18:49:59 +02:00
|
|
|
if (block_size > qe.max_block_size) block_size = qe.max_block_size;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2007-08-27 12:20:23 +02:00
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
std::cerr << " block_size = " << block_size << " amount = " << amount << std::endl;
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
if (amount < block_size / 2)
|
|
|
|
{
|
|
|
|
m_queue.push_front(qe);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// so, hand out max_assignable, but no more than
|
|
|
|
// the available bandwidth (amount) and no more
|
|
|
|
// than the max_bandwidth_block_size
|
|
|
|
int hand_out_amount = (std::min)((std::min)(block_size, max_assignable)
|
|
|
|
, amount);
|
|
|
|
assert(hand_out_amount > 0);
|
2007-09-19 03:08:50 +02:00
|
|
|
assert(amount == limit - m_current_quota);
|
2007-07-03 01:48:06 +02:00
|
|
|
amount -= hand_out_amount;
|
2007-07-15 16:24:06 +02:00
|
|
|
assert(hand_out_amount <= qe.max_block_size);
|
2007-07-03 01:48:06 +02:00
|
|
|
t->assign_bandwidth(m_channel, hand_out_amount, qe.max_block_size);
|
|
|
|
qe.peer->assign_bandwidth(m_channel, hand_out_amount);
|
|
|
|
add_history_entry(history_entry<PeerConnection, Torrent>(
|
|
|
|
qe.peer, t, hand_out_amount, now + bw_window_size));
|
2007-09-19 03:08:50 +02:00
|
|
|
assert(amount == limit - m_current_quota);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2007-08-08 23:22:59 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{ assert(false); };
|
2007-09-19 03:08:50 +02:00
|
|
|
|
|
|
|
m_in_hand_out_bandwidth = false;
|
2007-08-08 23:22:59 +02:00
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2007-01-29 08:39:33 +01:00
|
|
|
typedef boost::mutex mutex_t;
|
|
|
|
mutable mutex_t m_mutex;
|
|
|
|
|
2007-01-10 18:16:55 +01:00
|
|
|
// the io_service used for the timer
|
|
|
|
io_service& m_ios;
|
|
|
|
|
|
|
|
// the timer that is waiting for the entries
|
|
|
|
// in the history queue to expire (slide out
|
|
|
|
// of the history window)
|
|
|
|
deadline_timer m_history_timer;
|
|
|
|
|
|
|
|
// the rate limit (bytes per second)
|
|
|
|
int m_limit;
|
|
|
|
|
|
|
|
// the sum of all recently handed out bandwidth blocks
|
|
|
|
int m_current_quota;
|
|
|
|
|
|
|
|
// these are the consumers that want bandwidth
|
2007-07-03 01:48:06 +02:00
|
|
|
typedef std::deque<bw_queue_entry<PeerConnection> > queue_t;
|
|
|
|
queue_t m_queue;
|
2007-01-10 18:16:55 +01:00
|
|
|
|
|
|
|
// these are the consumers that have received bandwidth
|
|
|
|
// that will expire
|
2007-07-03 01:48:06 +02:00
|
|
|
typedef std::deque<history_entry<PeerConnection, Torrent> > history_t;
|
|
|
|
history_t m_history;
|
2007-01-10 18:16:55 +01:00
|
|
|
|
|
|
|
// this is the channel within the consumers
|
|
|
|
// that bandwidth is assigned to (upload or download)
|
|
|
|
int m_channel;
|
2007-09-19 03:08:50 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
bool m_in_hand_out_bandwidth;
|
|
|
|
#endif
|
|
|
|
|
2007-01-10 18:16:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
|