1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-02-03 02:14:16 +00:00

Clean up Session class variable initialization

This commit is contained in:
Chocobo1 2019-10-01 15:33:11 +08:00
parent abb3e7ace0
commit 865394a59c
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
2 changed files with 61 additions and 63 deletions

View File

@ -42,8 +42,10 @@
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QFile>
#include <QHostAddress> #include <QHostAddress>
#include <QNetworkAddressEntry> #include <QNetworkAddressEntry>
#include <QNetworkConfigurationManager>
#include <QNetworkInterface> #include <QNetworkInterface>
#include <QRegularExpression> #include <QRegularExpression>
#include <QString> #include <QString>
@ -243,9 +245,6 @@ Session *Session::m_instance = nullptr;
Session::Session(QObject *parent) Session::Session(QObject *parent)
: QObject(parent) : QObject(parent)
, m_deferredConfigureScheduled(false)
, m_IPFilteringChanged(true)
, m_listenInterfaceChanged(true)
, m_isDHTEnabled(BITTORRENT_SESSION_KEY("DHTEnabled"), true) , m_isDHTEnabled(BITTORRENT_SESSION_KEY("DHTEnabled"), true)
, m_isLSDEnabled(BITTORRENT_SESSION_KEY("LSDEnabled"), true) , m_isLSDEnabled(BITTORRENT_SESSION_KEY("LSDEnabled"), true)
, m_isPeXEnabled(BITTORRENT_SESSION_KEY("PeXEnabled"), true) , m_isPeXEnabled(BITTORRENT_SESSION_KEY("PeXEnabled"), true)
@ -346,10 +345,14 @@ Session::Session(QObject *parent)
return tmp; return tmp;
} }
) )
, m_wasPexEnabled(m_isPeXEnabled) , m_resumeFolderLock {new QFile {this}}
, m_numResumeData(0) , m_refreshTimer {new QTimer {this}}
, m_extraLimit(0) , m_seedingLimitTimer {new QTimer {this}}
, m_recentErroredTorrentsTimer(new QTimer(this)) , m_resumeDataTimer {new QTimer {this}}
, m_statistics {new Statistics {this}}
, m_ioThread {new QThread {this}}
, m_recentErroredTorrentsTimer {new QTimer {this}}
, m_networkManager {new QNetworkConfigurationManager {this}}
{ {
if (port() < 0) if (port() < 0)
m_port = Utils::Random::rand(1024, 65535); m_port = Utils::Random::rand(1024, 65535);
@ -358,9 +361,9 @@ Session::Session(QObject *parent)
m_recentErroredTorrentsTimer->setSingleShot(true); m_recentErroredTorrentsTimer->setSingleShot(true);
m_recentErroredTorrentsTimer->setInterval(1000); m_recentErroredTorrentsTimer->setInterval(1000);
connect(m_recentErroredTorrentsTimer, &QTimer::timeout, this, [this]() { m_recentErroredTorrents.clear(); }); connect(m_recentErroredTorrentsTimer, &QTimer::timeout
, this, [this]() { m_recentErroredTorrents.clear(); });
m_seedingLimitTimer = new QTimer(this);
m_seedingLimitTimer->setInterval(10000); m_seedingLimitTimer->setInterval(10000);
connect(m_seedingLimitTimer, &QTimer::timeout, this, &Session::processShareLimits); connect(m_seedingLimitTimer, &QTimer::timeout, this, &Session::processShareLimits);
@ -379,35 +382,31 @@ Session::Session(QObject *parent)
m_tags = QSet<QString>::fromList(m_storedTags.value()); m_tags = QSet<QString>::fromList(m_storedTags.value());
m_refreshTimer = new QTimer(this);
m_refreshTimer->setInterval(refreshInterval()); m_refreshTimer->setInterval(refreshInterval());
connect(m_refreshTimer, &QTimer::timeout, this, &Session::refresh); connect(m_refreshTimer, &QTimer::timeout, this, &Session::refresh);
m_refreshTimer->start(); m_refreshTimer->start();
m_statistics = new Statistics(this);
updateSeedingLimitTimer(); updateSeedingLimitTimer();
populateAdditionalTrackers(); populateAdditionalTrackers();
enableTracker(isTrackerEnabled()); enableTracker(isTrackerEnabled());
connect(Net::ProxyConfigurationManager::instance(), &Net::ProxyConfigurationManager::proxyConfigurationChanged connect(Net::ProxyConfigurationManager::instance()
, this, &Session::configureDeferred); , &Net::ProxyConfigurationManager::proxyConfigurationChanged
, this, &Session::configureDeferred);
// Network configuration monitor // Network configuration monitor
connect(&m_networkManager, &QNetworkConfigurationManager::onlineStateChanged, this, &Session::networkOnlineStateChanged); connect(m_networkManager, &QNetworkConfigurationManager::onlineStateChanged, this, &Session::networkOnlineStateChanged);
connect(&m_networkManager, &QNetworkConfigurationManager::configurationAdded, this, &Session::networkConfigurationChange); connect(m_networkManager, &QNetworkConfigurationManager::configurationAdded, this, &Session::networkConfigurationChange);
connect(&m_networkManager, &QNetworkConfigurationManager::configurationRemoved, this, &Session::networkConfigurationChange); connect(m_networkManager, &QNetworkConfigurationManager::configurationRemoved, this, &Session::networkConfigurationChange);
connect(&m_networkManager, &QNetworkConfigurationManager::configurationChanged, this, &Session::networkConfigurationChange); connect(m_networkManager, &QNetworkConfigurationManager::configurationChanged, this, &Session::networkConfigurationChange);
m_ioThread = new QThread(this);
m_resumeDataSavingManager = new ResumeDataSavingManager {m_resumeFolderPath}; m_resumeDataSavingManager = new ResumeDataSavingManager {m_resumeFolderPath};
m_resumeDataSavingManager->moveToThread(m_ioThread); m_resumeDataSavingManager->moveToThread(m_ioThread);
connect(m_ioThread, &QThread::finished, m_resumeDataSavingManager, &QObject::deleteLater); connect(m_ioThread, &QThread::finished, m_resumeDataSavingManager, &QObject::deleteLater);
m_ioThread->start(); m_ioThread->start();
// Regular saving of fastresume data // Regular saving of fastresume data
m_resumeDataTimer = new QTimer(this);
connect(m_resumeDataTimer, &QTimer::timeout, this, [this]() { generateResumeData(); }); connect(m_resumeDataTimer, &QTimer::timeout, this, [this]() { generateResumeData(); });
const uint saveInterval = saveResumeDataInterval(); const uint saveInterval = saveResumeDataInterval();
if (saveInterval > 0) { if (saveInterval > 0) {
@ -419,8 +418,6 @@ Session::Session(QObject *parent)
new PortForwarderImpl {m_nativeSession}; new PortForwarderImpl {m_nativeSession};
initMetrics(); initMetrics();
qDebug("* BitTorrent Session constructed");
} }
bool Session::isDHTEnabled() const bool Session::isDHTEnabled() const
@ -869,8 +866,8 @@ Session::~Session()
m_ioThread->quit(); m_ioThread->quit();
m_ioThread->wait(); m_ioThread->wait();
m_resumeFolderLock.close(); m_resumeFolderLock->close();
m_resumeFolderLock.remove(); m_resumeFolderLock->remove();
} }
void Session::initInstance() void Session::initInstance()
@ -3559,8 +3556,8 @@ void Session::initResumeFolder()
m_resumeFolderPath = Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Data) + RESUME_FOLDER); m_resumeFolderPath = Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Data) + RESUME_FOLDER);
const QDir resumeFolderDir(m_resumeFolderPath); const QDir resumeFolderDir(m_resumeFolderPath);
if (resumeFolderDir.exists() || resumeFolderDir.mkpath(resumeFolderDir.absolutePath())) { if (resumeFolderDir.exists() || resumeFolderDir.mkpath(resumeFolderDir.absolutePath())) {
m_resumeFolderLock.setFileName(resumeFolderDir.absoluteFilePath("session.lock")); m_resumeFolderLock->setFileName(resumeFolderDir.absoluteFilePath("session.lock"));
if (!m_resumeFolderLock.open(QFile::WriteOnly)) { if (!m_resumeFolderLock->open(QFile::WriteOnly)) {
throw RuntimeError {tr("Cannot write to torrent resume folder.")}; throw RuntimeError {tr("Cannot write to torrent resume folder.")};
} }
} }

View File

@ -34,9 +34,7 @@
#include <libtorrent/fwd.hpp> #include <libtorrent/fwd.hpp>
#include <QFile>
#include <QHash> #include <QHash>
#include <QNetworkConfigurationManager>
#include <QPointer> #include <QPointer>
#include <QSet> #include <QSet>
#include <QVector> #include <QVector>
@ -48,16 +46,19 @@
#include "sessionstatus.h" #include "sessionstatus.h"
#include "torrentinfo.h" #include "torrentinfo.h"
class QThread; class QFile;
class QTimer; class QNetworkConfiguration;
class QNetworkConfigurationManager;
class QString; class QString;
class QStringList; class QStringList;
class QThread;
class QTimer;
class QUrl; class QUrl;
class FilterParserThread;
class BandwidthScheduler; class BandwidthScheduler;
class Statistics; class FilterParserThread;
class ResumeDataSavingManager; class ResumeDataSavingManager;
class Statistics;
// These values should remain unchanged when adding new items // These values should remain unchanged when adding new items
// so as not to break the existing user settings. // so as not to break the existing user settings.
@ -89,9 +90,9 @@ namespace Net
namespace BitTorrent namespace BitTorrent
{ {
class InfoHash; class InfoHash;
class MagnetUri;
class TorrentHandle; class TorrentHandle;
class Tracker; class Tracker;
class MagnetUri;
class TrackerEntry; class TrackerEntry;
struct CreateTorrentParams; struct CreateTorrentParams;
@ -102,6 +103,14 @@ namespace BitTorrent
{ {
Q_NAMESPACE Q_NAMESPACE
enum class BTProtocol : int
{
Both = 0,
TCP = 1,
UTP = 2
};
Q_ENUM_NS(BTProtocol)
enum class ChokingAlgorithm : int enum class ChokingAlgorithm : int
{ {
FixedSlots = 0, FixedSlots = 0,
@ -109,14 +118,6 @@ namespace BitTorrent
}; };
Q_ENUM_NS(ChokingAlgorithm) Q_ENUM_NS(ChokingAlgorithm)
enum class SeedChokingAlgorithm : int
{
RoundRobin = 0,
FastestUpload = 1,
AntiLeech = 2
};
Q_ENUM_NS(SeedChokingAlgorithm)
enum class MixedModeAlgorithm : int enum class MixedModeAlgorithm : int
{ {
TCP = 0, TCP = 0,
@ -124,13 +125,13 @@ namespace BitTorrent
}; };
Q_ENUM_NS(MixedModeAlgorithm) Q_ENUM_NS(MixedModeAlgorithm)
enum class BTProtocol : int enum class SeedChokingAlgorithm : int
{ {
Both = 0, RoundRobin = 0,
TCP = 1, FastestUpload = 1,
UTP = 2 AntiLeech = 2
}; };
Q_ENUM_NS(BTProtocol) Q_ENUM_NS(SeedChokingAlgorithm)
} }
using namespace SessionSettingsEnums; using namespace SessionSettingsEnums;
@ -492,7 +493,7 @@ namespace BitTorrent
// Session reconfiguration triggers // Session reconfiguration triggers
void networkOnlineStateChanged(bool online); void networkOnlineStateChanged(bool online);
void networkConfigurationChange(const QNetworkConfiguration&); void networkConfigurationChange(const QNetworkConfiguration &);
private: private:
struct RemovingTorrentData struct RemovingTorrentData
@ -570,11 +571,11 @@ namespace BitTorrent
void getPendingAlerts(std::vector<lt::alert *> &out, ulong time = 0); void getPendingAlerts(std::vector<lt::alert *> &out, ulong time = 0);
// BitTorrent // BitTorrent
lt::session *m_nativeSession; lt::session *m_nativeSession = nullptr;
bool m_deferredConfigureScheduled; bool m_deferredConfigureScheduled = false;
bool m_IPFilteringChanged; bool m_IPFilteringChanged = true; // initialization require this to be true
bool m_listenInterfaceChanged; // optimization bool m_listenInterfaceChanged = true; // initialization require this to be true
CachedSettingValue<bool> m_isDHTEnabled; CachedSettingValue<bool> m_isDHTEnabled;
CachedSettingValue<bool> m_isLSDEnabled; CachedSettingValue<bool> m_isLSDEnabled;
@ -664,26 +665,26 @@ namespace BitTorrent
// Order is important. This needs to be declared after its CachedSettingsValue // Order is important. This needs to be declared after its CachedSettingsValue
// counterpart, because it uses it for initialization in the constructor // counterpart, because it uses it for initialization in the constructor
// initialization list. // initialization list.
const bool m_wasPexEnabled; const bool m_wasPexEnabled = m_isPeXEnabled;
int m_numResumeData; int m_numResumeData = 0;
int m_extraLimit; int m_extraLimit = 0;
QVector<BitTorrent::TrackerEntry> m_additionalTrackerList; QVector<BitTorrent::TrackerEntry> m_additionalTrackerList;
QString m_resumeFolderPath; QString m_resumeFolderPath;
QFile m_resumeFolderLock; QFile *m_resumeFolderLock = nullptr;
QTimer *m_refreshTimer; QTimer *m_refreshTimer = nullptr;
QTimer *m_seedingLimitTimer; QTimer *m_seedingLimitTimer = nullptr;
QTimer *m_resumeDataTimer; QTimer *m_resumeDataTimer = nullptr;
Statistics *m_statistics; Statistics *m_statistics = nullptr;
// IP filtering // IP filtering
QPointer<FilterParserThread> m_filterParser; QPointer<FilterParserThread> m_filterParser;
QPointer<BandwidthScheduler> m_bwScheduler; QPointer<BandwidthScheduler> m_bwScheduler;
// Tracker // Tracker
QPointer<Tracker> m_tracker; QPointer<Tracker> m_tracker;
// fastresume data writing thread // fastresume data writing thread
QThread *m_ioThread; QThread *m_ioThread = nullptr;
ResumeDataSavingManager *m_resumeDataSavingManager; ResumeDataSavingManager *m_resumeDataSavingManager = nullptr;
QHash<InfoHash, TorrentInfo> m_loadedMetadata; QHash<InfoHash, TorrentInfo> m_loadedMetadata;
QHash<InfoHash, TorrentHandle *> m_torrents; QHash<InfoHash, TorrentHandle *> m_torrents;
@ -695,7 +696,7 @@ namespace BitTorrent
// I/O errored torrents // I/O errored torrents
QSet<InfoHash> m_recentErroredTorrents; QSet<InfoHash> m_recentErroredTorrents;
QTimer *m_recentErroredTorrentsTimer; QTimer *m_recentErroredTorrentsTimer = nullptr;
SessionMetricIndices m_metricIndices; SessionMetricIndices m_metricIndices;
lt::time_point m_statsLastTimestamp = lt::clock_type::now(); lt::time_point m_statsLastTimestamp = lt::clock_type::now();
@ -703,7 +704,7 @@ namespace BitTorrent
SessionStatus m_status; SessionStatus m_status;
CacheStatus m_cacheStatus; CacheStatus m_cacheStatus;
QNetworkConfigurationManager m_networkManager; QNetworkConfigurationManager *m_networkManager = nullptr;
static Session *m_instance; static Session *m_instance;
}; };