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. 12
      src/base/torrentfilter.h

15
src/base/bittorrent/session.cpp

@ -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; libt::add_torrent_params p;
InfoHash hash; 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) { if (fromMagnetUri) {
hash = magnetUri.hash(); hash = magnetUri.hash();
@ -2195,9 +2190,7 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
p.flags |= libt::add_torrent_params::flag_use_resume_save_path; p.flags |= libt::add_torrent_params::flag_use_resume_save_path;
} }
else { else {
foreach (int prio, params.filePriorities) p.file_priorities = {params.filePriorities.begin(), params.filePriorities.end()};
filePriorities.push_back(prio);
p.file_priorities = filePriorities;
} }
// We should not add torrent if it already // We should not add torrent if it already

16
src/base/bittorrent/torrenthandle.cpp

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

1
src/base/bittorrent/torrenthandle.h

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

10
src/base/torrentfilter.cpp

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

12
src/base/torrentfilter.h

@ -58,7 +58,7 @@ public:
// These mean any permutation, including no category / tag. // These mean any permutation, including no category / tag.
static const QString AnyCategory; static const QString AnyCategory;
static const QStringSet AnyHash; static const QStringSet AnyHash;
static const QString AnyTag; static const QString AnyTag;
static const TorrentFilter DownloadingTorrent; static const TorrentFilter DownloadingTorrent;
static const TorrentFilter SeedingTorrent; static const TorrentFilter SeedingTorrent;
@ -81,13 +81,13 @@ public:
bool setCategory(const QString &category); bool setCategory(const QString &category);
bool setTag(const QString &tag); bool setTag(const QString &tag);
bool match(BitTorrent::TorrentHandle *const torrent) const; bool match(const BitTorrent::TorrentHandle *torrent) const;
private: private:
bool matchState(BitTorrent::TorrentHandle *const torrent) const; bool matchState(const BitTorrent::TorrentHandle *torrent) const;
bool matchHash(BitTorrent::TorrentHandle *const torrent) const; bool matchHash(const BitTorrent::TorrentHandle *torrent) const;
bool matchCategory(BitTorrent::TorrentHandle *const torrent) const; bool matchCategory(const BitTorrent::TorrentHandle *torrent) const;
bool matchTag(BitTorrent::TorrentHandle *const torrent) const; bool matchTag(const BitTorrent::TorrentHandle *torrent) const;
Type m_type; Type m_type;
QString m_category; QString m_category;

Loading…
Cancel
Save