From a09159c23957003bbf347c5716c1d0eb8f03462e Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 1 Oct 2005 15:12:10 +0000 Subject: [PATCH] fixed the buggy changes from last revision, modified the invariant checks --- include/libtorrent/torrent.hpp | 6 ++++++ src/peer_connection.cpp | 11 +++++++++++ src/policy.cpp | 18 ++++++++++++------ src/torrent.cpp | 13 ++++++++++--- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 4fe1ec728..1b9383e93 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -437,7 +437,13 @@ namespace libtorrent int m_complete; int m_incomplete; +#ifndef NDEBUG + public: +#endif std::map m_connections; +#ifndef NDEBUG + private: +#endif // this is the upload and download statistics for the whole torrent. // it's updated from all its peers once every second. diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 07e916430..79f35480d 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -367,6 +367,17 @@ namespace libtorrent assert(m_torrent != 0); m_torrent->remove_peer(this); } +#ifndef NDEBUG + else + { + if (m_torrent) + { + torrent::peer_iterator i = m_torrent->m_connections.find( + get_socket()->sender()); + assert(i != m_torrent->m_connections.end()); + } + } +#endif } void peer_connection::announce_piece(int index) diff --git a/src/policy.cpp b/src/policy.cpp index a819fbea9..3be4bff8a 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -1283,13 +1283,19 @@ namespace libtorrent ++num_torrent_peers; } - // the second case is for when new connections - // are attached to the torrent, they are first - // added to the policy and then to the torrent. - // so in the destructor of new_connection this - // case will be used. + // this invariant is a bit complicated. + // the usual case should be that connected_peers + // == num_torrent_peers. But when there's an incoming + // connection, it will first be added to the policy + // and then be added to the torrent. + // When there's an outgoing connection, it will first + // be added to the torrent and then to the policy. + // that's why the two second cases are in there. assert(connected_peers == num_torrent_peers - || connected_peers == num_torrent_peers - 1); + || (connected_peers == num_torrent_peers + 1 + && connected_peers > 0) + || (connected_peers + 1 == num_torrent_peers + && num_torrent_peers > 0)); // TODO: Make sure the number of peers in m_torrent is equal // to the number of connected peers in m_peers. diff --git a/src/torrent.cpp b/src/torrent.cpp index f3b44e69d..28f4bfe4e 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -900,13 +900,20 @@ namespace libtorrent = m_ses.m_connections.find(p->get_socket()); assert(i != m_ses.m_connections.end()); + // it's important that we call new_connection before + // the connection is added to the torrent's list. + // because if this fails, it will throw, and if this throws + // m_attatched_to_torrent won't be set in the peer_connections + // and the destructor won't remove the entry from the torrent's + // connection list. + m_policy->new_connection(*i->second); + + m_connections.insert(std::make_pair(p->get_socket()->sender(), p)); + #ifndef NDEBUG m_policy->check_invariant(); #endif - m_connections.insert(std::make_pair(p->get_socket()->sender(), p)); - - m_policy->new_connection(*i->second); } void torrent::disconnect_all()