fixed the buggy changes from last revision, modified the invariant checks

This commit is contained in:
Arvid Norberg 2005-10-01 15:12:10 +00:00
parent cbcf8df504
commit a09159c239
4 changed files with 39 additions and 9 deletions

View File

@ -437,7 +437,13 @@ namespace libtorrent
int m_complete; int m_complete;
int m_incomplete; int m_incomplete;
#ifndef NDEBUG
public:
#endif
std::map<address, peer_connection*> m_connections; std::map<address, peer_connection*> m_connections;
#ifndef NDEBUG
private:
#endif
// this is the upload and download statistics for the whole torrent. // this is the upload and download statistics for the whole torrent.
// it's updated from all its peers once every second. // it's updated from all its peers once every second.

View File

@ -367,6 +367,17 @@ namespace libtorrent
assert(m_torrent != 0); assert(m_torrent != 0);
m_torrent->remove_peer(this); 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) void peer_connection::announce_piece(int index)

View File

@ -1283,13 +1283,19 @@ namespace libtorrent
++num_torrent_peers; ++num_torrent_peers;
} }
// the second case is for when new connections // this invariant is a bit complicated.
// are attached to the torrent, they are first // the usual case should be that connected_peers
// added to the policy and then to the torrent. // == num_torrent_peers. But when there's an incoming
// so in the destructor of new_connection this // connection, it will first be added to the policy
// case will be used. // 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 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 // TODO: Make sure the number of peers in m_torrent is equal
// to the number of connected peers in m_peers. // to the number of connected peers in m_peers.

View File

@ -900,13 +900,20 @@ namespace libtorrent
= m_ses.m_connections.find(p->get_socket()); = m_ses.m_connections.find(p->get_socket());
assert(i != m_ses.m_connections.end()); 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 #ifndef NDEBUG
m_policy->check_invariant(); m_policy->check_invariant();
#endif #endif
m_connections.insert(std::make_pair(p->get_socket()->sender(), p));
m_policy->new_connection(*i->second);
} }
void torrent::disconnect_all() void torrent::disconnect_all()