Skip to content

Commit

Permalink
Use std::chrono classes
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Nov 25, 2024
1 parent 86a9456 commit ffb0cdc
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(qbt_base STATIC
asyncfilestorage.h
bittorrent/abstractfilestorage.h
bittorrent/addtorrentparams.h
bittorrent/announcetimepoint.h
bittorrent/bandwidthscheduler.h
bittorrent/bencoderesumedatastorage.h
bittorrent/cachestatus.h
Expand Down
36 changes: 36 additions & 0 deletions src/base/bittorrent/announcetimepoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 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
* 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;
}
15 changes: 4 additions & 11 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "sessionimpl.h"

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <queue>
Expand Down Expand Up @@ -1605,16 +1604,16 @@ void SessionImpl::endStartup(ResumeSessionContext *context)
m_wakeupCheckTimer = new QTimer(this);
connect(m_wakeupCheckTimer, &QTimer::timeout, this, [this]
{
const auto now = QDateTime::currentDateTime();
if (m_wakeupCheckTimestamp.secsTo(now) > 100)
const auto now = std::chrono::steady_clock::now();
if ((now - m_wakeupCheckTimestamp) > 100s)
{
LogMsg(tr("System wake-up event detected. Re-announcing to all the trackers..."));
reannounceToAllTrackers();
}

m_wakeupCheckTimestamp = QDateTime::currentDateTime();
m_wakeupCheckTimestamp = now;
});
m_wakeupCheckTimestamp = QDateTime::currentDateTime();
m_wakeupCheckTimestamp = std::chrono::steady_clock::now();
m_wakeupCheckTimer->start(30s);

m_isRestored = true;
Expand Down Expand Up @@ -6397,9 +6396,3 @@ void SessionImpl::handleRemovedTorrent(const TorrentID &torrentID, const QString

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;
}
5 changes: 2 additions & 3 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#pragma once

#include <chrono>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -477,8 +478,6 @@ namespace BitTorrent
void addMappedPorts(const QSet<quint16> &ports);
void removeMappedPorts(const QSet<quint16> &ports);

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

template <typename Func>
void invoke(Func &&func)
{
Expand Down Expand Up @@ -826,7 +825,7 @@ namespace BitTorrent
QHash<quint16, std::vector<lt::port_mapping_t>> m_mappedPorts;

QTimer *m_wakeupCheckTimer = nullptr;
QDateTime m_wakeupCheckTimestamp;
std::chrono::steady_clock::time_point m_wakeupCheckTimestamp;

QList<TorrentImpl *> m_pendingFinishedTorrents;

Expand Down
20 changes: 7 additions & 13 deletions src/base/bittorrent/torrentimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ namespace
return endpointName;
}

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 ToSecsSinceEpochFunc &toSecsSinceEpoch)
, const QSet<int> &btProtocols, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo)
{
Q_ASSERT(trackerEntryStatus.url == QString::fromStdString(nativeEntry.url));

Expand Down Expand Up @@ -146,8 +144,8 @@ namespace
trackerEndpointStatus.numSeeds = ltAnnounceInfo.scrape_complete;
trackerEndpointStatus.numLeeches = ltAnnounceInfo.scrape_incomplete;
trackerEndpointStatus.numDownloaded = ltAnnounceInfo.scrape_downloaded;
trackerEndpointStatus.nextAnnounceTime = toSecsSinceEpoch(ltAnnounceInfo.next_announce);
trackerEndpointStatus.minAnnounceTime = toSecsSinceEpoch(ltAnnounceInfo.min_announce);
trackerEndpointStatus.nextAnnounceTime = ltAnnounceInfo.next_announce;
trackerEndpointStatus.minAnnounceTime = ltAnnounceInfo.min_announce;

if (ltAnnounceInfo.updating)
{
Expand Down Expand Up @@ -238,8 +236,8 @@ namespace
trackerEntryStatus.numSeeds = -1;
trackerEntryStatus.numLeeches = -1;
trackerEntryStatus.numDownloaded = -1;
trackerEntryStatus.nextAnnounceTime = 0;
trackerEntryStatus.minAnnounceTime = 0;
trackerEntryStatus.nextAnnounceTime = {};
trackerEntryStatus.minAnnounceTime = {};
trackerEntryStatus.message.clear();

for (const TrackerEndpointStatus &endpointStatus : asConst(trackerEntryStatus.endpoints))
Expand All @@ -251,7 +249,7 @@ namespace

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.minAnnounceTime = endpointStatus.minAnnounceTime;
Expand Down Expand Up @@ -1777,11 +1775,7 @@ TrackerEntryStatus TorrentImpl::updateTrackerEntryStatus(const lt::announce_entr
const QSet<int> btProtocols {1};
#endif

const auto toSecsSinceEpoch = [this](const lt::time_point32 &timePoint)
{
return m_session->toSecsSinceEpoch(timePoint);
};
::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo, toSecsSinceEpoch);
::updateTrackerEntryStatus(*it, announceEntry, btProtocols, updateInfo);

return *it;
}
Expand Down
10 changes: 6 additions & 4 deletions src/base/bittorrent/trackerentrystatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <QHash>
#include <QString>

#include "announcetimepoint.h"

class QStringView;

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

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

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

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

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

Expand Down
24 changes: 15 additions & 9 deletions src/gui/trackerlist/trackerlistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "trackerlistmodel.h"

#include <algorithm>
#include <chrono>

#include <boost/multi_index_container.hpp>
Expand All @@ -46,6 +47,7 @@
#include <QScopeGuard>
#include <QTimer>

#include "base/bittorrent/announcetimepoint.h"
#include "base/bittorrent/peerinfo.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h"
Expand Down Expand Up @@ -142,12 +144,12 @@ struct TrackerListModel::Item final
int numLeeches = -1;
int numDownloaded = -1;

qint64 nextAnnounceTime = 0;
qint64 minAnnounceTime = 0;
BitTorrent::AnnounceTimePoint nextAnnounceTime;
BitTorrent::AnnounceTimePoint minAnnounceTime;

qint64 secsToNextAnnounce = 0;
qint64 secsToMinAnnounce = 0;
qint64 announceTimestamp = 0;
BitTorrent::AnnounceTimePoint announceTimestamp;

std::weak_ptr<Item> parentItem {};

Expand Down Expand Up @@ -212,7 +214,7 @@ void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEntryStatus &trac
minAnnounceTime = trackerEntryStatus.minAnnounceTime;
secsToNextAnnounce = 0;
secsToMinAnnounce = 0;
announceTimestamp = 0;
announceTimestamp = {};
}

void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &endpointStatus)
Expand All @@ -231,7 +233,7 @@ void TrackerListModel::Item::fillFrom(const BitTorrent::TrackerEndpointStatus &e
minAnnounceTime = endpointStatus.minAnnounceTime;
secsToNextAnnounce = 0;
secsToMinAnnounce = 0;
announceTimestamp = 0;
announceTimestamp = {};
}

TrackerListModel::TrackerListModel(BitTorrent::Session *btSession, QObject *parent)
Expand Down Expand Up @@ -370,7 +372,7 @@ void TrackerListModel::populate()
for (const BitTorrent::TrackerEntryStatus &status : trackers)
addTrackerItem(status);

m_announceTimestamp = QDateTime::currentSecsSinceEpoch();
m_announceTimestamp = BitTorrent::AnnounceTimePoint::clock::now();
m_announceRefreshTimer->start(ANNOUNCE_TIME_REFRESH_INTERVAL);
}

Expand Down Expand Up @@ -449,7 +451,7 @@ void TrackerListModel::refreshAnnounceTimes()
if (!m_torrent)
return;

m_announceTimestamp = QDateTime::currentSecsSinceEpoch();
m_announceTimestamp = BitTorrent::AnnounceTimePoint::clock::now();
emit dataChanged(index(0, COL_NEXT_ANNOUNCE), index((rowCount() - 1), COL_MIN_ANNOUNCE));
for (int i = 0; i < rowCount(); ++i)
{
Expand Down Expand Up @@ -546,8 +548,12 @@ QVariant TrackerListModel::data(const QModelIndex &index, const int role) const

if (itemPtr->announceTimestamp != m_announceTimestamp)
{
itemPtr->secsToNextAnnounce = std::max<qint64>(0, (itemPtr->nextAnnounceTime - m_announceTimestamp));
itemPtr->secsToMinAnnounce = std::max<qint64>(0, (itemPtr->minAnnounceTime - m_announceTimestamp));
const auto timeToNextAnnounce = std::chrono::duration_cast<std::chrono::seconds>(itemPtr->nextAnnounceTime - 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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/gui/trackerlist/trackerlistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <QtContainerFwd>
#include <QAbstractItemModel>

#include "base/bittorrent/announcetimepoint.h"
#include "base/bittorrent/trackerentrystatus.h"

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

qint64 m_announceTimestamp = 0;
BitTorrent::AnnounceTimePoint m_announceTimestamp;
QTimer *m_announceRefreshTimer = nullptr;
};

0 comments on commit ffb0cdc

Please sign in to comment.