refactor: make some tr_torrent fields private (#6259)
* refactor: make tr_torrent::is_stopping_ private * refactor: make tr_torrent::is_queued_ private * refactor: make tr_torrent::is_dirty_ private * refactor: make tr_torrent::is_deleting_ private * refactor: make tr_torrent::obfuscated_hash_ private * refactor: demeter
This commit is contained in:
parent
7c177224ed
commit
ad63c7d77f
|
@ -236,7 +236,7 @@ char const* torrentStop(tr_session* session, tr_variant* args_in, tr_variant* /*
|
|||
{
|
||||
if (tor->activity() != TR_STATUS_STOPPED)
|
||||
{
|
||||
tor->is_stopping_ = true;
|
||||
tor->stop_soon();
|
||||
session->rpcNotify(TR_RPC_TORRENT_STOPPED, tor);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -389,7 +389,7 @@ double tr_torrentGetMetadataPercent(tr_torrent const* tor)
|
|||
|
||||
std::string tr_torrentGetMagnetLink(tr_torrent const* tor)
|
||||
{
|
||||
return tor->metainfo_.magnet();
|
||||
return tor->magnet();
|
||||
}
|
||||
|
||||
size_t tr_torrentGetMagnetLinkToBuf(tr_torrent const* tor, char* buf, size_t buflen)
|
||||
|
|
|
@ -148,16 +148,6 @@ bool tr_torrentSetMetainfoFromFile(tr_torrent* tor, tr_torrent_metainfo const* m
|
|||
|
||||
namespace
|
||||
{
|
||||
constexpr void torrentSetQueued(tr_torrent* tor, bool queued)
|
||||
{
|
||||
if (tor->is_queued_ != queued)
|
||||
{
|
||||
tor->is_queued_ = queued;
|
||||
tor->mark_changed();
|
||||
tor->set_dirty();
|
||||
}
|
||||
}
|
||||
|
||||
bool setLocalErrorIfFilesDisappeared(tr_torrent* tor, std::optional<bool> has_local_data = {})
|
||||
{
|
||||
auto const has = has_local_data ? *has_local_data : tor->has_any_local_data();
|
||||
|
@ -458,7 +448,7 @@ void tr_torrentCheckSeedLimit(tr_torrent* tor)
|
|||
if (tr_torrentIsSeedRatioDone(tor))
|
||||
{
|
||||
tr_logAddInfoTor(tor, _("Seed ratio reached; pausing torrent"));
|
||||
tor->is_stopping_ = true;
|
||||
tor->stop_soon();
|
||||
tor->session->onRatioLimitHit(tor);
|
||||
}
|
||||
/* if we're seeding and reach our inactivity limit, stop the torrent */
|
||||
|
@ -466,7 +456,7 @@ void tr_torrentCheckSeedLimit(tr_torrent* tor)
|
|||
{
|
||||
tr_logAddInfoTor(tor, _("Seeding idle limit reached; pausing torrent"));
|
||||
|
||||
tor->is_stopping_ = true;
|
||||
tor->stop_soon();
|
||||
tor->finished_seeding_by_idle_ = true;
|
||||
tor->session->onIdleLimitHit(tor);
|
||||
}
|
||||
|
@ -642,7 +632,7 @@ void torrentStartImpl(tr_torrent* const tor)
|
|||
TR_ASSERT(tr_isTorrent(tor));
|
||||
|
||||
tor->recheck_completeness();
|
||||
torrentSetQueued(tor, false);
|
||||
tor->set_is_queued(false);
|
||||
|
||||
time_t const now = tr_time();
|
||||
|
||||
|
@ -764,7 +754,7 @@ void torrentStart(tr_torrent* tor, torrent_start_opts opts)
|
|||
case TR_STATUS_STOPPED:
|
||||
if (!opts.bypass_queue && torrentShouldQueue(tor))
|
||||
{
|
||||
torrentSetQueued(tor, true);
|
||||
tor->set_is_queued();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -788,37 +778,36 @@ void torrentStart(tr_torrent* tor, torrent_start_opts opts)
|
|||
tor->set_dirty();
|
||||
tor->session->runInSessionThread(torrentStartImpl, tor);
|
||||
}
|
||||
|
||||
void torrentStop(tr_torrent* const tor)
|
||||
{
|
||||
TR_ASSERT(tr_isTorrent(tor));
|
||||
TR_ASSERT(tor->session->am_in_session_thread());
|
||||
auto const lock = tor->unique_lock();
|
||||
|
||||
tor->is_running_ = false;
|
||||
tor->is_stopping_ = false;
|
||||
|
||||
if (!tor->session->isClosing())
|
||||
{
|
||||
tr_logAddInfoTor(tor, _("Pausing torrent"));
|
||||
}
|
||||
|
||||
tor->session->verify_remove(tor);
|
||||
|
||||
tor->stopped_.emit(tor);
|
||||
tor->session->announcer_->stopTorrent(tor);
|
||||
|
||||
tor->session->closeTorrentFiles(tor);
|
||||
|
||||
if (!tor->is_deleting_)
|
||||
{
|
||||
tr_torrentSave(tor);
|
||||
}
|
||||
|
||||
torrentSetQueued(tor, false);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void tr_torrent::stop_now()
|
||||
{
|
||||
TR_ASSERT(session->am_in_session_thread());
|
||||
auto const lock = unique_lock();
|
||||
|
||||
is_running_ = false;
|
||||
is_stopping_ = false;
|
||||
|
||||
if (!session->isClosing())
|
||||
{
|
||||
tr_logAddInfoTor(this, _("Pausing torrent"));
|
||||
}
|
||||
|
||||
session->verify_remove(this);
|
||||
|
||||
stopped_.emit(this);
|
||||
session->announcer_->stopTorrent(this);
|
||||
|
||||
session->closeTorrentFiles(this);
|
||||
|
||||
if (!is_deleting_)
|
||||
{
|
||||
tr_torrentSave(this);
|
||||
}
|
||||
|
||||
set_is_queued(false);
|
||||
}
|
||||
|
||||
void tr_torrentStop(tr_torrent* tor)
|
||||
{
|
||||
if (!tr_isTorrent(tor))
|
||||
|
@ -830,7 +819,7 @@ void tr_torrentStop(tr_torrent* tor)
|
|||
|
||||
tor->start_when_stable = false;
|
||||
tor->set_dirty();
|
||||
tor->session->runInSessionThread(torrentStop, tor);
|
||||
tor->session->runInSessionThread([tor]() { tor->stop_now(); });
|
||||
}
|
||||
|
||||
void tr_torrentRemove(tr_torrent* tor, bool delete_flag, tr_fileFunc delete_func, void* user_data)
|
||||
|
@ -857,7 +846,7 @@ void tr_torrentFreeInSessionThread(tr_torrent* tor)
|
|||
tr_logAddInfoTor(tor, _("Removing torrent"));
|
||||
}
|
||||
|
||||
torrentStop(tor);
|
||||
tor->stop_now();
|
||||
|
||||
if (tor->is_deleting_)
|
||||
{
|
||||
|
@ -948,7 +937,7 @@ void on_metainfo_completed(tr_torrent* tor)
|
|||
void tr_torrent::on_metainfo_updated()
|
||||
{
|
||||
completion = tr_completion{ this, &block_info() };
|
||||
obfuscated_hash = tr_sha1::digest("req2"sv, info_hash());
|
||||
obfuscated_hash_ = tr_sha1::digest("req2"sv, info_hash());
|
||||
fpm_.reset(metainfo_);
|
||||
file_mtimes_.resize(file_count());
|
||||
file_priorities_.reset(&fpm_);
|
||||
|
@ -967,7 +956,6 @@ void tr_torrent::init(tr_ctor const* const ctor)
|
|||
queuePosition = std::size(session->torrents());
|
||||
|
||||
on_metainfo_updated();
|
||||
|
||||
char const* dir = nullptr;
|
||||
if (tr_ctorGetDownloadDir(ctor, TR_FORCE, &dir) || tr_ctorGetDownloadDir(ctor, TR_FALLBACK, &dir))
|
||||
{
|
||||
|
@ -1603,65 +1591,36 @@ void tr_torrentStartMagnet(tr_torrent* tor)
|
|||
|
||||
// ---
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace verify_helpers
|
||||
{
|
||||
void onVerifyDoneThreadFunc(tr_torrent* const tor)
|
||||
{
|
||||
TR_ASSERT(tor->session->am_in_session_thread());
|
||||
|
||||
if (tor->is_deleting_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
tor->recheck_completeness();
|
||||
|
||||
if (tor->start_when_stable)
|
||||
{
|
||||
auto opts = torrent_start_opts{};
|
||||
opts.has_local_data = !tor->checked_pieces_.has_none();
|
||||
torrentStart(tor, opts);
|
||||
}
|
||||
}
|
||||
|
||||
void verifyTorrent(tr_torrent* const tor, bool force)
|
||||
{
|
||||
TR_ASSERT(tor->session->am_in_session_thread());
|
||||
auto const lock = tor->unique_lock();
|
||||
|
||||
if (tor->is_deleting_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* if the torrent's already being verified, stop it */
|
||||
tor->session->verify_remove(tor);
|
||||
|
||||
if (!tor->has_metainfo())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (tor->is_running())
|
||||
{
|
||||
torrentStop(tor);
|
||||
}
|
||||
|
||||
if (force || !setLocalErrorIfFilesDisappeared(tor))
|
||||
{
|
||||
tor->session->verify_add(tor);
|
||||
}
|
||||
}
|
||||
} // namespace verify_helpers
|
||||
} // namespace
|
||||
|
||||
void tr_torrentVerify(tr_torrent* tor, bool force)
|
||||
{
|
||||
using namespace verify_helpers;
|
||||
tor->session->runInSessionThread(
|
||||
[tor, force]()
|
||||
{
|
||||
TR_ASSERT(tor->session->am_in_session_thread());
|
||||
auto const lock = tor->unique_lock();
|
||||
|
||||
tor->session->runInSessionThread(verifyTorrent, tor, force);
|
||||
if (tor->is_deleting_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
tor->session->verify_remove(tor);
|
||||
|
||||
if (!tor->has_metainfo())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (tor->is_running())
|
||||
{
|
||||
tor->stop_now();
|
||||
}
|
||||
|
||||
if (force || !setLocalErrorIfFilesDisappeared(tor))
|
||||
{
|
||||
tor->session->verify_add(tor);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void tr_torrent::set_verify_state(VerifyState const state)
|
||||
|
@ -1716,10 +1675,9 @@ void tr_torrent::VerifyMediator::on_piece_checked(tr_piece_index_t const piece,
|
|||
tor_->verify_progress_ = std::clamp(static_cast<float>(piece + 1U) / tor_->metainfo_.piece_count(), 0.0F, 1.0F);
|
||||
}
|
||||
|
||||
// (usually called from tr_verify_worker's thread)
|
||||
void tr_torrent::VerifyMediator::on_verify_done(bool const aborted)
|
||||
{
|
||||
using namespace verify_helpers;
|
||||
|
||||
if (time_started_.has_value())
|
||||
{
|
||||
auto const total_size = tor_->total_size();
|
||||
|
@ -1737,12 +1695,28 @@ void tr_torrent::VerifyMediator::on_verify_done(bool const aborted)
|
|||
|
||||
if (!aborted && !tor_->is_deleting_)
|
||||
{
|
||||
tor_->session->runInSessionThread(onVerifyDoneThreadFunc, tor_);
|
||||
}
|
||||
tor_->session->runInSessionThread(
|
||||
[tor = tor_]()
|
||||
{
|
||||
if (tor->is_deleting_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (tor_->verify_done_callback_)
|
||||
{
|
||||
tor_->verify_done_callback_(tor_);
|
||||
tor->recheck_completeness();
|
||||
|
||||
if (tor->verify_done_callback_)
|
||||
{
|
||||
tor->verify_done_callback_(tor);
|
||||
}
|
||||
|
||||
if (tor->start_when_stable)
|
||||
{
|
||||
auto opts = torrent_start_opts{};
|
||||
opts.has_local_data = !tor->checked_pieces_.has_none();
|
||||
torrentStart(tor, opts);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1752,9 +1726,9 @@ void tr_torrentSave(tr_torrent* tor)
|
|||
{
|
||||
TR_ASSERT(tr_isTorrent(tor));
|
||||
|
||||
if (tor->is_dirty_)
|
||||
if (tor->is_dirty())
|
||||
{
|
||||
tor->is_dirty_ = false;
|
||||
tor->set_dirty(false);
|
||||
tr_resume::save(tor);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -559,6 +559,16 @@ public:
|
|||
return this->is_queued_;
|
||||
}
|
||||
|
||||
void set_is_queued(bool queued = true) noexcept
|
||||
{
|
||||
if (is_queued_ != queued)
|
||||
{
|
||||
is_queued_ = queued;
|
||||
mark_changed();
|
||||
set_dirty();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto queue_direction() const noexcept
|
||||
{
|
||||
return this->is_done() ? TR_UP : TR_DOWN;
|
||||
|
@ -678,6 +688,11 @@ public:
|
|||
return is_stopping_;
|
||||
}
|
||||
|
||||
constexpr void stop_soon() noexcept
|
||||
{
|
||||
is_stopping_ = true;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto is_dirty() const noexcept
|
||||
{
|
||||
return is_dirty_;
|
||||
|
@ -900,6 +915,11 @@ public:
|
|||
|
||||
void init(tr_ctor const* ctor);
|
||||
|
||||
[[nodiscard]] TR_CONSTEXPR20 auto obfuscated_hash_equals(tr_sha1_digest_t const& test) const noexcept
|
||||
{
|
||||
return obfuscated_hash_ == test;
|
||||
}
|
||||
|
||||
tr_torrent_metainfo metainfo_;
|
||||
|
||||
tr_bandwidth bandwidth_;
|
||||
|
@ -937,8 +957,6 @@ public:
|
|||
// Will equal either download_dir or incomplete_dir
|
||||
tr_interned_string current_dir_;
|
||||
|
||||
tr_sha1_digest_t obfuscated_hash = {};
|
||||
|
||||
/* Used when the torrent has been created with a magnet link
|
||||
* and we're in the process of downloading the metainfo from
|
||||
* other peers */
|
||||
|
@ -976,11 +994,7 @@ public:
|
|||
|
||||
bool finished_seeding_by_idle_ = false;
|
||||
|
||||
bool is_deleting_ = false;
|
||||
bool is_dirty_ = false;
|
||||
bool is_queued_ = false;
|
||||
bool is_running_ = false;
|
||||
bool is_stopping_ = false;
|
||||
|
||||
// start the torrent after all the startup scaffolding is done,
|
||||
// e.g. fetching metadata from peers and/or verifying the torrent
|
||||
|
@ -991,7 +1005,12 @@ private:
|
|||
friend tr_stat const* tr_torrentStat(tr_torrent* tor);
|
||||
friend tr_torrent* tr_torrentNew(tr_ctor* ctor, tr_torrent** setme_duplicate_of);
|
||||
friend uint64_t tr_torrentGetBytesLeftToAllocate(tr_torrent const* tor);
|
||||
friend void tr_torrentCheckSeedLimit(tr_torrent* tor);
|
||||
friend void tr_torrentFreeInSessionThread(tr_torrent* tor);
|
||||
friend void tr_torrentGotBlock(tr_torrent* tor, tr_block_index_t block);
|
||||
friend void tr_torrentRemove(tr_torrent* tor, bool delete_flag, tr_fileFunc delete_func, void* user_data);
|
||||
friend void tr_torrentStop(tr_torrent* tor);
|
||||
friend void tr_torrentVerify(tr_torrent* tor, bool force);
|
||||
|
||||
enum class VerifyState : uint8_t
|
||||
{
|
||||
|
@ -1165,6 +1184,8 @@ private:
|
|||
|
||||
void on_metainfo_updated();
|
||||
|
||||
void stop_now();
|
||||
|
||||
tr_stat stats_ = {};
|
||||
|
||||
Error error_;
|
||||
|
@ -1175,6 +1196,8 @@ private:
|
|||
|
||||
tr_interned_string bandwidth_group_;
|
||||
|
||||
tr_sha1_digest_t obfuscated_hash_ = {};
|
||||
|
||||
mutable SimpleSmoothedSpeed eta_speed_;
|
||||
|
||||
tr_files_wanted files_wanted_{ &fpm_ };
|
||||
|
@ -1206,6 +1229,11 @@ private:
|
|||
|
||||
uint16_t idle_limit_minutes_ = 0;
|
||||
|
||||
bool is_deleting_ = false;
|
||||
bool is_dirty_ = false;
|
||||
bool is_queued_ = false;
|
||||
bool is_stopping_ = false;
|
||||
|
||||
bool needs_completeness_check_ = true;
|
||||
|
||||
bool sequential_download_ = false;
|
||||
|
|
|
@ -59,7 +59,7 @@ tr_torrent* tr_torrents::find_from_obfuscated_hash(tr_sha1_digest_t const& obfus
|
|||
{
|
||||
for (auto* const tor : *this)
|
||||
{
|
||||
if (tor->obfuscated_hash == obfuscated_hash)
|
||||
if (tor->obfuscated_hash_equals(obfuscated_hash))
|
||||
{
|
||||
return tor;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue