factor out logic around adding torrents to the session into insert_torrent()

This commit is contained in:
Arvid Norberg 2019-03-02 10:03:55 +01:00 committed by Arvid Norberg
parent d733dd7e51
commit 57e56d5070
3 changed files with 55 additions and 44 deletions

View File

@ -373,7 +373,10 @@ namespace aux {
int num_torrents() const override { return int(m_torrents.size()); } int num_torrents() const override { return int(m_torrents.size()); }
void insert_torrent(sha1_hash const& ih, std::shared_ptr<torrent> const& t void insert_torrent(sha1_hash const& ih, std::shared_ptr<torrent> const& t
, std::string uuid) override; #if TORRENT_ABI_VERSION == 1
, std::string uuid
#endif
) override;
std::shared_ptr<torrent> delay_load_torrent(sha1_hash const& info_hash std::shared_ptr<torrent> delay_load_torrent(sha1_hash const& info_hash
, peer_connection* pc) override; , peer_connection* pc) override;

View File

@ -186,7 +186,10 @@ namespace aux {
virtual std::shared_ptr<torrent> delay_load_torrent(sha1_hash const& info_hash virtual std::shared_ptr<torrent> delay_load_torrent(sha1_hash const& info_hash
, peer_connection* pc) = 0; , peer_connection* pc) = 0;
virtual void insert_torrent(sha1_hash const& ih, std::shared_ptr<torrent> const& t virtual void insert_torrent(sha1_hash const& ih, std::shared_ptr<torrent> const& t
, std::string uuid) = 0; #if TORRENT_ABI_VERSION == 1
, std::string uuid
#endif
) = 0;
#if TORRENT_ABI_VERSION == 1 #if TORRENT_ABI_VERSION == 1
//deprecated in 1.2 //deprecated in 1.2
virtual void insert_uuid_torrent(std::string uuid, std::shared_ptr<torrent> const& t) = 0; virtual void insert_uuid_torrent(std::string uuid, std::shared_ptr<torrent> const& t) = 0;

View File

@ -4363,15 +4363,50 @@ namespace aux {
} }
void session_impl::insert_torrent(sha1_hash const& ih, std::shared_ptr<torrent> const& t void session_impl::insert_torrent(sha1_hash const& ih, std::shared_ptr<torrent> const& t
, std::string uuid) #if TORRENT_ABI_VERSION == 1
, std::string const uuid
#endif
)
{ {
m_torrents.insert(std::make_pair(ih, t)); sha1_hash const next_lsd = m_next_lsd_torrent != m_torrents.end()
? m_next_lsd_torrent->first : sha1_hash();
#ifndef TORRENT_DISABLE_DHT
sha1_hash const next_dht = m_next_dht_torrent != m_torrents.end()
? m_next_dht_torrent->first : sha1_hash();
#endif
float const load_factor = m_torrents.load_factor();
m_torrents.emplace(ih, t);
#if !defined TORRENT_DISABLE_ENCRYPTION
static char const req2[4] = {'r', 'e', 'q', '2'};
hasher h(req2);
h.update(ih);
// this is SHA1("req2" + info-hash), used for
// encrypted hand shakes
m_obfuscated_torrents.emplace(h.final(), t);
#endif
// if this insert made the hash grow, the iterators became invalid
// we need to reset them
if (m_torrents.load_factor() < load_factor)
{
// this indicates the hash table re-hashed
if (!next_lsd.is_all_zeros())
m_next_lsd_torrent = m_torrents.find(next_lsd);
#ifndef TORRENT_DISABLE_DHT
if (!next_dht.is_all_zeros())
m_next_dht_torrent = m_torrents.find(next_dht);
#endif
}
#if TORRENT_ABI_VERSION == 1 #if TORRENT_ABI_VERSION == 1
//deprecated in 1.2 //deprecated in 1.2
if (!uuid.empty()) m_uuids.insert(std::make_pair(uuid, t)); if (!uuid.empty()) m_uuids.insert(std::make_pair(uuid, t));
#else
TORRENT_UNUSED(uuid);
#endif #endif
t->added();
} }
void session_impl::set_queue_position(torrent* me, queue_position_t p) void session_impl::set_queue_position(torrent* me, queue_position_t p)
@ -4772,48 +4807,18 @@ namespace aux {
add_extensions_to_torrent(torrent_ptr, params.userdata); add_extensions_to_torrent(torrent_ptr, params.userdata);
#endif #endif
sha1_hash const next_lsd = m_next_lsd_torrent != m_torrents.end() insert_torrent(params.info_hash, torrent_ptr
? m_next_lsd_torrent->first : sha1_hash(); #if TORRENT_ABI_VERSION == 1
#ifndef TORRENT_DISABLE_DHT //deprecated in 1.2
sha1_hash const next_dht = m_next_dht_torrent != m_torrents.end() , params.uuid.empty()
? m_next_dht_torrent->first : sha1_hash(); ? params.url.empty() ? std::string()
#endif : params.url
float const load_factor = m_torrents.load_factor(); : params.uuid
m_torrents.emplace(params.info_hash, torrent_ptr);
#if !defined TORRENT_DISABLE_ENCRYPTION
static char const req2[4] = {'r', 'e', 'q', '2'};
hasher h(req2);
h.update(params.info_hash);
// this is SHA1("req2" + info-hash), used for
// encrypted hand shakes
m_obfuscated_torrents.emplace(h.final(), torrent_ptr);
#endif #endif
);
// once we successfully add the torrent, we can disarm the abort action // once we successfully add the torrent, we can disarm the abort action
abort_torrent.disarm(); abort_torrent.disarm();
torrent_ptr->added();
// if this insert made the hash grow, the iterators became invalid
// we need to reset them
if (m_torrents.load_factor() < load_factor)
{
// this indicates the hash table re-hashed
if (!next_lsd.is_all_zeros())
m_next_lsd_torrent = m_torrents.find(next_lsd);
#ifndef TORRENT_DISABLE_DHT
if (!next_dht.is_all_zeros())
m_next_dht_torrent = m_torrents.find(next_dht);
#endif
}
#if TORRENT_ABI_VERSION == 1
//deprecated in 1.2
if (!params.uuid.empty() || !params.url.empty())
m_uuids.emplace(params.uuid.empty()
? params.url : params.uuid, torrent_ptr);
#endif
// recalculate auto-managed torrents sooner (or put it off) // recalculate auto-managed torrents sooner (or put it off)
// if another torrent will be added within one second from now // if another torrent will be added within one second from now