Skip to content

Commit

Permalink
Improve interaction with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Oct 2, 2024
1 parent b5b34c9 commit 4eec2f0
Show file tree
Hide file tree
Showing 15 changed files with 679 additions and 370 deletions.
2 changes: 2 additions & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_library(qbt_base STATIC
bittorrent/speedmonitor.h
bittorrent/sslparameters.h
bittorrent/torrent.h
bittorrent/torrentbackend.h
bittorrent/torrentcontenthandler.h
bittorrent/torrentcontentlayout.h
bittorrent/torrentcontentremoveoption.h
Expand Down Expand Up @@ -146,6 +147,7 @@ add_library(qbt_base STATIC
bittorrent/speedmonitor.cpp
bittorrent/sslparameters.cpp
bittorrent/torrent.cpp
bittorrent/torrentbackend.cpp
bittorrent/torrentcontenthandler.cpp
bittorrent/torrentcontentremover.cpp
bittorrent/torrentcreationmanager.cpp
Expand Down
26 changes: 6 additions & 20 deletions src/base/bittorrent/peerinfo.cpp
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 @@ -33,14 +33,16 @@
#include "base/bittorrent/ltqbitarray.h"
#include "base/net/geoipmanager.h"
#include "base/unicodestrings.h"
#include "base/utils/bytearray.h"
#include "peeraddress.h"

#ifdef QBT_USES_LIBTORRENT2
#include "base/utils/bytearray.h"
#endif

using namespace BitTorrent;

PeerInfo::PeerInfo(const lt::peer_info &nativeInfo, const QBitArray &allPieces)
PeerInfo::PeerInfo(const lt::peer_info &nativeInfo)
: m_nativeInfo(nativeInfo)
, m_relevance(calcRelevance(allPieces))
{
determineFlags();
}
Expand Down Expand Up @@ -271,22 +273,6 @@ QString PeerInfo::connectionType() const
: u"Web"_s;
}

qreal PeerInfo::calcRelevance(const QBitArray &allPieces) const
{
const int localMissing = allPieces.count(false);
if (localMissing <= 0)
return 0;

const QBitArray peerPieces = pieces();
const int remoteHaves = (peerPieces & (~allPieces)).count(true);
return static_cast<qreal>(remoteHaves) / localMissing;
}

qreal PeerInfo::relevance() const
{
return m_relevance;
}

void PeerInfo::determineFlags()
{
const auto updateFlags = [this](const QChar specifier, const QString &explanation)
Expand Down
7 changes: 2 additions & 5 deletions src/base/bittorrent/peerinfo.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 @@ -44,7 +44,7 @@ namespace BitTorrent

public:
PeerInfo() = default;
PeerInfo(const lt::peer_info &nativeInfo, const QBitArray &allPieces);
explicit PeerInfo(const lt::peer_info &nativeInfo);

bool fromDHT() const;
bool fromPeX() const;
Expand Down Expand Up @@ -86,18 +86,15 @@ namespace BitTorrent
qlonglong totalDownload() const;
QBitArray pieces() const;
QString connectionType() const;
qreal relevance() const;
QString flags() const;
QString flagsDescription() const;
QString country() const;
int downloadingPieceIndex() const;

private:
qreal calcRelevance(const QBitArray &allPieces) const;
void determineFlags();

lt::peer_info m_nativeInfo = {};
qreal m_relevance = 0;
QString m_flags;
QString m_flagsDescription;

Expand Down
12 changes: 11 additions & 1 deletion src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
#include "nativesessionextension.h"
#include "portforwarderimpl.h"
#include "resumedatastorage.h"
#include "torrentbackend.h"
#include "torrentcontentremover.h"
#include "torrentdescriptor.h"
#include "torrentimpl.h"
Expand Down Expand Up @@ -530,6 +531,7 @@ SessionImpl::SessionImpl(QObject *parent)
, m_startPaused {BITTORRENT_SESSION_KEY(u"StartPaused"_s)}
, m_seedingLimitTimer {new QTimer(this)}
, m_resumeDataTimer {new QTimer(this)}
, m_backendThread {new QThread}
, m_ioThread {new QThread}
, m_asyncWorker {new QThreadPool(this)}
, m_recentErroredTorrentsTimer {new QTimer(this)}
Expand Down Expand Up @@ -590,6 +592,8 @@ SessionImpl::SessionImpl(QObject *parent)
, &Net::ProxyConfigurationManager::proxyConfigurationChanged
, this, &SessionImpl::configureDeferred);

m_backendThread->start();

m_fileSearcher = new FileSearcher;
m_fileSearcher->moveToThread(m_ioThread.get());
connect(m_ioThread.get(), &QThread::finished, m_fileSearcher, &QObject::deleteLater);
Expand Down Expand Up @@ -5706,7 +5710,13 @@ void SessionImpl::dispatchTorrentAlert(const lt::torrent_alert *alert)

TorrentImpl *SessionImpl::createTorrent(const lt::torrent_handle &nativeHandle, const LoadTorrentParams &params)
{
auto *const torrent = new TorrentImpl(this, m_nativeSession, nativeHandle, params);
auto *const torrentBackend = new TorrentBackend(m_nativeSession, nativeHandle);
torrentBackend->moveToThread(m_backendThread.get());
connect(m_backendThread.get(), &QThread::finished, torrentBackend, &QObject::deleteLater);

auto *const torrent = new TorrentImpl(this, torrentBackend, m_nativeSession, nativeHandle, params);
connect(torrent, &QObject::destroyed, torrentBackend, &QObject::deleteLater);

m_torrents.insert(torrent->id(), torrent);
if (const InfoHash infoHash = torrent->infoHash(); infoHash.isHybrid())
m_hybridTorrentsByAltID.insert(TorrentID::fromSHA1Hash(infoHash.v1()), torrent);
Expand Down
1 change: 1 addition & 0 deletions src/base/bittorrent/sessionimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ namespace BitTorrent
// Tracker
QPointer<Tracker> m_tracker;

Utils::Thread::UniquePtr m_backendThread;
Utils::Thread::UniquePtr m_ioThread;
QThreadPool *m_asyncWorker = nullptr;
ResumeDataStorage *m_resumeDataStorage = nullptr;
Expand Down
9 changes: 6 additions & 3 deletions src/base/bittorrent/torrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <QtContainerFwd>
#include <QtTypes>
#include <QFuture>
#include <QMetaType>
#include <QString>

Expand Down Expand Up @@ -274,10 +275,10 @@ namespace BitTorrent
virtual bool isDHTDisabled() const = 0;
virtual bool isPEXDisabled() const = 0;
virtual bool isLSDDisabled() const = 0;
virtual QList<PeerInfo> peers() const = 0;
virtual QBitArray pieces() const = 0;
virtual QBitArray downloadingPieces() const = 0;
virtual QList<int> pieceAvailability() const = 0;
virtual QFuture<QList<PeerInfo>> peers() const = 0;
virtual QFuture<QBitArray> downloadingPieces() const = 0;
virtual QFuture<QList<int>> pieceAvailability() const = 0;
virtual qreal distributedCopies() const = 0;
virtual qreal maxRatio() const = 0;
virtual int maxSeedingTime() const = 0;
Expand Down Expand Up @@ -329,6 +330,8 @@ namespace BitTorrent
virtual void fetchPieceAvailability(std::function<void (QList<int>)> resultHandler) const = 0;
virtual void fetchDownloadingPieces(std::function<void (QBitArray)> resultHandler) const = 0;

virtual qreal peerRelevance(const PeerInfo &peerInfo) const = 0;

TorrentID id() const;
bool isRunning() const;
qlonglong remainingSize() const;
Expand Down
Loading

0 comments on commit 4eec2f0

Please sign in to comment.