From d0e94586e3408d10a94d44d1e218df0c328ceb1e Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 24 Nov 2024 16:12:38 +0300 Subject: [PATCH] Avoid using QDateTime for announce timestamps --- src/base/bittorrent/sessionimpl.cpp | 8 +++++--- src/base/bittorrent/sessionimpl.h | 6 ++---- src/base/bittorrent/torrentimpl.cpp | 18 +++++++++--------- src/base/bittorrent/trackerentrystatus.h | 11 +++++------ src/gui/trackerlist/trackerlistmodel.cpp | 21 +++++++++++---------- src/gui/trackerlist/trackerlistmodel.h | 5 ++--- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index efcabbf0d068..cd93b7103f00 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -58,6 +58,7 @@ #include #include +#include #include #include #include @@ -106,6 +107,7 @@ #include "torrentimpl.h" #include "tracker.h" #include "trackerentry.h" +#include "trackerentrystatus.h" using namespace std::chrono_literals; using namespace BitTorrent; @@ -5515,7 +5517,7 @@ 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_qNow = QDateTime::currentDateTime(); + m_currentSecsSinceEpoch = QDateTime::currentSecsSinceEpoch(); const std::vector alerts = getPendingAlerts(); @@ -6396,8 +6398,8 @@ void SessionImpl::handleRemovedTorrent(const TorrentID &torrentID, const QString m_removingTorrents.erase(removingTorrentDataIter); } -QDateTime SessionImpl::fromLTTimePoint32(const libtorrent::time_point32 &timePoint) const +qint64 SessionImpl::toSecsSinceEpoch(const lt::time_point32 &timePoint) const { const auto secsSinceNow = lt::duration_cast(timePoint - m_ltNow + lt::milliseconds(500)).count(); - return m_qNow.addSecs(secsSinceNow); + return m_currentSecsSinceEpoch + secsSinceNow; } diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index 83f36e6681a5..c78b65ba4525 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -37,7 +37,6 @@ #include #include -#include #include #include #include @@ -55,7 +54,6 @@ #include "session.h" #include "sessionstatus.h" #include "torrentinfo.h" -#include "trackerentrystatus.h" class QString; class QTimer; @@ -479,7 +477,7 @@ namespace BitTorrent void addMappedPorts(const QSet &ports); void removeMappedPorts(const QSet &ports); - QDateTime fromLTTimePoint32(const lt::time_point32 &timePoint) const; + qint64 toSecsSinceEpoch(const lt::time_point32 &timePoint) const; template void invoke(Func &&func) @@ -832,7 +830,7 @@ namespace BitTorrent QList m_pendingFinishedTorrents; - QDateTime m_qNow; + qint64 m_currentSecsSinceEpoch; lt::clock_type::time_point m_ltNow; friend void Session::initInstance(); diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index ecfcb66e539d..de83babc0ae3 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -109,7 +109,7 @@ namespace template void updateTrackerEntryStatus(TrackerEntryStatus &trackerEntryStatus, const lt::announce_entry &nativeEntry , const QSet &btProtocols, const QHash> &updateInfo - , const FromLTTimePoint32Func &fromLTTimePoint32) + , const FromLTTimePoint32Func &toSecsSinceEpoch) { Q_ASSERT(trackerEntryStatus.url == QString::fromStdString(nativeEntry.url)); @@ -146,8 +146,8 @@ namespace trackerEndpointStatus.numSeeds = ltAnnounceInfo.scrape_complete; trackerEndpointStatus.numLeeches = ltAnnounceInfo.scrape_incomplete; trackerEndpointStatus.numDownloaded = ltAnnounceInfo.scrape_downloaded; - trackerEndpointStatus.nextAnnounceTime = fromLTTimePoint32(ltAnnounceInfo.next_announce); - trackerEndpointStatus.minAnnounceTime = fromLTTimePoint32(ltAnnounceInfo.min_announce); + trackerEndpointStatus.nextAnnounceTime = toSecsSinceEpoch(ltAnnounceInfo.next_announce); + trackerEndpointStatus.minAnnounceTime = toSecsSinceEpoch(ltAnnounceInfo.min_announce); if (ltAnnounceInfo.updating) { @@ -238,8 +238,8 @@ namespace trackerEntryStatus.numSeeds = -1; trackerEntryStatus.numLeeches = -1; trackerEntryStatus.numDownloaded = -1; - trackerEntryStatus.nextAnnounceTime = QDateTime(); - trackerEntryStatus.minAnnounceTime = QDateTime(); + trackerEntryStatus.nextAnnounceTime = 0; + trackerEntryStatus.minAnnounceTime = 0; trackerEntryStatus.message.clear(); for (const TrackerEndpointStatus &endpointStatus : asConst(trackerEntryStatus.endpoints)) @@ -251,7 +251,7 @@ namespace if (endpointStatus.state == trackerEntryStatus.state) { - if (!trackerEntryStatus.nextAnnounceTime.isValid() || (trackerEntryStatus.nextAnnounceTime > endpointStatus.nextAnnounceTime)) + if ((trackerEntryStatus.nextAnnounceTime <= 0) || (trackerEntryStatus.nextAnnounceTime > endpointStatus.nextAnnounceTime)) { trackerEntryStatus.nextAnnounceTime = endpointStatus.nextAnnounceTime; trackerEntryStatus.minAnnounceTime = endpointStatus.minAnnounceTime; @@ -1777,11 +1777,11 @@ TrackerEntryStatus TorrentImpl::updateTrackerEntryStatus(const lt::announce_entr const QSet btProtocols {1}; #endif - const auto fromLTTimePoint32 = [this](const lt::time_point32 &timePoint) + const auto toSecsSinceEpoch = [this](const lt::time_point32 &timePoint) { - return m_session->fromLTTimePoint32(timePoint); + return m_session->toSecsSinceEpoch(timePoint); }; - ::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo, fromLTTimePoint32); + ::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo, toSecsSinceEpoch); return *it; } diff --git a/src/base/bittorrent/trackerentrystatus.h b/src/base/bittorrent/trackerentrystatus.h index 1f271d41acde..792ce303a39f 100644 --- a/src/base/bittorrent/trackerentrystatus.h +++ b/src/base/bittorrent/trackerentrystatus.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015-2023 Vladimir Golovnev + * Copyright (C) 2015-2024 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,7 +28,6 @@ #pragma once -#include #include #include @@ -59,8 +58,8 @@ namespace BitTorrent int numLeeches = -1; int numDownloaded = -1; - QDateTime nextAnnounceTime {}; - QDateTime minAnnounceTime {}; + qint64 nextAnnounceTime = 0; + qint64 minAnnounceTime = 0; }; struct TrackerEntryStatus @@ -76,8 +75,8 @@ namespace BitTorrent int numLeeches = -1; int numDownloaded = -1; - QDateTime nextAnnounceTime {}; - QDateTime minAnnounceTime {}; + qint64 nextAnnounceTime = 0; + qint64 minAnnounceTime = 0; QHash, TrackerEndpointStatus> endpoints {}; diff --git a/src/gui/trackerlist/trackerlistmodel.cpp b/src/gui/trackerlist/trackerlistmodel.cpp index 0d0b8fdf7a79..5d1608cddb5d 100644 --- a/src/gui/trackerlist/trackerlistmodel.cpp +++ b/src/gui/trackerlist/trackerlistmodel.cpp @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2023 Vladimir Golovnev + * Copyright (C) 2023-2024 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -141,12 +142,12 @@ struct TrackerListModel::Item final int numLeeches = -1; int numDownloaded = -1; - QDateTime nextAnnounceTime {}; - QDateTime minAnnounceTime {}; + qint64 nextAnnounceTime = 0; + qint64 minAnnounceTime = 0; qint64 secsToNextAnnounce = 0; qint64 secsToMinAnnounce = 0; - QDateTime announceTimestamp; + qint64 announceTimestamp = 0; std::weak_ptr parentItem {}; @@ -211,7 +212,7 @@ void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEntryStatus &trac minAnnounceTime = trackerEntryStatus.minAnnounceTime; secsToNextAnnounce = 0; secsToMinAnnounce = 0; - announceTimestamp = QDateTime(); + announceTimestamp = 0; } void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &endpointStatus) @@ -230,7 +231,7 @@ void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &e minAnnounceTime = endpointStatus.minAnnounceTime; secsToNextAnnounce = 0; secsToMinAnnounce = 0; - announceTimestamp = QDateTime(); + announceTimestamp = 0; } TrackerListModel::TrackerListModel(BitTorrent::Session *btSession, QObject *parent) @@ -369,7 +370,7 @@ void TrackerListModel::populate() for (const BitTorrent::TrackerEntryStatus &status : trackers) addTrackerItem(status); - m_announceTimestamp = QDateTime::currentDateTime(); + m_announceTimestamp = QDateTime::currentSecsSinceEpoch(); m_announceRefreshTimer->start(ANNOUNCE_TIME_REFRESH_INTERVAL); } @@ -448,7 +449,7 @@ void TrackerListModel::refreshAnnounceTimes() if (!m_torrent) return; - m_announceTimestamp = QDateTime::currentDateTime(); + m_announceTimestamp = QDateTime::currentSecsSinceEpoch(); emit dataChanged(index(0, COL_NEXT_ANNOUNCE), index((rowCount() - 1), COL_MIN_ANNOUNCE)); for (int i = 0; i < rowCount(); ++i) { @@ -545,8 +546,8 @@ QVariant TrackerListModel::data(const QModelIndex &index, const int role) const if (itemPtr->announceTimestamp != m_announceTimestamp) { - itemPtr->secsToNextAnnounce = std::max(0, m_announceTimestamp.secsTo(itemPtr->nextAnnounceTime)); - itemPtr->secsToMinAnnounce = std::max(0, m_announceTimestamp.secsTo(itemPtr->minAnnounceTime)); + itemPtr->secsToNextAnnounce = std::max(0, (itemPtr->nextAnnounceTime - m_announceTimestamp)); + itemPtr->secsToMinAnnounce = std::max(0, (itemPtr->minAnnounceTime - m_announceTimestamp)); itemPtr->announceTimestamp = m_announceTimestamp; } diff --git a/src/gui/trackerlist/trackerlistmodel.h b/src/gui/trackerlist/trackerlistmodel.h index 9a633bf8ede9..79cc885f7cb7 100644 --- a/src/gui/trackerlist/trackerlistmodel.h +++ b/src/gui/trackerlist/trackerlistmodel.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2023 Vladimir Golovnev + * Copyright (C) 2023-2024 Vladimir Golovnev * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or @@ -33,7 +33,6 @@ #include #include -#include #include "base/bittorrent/trackerentrystatus.h" @@ -115,6 +114,6 @@ class TrackerListModel final : public QAbstractItemModel class Items; std::unique_ptr m_items; - QDateTime m_announceTimestamp; + qint64 m_announceTimestamp; QTimer *m_announceRefreshTimer = nullptr; };