Handle libtorrent alerts in SessionImpl only

PR #22798.
This commit is contained in:
Vladimir Golovnev 2025-06-03 08:18:12 +03:00 committed by GitHub
parent 7982f66fa6
commit 0729c9a2f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 186 additions and 181 deletions

View file

@ -5845,20 +5845,41 @@ void SessionImpl::handleAlert(const lt::alert *alert)
{
#ifdef QBT_USES_LIBTORRENT2
case lt::file_prio_alert::alert_type:
handleFilePrioAlert(static_cast<const lt::file_prio_alert *>(alert));
break;
#endif
case lt::file_renamed_alert::alert_type:
handleFileRenamedAlert(static_cast<const lt::file_renamed_alert *>(alert));
break;
case lt::file_rename_failed_alert::alert_type:
handleFileRenameFailedAlert(static_cast<const lt::file_rename_failed_alert *>(alert));
break;
case lt::file_completed_alert::alert_type:
handleFileCompletedAlert(static_cast<const lt::file_completed_alert *>(alert));
break;
case lt::file_error_alert::alert_type:
handleFileErrorAlert(static_cast<const lt::file_error_alert *>(alert));
break;
case lt::torrent_finished_alert::alert_type:
handleTorrentFinishedAlert(static_cast<const lt::torrent_finished_alert *>(alert));
break;
case lt::save_resume_data_alert::alert_type:
handleSaveResumeDataAlert(static_cast<const lt::save_resume_data_alert *>(alert));
break;
case lt::save_resume_data_failed_alert::alert_type:
case lt::torrent_paused_alert::alert_type:
case lt::torrent_resumed_alert::alert_type:
case lt::fastresume_rejected_alert::alert_type:
case lt::torrent_checked_alert::alert_type:
handleSaveResumeDataFailedAlert(static_cast<const lt::save_resume_data_failed_alert *>(alert));
break;
case lt::metadata_received_alert::alert_type:
handleMetadataReceivedAlert(static_cast<const lt::metadata_received_alert *>(alert));
break;
case lt::fastresume_rejected_alert::alert_type:
handleFastResumeRejectedAlert(static_cast<const lt::fastresume_rejected_alert *>(alert));
break;
case lt::torrent_checked_alert::alert_type:
handleTorrentCheckedAlert(static_cast<const lt::torrent_checked_alert *>(alert));
break;
case lt::performance_alert::alert_type:
dispatchTorrentAlert(static_cast<const lt::torrent_alert *>(alert));
handlePerformanceAlert(static_cast<const lt::performance_alert *>(alert));
break;
case lt::state_update_alert::alert_type:
handleStateUpdateAlert(static_cast<const lt::state_update_alert *>(alert));
@ -5875,9 +5896,6 @@ void SessionImpl::handleAlert(const lt::alert *alert)
case lt::tracker_warning_alert::alert_type:
handleTrackerAlert(static_cast<const lt::tracker_alert *>(alert));
break;
case lt::file_error_alert::alert_type:
handleFileErrorAlert(static_cast<const lt::file_error_alert *>(alert));
break;
case lt::add_torrent_alert::alert_type:
handleAddTorrentAlert(static_cast<const lt::add_torrent_alert *>(alert));
break;
@ -5945,41 +5963,6 @@ void SessionImpl::handleAlert(const lt::alert *alert)
}
}
void SessionImpl::dispatchTorrentAlert(const lt::torrent_alert *alert)
{
// The torrent can be deleted between the time the resume data was requested and
// the time we received the appropriate alert. We have to decrease `m_numResumeData` anyway,
// so we do this before checking for an existing torrent.
if ((alert->type() == lt::save_resume_data_alert::alert_type)
|| (alert->type() == lt::save_resume_data_failed_alert::alert_type))
{
--m_numResumeData;
}
TorrentImpl *torrent = getTorrent(alert->handle);
#ifdef QBT_USES_LIBTORRENT2
if (!torrent && (alert->type() == lt::metadata_received_alert::alert_type))
{
const InfoHash infoHash = getInfoHash(alert->handle);
if (infoHash.isHybrid())
torrent = m_torrents.value(TorrentID::fromSHA1Hash(infoHash.v1()));
}
#endif
if (torrent)
{
torrent->handleAlert(alert);
return;
}
switch (alert->type())
{
case lt::metadata_received_alert::alert_type:
handleMetadataReceivedAlert(static_cast<const lt::metadata_received_alert *>(alert));
break;
}
}
TorrentImpl *SessionImpl::createTorrent(const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params)
{
auto *const torrent = new TorrentImpl(this, nativeHandle, params);
@ -6040,6 +6023,19 @@ void SessionImpl::handleTorrentNeedCertAlert(const lt::torrent_need_cert_alert *
void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert *alert)
{
TorrentImpl *torrent = getTorrent(alert->handle);
#ifdef QBT_USES_LIBTORRENT2
if (!torrent && (alert->type() == lt::metadata_received_alert::alert_type))
{
const InfoHash infoHash = getInfoHash(alert->handle);
if (infoHash.isHybrid())
torrent = m_torrents.value(TorrentID::fromSHA1Hash(infoHash.v1()));
}
#endif
if (torrent)
return torrent->handleMetadataReceived();
const InfoHash infoHash = getInfoHash(alert->handle);
const TorrentID torrentID = infoHash.toTorrentID();
@ -6072,10 +6068,10 @@ void SessionImpl::handleMetadataReceivedAlert(const lt::metadata_received_alert
void SessionImpl::handleFileErrorAlert(const lt::file_error_alert *alert)
{
TorrentImpl *const torrent = getTorrent(alert->handle);
if (!torrent)
if (!torrent) [[unlikely]]
return;
torrent->handleAlert(alert);
torrent->handleFileError({.error = alert->error, .operation = alert->op});
const TorrentID id = torrent->id();
if (!m_recentErroredTorrents.contains(id))
@ -6467,8 +6463,106 @@ void SessionImpl::handleTorrentConflictAlert(const lt::torrent_conflict_alert *a
if (!torrent1 || !torrent2)
emit metadataDownloaded(TorrentInfo(*alert->metadata));
}
void SessionImpl::handleFilePrioAlert(const lt::file_prio_alert *alert)
{
if (TorrentImpl *torrent = getTorrent(alert->handle)) [[likely]]
torrent->deferredRequestResumeData();
}
#endif
void SessionImpl::handleTorrentCheckedAlert(const lt::torrent_checked_alert *alert)
{
if (TorrentImpl *torrent = getTorrent(alert->handle)) [[likely]]
torrent->handleTorrentChecked();
}
void SessionImpl::handleTorrentFinishedAlert([[maybe_unused]] const lt::torrent_finished_alert *alert)
{
if (TorrentImpl *torrent = getTorrent(alert->handle)) [[likely]]
torrent->handleTorrentFinished();
}
void SessionImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *alert)
{
// The torrent can be deleted between the time the resume data was requested and
// the time we received the appropriate alert. We have to decrease `m_numResumeData` anyway,
// so we do this before checking for an existing torrent.
--m_numResumeData;
if (TorrentImpl *torrent = getTorrent(alert->handle)) [[likely]]
torrent->handleSaveResumeData(alert->params); // TODO: Try to move `alert->params`
}
void SessionImpl::handleSaveResumeDataFailedAlert(const lt::save_resume_data_failed_alert *alert)
{
// The torrent can be deleted between the time the resume data was requested and
// the time we received the appropriate alert. We have to decrease `m_numResumeData` anyway,
// so we do this before checking for an existing torrent.
--m_numResumeData;
TorrentImpl *torrent = getTorrent(alert->handle);
if (!torrent) [[unlikely]]
return;
if (alert->error != lt::errors::resume_data_not_modified)
{
LogMsg(tr("Generate resume data failed. Torrent: \"%1\". Reason: \"%2\"")
.arg(torrent->name(), Utils::String::fromLocal8Bit(alert->error.message())), Log::CRITICAL);
}
}
void SessionImpl::handleFastResumeRejectedAlert(const lt::fastresume_rejected_alert *alert)
{
TorrentImpl *torrent = getTorrent(alert->handle);
if (!torrent) [[unlikely]]
return;
torrent->handleFastResumeRejected();
LogMsg(tr("Failed to restore torrent. Files were probably moved or storage isn't accessible. Torrent: \"%1\". Reason: \"%2\"")
.arg(torrent->name(), QString::fromStdString(alert->message())), Log::WARNING);
}
void SessionImpl::handleFileRenamedAlert(const lt::file_renamed_alert *alert)
{
TorrentImpl *torrent = getTorrent(alert->handle);
if (!torrent) [[unlikely]]
return;
const Path newFilePath {QString::fromUtf8(alert->new_name())};
#ifdef QBT_USES_LIBTORRENT2
const Path oldFilePath {QString::fromUtf8(alert->old_name())};
#else
const Path oldFilePath;
#endif
torrent->handleFileRenamed(alert->index, newFilePath, oldFilePath);
}
void SessionImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert *alert)
{
TorrentImpl *torrent = getTorrent(alert->handle);
if (!torrent) [[unlikely]]
return;
torrent->handleFileRenameFailed(alert->index);
LogMsg(tr("File rename failed. Torrent: \"%1\", file: \"%2\", reason: \"%3\"")
.arg(torrent->name(), torrent->filePath(torrent->fileIndexFromNative(alert->index)).toString()
, Utils::String::fromLocal8Bit(alert->error.message())), Log::WARNING);
}
void SessionImpl::handleFileCompletedAlert(const lt::file_completed_alert *alert)
{
if (TorrentImpl *torrent = getTorrent(alert->handle)) [[likely]]
torrent->handleFileCompleted(alert->index);
}
void SessionImpl::handlePerformanceAlert(const lt::performance_alert *alert) const
{
LogMsg((tr("Performance alert: %1. More info: %2").arg(QString::fromStdString(alert->message())
, u"https://libtorrent.org/reference-Alerts.html#enum-performance-warning-t"_s)), Log::INFO);
}
void SessionImpl::saveStatistics() const
{
if (!m_isStatisticsDirty)