Skip to content

Commit

Permalink
Avoid using QDateTime for announce timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Nov 24, 2024
1 parent f022ce8 commit 86a9456
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 36 deletions.
8 changes: 5 additions & 3 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <libtorrent/session_status.hpp>
#include <libtorrent/torrent_info.hpp>

#include <QDateTime>
#include <QDeadlineTimer>
#include <QDebug>
#include <QDir>
Expand Down Expand Up @@ -106,6 +107,7 @@
#include "torrentimpl.h"
#include "tracker.h"
#include "trackerentry.h"
#include "trackerentrystatus.h"

using namespace std::chrono_literals;
using namespace BitTorrent;
Expand Down Expand Up @@ -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<lt::alert *> alerts = getPendingAlerts();

Expand Down Expand Up @@ -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<lt::seconds>(timePoint - m_ltNow + lt::milliseconds(500)).count();
return m_qNow.addSecs(secsSinceNow);
return m_currentSecsSinceEpoch + secsSinceNow;
}
6 changes: 2 additions & 4 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <libtorrent/torrent_handle.hpp>

#include <QtContainerFwd>
#include <QDateTime>
#include <QElapsedTimer>
#include <QHash>
#include <QList>
Expand All @@ -55,7 +54,6 @@
#include "session.h"
#include "sessionstatus.h"
#include "torrentinfo.h"
#include "trackerentrystatus.h"

class QString;
class QTimer;
Expand Down Expand Up @@ -479,7 +477,7 @@ namespace BitTorrent
void addMappedPorts(const QSet<quint16> &ports);
void removeMappedPorts(const QSet<quint16> &ports);

QDateTime fromLTTimePoint32(const lt::time_point32 &timePoint) const;
qint64 toSecsSinceEpoch(const lt::time_point32 &timePoint) const;

template <typename Func>
void invoke(Func &&func)
Expand Down Expand Up @@ -832,7 +830,7 @@ namespace BitTorrent

QList<TorrentImpl *> m_pendingFinishedTorrents;

QDateTime m_qNow;
qint64 m_currentSecsSinceEpoch = 0;
lt::clock_type::time_point m_ltNow;

friend void Session::initInstance();
Expand Down
20 changes: 10 additions & 10 deletions src/base/bittorrent/torrentimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ namespace
return endpointName;
}

template <typename FromLTTimePoint32Func>
template <typename ToSecsSinceEpochFunc>
void updateTrackerEntryStatus(TrackerEntryStatus &trackerEntryStatus, const lt::announce_entry &nativeEntry
, const QSet<int> &btProtocols, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo
, const FromLTTimePoint32Func &fromLTTimePoint32)
, const ToSecsSinceEpochFunc &toSecsSinceEpoch)
{
Q_ASSERT(trackerEntryStatus.url == QString::fromStdString(nativeEntry.url));

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
Expand All @@ -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;
Expand Down Expand Up @@ -1777,11 +1777,11 @@ TrackerEntryStatus TorrentImpl::updateTrackerEntryStatus(const lt::announce_entr
const QSet<int> 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;
}
Expand Down
11 changes: 5 additions & 6 deletions src/base/bittorrent/trackerentrystatus.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015-2023 Vladimir Golovnev <[email protected]>
* Copyright (C) 2015-2024 Vladimir Golovnev <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -28,7 +28,6 @@

#pragma once

#include <QDateTime>
#include <QHash>
#include <QString>

Expand Down Expand Up @@ -59,8 +58,8 @@ namespace BitTorrent
int numLeeches = -1;
int numDownloaded = -1;

QDateTime nextAnnounceTime {};
QDateTime minAnnounceTime {};
qint64 nextAnnounceTime = 0;
qint64 minAnnounceTime = 0;
};

struct TrackerEntryStatus
Expand All @@ -76,8 +75,8 @@ namespace BitTorrent
int numLeeches = -1;
int numDownloaded = -1;

QDateTime nextAnnounceTime {};
QDateTime minAnnounceTime {};
qint64 nextAnnounceTime = 0;
qint64 minAnnounceTime = 0;

QHash<std::pair<QString, int>, TrackerEndpointStatus> endpoints {};

Expand Down
21 changes: 11 additions & 10 deletions src/gui/trackerlist/trackerlistmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <[email protected]>
* Copyright (C) 2023-2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2006 Christophe Dumez <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -40,6 +40,7 @@
#include <boost/multi_index/tag.hpp>

#include <QColor>
#include <QDateTime>
#include <QList>
#include <QPointer>
#include <QScopeGuard>
Expand Down Expand Up @@ -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<Item> parentItem {};

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -545,8 +546,8 @@ QVariant TrackerListModel::data(const QModelIndex &index, const int role) const

if (itemPtr->announceTimestamp != m_announceTimestamp)
{
itemPtr->secsToNextAnnounce = std::max<qint64>(0, m_announceTimestamp.secsTo(itemPtr->nextAnnounceTime));
itemPtr->secsToMinAnnounce = std::max<qint64>(0, m_announceTimestamp.secsTo(itemPtr->minAnnounceTime));
itemPtr->secsToNextAnnounce = std::max<qint64>(0, (itemPtr->nextAnnounceTime - m_announceTimestamp));
itemPtr->secsToMinAnnounce = std::max<qint64>(0, (itemPtr->minAnnounceTime - m_announceTimestamp));
itemPtr->announceTimestamp = m_announceTimestamp;
}

Expand Down
5 changes: 2 additions & 3 deletions src/gui/trackerlist/trackerlistmodel.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <[email protected]>
* Copyright (C) 2023-2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2006 Christophe Dumez <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -33,7 +33,6 @@

#include <QtContainerFwd>
#include <QAbstractItemModel>
#include <QDateTime>

#include "base/bittorrent/trackerentrystatus.h"

Expand Down Expand Up @@ -115,6 +114,6 @@ class TrackerListModel final : public QAbstractItemModel
class Items;
std::unique_ptr<Items> m_items;

QDateTime m_announceTimestamp;
qint64 m_announceTimestamp = 0;
QTimer *m_announceRefreshTimer = nullptr;
};

0 comments on commit 86a9456

Please sign in to comment.