connect candidate fix

This commit is contained in:
Arvid Norberg 2009-08-01 22:48:43 +00:00
parent 838a246f6b
commit 78a6df0b2a
5 changed files with 41 additions and 6 deletions

View File

@ -89,6 +89,8 @@ namespace libtorrent
void connection_closed(const peer_connection& c, int session_time); void connection_closed(const peer_connection& c, int session_time);
void ban_peer(policy::peer* p); void ban_peer(policy::peer* p);
void set_connection(policy::peer* p, peer_connection* c);
void set_failcount(policy::peer* p, int f);
// the peer has got at least one interesting piece // the peer has got at least one interesting piece
void peer_is_interesting(peer_connection& c); void peer_is_interesting(peer_connection& c);

View File

@ -2775,7 +2775,7 @@ namespace libtorrent
// since it most likely is ourself then // since it most likely is ourself then
if (pid == m_ses.get_peer_id()) if (pid == m_ses.get_peer_id())
{ {
if (peer_info_struct()) peer_info_struct()->banned = true; if (peer_info_struct()) t->get_policy().ban_peer(peer_info_struct());
disconnect(error_code(errors::self_connection, libtorrent_category), 1); disconnect(error_code(errors::self_connection, libtorrent_category), 1);
return; return;
} }
@ -2810,7 +2810,7 @@ namespace libtorrent
(*m_logger) << time_now_string() << " <== HANDSHAKE\n"; (*m_logger) << time_now_string() << " <== HANDSHAKE\n";
#endif #endif
// consider this a successful connection, reset the failcount // consider this a successful connection, reset the failcount
if (peer_info_struct()) peer_info_struct()->failcount = 0; if (peer_info_struct()) t->get_policy().set_failcount(peer_info_struct(), 0);
#ifndef TORRENT_DISABLE_ENCRYPTION #ifndef TORRENT_DISABLE_ENCRYPTION
// Toggle pe_support back to false if this is a // Toggle pe_support back to false if this is a

View File

@ -2712,7 +2712,12 @@ namespace libtorrent
void peer_connection::send_not_interested() void peer_connection::send_not_interested()
{ {
if (!m_interesting) return; if (!m_interesting)
{
disconnect_if_redundant();
return;
}
boost::shared_ptr<torrent> t = m_torrent.lock(); boost::shared_ptr<torrent> t = m_torrent.lock();
if (!t->ready_for_connections()) return; if (!t->ready_for_connections()) return;
m_interesting = false; m_interesting = false;

View File

@ -288,6 +288,8 @@ namespace libtorrent
// disconnects and removes all peers that are now filtered // disconnects and removes all peers that are now filtered
void policy::ip_filter_updated() void policy::ip_filter_updated()
{ {
INVARIANT_CHECK;
aux::session_impl& ses = m_torrent->session(); aux::session_impl& ses = m_torrent->session();
piece_picker* p = 0; piece_picker* p = 0;
if (m_torrent->has_picker()) p = &m_torrent->picker(); if (m_torrent->has_picker()) p = &m_torrent->picker();
@ -345,6 +347,7 @@ namespace libtorrent
TORRENT_ASSERT(m_num_connect_candidates > 0); TORRENT_ASSERT(m_num_connect_candidates > 0);
--m_num_connect_candidates; --m_num_connect_candidates;
} }
TORRENT_ASSERT(m_num_connect_candidates < m_peers.size());
if (m_round_robin > i - m_peers.begin()) --m_round_robin; if (m_round_robin > i - m_peers.begin()) --m_round_robin;
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
@ -432,6 +435,31 @@ namespace libtorrent
--m_num_connect_candidates; --m_num_connect_candidates;
p->banned = true; p->banned = true;
TORRENT_ASSERT(!is_connect_candidate(*p, m_finished));
}
void policy::set_connection(policy::peer* p, peer_connection* c)
{
INVARIANT_CHECK;
TORRENT_ASSERT(c);
const bool was_conn_cand = is_connect_candidate(*p, m_finished);
p->connection = c;
if (was_conn_cand) --m_num_connect_candidates;
}
void policy::set_failcount(policy::peer* p, int f)
{
INVARIANT_CHECK;
const bool was_conn_cand = is_connect_candidate(*p, m_finished);
p->failcount = f;
if (was_conn_cand != is_connect_candidate(*p, m_finished))
{
if (was_conn_cand) --m_num_connect_candidates;
else ++m_num_connect_candidates;
}
} }
bool policy::is_connect_candidate(peer const& p, bool finished) const bool policy::is_connect_candidate(peer const& p, bool finished) const
@ -1047,14 +1075,14 @@ namespace libtorrent
if (!m_torrent->connect_to_peer(&p)) if (!m_torrent->connect_to_peer(&p))
{ {
// failcount is a 5 bit value // failcount is a 5 bit value
const bool was_conn_cand = is_connect_candidate(p, m_finished);
if (p.failcount < 31) ++p.failcount; if (p.failcount < 31) ++p.failcount;
if (!is_connect_candidate(p, m_finished)) if (was_conn_cand && !is_connect_candidate(p, m_finished))
--m_num_connect_candidates; --m_num_connect_candidates;
return false; return false;
} }
TORRENT_ASSERT(p.connection); TORRENT_ASSERT(p.connection);
TORRENT_ASSERT(!is_connect_candidate(p, m_finished)); TORRENT_ASSERT(!is_connect_candidate(p, m_finished));
--m_num_connect_candidates;
return true; return true;
} }

View File

@ -3887,7 +3887,7 @@ namespace libtorrent
// add the newly connected peer to this torrent's peer list // add the newly connected peer to this torrent's peer list
m_connections.insert(boost::get_pointer(c)); m_connections.insert(boost::get_pointer(c));
m_ses.m_connections.insert(c); m_ses.m_connections.insert(c);
peerinfo->connection = c.get(); m_policy.set_connection(peerinfo, c.get());
c->start(); c->start();
int timeout = settings().peer_connect_timeout; int timeout = settings().peer_connect_timeout;