refactor find_disconnect_candidate_torrent to match libtorrent_aio

This commit is contained in:
Arvid Norberg 2013-01-02 07:39:02 +00:00
parent c37420f82d
commit 970ddba29b
3 changed files with 28 additions and 9 deletions

View File

@ -283,6 +283,7 @@ namespace libtorrent
boost::weak_ptr<torrent> find_torrent(sha1_hash const& info_hash);
boost::weak_ptr<torrent> find_torrent(std::string const& uuid);
boost::weak_ptr<torrent> find_disconnect_candidate_torrent();
peer_id const& get_peer_id() const { return m_peer_id; }

View File

@ -1079,20 +1079,26 @@ namespace libtorrent
{
// find a peer in some torrent (presumably the one with most peers)
// and disconnect the lowest ranking peer
aux::session_impl::torrent_map::iterator i = std::max_element(m_ses.m_torrents.begin(), m_ses.m_torrents.end()
, boost::bind(&torrent::num_peers, boost::bind(&session_impl::torrent_map::value_type::second, _1))
< boost::bind(&torrent::num_peers, boost::bind(&session_impl::torrent_map::value_type::second, _2)));
boost::weak_ptr<torrent> torr = m_ses.find_disconnect_candidate_torrent();
boost::shared_ptr<torrent> other_t = torr.lock();
TORRENT_ASSERT(i != m_ses.m_torrents.end());
if (i->second->num_peers() <= t->num_peers())
if (other_t)
{
if (other_t->num_peers() <= t->num_peers())
{
disconnect(errors::too_many_connections);
return;
}
// find the lowest ranking peer and disconnect that
peer_connection* p = other_t->find_lowest_ranking_peer();
p->disconnect(errors::too_many_connections);
peer_disconnected_other();
}
else
{
disconnect(errors::too_many_connections);
return;
}
// find the lowest ranking peer and disconnect that
peer_connection* p = i->second->find_lowest_ranking_peer();
p->disconnect(errors::too_many_connections);
peer_disconnected_other();
}
TORRENT_ASSERT(!m_torrent.expired());

View File

@ -4830,6 +4830,18 @@ retry:
return boost::weak_ptr<torrent>();
}
boost::weak_ptr<torrent> session_impl::find_disconnect_candidate_torrent()
{
aux::session_impl::torrent_map::iterator i = std::max_element(m_torrents.begin(), m_torrents.end()
, boost::bind(&torrent::num_peers, boost::bind(&session_impl::torrent_map::value_type::second, _1))
< boost::bind(&torrent::num_peers, boost::bind(&session_impl::torrent_map::value_type::second, _2)));
TORRENT_ASSERT(i != m_torrents.end());
if (i == m_torrents.end()) return boost::shared_ptr<torrent>();
return i->second;
}
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING
boost::shared_ptr<logger> session_impl::create_log(std::string const& name
, int instance, bool append)