premiere-libtorrent/src/policy.cpp

1146 lines
30 KiB
C++
Raw Normal View History

/*
Copyright (c) 2003, 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.
*/
#include <iostream>
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#include <boost/date_time/posix_time/posix_time.hpp>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include "libtorrent/policy.hpp"
#include "libtorrent/torrent.hpp"
#include "libtorrent/socket.hpp"
#include "libtorrent/peer_connection.hpp"
2004-01-18 20:12:18 +01:00
#include "libtorrent/alert_types.hpp"
2004-01-12 04:05:10 +01:00
2003-11-28 18:29:27 +01:00
#if defined(_MSC_VER) && _MSC_VER < 1300
# define for if (false) {} else for
#endif
2004-01-15 17:45:34 +01:00
namespace libtorrent
{
class peer_connection;
}
namespace
{
enum
{
2004-01-12 21:31:27 +01:00
// the limits of the download queue size
max_request_queue = 16,
2004-02-01 18:42:20 +01:00
min_request_queue = 2,
2003-12-14 06:56:12 +01:00
// the amount of free upload allowed before
// the peer is choked
free_upload_amount = 4 * 16 * 1024
};
using namespace libtorrent;
2003-11-05 00:27:06 +01:00
2004-02-01 11:45:54 +01:00
// the case where ignore_peer is motivated is if two peers
2004-02-01 10:47:58 +01:00
// have only one piece that we don't have, and it's the
// same piece for both peers. Then they might get into an
2004-02-01 11:45:54 +01:00
// infinite recursion, fighting to request the same blocks.
2004-02-01 14:05:29 +01:00
void request_a_block(
torrent& t
, peer_connection& c
, std::vector<peer_connection*> ignore = std::vector<peer_connection*>())
{
2004-01-31 11:20:19 +01:00
// this will make the number of requests linearly dependent
// on the rate in which we download from the peer. 2.5kB/s and
// less will make the desired queue size 2 and at about 70 kB/s
// it will reach the maximum of 16 requests.
// matlab expression to plot:
2004-01-31 18:13:40 +01:00
// x = 1:100:100000; plot(x, round(min(max(x ./ 5000 + 1.5, 4), 16)));
2004-01-31 11:20:19 +01:00
2004-02-01 18:26:09 +01:00
int desired_queue_size = static_cast<int>(c.statistics().download_rate() / 5000.f + 1.5f);
2004-01-12 21:31:27 +01:00
if (desired_queue_size > max_request_queue) desired_queue_size = max_request_queue;
if (desired_queue_size < min_request_queue) desired_queue_size = min_request_queue;
assert(desired_queue_size >= min_request_queue);
int num_requests = desired_queue_size - (int)c.download_queue().size();
2003-11-05 00:27:06 +01:00
// if our request queue is already full, we
// don't have to make any new requests yet
if (num_requests <= 0) return;
piece_picker& p = t.picker();
std::vector<piece_block> interesting_pieces;
interesting_pieces.reserve(100);
// picks the interesting pieces from this peer
// the integer is the number of pieces that
// should be guaranteed to be available for download
2004-01-12 21:31:27 +01:00
// (if num_requests is too big, too many pieces are
// picked and cpu-time is wasted)
p.pick_pieces(c.get_bitfield(), interesting_pieces, num_requests);
2004-01-12 21:31:27 +01:00
// this vector is filled with the interesting pieces
// that some other peer is currently downloading
// we should then compare this peer's download speed
// with the other's, to see if we should abort another
// peer_connection in favour of this one
std::vector<piece_block> busy_pieces;
busy_pieces.reserve(10);
for (std::vector<piece_block>::iterator i = interesting_pieces.begin();
i != interesting_pieces.end();
++i)
{
if (p.is_downloading(*i))
{
busy_pieces.push_back(*i);
continue;
}
// ok, we found a piece that's not being downloaded
// by somebody else. request it from this peer
2004-02-01 10:47:58 +01:00
// and return
2004-01-12 21:31:27 +01:00
c.send_request(*i);
2003-11-05 00:27:06 +01:00
num_requests--;
if (num_requests <= 0) return;
}
2004-02-01 10:47:58 +01:00
// in this case, we could not find any blocks
// that was free. If we couldn't find any busy
// blocks as well, we cannot download anything
// more from this peer.
2003-11-05 00:27:06 +01:00
if (busy_pieces.empty()) return;
// first look for blocks that are just queued
// and not actually sent to us yet
// (then we can cancel those and request them
// from this peer instead)
2004-02-01 18:42:20 +01:00
while (num_requests > 0)
2003-11-05 00:27:06 +01:00
{
2004-02-01 18:42:20 +01:00
peer_connection* peer = 0;
float min_weight = std::numeric_limits<float>::max();
// find the peer with the lowest download
// speed that also has a piece that this
// peer could send us
for (torrent::peer_iterator i = t.begin();
i != t.end();
++i)
2003-11-05 00:27:06 +01:00
{
2004-02-01 18:42:20 +01:00
// don't try to take over blocks from ourself
if (i->second == &c)
continue;
// ignore all peers in the ignore list
if (std::find(ignore.begin(), ignore.end(), i->second) != ignore.end())
continue;
const std::deque<piece_block>& queue = i->second->download_queue();
const int queue_size = (int)i->second->download_queue().size();
const float weight = queue_size == 0
? std::numeric_limits<float>::max()
2004-04-18 14:28:02 +02:00
: i->second->statistics().download_payload_rate() / queue_size;
2004-02-01 18:42:20 +01:00
if (weight < min_weight
&& std::find_first_of(
busy_pieces.begin()
, busy_pieces.end()
, queue.begin()
, queue.end()) != busy_pieces.end())
{
peer = i->second;
min_weight = weight;
}
2003-11-05 00:27:06 +01:00
}
2004-02-01 18:42:20 +01:00
if (peer == 0)
{
// we probably couldn't request the block because
// we are ignoring some peers
2004-02-04 12:00:29 +01:00
break;
2004-02-01 18:42:20 +01:00
}
2003-11-05 00:27:06 +01:00
2004-02-01 18:42:20 +01:00
// find a suitable block to take over from this peer
2004-02-01 01:07:20 +01:00
2004-02-01 18:42:20 +01:00
std::deque<piece_block>::const_reverse_iterator common_block =
std::find_first_of(
peer->download_queue().rbegin()
, peer->download_queue().rend()
, busy_pieces.begin()
, busy_pieces.end());
2004-02-01 01:07:20 +01:00
2004-02-01 18:42:20 +01:00
assert(common_block != peer->download_queue().rend());
piece_block block = *common_block;
peer->send_cancel(block);
c.send_request(block);
2003-11-05 00:27:06 +01:00
2004-02-01 18:42:20 +01:00
// the one we interrupted may need to request a new piece
// make sure it doesn't take over a block from the peer
// that just took over its block
ignore.push_back(&c);
request_a_block(t, *peer, ignore);
num_requests--;
2004-02-04 12:00:29 +01:00
// this peer doesn't have a faster connection than the
// slowest peer. Don't take over any blocks
const int queue_size = (int)c.download_queue().size();
const float weight = queue_size == 0
? std::numeric_limits<float>::max()
2004-04-18 14:28:02 +02:00
: c.statistics().download_payload_rate() / queue_size;
2004-02-04 12:00:29 +01:00
if (weight <= min_weight) break;
2004-02-01 18:42:20 +01:00
}
}
2003-12-14 23:55:32 +01:00
size_type collect_free_download(
2003-12-14 23:55:32 +01:00
torrent::peer_iterator start
, torrent::peer_iterator end)
{
size_type accumulator = 0;
2003-12-14 23:55:32 +01:00
for (torrent::peer_iterator i = start; i != end; ++i)
{
// if the peer is interested in us, it means it may
// want to trade it's surplus uploads for downloads itself
// (and we should consider it free). If the share diff is
// negative, there's no free download to get from this peer.
size_type diff = i->second->share_diff();
2004-01-14 13:53:17 +01:00
if (i->second->is_peer_interested() || diff <= 0)
2003-12-14 23:55:32 +01:00
continue;
assert(diff > 0);
2004-01-13 04:08:59 +01:00
i->second->add_free_upload(-diff);
2003-12-14 23:55:32 +01:00
accumulator += diff;
assert(accumulator > 0);
2003-12-14 23:55:32 +01:00
}
assert(accumulator >= 0);
2003-12-14 23:55:32 +01:00
return accumulator;
}
// returns the amount of free upload left after
// it has been distributed to the peers
size_type distribute_free_upload(
2003-12-14 23:55:32 +01:00
torrent::peer_iterator start
, torrent::peer_iterator end
, size_type free_upload)
2003-12-14 23:55:32 +01:00
{
if (free_upload <= 0) return free_upload;
2003-12-14 23:55:32 +01:00
int num_peers = 0;
size_type total_diff = 0;
2003-12-14 23:55:32 +01:00
for (torrent::peer_iterator i = start; i != end; ++i)
{
2004-01-14 13:53:17 +01:00
total_diff += i->second->share_diff();
if (!i->second->is_peer_interested() || i->second->share_diff() >= 0) continue;
2003-12-14 23:55:32 +01:00
++num_peers;
}
if (num_peers == 0) return free_upload;
size_type upload_share;
2003-12-15 04:14:01 +01:00
if (total_diff >= 0)
{
upload_share = std::min(free_upload, total_diff) / num_peers;
}
else
{
upload_share = (free_upload + total_diff) / num_peers;
}
if (upload_share < 0) return free_upload;
2003-12-14 23:55:32 +01:00
for (torrent::peer_iterator i = start; i != end; ++i)
{
2004-01-13 04:08:59 +01:00
peer_connection* p = i->second;
if (!p->is_peer_interested() || p->share_diff() >= 0) continue;
p->add_free_upload(upload_share);
2003-12-14 23:55:32 +01:00
free_upload -= upload_share;
}
return free_upload;
}
2004-01-15 17:45:34 +01:00
struct match_peer_ip
{
match_peer_ip(const address& id)
: m_id(id)
{}
bool operator()(const policy::peer& p) const
{ return p.id.ip() == m_id.ip(); }
address m_id;
};
struct match_peer_connection
{
match_peer_connection(const peer_connection& c)
: m_conn(c)
{}
bool operator()(const policy::peer& p) const
{ return p.connection == &m_conn; }
const peer_connection& m_conn;
};
}
namespace libtorrent
{
policy::policy(torrent* t)
: m_num_peers(0)
, m_torrent(t)
2004-01-15 01:46:44 +01:00
, m_max_uploads(std::numeric_limits<int>::max())
2004-01-21 01:59:38 +01:00
, m_max_connections(std::numeric_limits<int>::max())
2003-12-14 06:56:12 +01:00
, m_num_unchoked(0)
2003-12-14 23:55:32 +01:00
, m_available_free_upload(0)
2004-01-21 01:59:38 +01:00
, m_last_optimistic_disconnect(boost::gregorian::date(1970,boost::gregorian::Jan,1))
2004-01-25 23:41:55 +01:00
{ assert(t); }
2003-12-14 06:56:12 +01:00
// finds the peer that has the worst download rate
// and returns it. May return 0 if all peers are
// choked.
policy::peer* policy::find_choke_candidate()
{
peer* worst_peer = 0;
size_type min_weight = std::numeric_limits<int>::max();
2003-12-14 06:56:12 +01:00
2004-01-12 21:31:27 +01:00
// TODO: make this selection better
2003-12-14 06:56:12 +01:00
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
peer_connection* c = i->connection;
if (c == 0) continue;
if (c->is_choked()) continue;
// if the peer isn't interested, just choke it
if (!c->is_peer_interested())
return &(*i);
size_type diff = i->total_download()
2004-01-14 13:53:17 +01:00
- i->total_upload();
2003-12-14 06:56:12 +01:00
size_type weight = static_cast<int>(c->statistics().download_rate() * 10.f)
2003-12-14 06:56:12 +01:00
+ diff
+ (c->has_peer_choked()?-10:10)*1024;
if (weight > min_weight) continue;
min_weight = weight;
worst_peer = &(*i);
continue;
}
return worst_peer;
}
policy::peer* policy::find_unchoke_candidate()
{
// if all of our peers are unchoked, there's
// no left to unchoke
if (m_num_unchoked == m_torrent->num_peers())
return 0;
using namespace boost::posix_time;
using namespace boost::gregorian;
peer* unchoke_peer = 0;
ptime min_time(date(9999,Jan,1));
2004-01-12 21:31:27 +01:00
float max_down_speed = 0.f;
// TODO: make this selection better
2003-12-14 06:56:12 +01:00
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
peer_connection* c = i->connection;
if (c == 0) continue;
2004-01-21 01:59:38 +01:00
if (c->is_disconnecting()) continue;
2003-12-14 06:56:12 +01:00
if (!c->is_choked()) continue;
if (!c->is_peer_interested()) continue;
2003-12-14 23:55:32 +01:00
if (c->share_diff()
2003-12-14 06:56:12 +01:00
< -free_upload_amount) continue;
2004-01-12 21:31:27 +01:00
if (c->statistics().download_rate() < max_down_speed) continue;
// if (i->last_optimistically_unchoked > min_time) continue;
2003-12-14 06:56:12 +01:00
min_time = i->last_optimistically_unchoked;
2004-01-12 21:31:27 +01:00
max_down_speed = c->statistics().download_rate();
2003-12-14 06:56:12 +01:00
unchoke_peer = &(*i);
}
return unchoke_peer;
}
2004-01-21 01:59:38 +01:00
policy::peer* policy::find_disconnect_candidate()
{
peer *disconnect_peer = 0;
double slowest_transfer_rate = std::numeric_limits<double>::max();
2004-01-21 01:59:38 +01:00
boost::posix_time::ptime local_time
= boost::posix_time::second_clock::local_time();
2004-01-21 01:59:38 +01:00
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
peer_connection* c = i->connection;
if(c == 0)
continue;
if(c->is_disconnecting())
continue;
double transferred_amount
= (double)c->statistics().total_payload_download();
2004-01-21 01:59:38 +01:00
boost::posix_time::time_duration connected_time
= local_time - i->connected;
2004-01-21 01:59:38 +01:00
double connected_time_in_seconds
= connected_time.seconds()
+ connected_time.minutes()*60.0
+ connected_time.hours()*60.0*60.0;
2004-01-21 01:59:38 +01:00
double transfer_rate
= transferred_amount / (connected_time_in_seconds+1);
2004-01-21 01:59:38 +01:00
if (transfer_rate <= slowest_transfer_rate)
{
slowest_transfer_rate = transfer_rate;
disconnect_peer = &(*i);
2004-01-21 01:59:38 +01:00
}
}
return disconnect_peer;
}
2004-01-14 17:18:53 +01:00
policy::peer *policy::find_connect_candidate()
{
boost::posix_time::ptime local_time=boost::posix_time::second_clock::local_time();
boost::posix_time::ptime ptime(local_time);
policy::peer* candidate =0;
2004-01-14 17:18:53 +01:00
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
if(i->connection) continue;
if(i->banned) continue;
2004-03-21 03:03:37 +01:00
if(i->type == peer::not_connectable) continue;
2004-01-14 17:22:49 +01:00
2004-01-14 17:18:53 +01:00
assert(i->connected <= local_time);
2004-03-21 03:03:37 +01:00
boost::posix_time::ptime next_connect = i->connected;
2004-01-14 17:18:53 +01:00
if (next_connect <= ptime)
{
ptime = next_connect;
candidate = &(*i);
2004-01-14 17:18:53 +01:00
}
}
assert(ptime <= local_time);
return candidate;
}
2004-03-01 22:54:10 +01:00
policy::peer* policy::find_seed_choke_candidate()
{
peer* candidate = 0;
boost::posix_time::ptime last_unchoke
= boost::posix_time::ptime(boost::posix_time::ptime(boost::gregorian::date(1970, boost::gregorian::Jan, 1)));
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
peer_connection* c = i->connection;
2004-03-12 17:42:33 +01:00
// ignore peers that are choked or
// whose connection is closed
2004-03-01 22:54:10 +01:00
if (c == 0) continue;
if (c->is_choked()) continue;
2004-03-12 17:42:33 +01:00
// if we still owe this peer some upload
// don't choke it
if (c->share_diff() > 0) continue;
// select the one that has been waiting
// for an unchoke the longest
2004-03-01 22:54:10 +01:00
if (last_unchoke > i->last_optimistically_unchoked) continue;
last_unchoke = i->last_optimistically_unchoked;
candidate = &(*i);
}
return candidate;
}
policy::peer* policy::find_seed_unchoke_candidate()
{
peer* candidate = 0;
boost::posix_time::ptime last_unchoke
= boost::posix_time::second_clock::local_time();
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
peer_connection* c = i->connection;
if (c == 0) continue;
if (!c->is_choked()) continue;
if (!c->is_peer_interested()) continue;
if (c->is_disconnecting()) continue;
if (last_unchoke < i->last_optimistically_unchoked) continue;
last_unchoke = i->last_optimistically_unchoked;
candidate = &(*i);
}
return candidate;
}
bool policy::seed_unchoke_one_peer()
{
peer* p = find_seed_unchoke_candidate();
if (p != 0)
{
p->connection->send_unchoke();
p->last_optimistically_unchoked
= boost::posix_time::second_clock::local_time();
++m_num_unchoked;
}
return p != 0;
}
void policy::pulse()
{
using namespace boost::posix_time;
2004-02-25 00:55:42 +01:00
// TODO: we must also remove peers that
// we failed to connect to from this list
// to avoid being part of a DDOS-attack
2004-01-21 01:59:38 +01:00
// remove old disconnected peers from the list
m_peers.erase(
std::remove_if(m_peers.begin()
2004-04-06 22:11:18 +02:00
, m_peers.end()
, old_disconnected_peer())
, m_peers.end());
2004-03-01 22:54:10 +01:00
// -------------------------------------
// maintain the number of connections
// -------------------------------------
// count the number of connected peers except for peers
// that are currently in the process of disconnecting
int num_connected_peers = 0;
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
2004-01-21 01:59:38 +01:00
{
2004-03-01 22:54:10 +01:00
if (i->connection && !i->connection->is_disconnecting())
++num_connected_peers;
}
2004-01-21 01:59:38 +01:00
2004-03-01 22:54:10 +01:00
if (m_max_connections != std::numeric_limits<int>::max())
{
2004-01-21 01:59:38 +01:00
2004-03-01 22:54:10 +01:00
int max_connections = m_max_connections;
2004-01-21 01:59:38 +01:00
2004-03-01 22:54:10 +01:00
if (num_connected_peers >= max_connections)
2004-01-21 01:59:38 +01:00
{
// every minute, disconnect the worst peer in hope of finding a better peer
2004-03-01 22:54:10 +01:00
boost::posix_time::ptime local_time = boost::posix_time::second_clock::local_time();
if(m_last_optimistic_disconnect + boost::posix_time::seconds(120) <= local_time)
2004-01-21 01:59:38 +01:00
{
2004-03-01 22:54:10 +01:00
m_last_optimistic_disconnect = local_time;
2004-01-21 01:59:38 +01:00
--max_connections; // this will have the effect of disconnecting the worst peer
}
}
else
{
// don't do a disconnect earlier than 1 minute after some peer was connected
2004-03-01 22:54:10 +01:00
m_last_optimistic_disconnect = boost::posix_time::second_clock::local_time();
2004-01-21 01:59:38 +01:00
}
2004-03-01 22:54:10 +01:00
while (num_connected_peers > max_connections)
2004-01-21 01:59:38 +01:00
{
assert(disconnect_one_peer());
--num_connected_peers;
}
}
2004-03-01 22:54:10 +01:00
while (m_torrent->num_peers() < m_max_connections)
2004-01-21 01:59:38 +01:00
{
2004-03-01 22:54:10 +01:00
if (!connect_one_peer())
2004-01-21 01:59:38 +01:00
break;
}
2004-01-14 13:53:17 +01:00
2004-03-01 22:54:10 +01:00
// ------------------------
// upload shift
// ------------------------
// this part will shift downloads
// from peers that are seeds and peers
// that don't want to download from us
// to peers that cannot upload anything
// to us. The shifting will make sure
// that the torrent's share ratio
// will be maintained
2004-01-14 02:19:30 +01:00
// if the share ratio is 0 (infinite)
// m_available_free_upload isn't used
// because it isn't necessary
if (m_torrent->ratio() != 0.f)
{
// accumulate all the free download we get
// and add it to the available free upload
m_available_free_upload
+= collect_free_download(
m_torrent->begin()
, m_torrent->end());
// distribute the free upload among the peers
m_available_free_upload = distribute_free_upload(
2003-12-14 23:55:32 +01:00
m_torrent->begin()
2004-01-14 02:19:30 +01:00
, m_torrent->end()
, m_available_free_upload);
}
2004-01-05 00:51:54 +01:00
2004-01-15 01:46:44 +01:00
// ------------------------
2004-03-01 22:54:10 +01:00
// seed choking policy
2004-01-15 01:46:44 +01:00
// ------------------------
2004-01-05 00:51:54 +01:00
if (m_torrent->is_seed())
{
2004-03-01 22:54:10 +01:00
if (num_connected_peers > m_max_uploads)
{
// this means there are some peers that
// are choked. To have the choked peers
// rotate, unchoke one peer here
// and let the next condiional block
// make sure another peer is choked.
seed_unchoke_one_peer();
}
2004-02-01 17:30:13 +01:00
while (m_num_unchoked > m_max_uploads)
{
2004-03-01 22:54:10 +01:00
peer* p = find_seed_choke_candidate();
2004-05-10 08:12:29 +02:00
assert(p != 0);
2004-02-01 17:30:13 +01:00
p->connection->send_choke();
--m_num_unchoked;
}
2004-01-05 00:51:54 +01:00
// make sure we have enough
// unchoked peers
2004-01-15 01:46:44 +01:00
while (m_num_unchoked < m_max_uploads)
2004-01-05 00:51:54 +01:00
{
2004-03-01 22:54:10 +01:00
if (!seed_unchoke_one_peer()) break;
2004-01-05 00:51:54 +01:00
}
2004-05-10 08:12:29 +02:00
#ifndef NDEBUG
check_invariant();
#endif
2004-01-05 00:51:54 +01:00
}
2004-01-15 01:46:44 +01:00
2004-03-01 22:54:10 +01:00
// ----------------------------
// downloading choking policy
// ----------------------------
2004-01-14 13:53:17 +01:00
else
{
2004-01-14 13:53:17 +01:00
// choke peers that have leeched too much without giving anything back
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
peer_connection* c = i->connection;
if (c == 0) continue;
size_type diff = i->connection->share_diff();
2004-01-14 13:53:17 +01:00
if (diff < -free_upload_amount
&& !c->is_choked())
{
// if we have uploaded more than a piece for free, choke peer and
// wait until we catch up with our download.
c->send_choke();
2004-01-14 18:04:47 +01:00
--m_num_unchoked;
2004-01-14 13:53:17 +01:00
}
}
2004-03-01 01:50:00 +01:00
if (m_max_uploads < m_torrent->num_peers())
{
2004-03-01 01:50:00 +01:00
// make sure we don't have too many
// unchoked peers
while (m_num_unchoked > m_max_uploads)
{
peer* p = find_choke_candidate();
assert(p);
p->connection->send_choke();
--m_num_unchoked;
}
2003-12-14 06:56:12 +01:00
2004-03-01 01:50:00 +01:00
// optimistic unchoke. trade the 'worst'
// unchoked peer with one of the choked
assert(m_num_unchoked <= m_torrent->num_peers());
peer* p = find_choke_candidate();
if (p)
{
p->connection->send_choke();
--m_num_unchoked;
unchoke_one_peer();
}
2003-12-14 06:56:12 +01:00
}
2003-12-01 22:27:27 +01:00
2003-12-14 06:56:12 +01:00
// make sure we have enough
// unchoked peers
2004-01-15 01:46:44 +01:00
while (m_num_unchoked < m_max_uploads && unchoke_one_peer());
2003-12-14 06:56:12 +01:00
}
#ifndef NDEBUG
check_invariant();
#endif
}
2003-12-01 22:27:27 +01:00
void policy::ban_peer(const peer_connection& c)
{
2004-01-15 17:45:34 +01:00
std::vector<peer>::iterator i = std::find_if(
m_peers.begin()
, m_peers.end()
, match_peer_connection(c));
2004-01-15 02:29:43 +01:00
2003-12-01 22:27:27 +01:00
assert(i != m_peers.end());
2004-03-21 03:03:37 +01:00
i->type = peer::not_connectable;
i->id.port = address::any_port;
2003-12-01 22:27:27 +01:00
i->banned = true;
}
2004-01-12 21:31:27 +01:00
void policy::new_connection(peer_connection& c)
{
2004-01-15 02:29:43 +01:00
assert(!c.is_local());
2004-03-01 01:50:00 +01:00
// if the connection comes from the tracker,
// it's probably just a NAT-check. Ignore the
// num connections constraint then.
2004-04-06 22:11:18 +02:00
// TODO: make sure this works
2004-03-28 19:45:37 +02:00
// TODO: only allow _one_ connection to use this
// override at a time
2004-03-01 01:50:00 +01:00
if (m_torrent->num_peers() >= m_max_connections
&& c.get_socket()->sender().ip() != m_torrent->current_tracker().ip())
{
2004-01-21 01:59:38 +01:00
throw protocol_error("too many connections, refusing incoming connection"); // cause a disconnect
2004-03-01 01:50:00 +01:00
}
#ifndef NDEBUG
if (c.get_socket()->sender().ip() == m_torrent->current_tracker().ip())
{
m_torrent->debug_log("overriding connection limit for tracker NAT-check");
}
#endif
2004-01-21 01:59:38 +01:00
2004-01-15 17:45:34 +01:00
std::vector<peer>::iterator i = std::find_if(
m_peers.begin()
, m_peers.end()
, match_peer_ip(c.get_socket()->sender()));
2004-01-15 02:29:43 +01:00
if (i == m_peers.end())
{
2003-12-14 06:56:12 +01:00
using namespace boost::posix_time;
using namespace boost::gregorian;
// we don't have ny info about this peer.
// add a new entry
2004-01-14 17:18:53 +01:00
2004-01-15 20:32:03 +01:00
peer p(c.get_socket()->sender(), peer::not_connectable);
m_peers.push_back(p);
i = m_peers.end()-1;
}
else
{
2004-01-15 17:45:34 +01:00
if (i->connection != 0)
throw protocol_error("duplicate connection, closing");
if (i->banned)
2004-03-21 03:03:37 +01:00
throw protocol_error("ip address banned, closing");
}
2004-01-15 17:45:34 +01:00
assert(i->connection == 0);
2004-02-25 00:55:42 +01:00
c.add_stat(i->prev_amount_download, i->prev_amount_upload);
i->prev_amount_download = 0;
i->prev_amount_upload = 0;
2003-12-14 06:56:12 +01:00
i->connection = &c;
2004-01-14 17:18:53 +01:00
i->connected = boost::posix_time::second_clock::local_time();
2004-02-25 00:55:42 +01:00
m_last_optimistic_disconnect = boost::posix_time::second_clock::local_time();
}
void policy::peer_from_tracker(const address& remote, const peer_id& id)
{
2004-01-16 17:36:09 +01:00
// just ignore the obviously invalid entries from the tracker
2004-02-26 01:27:06 +01:00
if(remote.ip() == 0 || remote.port == 0)
2004-01-16 17:19:27 +01:00
return;
2003-10-27 17:06:00 +01:00
try
{
2004-01-15 17:45:34 +01:00
std::vector<peer>::iterator i = std::find_if(
m_peers.begin()
, m_peers.end()
, match_peer_ip(remote));
2004-01-15 02:29:43 +01:00
if (i == m_peers.end())
{
2003-12-14 06:56:12 +01:00
using namespace boost::posix_time;
using namespace boost::gregorian;
2004-05-10 08:12:29 +02:00
// we don't have any info about this peer.
// add a new entry
2004-01-15 20:32:03 +01:00
peer p(remote, peer::connectable);
m_peers.push_back(p);
2004-01-15 17:45:34 +01:00
// the iterator is invalid
// because of the push_back()
i = m_peers.end()-1;
}
2004-01-15 02:29:43 +01:00
else
{
2004-01-15 20:32:03 +01:00
i->type = peer::connectable;
2004-01-15 17:45:34 +01:00
2004-01-15 02:29:43 +01:00
// in case we got the ip from a remote connection, port is
// not known, so save it. Client may also have changed port
// for some reason.
2004-01-15 17:45:34 +01:00
i->id = remote;
2004-01-15 02:29:43 +01:00
if (i->connection)
{
// this means we're already connected
// to this peer. don't connect to
// it again.
assert(i->connection->associated_torrent() == m_torrent);
return;
}
}
2004-01-15 02:29:43 +01:00
if (i->banned) return;
2004-02-26 19:55:10 +01:00
if (m_torrent->num_peers() < m_max_connections)
2004-01-21 01:59:38 +01:00
{
2004-02-25 14:18:41 +01:00
connect_peer(&*i);
2004-01-21 01:59:38 +01:00
}
2004-01-14 17:18:53 +01:00
return;
2003-10-27 17:06:00 +01:00
}
2004-01-12 04:05:10 +01:00
catch(network_error& e)
{
if (m_torrent->alerts().should_post(alert::debug))
{
m_torrent->alerts().post_alert(
2004-03-28 19:45:37 +02:00
peer_error_alert(remote, id, e.what()));
2004-01-12 04:05:10 +01:00
}
}
catch(protocol_error& e)
{
if (m_torrent->alerts().should_post(alert::debug))
{
m_torrent->alerts().post_alert(
2004-03-28 19:45:37 +02:00
peer_error_alert(remote, id, e.what()));
2004-01-12 04:05:10 +01:00
}
}
}
// this is called when we are choked by a peer
// i.e. a peer lets us know that we will not receive
// anything for a while
void policy::choked(peer_connection& c)
{
}
2003-12-14 06:56:12 +01:00
void policy::piece_finished(int index, bool successfully_verified)
{
2004-01-25 23:41:55 +01:00
assert(index >= 0 && index < m_torrent->torrent_file().num_pieces());
2003-12-17 04:40:13 +01:00
if (successfully_verified)
{
// have all peers update their interested-flag
for (std::vector<peer>::iterator i = m_peers.begin();
i != m_peers.end();
++i)
{
if (i->connection == 0) continue;
// if we're not interested, we will not become interested
if (!i->connection->is_interesting()) continue;
2003-12-17 17:37:20 +01:00
if (!i->connection->has_piece(index)) continue;
2003-12-17 04:40:13 +01:00
bool interested = false;
const std::vector<bool>& peer_has = i->connection->get_bitfield();
const std::vector<bool>& we_have = m_torrent->pieces();
assert(we_have.size() == peer_has.size());
2004-01-26 11:29:00 +01:00
for (int j = 0; j != (int)we_have.size(); ++j)
2003-12-17 04:40:13 +01:00
{
if (!we_have[j] && peer_has[j])
{
interested = true;
break;
}
}
if (!interested)
2004-01-12 21:31:27 +01:00
i->connection->send_not_interested();
2003-12-17 04:40:13 +01:00
}
}
}
2003-12-17 04:40:13 +01:00
// TODO: we must be able to get interested
// in a peer again, if a piece fails that
// this peer has.
void policy::block_finished(peer_connection& c, piece_block b)
{
// if the peer hasn't choked us, ask for another piece
2003-12-17 04:40:13 +01:00
if (!c.has_peer_choked())
request_a_block(*m_torrent, c);
}
// this is called when we are unchoked by a peer
// i.e. a peer lets us know that we will receive
// data from now on
void policy::unchoked(peer_connection& c)
{
if (c.is_interesting())
{
request_a_block(*m_torrent, c);
}
}
2003-12-14 06:56:12 +01:00
// called when a peer is interested in us
void policy::interested(peer_connection& c)
{
2003-12-14 23:55:32 +01:00
}
2003-12-14 06:56:12 +01:00
// called when a peer is no longer interested in us
void policy::not_interested(peer_connection& c)
{
2004-01-17 21:04:19 +01:00
if (m_torrent->ratio() != 0.f)
{
assert(c.share_diff() < std::numeric_limits<int>::max());
size_type diff = c.share_diff();
2004-01-17 21:04:19 +01:00
if (diff > 0 && c.is_seed())
{
// the peer is a seed and has sent
// us more than we have sent it back.
// consider the download as free download
m_available_free_upload += diff;
c.add_free_upload(-diff);
}
}
2004-03-21 03:03:37 +01:00
if (!c.is_choked())
{
c.send_choke();
--m_num_unchoked;
}
2003-12-14 06:56:12 +01:00
}
bool policy::unchoke_one_peer()
{
peer* p = find_unchoke_candidate();
if (p == 0) return false;
2004-01-21 01:59:38 +01:00
assert(p->connection);
assert(!p->connection->is_disconnecting());
2003-12-14 06:56:12 +01:00
2004-01-12 21:31:27 +01:00
p->connection->send_unchoke();
2003-12-14 06:56:12 +01:00
p->last_optimistically_unchoked = boost::posix_time::second_clock::local_time();
++m_num_unchoked;
return true;
}
2004-01-14 17:18:53 +01:00
bool policy::connect_one_peer()
{
2004-01-21 01:59:38 +01:00
if(m_torrent->num_peers() >= m_max_connections)
return false;
2004-01-14 17:18:53 +01:00
peer* p = find_connect_candidate();
2004-01-15 17:45:34 +01:00
if (p == 0) return false;
2004-01-14 17:18:53 +01:00
assert(!p->banned);
2004-01-14 19:57:43 +01:00
assert(!p->connection);
2004-01-21 01:59:38 +01:00
assert(p->type==peer::connectable);
2004-02-25 14:18:41 +01:00
2004-02-26 19:55:10 +01:00
return connect_peer(p);
2004-02-25 14:18:41 +01:00
}
2004-02-26 19:55:10 +01:00
bool policy::connect_peer(peer *p)
2004-02-25 14:18:41 +01:00
{
2004-02-26 19:55:10 +01:00
try
{
p->connection = &m_torrent->connect_to_peer(p->id);
p->connection->add_stat(p->prev_amount_download, p->prev_amount_upload);
p->prev_amount_download = 0;
p->prev_amount_upload = 0;
p->connected =
m_last_optimistic_disconnect =
boost::posix_time::second_clock::local_time();
return true;
}
catch (network_error&)
{
// TODO: remove the peer
// m_peers.erase(std::find(m_peers.begin(), m_peers.end(), p));
}
return false;
2004-01-14 17:18:53 +01:00
}
2004-01-21 01:59:38 +01:00
bool policy::disconnect_one_peer()
{
2004-03-21 03:03:37 +01:00
peer *p = find_disconnect_candidate();
2004-01-21 01:59:38 +01:00
if(!p)
return false;
p->connection->disconnect();
return true;
}
// this is called whenever a peer connection is closed
void policy::connection_closed(const peer_connection& c)
{
2004-01-15 17:45:34 +01:00
std::vector<peer>::iterator i = std::find_if(
m_peers.begin()
, m_peers.end()
, match_peer_connection(c));
2004-03-21 03:03:37 +01:00
// if we couldn't find the connection in our list, just ignore it.
2004-01-15 17:45:34 +01:00
if (i == m_peers.end()) return;
assert(i->connection == &c);
i->connected = boost::posix_time::second_clock::local_time();
2003-12-14 06:56:12 +01:00
if (!i->connection->is_choked() && !m_torrent->is_aborted())
{
2004-03-21 03:03:37 +01:00
// if the peer that is diconnecting is unchoked
// then unchoke another peer in order to maintain
// the total number of unchoked peers
2003-12-14 06:56:12 +01:00
--m_num_unchoked;
unchoke_one_peer();
}
2004-01-14 02:19:30 +01:00
2004-03-21 03:03:37 +01:00
if (c.failed())
{
i->type = peer::not_connectable;
i->id.port = address::any_port;
}
2004-01-14 02:19:30 +01:00
// if the share ratio is 0 (infinite), the
// m_available_free_upload isn't used,
// because it isn't necessary.
if (m_torrent->ratio() != 0.f)
{
assert(i->connection->share_diff() < std::numeric_limits<int>::max());
m_available_free_upload += i->connection->share_diff();
}
2004-02-25 14:18:41 +01:00
i->prev_amount_download += c.statistics().total_payload_download();
i->prev_amount_upload += c.statistics().total_payload_upload();
2003-12-14 06:56:12 +01:00
i->connection = 0;
}
void policy::set_max_uploads(int max_uploads)
{
assert(max_uploads > 1 || max_uploads == -1);
2004-01-15 01:46:44 +01:00
if (max_uploads == -1) max_uploads = std::numeric_limits<int>::max();
2003-12-14 06:56:12 +01:00
m_max_uploads = max_uploads;
}
2004-01-21 01:59:38 +01:00
void policy::set_max_connections(int max_connections)
{
assert(max_connections > 1 || max_connections == -1);
if (max_connections == -1) max_connections = std::numeric_limits<int>::max();
2004-01-24 18:14:03 +01:00
assert(max_connections >= 2);
2004-01-21 01:59:38 +01:00
m_max_connections = max_connections;
}
void policy::peer_is_interesting(peer_connection& c)
{
2004-01-12 21:31:27 +01:00
c.send_interested();
if (c.has_peer_choked()) return;
request_a_block(*m_torrent, c);
}
#ifndef NDEBUG
2004-01-15 02:29:43 +01:00
bool policy::has_connection(const peer_connection* c)
{
2004-01-25 23:41:55 +01:00
assert(c);
2004-01-15 17:45:34 +01:00
return std::find_if(
m_peers.begin()
, m_peers.end()
, match_peer_ip(c->get_socket()->sender())) != m_peers.end();
}
2003-12-14 06:56:12 +01:00
2004-05-10 08:12:29 +02:00
void policy::check_invariant() const
2003-12-14 06:56:12 +01:00
{
2004-01-15 01:46:44 +01:00
assert(m_max_uploads >= 2);
2003-12-14 06:56:12 +01:00
int actual_unchoked = 0;
2004-05-10 08:12:29 +02:00
for (std::vector<peer>::const_iterator i = m_peers.begin();
2003-12-14 06:56:12 +01:00
i != m_peers.end();
++i)
{
if (!i->connection) continue;
if (!i->connection->is_choked()) actual_unchoked++;
}
2004-01-15 01:46:44 +01:00
assert(actual_unchoked <= m_max_uploads);
2003-12-14 06:56:12 +01:00
}
#endif
2003-12-14 06:56:12 +01:00
2004-01-15 20:32:03 +01:00
policy::peer::peer(const address& pid, peer::connection_type t)
2003-12-14 06:56:12 +01:00
: id(pid)
2004-01-15 20:32:03 +01:00
, type(t)
2003-12-14 06:56:12 +01:00
, last_optimistically_unchoked(
boost::gregorian::date(1970,boost::gregorian::Jan,1))
2004-01-14 17:18:53 +01:00
, connected(boost::gregorian::date(1970,boost::gregorian::Jan,1))
2003-12-14 06:56:12 +01:00
, prev_amount_upload(0)
, prev_amount_download(0)
, banned(false)
2004-01-14 17:18:53 +01:00
, connection(0)
{
assert(connected < boost::posix_time::second_clock::local_time());
}
2003-12-14 06:56:12 +01:00
2004-01-21 01:59:38 +01:00
size_type policy::peer::total_download() const
2003-12-14 06:56:12 +01:00
{
if (connection != 0)
2004-01-15 17:45:34 +01:00
{
2004-02-25 00:55:42 +01:00
assert(prev_amount_download == 0);
return connection->statistics().total_payload_download();
2004-01-15 17:45:34 +01:00
}
2003-12-14 06:56:12 +01:00
else
2004-01-15 17:45:34 +01:00
{
2003-12-14 06:56:12 +01:00
return prev_amount_download;
2004-01-15 17:45:34 +01:00
}
2003-12-14 06:56:12 +01:00
}
2004-01-21 01:59:38 +01:00
size_type policy::peer::total_upload() const
2003-12-14 06:56:12 +01:00
{
if (connection != 0)
2004-01-15 17:45:34 +01:00
{
2004-02-25 00:55:42 +01:00
assert(prev_amount_upload == 0);
return connection->statistics().total_payload_upload();
2004-01-15 17:45:34 +01:00
}
2003-12-14 06:56:12 +01:00
else
2004-01-15 17:45:34 +01:00
{
2003-12-14 06:56:12 +01:00
return prev_amount_upload;
2004-01-15 17:45:34 +01:00
}
2003-12-14 06:56:12 +01:00
}
}