diff --git a/include/libtorrent/peer_info.hpp b/include/libtorrent/peer_info.hpp index 15ad34a7a..82a4675e7 100755 --- a/include/libtorrent/peer_info.hpp +++ b/include/libtorrent/peer_info.hpp @@ -56,10 +56,11 @@ namespace libtorrent connecting = 0x80, queued = 0x100, on_parole = 0x200, - seed = 0x400 + seed = 0x400, + optimistic_unchoke = 0x800 #ifndef TORRENT_DISABLE_ENCRYPTION - , rc4_encrypted = 0x800, - plaintext_encrypted = 0x1000 + , rc4_encrypted = 0x100000, + plaintext_encrypted = 0x200000 #endif }; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 1bbc43bfb..6bc604bab 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -1679,6 +1679,8 @@ namespace libtorrent { INVARIANT_CHECK; + assert(!m_peer_info || !m_peer_info->optimistically_unchoked); + if (m_choked) return; write_choke(); m_choked = true; @@ -1697,14 +1699,6 @@ namespace libtorrent { 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; write_unchoke(); m_choked = false; @@ -2009,6 +2003,7 @@ namespace libtorrent p.failcount = peer_info_struct()->failcount; p.num_hashfails = peer_info_struct()->hashfails; 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; } else @@ -2231,20 +2226,6 @@ namespace libtorrent m_reading_bytes += r.length; 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 { if (m_peer_info) + { assert(m_peer_info->connection == this || m_peer_info->connection == 0); - + + if (m_peer_info->optimistically_unchoked) + assert(!is_choked()); + } + boost::shared_ptr t = m_torrent.lock(); if (!t) { diff --git a/src/policy.cpp b/src/policy.cpp index bd9dafd1d..811c4cea9 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -1320,14 +1320,16 @@ namespace libtorrent INVARIANT_CHECK; 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.end() , 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); p->connection = 0; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 1af3e4bec..5a8ecd34a 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1106,6 +1106,7 @@ namespace detail } else { + assert(p->peer_info_struct()); if (!p->is_choked() && !p->peer_info_struct()->optimistically_unchoked) t->choke_peer(*p); } @@ -1159,8 +1160,8 @@ namespace detail { torrent* t = current_optimistic_unchoke->second->associated_torrent().lock().get(); assert(t); - t->choke_peer(*current_optimistic_unchoke->second); 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();