From 8e80324c36eb90613aefb22ce17d77ae399e8819 Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Mon, 6 Jan 2014 21:59:40 -0200 Subject: [PATCH] try to reduce the level of useless dht tracker requests by not announcing empty torrents we do not follow. --- libtorrent/include/libtorrent/torrent.hpp | 4 ++++ libtorrent/include/libtorrent/torrent_handle.hpp | 1 + libtorrent/src/torrent.cpp | 10 ++++++++++ libtorrent/src/torrent_handle.cpp | 6 ++++++ src/twister.cpp | 16 +++++++++------- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/libtorrent/include/libtorrent/torrent.hpp b/libtorrent/include/libtorrent/torrent.hpp index 5cc50f34..c754a738 100644 --- a/libtorrent/include/libtorrent/torrent.hpp +++ b/libtorrent/include/libtorrent/torrent.hpp @@ -513,6 +513,7 @@ namespace libtorrent } void super_seeding(bool on); + void set_following(bool on); int get_piece_to_super_seed(bitfield const&); // returns true if we have downloaded the given piece @@ -1219,6 +1220,9 @@ namespace libtorrent // if this is true, we're currently super seeding this // torrent. bool m_super_seeding:1; + + // if this is true, we're currently following this user + bool m_following:1; // this is set when we don't want to load seed_mode, // paused or auto_managed from the resume data diff --git a/libtorrent/include/libtorrent/torrent_handle.hpp b/libtorrent/include/libtorrent/torrent_handle.hpp index 29f0ec80..5471ef6c 100644 --- a/libtorrent/include/libtorrent/torrent_handle.hpp +++ b/libtorrent/include/libtorrent/torrent_handle.hpp @@ -412,6 +412,7 @@ namespace libtorrent #endif void super_seeding(bool on) const; + void set_following(bool on) const; sha1_hash info_hash() const; diff --git a/libtorrent/src/torrent.cpp b/libtorrent/src/torrent.cpp index 1e9e9dec..1601a05d 100644 --- a/libtorrent/src/torrent.cpp +++ b/libtorrent/src/torrent.cpp @@ -387,6 +387,7 @@ namespace libtorrent , m_got_tracker_response(false) , m_connections_initialized(false) , m_super_seeding(false) + , m_following(false) , m_override_resume_data(p.flags & add_torrent_params::flag_override_resume_data) #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES , m_resolving_country(false) @@ -827,6 +828,10 @@ namespace libtorrent // because the info-hash is just the URL hash if (!m_torrent_file->is_valid() && !m_url.empty()) return false; + // try to reduce the level of useless dht tracker requests by not + // announcing empty torrents we do not follow. + if (!m_following && last_have() == -1 ) return false; + // don't announce private torrents if (m_torrent_file->is_valid() && m_torrent_file->priv()) return false; if (m_trackers.empty()) return true; @@ -3509,6 +3514,11 @@ namespace libtorrent } } + void torrent::set_following(bool on) + { + m_following = on; + } + int torrent::get_piece_to_super_seed(bitfield const& bits) { // return a piece with low availability that is not in diff --git a/libtorrent/src/torrent_handle.cpp b/libtorrent/src/torrent_handle.cpp index 590bd4e6..99fa84b1 100644 --- a/libtorrent/src/torrent_handle.cpp +++ b/libtorrent/src/torrent_handle.cpp @@ -975,6 +975,12 @@ namespace libtorrent TORRENT_ASYNC_CALL1(super_seeding, on); } + void torrent_handle::set_following(bool on) const + { + INVARIANT_CHECK; + TORRENT_ASYNC_CALL1(set_following, on); + } + void torrent_handle::resolve_countries(bool r) { INVARIANT_CHECK; diff --git a/src/twister.cpp b/src/twister.cpp index 35ced31e..bdea3f7c 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -72,7 +72,7 @@ sha1_hash dhtTargetHash(std::string const &username, std::string const &resource return hasher(buf.data(), buf.size()).final(); } -torrent_handle startTorrentUser(std::string const &username) +torrent_handle startTorrentUser(std::string const &username, bool following) { bool userInTxDb = usernameExists(username); // keep this outside cs_twister to avoid deadlock if( !userInTxDb ) @@ -96,6 +96,8 @@ torrent_handle startTorrentUser(std::string const &username) m_userTorrent[username] = ses->add_torrent(tparams); m_userTorrent[username].force_dht_announce(); } + if( following ) + m_userTorrent[username].set_following(true); return m_userTorrent[username]; } @@ -298,7 +300,7 @@ void ThreadWaitExtIP() } // now restart the user torrents BOOST_FOREACH(string username, torrentsToStart) { - startTorrentUser(username); + startTorrentUser(username, true); } } @@ -601,7 +603,7 @@ void ThreadSessionAlerts() neighborCheck.count(ih) ) { // Do something! if( dd->m_resource == "tracker" ) { - startTorrentUser(dd->m_username); + startTorrentUser(dd->m_username, false); } else { printf("Neighbor of special resource - do something!\n"); } @@ -1285,7 +1287,7 @@ Value newpostmsg(const Array& params, bool fHelp) if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) ) throw JSONRPCError(RPC_INVALID_PARAMS,errmsg); - torrent_handle h = startTorrentUser(strUsername); + torrent_handle h = startTorrentUser(strUsername, true); if( h.is_valid() ) { // if member of torrent post it directly h.add_piece(k,buf.data(),buf.size()); @@ -1371,7 +1373,7 @@ Value newdirectmsg(const Array& params, bool fHelp) m_users[strFrom].m_directmsg[strTo].push_back(stoDM); } - torrent_handle h = startTorrentUser(strFrom); + torrent_handle h = startTorrentUser(strFrom, true); h.add_piece(k,buf.data(),buf.size()); hexcapePost(v); @@ -1413,7 +1415,7 @@ Value newrtmsg(const Array& params, bool fHelp) if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg,NULL) ) throw JSONRPCError(RPC_INVALID_PARAMS,errmsg); - torrent_handle h = startTorrentUser(strUsername); + torrent_handle h = startTorrentUser(strUsername, true); if( h.is_valid() ) { // if member of torrent post it directly h.add_piece(k,buf.data(),buf.size()); @@ -1618,7 +1620,7 @@ Value follow(const Array& params, bool fHelp) for( unsigned int u = 0; u < users.size(); u++ ) { string username = users[u].get_str(); - torrent_handle h = startTorrentUser(username); + torrent_handle h = startTorrentUser(username, true); if( h.is_valid() ) { LOCK(cs_twister);