Browse Source

Merge pull request #9257 from Chocobo1/prio

Avoid potentially setting the wrong piece priorities
adaptive-webui-19844
Mike Tzou 6 years ago committed by GitHub
parent
commit
fea7a96e68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      src/base/bittorrent/session.cpp
  2. 16
      src/base/bittorrent/torrenthandle.cpp
  3. 1
      src/base/bittorrent/torrenthandle.h
  4. 10
      src/base/torrentfilter.cpp
  5. 10
      src/base/torrentfilter.h

15
src/base/bittorrent/session.cpp

@ -2122,17 +2122,12 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne @@ -2122,17 +2122,12 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
}
}
// If empty then Automatic mode, otherwise Manual mode
QString savePath = params.savePath.isEmpty() ? categorySavePath(params.category) : params.savePath;
libt::add_torrent_params p;
InfoHash hash;
std::vector<boost::uint8_t> filePriorities;
const bool fromMagnetUri = magnetUri.isValid();
QString savePath;
if (params.savePath.isEmpty()) // using Automatic mode
savePath = categorySavePath(params.category);
else // using Manual mode
savePath = params.savePath;
bool fromMagnetUri = magnetUri.isValid();
if (fromMagnetUri) {
hash = magnetUri.hash();
@ -2195,9 +2190,7 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne @@ -2195,9 +2190,7 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
p.flags |= libt::add_torrent_params::flag_use_resume_save_path;
}
else {
foreach (int prio, params.filePriorities)
filePriorities.push_back(prio);
p.file_priorities = filePriorities;
p.file_priorities = {params.filePriorities.begin(), params.filePriorities.end()};
}
// We should not add torrent if it already

16
src/base/bittorrent/torrenthandle.cpp

@ -1300,13 +1300,21 @@ void TorrentHandle::toggleSequentialDownload() @@ -1300,13 +1300,21 @@ void TorrentHandle::toggleSequentialDownload()
void TorrentHandle::setFirstLastPiecePriority(const bool enabled)
{
setFirstLastPiecePriorityImpl(enabled);
}
void TorrentHandle::setFirstLastPiecePriorityImpl(const bool enabled, const QVector<int> &updatedFilePrio)
{
// Download first and last pieces first for every file in the torrent
if (!hasMetadata()) {
m_needsToSetFirstLastPiecePriority = enabled;
return;
}
// Download first and last pieces first for every file in the torrent
const std::vector<int> filePriorities = nativeHandle().file_priorities();
// Updating file priorities is an async operation in libtorrent, when we just updated it and immediately query it
// we might get the old/wrong values, so we rely on `updatedFilePrio` in this case.
const std::vector<int> filePriorities = !updatedFilePrio.isEmpty() ? updatedFilePrio.toStdVector() : nativeHandle().file_priorities();
std::vector<int> piecePriorities = nativeHandle().piece_priorities();
for (int index = 0; index < static_cast<int>(filePriorities.size()); ++index) {
const int filePrio = filePriorities[index];
@ -1991,7 +1999,7 @@ void TorrentHandle::prioritizeFiles(const QVector<int> &priorities) @@ -1991,7 +1999,7 @@ void TorrentHandle::prioritizeFiles(const QVector<int> &priorities)
if (priorities.size() != filesCount()) return;
// Save first/last piece first option state
bool firstLastPieceFirst = hasFirstLastPiecePriority();
const bool firstLastPieceFirst = hasFirstLastPiecePriority();
// Reset 'm_hasSeedStatus' if needed in order to react again to
// 'torrent_finished_alert' and eg show tray notifications
@ -2064,7 +2072,7 @@ void TorrentHandle::prioritizeFiles(const QVector<int> &priorities) @@ -2064,7 +2072,7 @@ void TorrentHandle::prioritizeFiles(const QVector<int> &priorities)
// Restore first/last piece first option if necessary
if (firstLastPieceFirst)
setFirstLastPiecePriority(true);
setFirstLastPiecePriorityImpl(true, priorities);
updateStatus();
}

1
src/base/bittorrent/torrenthandle.h

@ -420,6 +420,7 @@ namespace BitTorrent @@ -420,6 +420,7 @@ namespace BitTorrent
bool addTracker(const TrackerEntry &tracker);
bool addUrlSeed(const QUrl &urlSeed);
bool removeUrlSeed(const QUrl &urlSeed);
void setFirstLastPiecePriorityImpl(bool enabled, const QVector<int> &updatedFilePrio = {});
Session *const m_session;
libtorrent::torrent_handle m_nativeHandle;

10
src/base/torrentfilter.cpp

@ -138,14 +138,14 @@ bool TorrentFilter::setTag(const QString &tag) @@ -138,14 +138,14 @@ bool TorrentFilter::setTag(const QString &tag)
return false;
}
bool TorrentFilter::match(TorrentHandle *const torrent) const
bool TorrentFilter::match(const TorrentHandle *const torrent) const
{
if (!torrent) return false;
return (matchState(torrent) && matchHash(torrent) && matchCategory(torrent) && matchTag(torrent));
}
bool TorrentFilter::matchState(BitTorrent::TorrentHandle *const torrent) const
bool TorrentFilter::matchState(const BitTorrent::TorrentHandle *const torrent) const
{
switch (m_type) {
case All:
@ -171,19 +171,19 @@ bool TorrentFilter::matchState(BitTorrent::TorrentHandle *const torrent) const @@ -171,19 +171,19 @@ bool TorrentFilter::matchState(BitTorrent::TorrentHandle *const torrent) const
}
}
bool TorrentFilter::matchHash(BitTorrent::TorrentHandle *const torrent) const
bool TorrentFilter::matchHash(const BitTorrent::TorrentHandle *const torrent) const
{
if (m_hashSet == AnyHash) return true;
else return m_hashSet.contains(torrent->hash());
}
bool TorrentFilter::matchCategory(BitTorrent::TorrentHandle *const torrent) const
bool TorrentFilter::matchCategory(const BitTorrent::TorrentHandle *const torrent) const
{
if (m_category.isNull()) return true;
else return (torrent->belongsToCategory(m_category));
}
bool TorrentFilter::matchTag(BitTorrent::TorrentHandle *const torrent) const
bool TorrentFilter::matchTag(const BitTorrent::TorrentHandle *const torrent) const
{
// Empty tag is a special value to indicate we're filtering for untagged torrents.
if (m_tag.isNull()) return true;

10
src/base/torrentfilter.h

@ -81,13 +81,13 @@ public: @@ -81,13 +81,13 @@ public:
bool setCategory(const QString &category);
bool setTag(const QString &tag);
bool match(BitTorrent::TorrentHandle *const torrent) const;
bool match(const BitTorrent::TorrentHandle *torrent) const;
private:
bool matchState(BitTorrent::TorrentHandle *const torrent) const;
bool matchHash(BitTorrent::TorrentHandle *const torrent) const;
bool matchCategory(BitTorrent::TorrentHandle *const torrent) const;
bool matchTag(BitTorrent::TorrentHandle *const torrent) const;
bool matchState(const BitTorrent::TorrentHandle *torrent) const;
bool matchHash(const BitTorrent::TorrentHandle *torrent) const;
bool matchCategory(const BitTorrent::TorrentHandle *torrent) const;
bool matchTag(const BitTorrent::TorrentHandle *torrent) const;
Type m_type;
QString m_category;

Loading…
Cancel
Save