mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-22 22:33:34 -07:00
Use std::chrono classes
This commit is contained in:
parent
1cc202e4ee
commit
d49cf3a380
8 changed files with 72 additions and 48 deletions
|
@ -7,6 +7,7 @@ add_library(qbt_base STATIC
|
||||||
asyncfilestorage.h
|
asyncfilestorage.h
|
||||||
bittorrent/abstractfilestorage.h
|
bittorrent/abstractfilestorage.h
|
||||||
bittorrent/addtorrentparams.h
|
bittorrent/addtorrentparams.h
|
||||||
|
bittorrent/announcetimepoint.h
|
||||||
bittorrent/bandwidthscheduler.h
|
bittorrent/bandwidthscheduler.h
|
||||||
bittorrent/bencoderesumedatastorage.h
|
bittorrent/bencoderesumedatastorage.h
|
||||||
bittorrent/cachestatus.h
|
bittorrent/cachestatus.h
|
||||||
|
|
36
src/base/bittorrent/announcetimepoint.h
Normal file
36
src/base/bittorrent/announcetimepoint.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Bittorrent Client using Qt and libtorrent.
|
||||||
|
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* In addition, as a special exception, the copyright holders give permission to
|
||||||
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
||||||
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
||||||
|
* and distribute the linked executables. You must obey the GNU General Public
|
||||||
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
||||||
|
* modify file(s), you may extend this exception to your version of the file(s),
|
||||||
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
||||||
|
* exception statement from your version.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace BitTorrent
|
||||||
|
{
|
||||||
|
using AnnounceTimePoint = std::chrono::high_resolution_clock::time_point;
|
||||||
|
}
|
|
@ -30,7 +30,6 @@
|
||||||
#include "sessionimpl.h"
|
#include "sessionimpl.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
@ -1605,8 +1604,8 @@ void SessionImpl::endStartup(ResumeSessionContext *context)
|
||||||
m_wakeupCheckTimer = new QTimer(this);
|
m_wakeupCheckTimer = new QTimer(this);
|
||||||
connect(m_wakeupCheckTimer, &QTimer::timeout, this, [this]
|
connect(m_wakeupCheckTimer, &QTimer::timeout, this, [this]
|
||||||
{
|
{
|
||||||
const auto now = QDateTime::currentDateTime();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
if (m_wakeupCheckTimestamp.secsTo(now) > 100)
|
if ((now - m_wakeupCheckTimestamp) > 100s)
|
||||||
{
|
{
|
||||||
LogMsg(tr("System wake-up event detected. Re-announcing to all the trackers..."));
|
LogMsg(tr("System wake-up event detected. Re-announcing to all the trackers..."));
|
||||||
reannounceToAllTrackers();
|
reannounceToAllTrackers();
|
||||||
|
@ -1614,7 +1613,7 @@ void SessionImpl::endStartup(ResumeSessionContext *context)
|
||||||
|
|
||||||
m_wakeupCheckTimestamp = now;
|
m_wakeupCheckTimestamp = now;
|
||||||
});
|
});
|
||||||
m_wakeupCheckTimestamp = QDateTime::currentDateTime();
|
m_wakeupCheckTimestamp = std::chrono::steady_clock::now();
|
||||||
m_wakeupCheckTimer->start(30s);
|
m_wakeupCheckTimer->start(30s);
|
||||||
|
|
||||||
m_isRestored = true;
|
m_isRestored = true;
|
||||||
|
@ -5514,11 +5513,6 @@ void SessionImpl::setTorrentContentLayout(const TorrentContentLayout value)
|
||||||
// Read alerts sent by libtorrent session
|
// Read alerts sent by libtorrent session
|
||||||
void SessionImpl::readAlerts()
|
void SessionImpl::readAlerts()
|
||||||
{
|
{
|
||||||
// cache current datetime of Qt and libtorrent clocks in order
|
|
||||||
// to optimize conversion of time points from lt to Qt clocks
|
|
||||||
m_ltNow = lt::clock_type::now();
|
|
||||||
m_currentSecsSinceEpoch = QDateTime::currentSecsSinceEpoch();
|
|
||||||
|
|
||||||
const std::vector<lt::alert *> alerts = getPendingAlerts();
|
const std::vector<lt::alert *> alerts = getPendingAlerts();
|
||||||
|
|
||||||
Q_ASSERT(m_loadedTorrents.isEmpty());
|
Q_ASSERT(m_loadedTorrents.isEmpty());
|
||||||
|
@ -6397,9 +6391,3 @@ void SessionImpl::handleRemovedTorrent(const TorrentID &torrentID, const QString
|
||||||
|
|
||||||
m_removingTorrents.erase(removingTorrentDataIter);
|
m_removingTorrents.erase(removingTorrentDataIter);
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 SessionImpl::toSecsSinceEpoch(const lt::time_point32 &timePoint) const
|
|
||||||
{
|
|
||||||
const auto secsSinceNow = lt::duration_cast<lt::seconds>(timePoint - m_ltNow + lt::milliseconds(500)).count();
|
|
||||||
return m_currentSecsSinceEpoch + secsSinceNow;
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
@ -477,8 +478,6 @@ namespace BitTorrent
|
||||||
void addMappedPorts(const QSet<quint16> &ports);
|
void addMappedPorts(const QSet<quint16> &ports);
|
||||||
void removeMappedPorts(const QSet<quint16> &ports);
|
void removeMappedPorts(const QSet<quint16> &ports);
|
||||||
|
|
||||||
qint64 toSecsSinceEpoch(const lt::time_point32 &timePoint) const;
|
|
||||||
|
|
||||||
template <typename Func>
|
template <typename Func>
|
||||||
void invoke(Func &&func)
|
void invoke(Func &&func)
|
||||||
{
|
{
|
||||||
|
@ -826,13 +825,10 @@ namespace BitTorrent
|
||||||
QHash<quint16, std::vector<lt::port_mapping_t>> m_mappedPorts;
|
QHash<quint16, std::vector<lt::port_mapping_t>> m_mappedPorts;
|
||||||
|
|
||||||
QTimer *m_wakeupCheckTimer = nullptr;
|
QTimer *m_wakeupCheckTimer = nullptr;
|
||||||
QDateTime m_wakeupCheckTimestamp;
|
std::chrono::steady_clock::time_point m_wakeupCheckTimestamp;
|
||||||
|
|
||||||
QList<TorrentImpl *> m_pendingFinishedTorrents;
|
QList<TorrentImpl *> m_pendingFinishedTorrents;
|
||||||
|
|
||||||
qint64 m_currentSecsSinceEpoch = 0;
|
|
||||||
lt::clock_type::time_point m_ltNow;
|
|
||||||
|
|
||||||
friend void Session::initInstance();
|
friend void Session::initInstance();
|
||||||
friend void Session::freeInstance();
|
friend void Session::freeInstance();
|
||||||
friend Session *Session::instance();
|
friend Session *Session::instance();
|
||||||
|
|
|
@ -106,10 +106,8 @@ namespace
|
||||||
return endpointName;
|
return endpointName;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ToSecsSinceEpochFunc>
|
|
||||||
void updateTrackerEntryStatus(TrackerEntryStatus &trackerEntryStatus, const lt::announce_entry &nativeEntry
|
void updateTrackerEntryStatus(TrackerEntryStatus &trackerEntryStatus, const lt::announce_entry &nativeEntry
|
||||||
, const QSet<int> &btProtocols, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo
|
, const QSet<int> &btProtocols, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo)
|
||||||
, const ToSecsSinceEpochFunc &toSecsSinceEpoch)
|
|
||||||
{
|
{
|
||||||
Q_ASSERT(trackerEntryStatus.url == QString::fromStdString(nativeEntry.url));
|
Q_ASSERT(trackerEntryStatus.url == QString::fromStdString(nativeEntry.url));
|
||||||
|
|
||||||
|
@ -146,8 +144,8 @@ namespace
|
||||||
trackerEndpointStatus.numSeeds = ltAnnounceInfo.scrape_complete;
|
trackerEndpointStatus.numSeeds = ltAnnounceInfo.scrape_complete;
|
||||||
trackerEndpointStatus.numLeeches = ltAnnounceInfo.scrape_incomplete;
|
trackerEndpointStatus.numLeeches = ltAnnounceInfo.scrape_incomplete;
|
||||||
trackerEndpointStatus.numDownloaded = ltAnnounceInfo.scrape_downloaded;
|
trackerEndpointStatus.numDownloaded = ltAnnounceInfo.scrape_downloaded;
|
||||||
trackerEndpointStatus.nextAnnounceTime = toSecsSinceEpoch(ltAnnounceInfo.next_announce);
|
trackerEndpointStatus.nextAnnounceTime = ltAnnounceInfo.next_announce;
|
||||||
trackerEndpointStatus.minAnnounceTime = toSecsSinceEpoch(ltAnnounceInfo.min_announce);
|
trackerEndpointStatus.minAnnounceTime = ltAnnounceInfo.min_announce;
|
||||||
|
|
||||||
if (ltAnnounceInfo.updating)
|
if (ltAnnounceInfo.updating)
|
||||||
{
|
{
|
||||||
|
@ -238,8 +236,8 @@ namespace
|
||||||
trackerEntryStatus.numSeeds = -1;
|
trackerEntryStatus.numSeeds = -1;
|
||||||
trackerEntryStatus.numLeeches = -1;
|
trackerEntryStatus.numLeeches = -1;
|
||||||
trackerEntryStatus.numDownloaded = -1;
|
trackerEntryStatus.numDownloaded = -1;
|
||||||
trackerEntryStatus.nextAnnounceTime = 0;
|
trackerEntryStatus.nextAnnounceTime = {};
|
||||||
trackerEntryStatus.minAnnounceTime = 0;
|
trackerEntryStatus.minAnnounceTime = {};
|
||||||
trackerEntryStatus.message.clear();
|
trackerEntryStatus.message.clear();
|
||||||
|
|
||||||
for (const TrackerEndpointStatus &endpointStatus : asConst(trackerEntryStatus.endpoints))
|
for (const TrackerEndpointStatus &endpointStatus : asConst(trackerEntryStatus.endpoints))
|
||||||
|
@ -251,7 +249,7 @@ namespace
|
||||||
|
|
||||||
if (endpointStatus.state == trackerEntryStatus.state)
|
if (endpointStatus.state == trackerEntryStatus.state)
|
||||||
{
|
{
|
||||||
if ((trackerEntryStatus.nextAnnounceTime <= 0) || (trackerEntryStatus.nextAnnounceTime > endpointStatus.nextAnnounceTime))
|
if ((trackerEntryStatus.nextAnnounceTime == AnnounceTimePoint()) || (trackerEntryStatus.nextAnnounceTime > endpointStatus.nextAnnounceTime))
|
||||||
{
|
{
|
||||||
trackerEntryStatus.nextAnnounceTime = endpointStatus.nextAnnounceTime;
|
trackerEntryStatus.nextAnnounceTime = endpointStatus.nextAnnounceTime;
|
||||||
trackerEntryStatus.minAnnounceTime = endpointStatus.minAnnounceTime;
|
trackerEntryStatus.minAnnounceTime = endpointStatus.minAnnounceTime;
|
||||||
|
@ -1774,11 +1772,7 @@ TrackerEntryStatus TorrentImpl::updateTrackerEntryStatus(const lt::announce_entr
|
||||||
const QSet<int> btProtocols {1};
|
const QSet<int> btProtocols {1};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const auto toSecsSinceEpoch = [this](const lt::time_point32 &timePoint)
|
::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo);
|
||||||
{
|
|
||||||
return m_session->toSecsSinceEpoch(timePoint);
|
|
||||||
};
|
|
||||||
::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo, toSecsSinceEpoch);
|
|
||||||
|
|
||||||
return *it;
|
return *it;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
|
#include "announcetimepoint.h"
|
||||||
|
|
||||||
class QStringView;
|
class QStringView;
|
||||||
|
|
||||||
namespace BitTorrent
|
namespace BitTorrent
|
||||||
|
@ -58,8 +60,8 @@ namespace BitTorrent
|
||||||
int numLeeches = -1;
|
int numLeeches = -1;
|
||||||
int numDownloaded = -1;
|
int numDownloaded = -1;
|
||||||
|
|
||||||
qint64 nextAnnounceTime = 0;
|
AnnounceTimePoint nextAnnounceTime {};
|
||||||
qint64 minAnnounceTime = 0;
|
AnnounceTimePoint minAnnounceTime {};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TrackerEntryStatus
|
struct TrackerEntryStatus
|
||||||
|
@ -75,8 +77,8 @@ namespace BitTorrent
|
||||||
int numLeeches = -1;
|
int numLeeches = -1;
|
||||||
int numDownloaded = -1;
|
int numDownloaded = -1;
|
||||||
|
|
||||||
qint64 nextAnnounceTime = 0;
|
AnnounceTimePoint nextAnnounceTime {};
|
||||||
qint64 minAnnounceTime = 0;
|
AnnounceTimePoint minAnnounceTime {};
|
||||||
|
|
||||||
QHash<std::pair<QString, int>, TrackerEndpointStatus> endpoints {};
|
QHash<std::pair<QString, int>, TrackerEndpointStatus> endpoints {};
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
|
|
||||||
#include "trackerlistmodel.h"
|
#include "trackerlistmodel.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
#include <boost/multi_index_container.hpp>
|
#include <boost/multi_index_container.hpp>
|
||||||
|
@ -46,6 +47,7 @@
|
||||||
#include <QScopeGuard>
|
#include <QScopeGuard>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#include "base/bittorrent/announcetimepoint.h"
|
||||||
#include "base/bittorrent/peerinfo.h"
|
#include "base/bittorrent/peerinfo.h"
|
||||||
#include "base/bittorrent/session.h"
|
#include "base/bittorrent/session.h"
|
||||||
#include "base/bittorrent/torrent.h"
|
#include "base/bittorrent/torrent.h"
|
||||||
|
@ -142,12 +144,12 @@ struct TrackerListModel::Item final
|
||||||
int numLeeches = -1;
|
int numLeeches = -1;
|
||||||
int numDownloaded = -1;
|
int numDownloaded = -1;
|
||||||
|
|
||||||
qint64 nextAnnounceTime = 0;
|
BitTorrent::AnnounceTimePoint nextAnnounceTime;
|
||||||
qint64 minAnnounceTime = 0;
|
BitTorrent::AnnounceTimePoint minAnnounceTime;
|
||||||
|
|
||||||
qint64 secsToNextAnnounce = 0;
|
qint64 secsToNextAnnounce = 0;
|
||||||
qint64 secsToMinAnnounce = 0;
|
qint64 secsToMinAnnounce = 0;
|
||||||
qint64 announceTimestamp = 0;
|
BitTorrent::AnnounceTimePoint announceTimestamp;
|
||||||
|
|
||||||
std::weak_ptr<Item> parentItem {};
|
std::weak_ptr<Item> parentItem {};
|
||||||
|
|
||||||
|
@ -212,7 +214,7 @@ void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEntryStatus &trac
|
||||||
minAnnounceTime = trackerEntryStatus.minAnnounceTime;
|
minAnnounceTime = trackerEntryStatus.minAnnounceTime;
|
||||||
secsToNextAnnounce = 0;
|
secsToNextAnnounce = 0;
|
||||||
secsToMinAnnounce = 0;
|
secsToMinAnnounce = 0;
|
||||||
announceTimestamp = 0;
|
announceTimestamp = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &endpointStatus)
|
void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &endpointStatus)
|
||||||
|
@ -231,7 +233,7 @@ void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &e
|
||||||
minAnnounceTime = endpointStatus.minAnnounceTime;
|
minAnnounceTime = endpointStatus.minAnnounceTime;
|
||||||
secsToNextAnnounce = 0;
|
secsToNextAnnounce = 0;
|
||||||
secsToMinAnnounce = 0;
|
secsToMinAnnounce = 0;
|
||||||
announceTimestamp = 0;
|
announceTimestamp = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackerListModel::TrackerListModel(BitTorrent::Session *btSession, QObject *parent)
|
TrackerListModel::TrackerListModel(BitTorrent::Session *btSession, QObject *parent)
|
||||||
|
@ -370,7 +372,7 @@ void TrackerListModel::populate()
|
||||||
for (const BitTorrent::TrackerEntryStatus &status : trackers)
|
for (const BitTorrent::TrackerEntryStatus &status : trackers)
|
||||||
addTrackerItem(status);
|
addTrackerItem(status);
|
||||||
|
|
||||||
m_announceTimestamp = QDateTime::currentSecsSinceEpoch();
|
m_announceTimestamp = BitTorrent::AnnounceTimePoint::clock::now();
|
||||||
m_announceRefreshTimer->start(ANNOUNCE_TIME_REFRESH_INTERVAL);
|
m_announceRefreshTimer->start(ANNOUNCE_TIME_REFRESH_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -449,7 +451,7 @@ void TrackerListModel::refreshAnnounceTimes()
|
||||||
if (!m_torrent)
|
if (!m_torrent)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_announceTimestamp = QDateTime::currentSecsSinceEpoch();
|
m_announceTimestamp = BitTorrent::AnnounceTimePoint::clock::now();
|
||||||
emit dataChanged(index(0, COL_NEXT_ANNOUNCE), index((rowCount() - 1), COL_MIN_ANNOUNCE));
|
emit dataChanged(index(0, COL_NEXT_ANNOUNCE), index((rowCount() - 1), COL_MIN_ANNOUNCE));
|
||||||
for (int i = 0; i < rowCount(); ++i)
|
for (int i = 0; i < rowCount(); ++i)
|
||||||
{
|
{
|
||||||
|
@ -546,8 +548,12 @@ QVariant TrackerListModel::data(const QModelIndex &index, const int role) const
|
||||||
|
|
||||||
if (itemPtr->announceTimestamp != m_announceTimestamp)
|
if (itemPtr->announceTimestamp != m_announceTimestamp)
|
||||||
{
|
{
|
||||||
itemPtr->secsToNextAnnounce = std::max<qint64>(0, (itemPtr->nextAnnounceTime - m_announceTimestamp));
|
const auto timeToNextAnnounce = std::chrono::duration_cast<std::chrono::seconds>(itemPtr->nextAnnounceTime - m_announceTimestamp);
|
||||||
itemPtr->secsToMinAnnounce = std::max<qint64>(0, (itemPtr->minAnnounceTime - m_announceTimestamp));
|
itemPtr->secsToNextAnnounce = std::max<qint64>(0, timeToNextAnnounce.count());
|
||||||
|
|
||||||
|
const auto timeToMinAnnounce = std::chrono::duration_cast<std::chrono::seconds>(itemPtr->minAnnounceTime - m_announceTimestamp);
|
||||||
|
itemPtr->secsToMinAnnounce = std::max<qint64>(0, timeToMinAnnounce.count());
|
||||||
|
|
||||||
itemPtr->announceTimestamp = m_announceTimestamp;
|
itemPtr->announceTimestamp = m_announceTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include <QtContainerFwd>
|
#include <QtContainerFwd>
|
||||||
#include <QAbstractItemModel>
|
#include <QAbstractItemModel>
|
||||||
|
|
||||||
|
#include "base/bittorrent/announcetimepoint.h"
|
||||||
#include "base/bittorrent/trackerentrystatus.h"
|
#include "base/bittorrent/trackerentrystatus.h"
|
||||||
|
|
||||||
class QTimer;
|
class QTimer;
|
||||||
|
@ -114,6 +115,6 @@ private:
|
||||||
class Items;
|
class Items;
|
||||||
std::unique_ptr<Items> m_items;
|
std::unique_ptr<Items> m_items;
|
||||||
|
|
||||||
qint64 m_announceTimestamp = 0;
|
BitTorrent::AnnounceTimePoint m_announceTimestamp;
|
||||||
QTimer *m_announceRefreshTimer = nullptr;
|
QTimer *m_announceRefreshTimer = nullptr;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue