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>
|
|
|
|
#include <deque>
|
|
|
|
|
2008-01-12 19:47:26 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
#include <fstream>
|
|
|
|
#endif
|
|
|
|
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
2007-12-31 10:41:50 +01:00
|
|
|
#include "libtorrent/bandwidth_limit.hpp"
|
|
|
|
#include "libtorrent/bandwidth_queue_entry.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
|
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
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
template<class PeerConnection>
|
2007-01-10 18:16:55 +01:00
|
|
|
struct bandwidth_manager
|
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_manager(int channel
|
2008-01-12 19:47:26 +01:00
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
, bool log = false
|
|
|
|
#endif
|
|
|
|
)
|
2009-04-26 02:21:59 +02:00
|
|
|
: m_queued_bytes(0)
|
2007-07-03 01:48:06 +02:00
|
|
|
, m_channel(channel)
|
2007-11-08 08:52:41 +01:00
|
|
|
, m_abort(false)
|
2008-01-12 19:47:26 +01:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
if (log)
|
2008-01-13 06:14:49 +01:00
|
|
|
m_log.open("bandwidth_limiter.log", std::ios::trunc);
|
2008-01-12 19:47:26 +01:00
|
|
|
m_start = time_now();
|
|
|
|
#endif
|
|
|
|
}
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2007-11-08 08:52:41 +01:00
|
|
|
void close()
|
|
|
|
{
|
|
|
|
m_abort = true;
|
2008-02-25 11:28:53 +01:00
|
|
|
m_queue.clear();
|
2008-12-13 06:12:12 +01:00
|
|
|
m_queued_bytes = 0;
|
2008-10-21 10:45:42 +02:00
|
|
|
error_code ec;
|
2007-11-08 08:52:41 +01:00
|
|
|
}
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-12-31 10:41:50 +01:00
|
|
|
bool is_queued(PeerConnection const* peer) const
|
2008-01-10 07:19:20 +01:00
|
|
|
{
|
2007-12-31 10:41:50 +01:00
|
|
|
for (typename queue_t::const_iterator i = m_queue.begin()
|
2007-12-14 19:02:06 +01:00
|
|
|
, end(m_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->peer.get() == peer) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-01-17 18:40:46 +01:00
|
|
|
int queue_size() const
|
|
|
|
{
|
|
|
|
return m_queue.size();
|
|
|
|
}
|
2008-12-13 06:12:12 +01:00
|
|
|
|
|
|
|
int queued_bytes() const
|
|
|
|
{
|
|
|
|
return m_queued_bytes;
|
|
|
|
}
|
2007-12-14 19:02:06 +01:00
|
|
|
|
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-12-14 19:02:06 +01:00
|
|
|
void request_bandwidth(intrusive_ptr<PeerConnection> const& peer
|
2009-04-26 02:21:59 +02:00
|
|
|
, int blk, int priority
|
|
|
|
, bandwidth_channel* chan1 = 0
|
|
|
|
, bandwidth_channel* chan2 = 0
|
|
|
|
, bandwidth_channel* chan3 = 0
|
|
|
|
, bandwidth_channel* chan4 = 0
|
|
|
|
, bandwidth_channel* chan5 = 0
|
|
|
|
)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2008-02-25 11:28:53 +01:00
|
|
|
if (m_abort) return;
|
2007-01-10 18:16:55 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
TORRENT_ASSERT(blk > 0);
|
|
|
|
TORRENT_ASSERT(priority > 0);
|
|
|
|
TORRENT_ASSERT(!is_queued(peer.get()));
|
|
|
|
|
|
|
|
bw_request<PeerConnection> bwr(peer, blk, priority);
|
|
|
|
int i = 0;
|
|
|
|
if (chan1 && chan1->throttle() > 0) bwr.channel[i++] = chan1;
|
|
|
|
if (chan2 && chan2->throttle() > 0) bwr.channel[i++] = chan2;
|
|
|
|
if (chan3 && chan3->throttle() > 0) bwr.channel[i++] = chan3;
|
|
|
|
if (chan4 && chan4->throttle() > 0) bwr.channel[i++] = chan4;
|
|
|
|
if (chan5 && chan5->throttle() > 0) bwr.channel[i++] = chan5;
|
|
|
|
if (i == 0)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
// the connection is not rate limited by any of its
|
|
|
|
// bandwidth channels, or it doesn't belong to any
|
|
|
|
// channels. There's no point in adding it to
|
|
|
|
// the queue, just satisfy the request immediately
|
|
|
|
bwr.peer->assign_bandwidth(m_channel, blk);
|
|
|
|
return;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2008-12-13 06:12:12 +01:00
|
|
|
m_queued_bytes += blk;
|
2009-04-26 02:21:59 +02:00
|
|
|
m_queue.push_back(bwr);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-07-03 01:48:06 +02:00
|
|
|
void check_invariant() const
|
|
|
|
{
|
|
|
|
}
|
2007-01-10 18:16:55 +01:00
|
|
|
#endif
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
void update_quotas(time_duration const& dt)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2007-11-08 08:52:41 +01:00
|
|
|
if (m_abort) return;
|
2009-04-26 02:21:59 +02:00
|
|
|
if (m_queue.empty()) return;
|
2007-12-29 06:43:19 +01:00
|
|
|
|
2007-07-03 01:48:06 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
int dt_milliseconds = total_milliseconds(dt);
|
|
|
|
if (dt_milliseconds > 3000) dt_milliseconds = 3000;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
// for each bandwidth channel, call update_quota(dt)
|
2007-12-31 10:41:50 +01:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
std::vector<bandwidth_channel*> channels;
|
2008-05-05 19:08:14 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
for (typename queue_t::iterator i = m_queue.begin();
|
|
|
|
i != m_queue.end();)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
if (i->peer->is_disconnecting())
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
m_queued_bytes -= i->request_size - i->assigned;
|
|
|
|
|
|
|
|
// return all assigned quota to all the
|
|
|
|
// bandwidth channels this peer belongs to
|
|
|
|
for (int j = 0; j < 5 && i->channel[j]; ++j)
|
|
|
|
{
|
|
|
|
bandwidth_channel* bwc = i->channel[j];
|
|
|
|
bwc->return_quota(i->assigned);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = m_queue.erase(i);
|
2007-07-03 01:48:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-04-26 02:21:59 +02:00
|
|
|
for (int j = 0; j < 5 && i->channel[j]; ++j)
|
|
|
|
{
|
|
|
|
bandwidth_channel* bwc = i->channel[j];
|
|
|
|
bwc->tmp = 0;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
for (typename queue_t::iterator i = m_queue.begin()
|
|
|
|
, end(m_queue.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 5 && i->channel[j]; ++j)
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
bandwidth_channel* bwc = i->channel[j];
|
|
|
|
if (bwc->tmp == 0) channels.push_back(bwc);
|
|
|
|
bwc->tmp += i->priority;
|
|
|
|
TORRENT_ASSERT(i->priority > 0);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2009-04-26 02:21:59 +02:00
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
for (std::vector<bandwidth_channel*>::iterator i = channels.begin()
|
|
|
|
, end(channels.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
(*i)->update_quota(dt_milliseconds);
|
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
queue_t tm;
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
for (typename queue_t::iterator i = m_queue.begin();
|
|
|
|
i != m_queue.end();)
|
|
|
|
{
|
|
|
|
int a = i->assign_bandwidth();
|
|
|
|
if (i->assigned == i->request_size
|
|
|
|
|| (i->ttl <= 0 && i->assigned > 0))
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
TORRENT_ASSERT(i->assigned <= i->request_size);
|
|
|
|
tm.push_back(*i);
|
|
|
|
i = m_queue.erase(i);
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2009-04-26 02:21:59 +02:00
|
|
|
else
|
2007-07-03 01:48:06 +02:00
|
|
|
{
|
2009-04-26 02:21:59 +02:00
|
|
|
++i;
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
2009-04-26 02:21:59 +02:00
|
|
|
m_queued_bytes -= a;
|
|
|
|
}
|
2007-07-03 01:48:06 +02:00
|
|
|
|
2009-04-26 02:21:59 +02:00
|
|
|
while (!tm.empty())
|
|
|
|
{
|
|
|
|
bw_request<PeerConnection>& bwr = tm.back();
|
|
|
|
bwr.peer->assign_bandwidth(m_channel, bwr.assigned);
|
|
|
|
tm.pop_back();
|
2007-07-03 01:48:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:16:55 +01:00
|
|
|
|
|
|
|
// these are the consumers that want bandwidth
|
2009-04-26 02:21:59 +02:00
|
|
|
typedef std::vector<bw_request<PeerConnection> > queue_t;
|
2007-07-03 01:48:06 +02:00
|
|
|
queue_t m_queue;
|
2009-04-26 02:21:59 +02:00
|
|
|
// the number of bytes all the requests in queue are for
|
2008-12-13 06:12:12 +01:00
|
|
|
int m_queued_bytes;
|
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
|
|
|
|
2007-11-08 08:52:41 +01:00
|
|
|
bool m_abort;
|
2008-01-12 19:47:26 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_VERBOSE_BANDWIDTH_LIMIT
|
|
|
|
std::ofstream m_log;
|
|
|
|
ptime m_start;
|
|
|
|
#endif
|
2007-01-10 18:16:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2007-07-03 01:48:06 +02:00
|
|
|
|