From 9bdf2a9c8d55d7f9b3b5087c782de95b0d7d0026 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 10 Jul 2007 17:25:10 +0000 Subject: [PATCH] fixed invariant check in policy --- include/libtorrent/torrent.hpp | 8 +++++--- src/policy.cpp | 31 ++++++++++++++++++++++--------- src/torrent.cpp | 10 ++++++++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 8938d50af..bed076fe9 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -274,15 +274,17 @@ namespace libtorrent return i->second; } - peer_connection* connection_for(address const& a) +#ifndef NDEBUG + void connection_for(address const& a, std::vector& pc) { for (peer_iterator i = m_connections.begin() , end(m_connections.end()); i != end; ++i) { - if (i->first.address() == a) return i->second; + if (i->first.address() == a) pc.push_back(i->second); } - return 0; + return; } +#endif // the number of peers that belong to this torrent int num_peers() const { return (int)m_connections.size(); } diff --git a/src/policy.cpp b/src/policy.cpp index 7a4fc50c5..2444deeb7 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -140,14 +140,14 @@ namespace struct match_peer_ip { - match_peer_ip(tcp::endpoint const& ip) + match_peer_ip(address const& ip) : m_ip(ip) {} bool operator()(policy::peer const& p) const - { return p.ip.address() == m_ip.address(); } + { return p.ip.address() == m_ip; } - tcp::endpoint const& m_ip; + address const& m_ip; }; struct match_peer_id @@ -857,7 +857,7 @@ namespace libtorrent i = std::find_if( m_peers.begin() , m_peers.end() - , match_peer_ip(c.remote())); + , match_peer_ip(c.remote().address())); } if (i != m_peers.end()) @@ -882,19 +882,27 @@ namespace libtorrent "connection in favour of this one"); #endif i->connection->disconnect(); +#ifndef NDEBUG + check_invariant(); +#endif } } } else { - // we don't have ny info about this peer. + // we don't have any info about this peer. // add a new entry assert(c.remote() == c.get_socket()->remote_endpoint()); peer p(c.remote(), peer::not_connectable, 0); m_peers.push_back(p); i = boost::prior(m_peers.end()); +#ifndef NDEBUG + check_invariant(); +#endif } + + assert(m_torrent->connection_for(c.remote()) == &c); c.set_peer_info(&*i); assert(i->connection == 0); @@ -945,7 +953,7 @@ namespace libtorrent i = std::find_if( m_peers.begin() , m_peers.end() - , match_peer_ip(remote)); + , match_peer_ip(remote.address())); } if (i == m_peers.end()) @@ -1232,7 +1240,7 @@ namespace libtorrent if (c.failed()) { ++i->failcount; - i->connected = time_now(); +// i->connected = time_now(); } // if the share ratio is 0 (infinite), the @@ -1308,8 +1316,13 @@ namespace libtorrent ++total_connections; if (!p.connection) continue; if (!m_torrent->settings().allow_multiple_connections_per_ip) - assert(p.connection == m_torrent->connection_for(p.ip.address()) - || p.connection == m_torrent->connection_for(p.ip)); + { + std::vector conns; + m_torrent->connection_for(p.ip.address(), conns); + assert(std::find_if(conns.begin(), conns.end() + , boost::bind(std::equal_to(), _1, p.connection)) + != conns.end()); + } assert(p.connection->peer_info_struct() == 0 || p.connection->peer_info_struct() == &p); ++nonempty_connections; diff --git a/src/torrent.cpp b/src/torrent.cpp index b4a5efcd1..db51d90f1 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -938,8 +938,14 @@ namespace libtorrent if (p == 0) continue; #ifndef NDEBUG if (!settings().allow_multiple_connections_per_ip) - assert(p->connection == 0 || p->connection == connection_for(p->ip.address()) - || p->connection == connection_for(p->ip)); + { + std::vector conns; + connection_for(p->ip.address(), conns); + assert(p->connection == 0 + || std::find_if(conns.begin(), conns.end() + , boost::bind(std::equal_to(), _1, p->connection)) + != conns.end()); + } #endif if (p->connection) p->connection->received_invalid_data(index);