when evicting peers waiting to establish connection, evict the ones that have been trying for the longest first

This commit is contained in:
Arvid Norberg 2013-09-14 21:47:31 +00:00
parent a386d8aca0
commit 2df72d276e
3 changed files with 18 additions and 8 deletions

View File

@ -1751,7 +1751,6 @@ namespace libtorrent
std::string myip = root.dict_find_string_value("yourip");
if (!myip.empty())
{
// TODO: don't trust this blindly
if (myip.size() == address_v4::bytes_type().size())
{
address_v4::bytes_type bytes;

View File

@ -3983,7 +3983,6 @@ namespace libtorrent
boost::shared_ptr<torrent> t = m_torrent.lock();
assert(t);
// TODO: we should probably just send a HAVE_ALL here
for (int i = 0; i < int(m_have_piece.size()); ++i)
{
if (m_have_piece[i] || !t->have_piece(i)) continue;

View File

@ -5964,6 +5964,19 @@ namespace libtorrent
return true;
}
bool connecting_time_compare(peer_connection const* lhs, peer_connection const* rhs)
{
bool lhs_connecting = lhs->is_connecting() && !lhs->is_disconnecting();
bool rhs_connecting = rhs->is_connecting() && !rhs->is_disconnecting();
if (lhs_connecting > rhs_connecting) return false;
if (lhs_connecting < rhs_connecting) return true;
// a lower value of connected_time means it's been waiting
// longer. This is a less-than comparison, so if lhs has
// waited longer than rhs, we should return false.
return lhs->connected_time() >= rhs->connected_time();
}
bool torrent::attach_peer(peer_connection* p)
{
// INVARIANT_CHECK;
@ -6083,13 +6096,12 @@ namespace libtorrent
// find any peer that's connecting (i.e. a half-open TCP connection)
// that's also not disconnecting
// TODO: 1 ideally, we would disconnect the oldest connection
// i.e. the one that has waited the longest to connect.
std::set<peer_connection*>::iterator i = std::find_if(begin(), end()
, boost::bind(&peer_connection::is_connecting, _1)
&& !boost::bind(&peer_connection::is_disconnecting, _1));
// disconnect the peer that's been wating to establish a connection
// the longest
std::set<peer_connection*>::iterator i = std::max_element(begin(), end()
, &connecting_time_compare);
if (i == end())
if (i == end() || !(*i)->is_connecting() || (*i)->is_disconnecting())
{
// this seems odd, but we might as well handle it
p->disconnect(errors::too_many_connections);