Don't access libtorrent session directly from TorrentImpl

This commit is contained in:
Vladimir Golovnev (Glassez) 2025-05-05 13:57:53 +03:00
commit 582dc99cae
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
4 changed files with 27 additions and 24 deletions

View file

@ -5443,6 +5443,19 @@ bool SessionImpl::addMoveTorrentStorageJob(TorrentImpl *torrent, const Path &new
return true;
}
lt::torrent_handle SessionImpl::reloadTorrent(const lt::torrent_handle &currentHandle, lt::add_torrent_params params)
{
m_nativeSession->remove_torrent(currentHandle, lt::session::delete_partfile);
auto *const extensionData = new ExtensionData;
params.userdata = LTClientData(extensionData);
#ifndef QBT_USES_LIBTORRENT2
params.storage = customStorageConstructor;
#endif
return m_nativeSession->add_torrent(std::move(params));
}
void SessionImpl::moveTorrentStorage(const MoveStorageJob &job) const
{
const TorrentImpl *torrent = getTorrent(job.torrentHandle);
@ -5971,7 +5984,7 @@ void SessionImpl::dispatchTorrentAlert(const lt::torrent_alert *alert)
TorrentImpl *SessionImpl::createTorrent(const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params)
{
auto *const torrent = new TorrentImpl(this, m_nativeSession, nativeHandle, params);
auto *const torrent = new TorrentImpl(this, nativeHandle, params);
m_torrents.insert(torrent->id(), torrent);
if (const InfoHash infoHash = torrent->infoHash(); infoHash.isHybrid())
m_hybridTorrentsByAltID.insert(TorrentID::fromSHA1Hash(infoHash.v1()), torrent);

View file

@ -482,6 +482,8 @@ namespace BitTorrent
bool addMoveTorrentStorageJob(TorrentImpl *torrent, const Path &newPath, MoveStorageMode mode, MoveStorageContext context);
lt::torrent_handle reloadTorrent(const lt::torrent_handle &currentHandle, lt::add_torrent_params params);
QFuture<FileSearchResult> findIncompleteFiles(const Path &savePath, const Path &downloadPath, const PathList &filePaths = {}) const;
void enablePortMapping();

View file

@ -290,11 +290,9 @@ namespace
// TorrentImpl
TorrentImpl::TorrentImpl(SessionImpl *session, lt::session *nativeSession
, const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params)
TorrentImpl::TorrentImpl(SessionImpl *session, const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params)
: Torrent(session)
, m_session(session)
, m_nativeSession(nativeSession)
, m_nativeHandle(nativeHandle)
#ifdef QBT_USES_LIBTORRENT2
, m_infoHash(m_nativeHandle.info_hashes())
@ -1836,16 +1834,6 @@ void TorrentImpl::reload()
{
try
{
m_completedFiles.fill(false);
m_filesProgress.fill(0);
m_pieces.fill(false);
m_nativeStatus.pieces.clear_all();
m_nativeStatus.num_pieces = 0;
const auto queuePos = m_nativeHandle.queue_position();
m_nativeSession->remove_torrent(m_nativeHandle, lt::session::delete_partfile);
lt::add_torrent_params p = m_ltAddTorrentParams;
p.flags |= lt::torrent_flags::update_subscribe
| lt::torrent_flags::override_trackers
@ -1865,19 +1853,21 @@ void TorrentImpl::reload()
p.flags &= ~(lt::torrent_flags::auto_managed | lt::torrent_flags::paused);
}
auto *const extensionData = new ExtensionData;
p.userdata = LTClientData(extensionData);
#ifndef QBT_USES_LIBTORRENT2
p.storage = customStorageConstructor;
#endif
m_nativeHandle = m_nativeSession->add_torrent(p);
const auto queuePos = m_nativeHandle.queue_position();
m_nativeStatus = extensionData->status;
m_nativeHandle = m_session->reloadTorrent(m_nativeHandle, std::move(p));
m_nativeStatus = static_cast<ExtensionData *>(m_nativeHandle.userdata())->status;
if (queuePos >= lt::queue_position_t {})
m_nativeHandle.queue_position_set(queuePos);
m_nativeStatus.queue_position = queuePos;
m_completedFiles.fill(false);
m_filesProgress.fill(0);
m_pieces.fill(false);
m_nativeStatus.pieces.clear_all();
m_nativeStatus.num_pieces = 0;
updateState();
}
catch (const lt::system_error &err)

View file

@ -94,8 +94,7 @@ namespace BitTorrent
Q_DISABLE_COPY_MOVE(TorrentImpl)
public:
TorrentImpl(SessionImpl *session, lt::session *nativeSession
, const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params);
TorrentImpl(SessionImpl *session, const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params);
~TorrentImpl() override;
bool isValid() const;
@ -325,7 +324,6 @@ namespace BitTorrent
QFuture<std::invoke_result_t<Func>> invokeAsync(Func &&func) const;
SessionImpl *const m_session = nullptr;
lt::session *m_nativeSession = nullptr;
lt::torrent_handle m_nativeHandle;
mutable lt::torrent_status m_nativeStatus;
TorrentState m_state = TorrentState::Unknown;