added more asserts for choke/unchoke states and a peer_info flag to indicate a peer being optimistically unchoked

This commit is contained in:
Arvid Norberg 2007-08-21 17:45:28 +00:00
parent 89cba6df58
commit 23a20a24c1
4 changed files with 21 additions and 31 deletions

View File

@ -56,10 +56,11 @@ namespace libtorrent
connecting = 0x80, connecting = 0x80,
queued = 0x100, queued = 0x100,
on_parole = 0x200, on_parole = 0x200,
seed = 0x400 seed = 0x400,
optimistic_unchoke = 0x800
#ifndef TORRENT_DISABLE_ENCRYPTION #ifndef TORRENT_DISABLE_ENCRYPTION
, rc4_encrypted = 0x800, , rc4_encrypted = 0x100000,
plaintext_encrypted = 0x1000 plaintext_encrypted = 0x200000
#endif #endif
}; };

View File

@ -1679,6 +1679,8 @@ namespace libtorrent
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
assert(!m_peer_info || !m_peer_info->optimistically_unchoked);
if (m_choked) return; if (m_choked) return;
write_choke(); write_choke();
m_choked = true; m_choked = true;
@ -1697,14 +1699,6 @@ namespace libtorrent
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
#ifndef NDEBUG
// TODO: once the policy lowers the interval for optimistic
// unchoke, increase this value that interval
// this condition cannot be guaranteed since if peers disconnect
// a new one will be unchoked ignoring when it was last choked
//assert(time_now() - m_last_choke > seconds(9));
#endif
if (!m_choked) return; if (!m_choked) return;
write_unchoke(); write_unchoke();
m_choked = false; m_choked = false;
@ -2009,6 +2003,7 @@ namespace libtorrent
p.failcount = peer_info_struct()->failcount; p.failcount = peer_info_struct()->failcount;
p.num_hashfails = peer_info_struct()->hashfails; p.num_hashfails = peer_info_struct()->hashfails;
p.flags |= peer_info_struct()->on_parole ? peer_info::on_parole : 0; p.flags |= peer_info_struct()->on_parole ? peer_info::on_parole : 0;
p.flags |= peer_info_struct()->optimistically_unchoked ? peer_info::optimistic_unchoke : 0;
p.remote_dl_rate = m_remote_dl_rate; p.remote_dl_rate = m_remote_dl_rate;
} }
else else
@ -2231,20 +2226,6 @@ namespace libtorrent
m_reading_bytes += r.length; m_reading_bytes += r.length;
m_requests.erase(m_requests.begin()); m_requests.erase(m_requests.begin());
/*
if (m_requests.empty()
&& m_num_invalid_requests > 0
&& is_peer_interested()
&& !is_seed())
{
// this will make the peer clear
// its download queue and re-request
// pieces. Hopefully it will not
// send invalid requests then
send_choke();
send_unchoke();
}
*/
} }
} }
@ -2785,9 +2766,14 @@ namespace libtorrent
void peer_connection::check_invariant() const void peer_connection::check_invariant() const
{ {
if (m_peer_info) if (m_peer_info)
{
assert(m_peer_info->connection == this assert(m_peer_info->connection == this
|| m_peer_info->connection == 0); || m_peer_info->connection == 0);
if (m_peer_info->optimistically_unchoked)
assert(!is_choked());
}
boost::shared_ptr<torrent> t = m_torrent.lock(); boost::shared_ptr<torrent> t = m_torrent.lock();
if (!t) if (!t)
{ {

View File

@ -1320,14 +1320,16 @@ namespace libtorrent
INVARIANT_CHECK; INVARIANT_CHECK;
peer* p = c.peer_info_struct(); peer* p = c.peer_info_struct();
// if we couldn't find the connection in our list, just ignore it.
if (p == 0) return;
assert(std::find_if( assert((std::find_if(
m_peers.begin() m_peers.begin()
, m_peers.end() , m_peers.end()
, match_peer_connection(c)) , match_peer_connection(c))
!= m_peers.end()); != m_peers.end()) == (p != 0));
// if we couldn't find the connection in our list, just ignore it.
if (p == 0) return;
assert(p->connection == &c); assert(p->connection == &c);
p->connection = 0; p->connection = 0;

View File

@ -1106,6 +1106,7 @@ namespace detail
} }
else else
{ {
assert(p->peer_info_struct());
if (!p->is_choked() && !p->peer_info_struct()->optimistically_unchoked) if (!p->is_choked() && !p->peer_info_struct()->optimistically_unchoked)
t->choke_peer(*p); t->choke_peer(*p);
} }
@ -1159,8 +1160,8 @@ namespace detail
{ {
torrent* t = current_optimistic_unchoke->second->associated_torrent().lock().get(); torrent* t = current_optimistic_unchoke->second->associated_torrent().lock().get();
assert(t); assert(t);
t->choke_peer(*current_optimistic_unchoke->second);
current_optimistic_unchoke->second->peer_info_struct()->optimistically_unchoked = false; current_optimistic_unchoke->second->peer_info_struct()->optimistically_unchoked = false;
t->choke_peer(*current_optimistic_unchoke->second);
} }
torrent* t = optimistic_unchoke_candidate->second->associated_torrent().lock().get(); torrent* t = optimistic_unchoke_candidate->second->associated_torrent().lock().get();