From 78d6b14fe86ff25e77022b60f13df353b8343c20 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 4 May 2017 13:05:12 +0300 Subject: [PATCH 01/12] Don't use deprecated torrent_info fields --- src/base/bittorrent/torrentinfo.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index 357378bd2..6441c5a0e 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -186,7 +186,7 @@ qlonglong TorrentInfo::fileSize(int index) const qlonglong TorrentInfo::fileOffset(int index) const { if (!isValid()) return -1; - return m_nativeInfo->file_at(index).offset; + return m_nativeInfo->files().file_offset(index); } QList TorrentInfo::trackers() const From cb678a254dffe1d62423f8b6f4da3f17c80543f8 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 4 May 2017 13:15:04 +0300 Subject: [PATCH 02/12] Fix statsdialog.* coding style --- src/gui/statsdialog.cpp | 126 ++++++++++++++++++++-------------------- src/gui/statsdialog.h | 24 ++++---- 2 files changed, 76 insertions(+), 74 deletions(-) diff --git a/src/gui/statsdialog.cpp b/src/gui/statsdialog.cpp index 7a5dcb531..e54a1339f 100644 --- a/src/gui/statsdialog.cpp +++ b/src/gui/statsdialog.cpp @@ -1,6 +1,6 @@ /* - * Bittorrent Client using Qt4 and libtorrent. - * Copyright (C) 2013 Nick Tiskov + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2013 Nick Tiskov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -24,84 +24,86 @@ * 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. - * - * Contact : daymansmail@gmail.com */ #include "statsdialog.h" -#include "ui_statsdialog.h" -#include "base/utils/misc.h" -#include "base/utils/string.h" +#include "base/bittorrent/cachestatus.h" #include "base/bittorrent/session.h" #include "base/bittorrent/sessionstatus.h" -#include "base/bittorrent/cachestatus.h" #include "base/bittorrent/torrenthandle.h" +#include "base/utils/misc.h" +#include "base/utils/string.h" +#include "ui_statsdialog.h" StatsDialog::StatsDialog(QWidget *parent) : QDialog(parent) - , ui(new Ui::StatsDialog) + , m_ui(new Ui::StatsDialog) { - ui->setupUi(this); - setAttribute(Qt::WA_DeleteOnClose); - connect(ui->buttonBox, SIGNAL(accepted()), SLOT(close())); - updateUI(); - t = new QTimer(this); - t->setInterval(1500); - connect(t, SIGNAL(timeout()), SLOT(updateUI())); - t->start(); + m_ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &StatsDialog::close); - show(); + update(); + m_timer = new QTimer(this); + m_timer->setInterval(1500); + connect(m_timer, &QTimer::timeout, this, &StatsDialog::update); + m_timer->start(); + + show(); } -StatsDialog::~StatsDialog() { - t->stop(); - delete t; - delete ui; +StatsDialog::~StatsDialog() +{ + m_timer->stop(); + delete m_timer; + delete m_ui; } -void StatsDialog::updateUI() { - BitTorrent::SessionStatus ss = BitTorrent::Session::instance()->status(); - BitTorrent::CacheStatus cs = BitTorrent::Session::instance()->cacheStatus(); +void StatsDialog::update() +{ + BitTorrent::SessionStatus ss = BitTorrent::Session::instance()->status(); + BitTorrent::CacheStatus cs = BitTorrent::Session::instance()->cacheStatus(); - // Alltime DL/UL - quint64 atd = BitTorrent::Session::instance()->getAlltimeDL(); - quint64 atu = BitTorrent::Session::instance()->getAlltimeUL(); - ui->labelAlltimeDL->setText(Utils::Misc::friendlyUnit(atd)); - ui->labelAlltimeUL->setText(Utils::Misc::friendlyUnit(atu)); - // Total waste (this session) - ui->labelWaste->setText(Utils::Misc::friendlyUnit(ss.totalWasted())); - // Global ratio - ui->labelGlobalRatio->setText( - ( atd > 0 && atu > 0 ) ? - Utils::String::fromDouble((qreal)atu / (qreal)atd, 2) : - "-" - ); - // Cache hits - qreal readRatio = cs.readRatio(); - ui->labelCacheHits->setText((readRatio >= 0) ? Utils::String::fromDouble(100 * readRatio, 2) : "-"); - // Buffers size - ui->labelTotalBuf->setText(Utils::Misc::friendlyUnit(cs.totalUsedBuffers() * 16 * 1024)); - // Disk overload (100%) equivalent - // From lt manual: disk_write_queue and disk_read_queue are the number of peers currently waiting on a disk write or disk read - // to complete before it receives or sends any more data on the socket. It's a metric of how disk bound you are. + // Alltime DL/UL + quint64 atd = BitTorrent::Session::instance()->getAlltimeDL(); + quint64 atu = BitTorrent::Session::instance()->getAlltimeUL(); + m_ui->labelAlltimeDL->setText(Utils::Misc::friendlyUnit(atd)); + m_ui->labelAlltimeUL->setText(Utils::Misc::friendlyUnit(atu)); + // Total waste (this session) + m_ui->labelWaste->setText(Utils::Misc::friendlyUnit(ss.totalWasted())); + // Global ratio + m_ui->labelGlobalRatio->setText( + ((atd > 0) && (atu > 0)) + ? Utils::String::fromDouble((qreal)atu / (qreal)atd, 2) + : "-"); + // Cache hits + qreal readRatio = cs.readRatio(); + m_ui->labelCacheHits->setText((readRatio >= 0) ? Utils::String::fromDouble(100 * readRatio, 2) : "-"); + // Buffers size + m_ui->labelTotalBuf->setText(Utils::Misc::friendlyUnit(cs.totalUsedBuffers() * 16 * 1024)); + // Disk overload (100%) equivalent + // From lt manual: disk_write_queue and disk_read_queue are the number of peers currently waiting on a disk write or disk read + // to complete before it receives or sends any more data on the socket. It's a metric of how disk bound you are. - // num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake) - quint32 peers = 0; - foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents()) - peers += torrent->peersCount(); + // num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake) + quint32 peers = 0; + foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents()) + peers += torrent->peersCount(); - ui->labelWriteStarve->setText(QString("%1%").arg(((ss.diskWriteQueue() > 0) && (peers > 0)) - ? Utils::String::fromDouble((100. * ss.diskWriteQueue()) / peers, 2) - : "0")); - ui->labelReadStarve->setText(QString("%1%").arg(((ss.diskReadQueue() > 0) && (peers > 0)) - ? Utils::String::fromDouble((100. * ss.diskReadQueue()) / peers, 2) - : "0")); - // Disk queues - ui->labelQueuedJobs->setText(QString::number(cs.jobQueueLength())); - ui->labelJobsTime->setText(tr("%1 ms", "18 milliseconds").arg(cs.averageJobTime())); - ui->labelQueuedBytes->setText(Utils::Misc::friendlyUnit(cs.queuedBytes())); + m_ui->labelWriteStarve->setText(QString("%1%") + .arg(((ss.diskWriteQueue() > 0) && (peers > 0)) + ? Utils::String::fromDouble((100. * ss.diskWriteQueue()) / peers, 2) + : "0")); + m_ui->labelReadStarve->setText(QString("%1%") + .arg(((ss.diskReadQueue() > 0) && (peers > 0)) + ? Utils::String::fromDouble((100. * ss.diskReadQueue()) / peers, 2) + : "0")); + // Disk queues + m_ui->labelQueuedJobs->setText(QString::number(cs.jobQueueLength())); + m_ui->labelJobsTime->setText(tr("%1 ms", "18 milliseconds").arg(cs.averageJobTime())); + m_ui->labelQueuedBytes->setText(Utils::Misc::friendlyUnit(cs.queuedBytes())); - // Total connected peers - ui->labelPeers->setText(QString::number(ss.peersCount())); + // Total connected peers + m_ui->labelPeers->setText(QString::number(ss.peersCount())); } diff --git a/src/gui/statsdialog.h b/src/gui/statsdialog.h index 46c41d8a8..9f2602c5c 100644 --- a/src/gui/statsdialog.h +++ b/src/gui/statsdialog.h @@ -1,6 +1,6 @@ /* - * Bittorrent Client using Qt4 and libtorrent. - * Copyright (C) 2013 Nick Tiskov + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2013 Nick Tiskov * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -24,8 +24,6 @@ * 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. - * - * Contact : daymansmail@gmail.com */ #ifndef STATSDIALOG_H @@ -34,23 +32,25 @@ #include #include -namespace Ui { - class StatsDialog; +namespace Ui +{ + class StatsDialog; } -class StatsDialog : public QDialog { - Q_OBJECT +class StatsDialog: public QDialog +{ + Q_OBJECT public: explicit StatsDialog(QWidget *parent); - ~StatsDialog(); + ~StatsDialog() override; private slots: - void updateUI(); + void update(); private: - Ui::StatsDialog *ui; - QTimer* t; + Ui::StatsDialog *m_ui; + QTimer *m_timer; }; #endif // STATSDIALOG_H From 8a6d8f3953c3b0b5fd769a0b48b57feeb58c14d0 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sat, 29 Apr 2017 14:45:30 +0300 Subject: [PATCH 03/12] Use cached SessionStatus and CacheStatus --- src/base/base.pri | 2 - src/base/bittorrent/cachestatus.cpp | 69 ----------- src/base/bittorrent/cachestatus.h | 21 ++-- src/base/bittorrent/private/statistics.cpp | 10 +- src/base/bittorrent/session.cpp | 54 +++++++- src/base/bittorrent/session.h | 17 +-- src/base/bittorrent/sessionstatus.cpp | 136 --------------------- src/base/bittorrent/sessionstatus.h | 55 ++++----- src/gui/mainwindow.cpp | 14 +-- src/gui/properties/speedwidget.cpp | 25 ++-- src/gui/statsdialog.cpp | 34 +++--- src/gui/statsdialog.h | 2 - src/gui/statusbar.cpp | 39 +++--- src/gui/statusbar.h | 19 ++- src/webui/btjson.cpp | 34 +++--- 15 files changed, 171 insertions(+), 360 deletions(-) delete mode 100644 src/base/bittorrent/cachestatus.cpp delete mode 100644 src/base/bittorrent/sessionstatus.cpp diff --git a/src/base/base.pri b/src/base/base.pri index 21a67f7b1..041e2d4a3 100644 --- a/src/base/base.pri +++ b/src/base/base.pri @@ -89,8 +89,6 @@ SOURCES += \ $$PWD/net/private/geoipdatabase.cpp \ $$PWD/bittorrent/infohash.cpp \ $$PWD/bittorrent/session.cpp \ - $$PWD/bittorrent/sessionstatus.cpp \ - $$PWD/bittorrent/cachestatus.cpp \ $$PWD/bittorrent/magneturi.cpp \ $$PWD/bittorrent/torrentinfo.cpp \ $$PWD/bittorrent/torrenthandle.cpp \ diff --git a/src/base/bittorrent/cachestatus.cpp b/src/base/bittorrent/cachestatus.cpp deleted file mode 100644 index 5117f531a..000000000 --- a/src/base/bittorrent/cachestatus.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev - * - * 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. - */ - -#include -#include "cachestatus.h" - -using namespace BitTorrent; - -CacheStatus::CacheStatus(const libtorrent::cache_status &nativeStatus) - : m_nativeStatus(nativeStatus) -{ -} - -int CacheStatus::totalUsedBuffers() const -{ - return m_nativeStatus.total_used_buffers; -} - -qreal CacheStatus::readRatio() const -{ - if (m_nativeStatus.blocks_read > 0) - return (static_cast(m_nativeStatus.blocks_read_hit) / m_nativeStatus.blocks_read); - else - return -1; -} - -int CacheStatus::jobQueueLength() const -{ -#if LIBTORRENT_VERSION_NUM < 10100 - return m_nativeStatus.job_queue_length; -#else - return m_nativeStatus.queued_jobs; -#endif -} - -int CacheStatus::averageJobTime() const -{ - return m_nativeStatus.average_job_time; -} - -qlonglong CacheStatus::queuedBytes() const -{ - return m_nativeStatus.queued_bytes; -} diff --git a/src/base/bittorrent/cachestatus.h b/src/base/bittorrent/cachestatus.h index 7f03cff82..d5282c9d4 100644 --- a/src/base/bittorrent/cachestatus.h +++ b/src/base/bittorrent/cachestatus.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2015, 2017 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -30,23 +30,16 @@ #define BITTORRENT_CACHESTATUS_H #include -#include namespace BitTorrent { - class CacheStatus + struct CacheStatus { - public: - CacheStatus(const libtorrent::cache_status &nativeStatus); - - int totalUsedBuffers() const; - qreal readRatio() const; - int jobQueueLength() const; - int averageJobTime() const; - qlonglong queuedBytes() const; - - private: - libtorrent::cache_status m_nativeStatus; + quint64 totalUsedBuffers = 0; + quint64 jobQueueLength = 0; + quint64 averageJobTime = 0; + quint64 queuedBytes = 0; + qreal readRatio = 0.0; }; } diff --git a/src/base/bittorrent/private/statistics.cpp b/src/base/bittorrent/private/statistics.cpp index d2e562373..97e3f0424 100644 --- a/src/base/bittorrent/private/statistics.cpp +++ b/src/base/bittorrent/private/statistics.cpp @@ -45,13 +45,13 @@ quint64 Statistics::getAlltimeUL() const void Statistics::gather() { - SessionStatus ss = m_session->status(); - if (ss.totalDownload() > m_sessionDL) { - m_sessionDL = ss.totalDownload(); + const SessionStatus &ss = m_session->status(); + if (ss.totalDownload > m_sessionDL) { + m_sessionDL = ss.totalDownload; m_dirty = true; } - if (ss.totalUpload() > m_sessionUL) { - m_sessionUL = ss.totalUpload(); + if (ss.totalUpload > m_sessionUL) { + m_sessionUL = ss.totalUpload; m_dirty = true; } diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 9c08c15ae..e4236bf81 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -52,6 +52,7 @@ #include #endif #include +#include #include #include #include @@ -63,6 +64,7 @@ #endif #include #include +#include #include #include "base/logger.h" @@ -79,13 +81,11 @@ #include "base/utils/fs.h" #include "base/utils/random.h" #include "base/utils/string.h" -#include "cachestatus.h" #include "magneturi.h" #include "private/filterparserthread.h" #include "private/statistics.h" #include "private/bandwidthscheduler.h" #include "private/resumedatasavingmanager.h" -#include "sessionstatus.h" #include "torrenthandle.h" #include "tracker.h" #include "trackerentry.h" @@ -3088,14 +3088,14 @@ void Session::recursiveTorrentDownload(const InfoHash &hash) } } -SessionStatus Session::status() const +const SessionStatus &Session::status() const { - return m_nativeSession->status(); + return m_status; } -CacheStatus Session::cacheStatus() const +const CacheStatus &Session::cacheStatus() const { - return m_nativeSession->get_cache_status(); + return m_cacheStatus; } // Will resume torrents in backup directory @@ -3581,8 +3581,50 @@ void Session::handleExternalIPAlert(libt::external_ip_alert *p) Logger::instance()->addMessage(tr("External IP: %1", "e.g. External IP: 192.168.0.1").arg(p->external_address.to_string(ec).c_str()), Log::INFO); } +void Session::updateStats() +{ + libt::session_status ss = m_nativeSession->status(); + m_status.hasIncomingConnections = ss.has_incoming_connections; + m_status.payloadDownloadRate = ss.payload_download_rate; + m_status.payloadUploadRate = ss.payload_upload_rate; + m_status.downloadRate = ss.download_rate; + m_status.uploadRate = ss.upload_rate; + m_status.ipOverheadDownloadRate = ss.ip_overhead_download_rate; + m_status.ipOverheadUploadRate = ss.ip_overhead_upload_rate; + m_status.dhtDownloadRate = ss.dht_download_rate; + m_status.dhtUploadRate = ss.dht_upload_rate; + m_status.trackerDownloadRate = ss.tracker_download_rate; + m_status.trackerUploadRate = ss.tracker_upload_rate; + m_status.totalDownload = ss.total_download; + m_status.totalUpload = ss.total_upload; + m_status.totalPayloadDownload = ss.total_payload_download; + m_status.totalPayloadUpload = ss.total_payload_upload; + m_status.totalWasted = ss.total_redundant_bytes + ss.total_failed_bytes; + m_status.diskReadQueue = ss.disk_read_queue; + m_status.diskWriteQueue = ss.disk_write_queue; + m_status.dhtNodes = ss.dht_nodes; + m_status.peersCount = ss.num_peers; + + libt::cache_status cs = m_nativeSession->get_cache_status(); + m_cacheStatus.totalUsedBuffers = cs.total_used_buffers; + m_cacheStatus.readRatio = cs.blocks_read > 0 + ? static_cast(cs.blocks_read_hit) / cs.blocks_read + : -1; +#if LIBTORRENT_VERSION_NUM < 10100 + m_cacheStatus.jobQueueLength = cs.job_queue_length; +#else + m_cacheStatus.jobQueueLength = cs.queued_jobs; +#endif + m_cacheStatus.averageJobTime = cs.average_job_time; + m_cacheStatus.queuedBytes = cs.queued_bytes; + + emit statsUpdated(); +} + void Session::handleStateUpdateAlert(libt::state_update_alert *p) { + updateStats(); + foreach (const libt::torrent_status &status, p->status) { TorrentHandle *const torrent = m_torrents.value(status.info_hash); if (torrent) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index dc7d6cfcd..6ed134056 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -49,6 +49,8 @@ #include "base/tristatebool.h" #include "base/types.h" #include "addtorrentparams.h" +#include "cachestatus.h" +#include "sessionstatus.h" #include "torrentinfo.h" namespace libtorrent @@ -56,15 +58,12 @@ namespace libtorrent class session; struct torrent_handle; class entry; - struct add_torrent_params; struct ip_filter; - struct pe_settings; #if LIBTORRENT_VERSION_NUM < 10100 struct session_settings; #else struct settings_pack; #endif - struct session_status; class alert; struct torrent_alert; @@ -127,8 +126,6 @@ enum TorrentExportFolder namespace BitTorrent { class InfoHash; - class CacheStatus; - class SessionStatus; class TorrentHandle; class Tracker; class MagnetUri; @@ -324,8 +321,8 @@ namespace BitTorrent TorrentStatusReport torrentStatusReport() const; bool hasActiveTorrents() const; bool hasUnfinishedTorrents() const; - SessionStatus status() const; - CacheStatus cacheStatus() const; + const SessionStatus &status() const; + const CacheStatus &cacheStatus() const; quint64 getAlltimeDL() const; quint64 getAlltimeUL() const; bool isListening() const; @@ -371,6 +368,7 @@ namespace BitTorrent void handleTorrentTrackerAuthenticationRequired(TorrentHandle *const torrent, const QString &trackerUrl); signals: + void statsUpdated(); void torrentsUpdated(); void addTorrentFailed(const QString &error); void torrentAdded(BitTorrent::TorrentHandle *const torrent); @@ -485,6 +483,8 @@ namespace BitTorrent #endif void getPendingAlerts(std::vector &out, ulong time = 0); + void updateStats(); + // BitTorrent libtorrent::session *m_nativeSession; @@ -600,6 +600,9 @@ namespace BitTorrent std::vector m_alerts; #endif + SessionStatus m_status; + CacheStatus m_cacheStatus; + QNetworkConfigurationManager m_networkManager; static Session *m_instance; diff --git a/src/base/bittorrent/sessionstatus.cpp b/src/base/bittorrent/sessionstatus.cpp deleted file mode 100644 index 4bfc5a877..000000000 --- a/src/base/bittorrent/sessionstatus.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev - * - * 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. - */ - -#include "sessionstatus.h" - -using namespace BitTorrent; - -SessionStatus::SessionStatus(const libtorrent::session_status &nativeStatus) - : m_nativeStatus(nativeStatus) -{ -} - -bool SessionStatus::hasIncomingConnections() const -{ - return m_nativeStatus.has_incoming_connections; -} - -int SessionStatus::payloadDownloadRate() const -{ - return m_nativeStatus.payload_download_rate; -} - -int SessionStatus::payloadUploadRate() const -{ - return m_nativeStatus.payload_upload_rate; -} - -int SessionStatus::downloadRate() const -{ - return m_nativeStatus.download_rate; -} - -int SessionStatus::uploadRate() const -{ - return m_nativeStatus.upload_rate; -} - -int SessionStatus::ipOverheadDownloadRate() const -{ - return m_nativeStatus.ip_overhead_download_rate; -} - -int SessionStatus::ipOverheadUploadRate() const -{ - return m_nativeStatus.ip_overhead_upload_rate; -} - -int SessionStatus::dhtDownloadRate() const -{ - return m_nativeStatus.dht_download_rate; -} - -int SessionStatus::dhtUploadRate() const -{ - return m_nativeStatus.dht_upload_rate; -} - -int SessionStatus::trackerDownloadRate() const -{ - return m_nativeStatus.tracker_download_rate; -} - -int SessionStatus::trackerUploadRate() const -{ - return m_nativeStatus.tracker_upload_rate; -} - -qlonglong SessionStatus::totalDownload() const -{ - return m_nativeStatus.total_download; -} - -qlonglong SessionStatus::totalUpload() const -{ - return m_nativeStatus.total_upload; -} - -qlonglong SessionStatus::totalPayloadDownload() const -{ - return m_nativeStatus.total_payload_download; -} - -qlonglong SessionStatus::totalPayloadUpload() const -{ - return m_nativeStatus.total_payload_upload; -} - -qlonglong SessionStatus::totalWasted() const -{ - return (m_nativeStatus.total_redundant_bytes + m_nativeStatus.total_failed_bytes); -} - -int SessionStatus::diskReadQueue() const -{ - return m_nativeStatus.disk_read_queue; -} - -int SessionStatus::diskWriteQueue() const -{ - return m_nativeStatus.disk_write_queue; -} - -int SessionStatus::dhtNodes() const -{ - return m_nativeStatus.dht_nodes; -} - -int SessionStatus::peersCount() const -{ - return m_nativeStatus.num_peers; -} diff --git a/src/base/bittorrent/sessionstatus.h b/src/base/bittorrent/sessionstatus.h index e306207c1..d899b8545 100644 --- a/src/base/bittorrent/sessionstatus.h +++ b/src/base/bittorrent/sessionstatus.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2015, 2017 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -29,50 +29,43 @@ #ifndef BITTORRENT_SESSIONSTATUS_H #define BITTORRENT_SESSIONSTATUS_H -#include #include namespace BitTorrent { - class SessionStatus + struct SessionStatus { - public: - SessionStatus(const libtorrent::session_status &nativeStatus); + bool hasIncomingConnections = false; - bool hasIncomingConnections() const; - - // Return current download rate for the BT + // Current download rate for the BT // session. Payload means that it only take into // account "useful" part of the rate - int payloadDownloadRate() const; + quint64 payloadDownloadRate = 0; - // Return current upload rate for the BT + // Current upload rate for the BT // session. Payload means that it only take into // account "useful" part of the rate - int payloadUploadRate() const; + quint64 payloadUploadRate = 0; // Additional download/upload rates - int uploadRate() const; - int downloadRate() const; - int ipOverheadUploadRate() const; - int ipOverheadDownloadRate() const; - int dhtUploadRate() const; - int dhtDownloadRate() const; - int trackerUploadRate() const; - int trackerDownloadRate() const; + quint64 uploadRate = 0; + quint64 downloadRate = 0; + quint64 ipOverheadUploadRate = 0; + quint64 ipOverheadDownloadRate = 0; + quint64 dhtUploadRate = 0; + quint64 dhtDownloadRate = 0; + quint64 trackerUploadRate = 0; + quint64 trackerDownloadRate = 0; - qlonglong totalDownload() const; - qlonglong totalUpload() const; - qlonglong totalPayloadDownload() const; - qlonglong totalPayloadUpload() const; - qlonglong totalWasted() const; - int diskReadQueue() const; - int diskWriteQueue() const; - int dhtNodes() const; - int peersCount() const; - - private: - libtorrent::session_status m_nativeStatus; + quint64 totalDownload = 0; + quint64 totalUpload = 0; + quint64 totalPayloadDownload = 0; + quint64 totalPayloadUpload = 0; + quint64 totalWasted = 0; + quint64 diskReadQueue = 0; + quint64 diskWriteQueue = 0; + quint64 dhtNodes = 0; + quint64 peersCount = 0; }; } diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 314d14455..9632e2988 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1319,7 +1319,7 @@ void MainWindow::trackerAuthenticationRequired(BitTorrent::TorrentHandle *const // Check connection status and display right icon void MainWindow::updateGUI() { - BitTorrent::SessionStatus status = BitTorrent::Session::instance()->status(); + const BitTorrent::SessionStatus &status = BitTorrent::Session::instance()->status(); // update global informations if (m_systrayIcon) { @@ -1328,24 +1328,24 @@ void MainWindow::updateGUI() html += "qBittorrent"; html += ""; html += "
"; - html += " " + tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate(), true)); + html += " " + tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate, true)); html += "
"; html += "
"; - html += " " + tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadUploadRate(), true)); + html += " " + tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadUploadRate, true)); html += "
"; #else // OSes such as Windows do not support html here - QString html = tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate(), true)); + QString html = tr("DL speed: %1", "e.g: Download speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate, true)); html += "\n"; - html += tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadUploadRate(), true)); + html += tr("UP speed: %1", "e.g: Upload speed: 10 KiB/s").arg(Utils::Misc::friendlyUnit(status.payloadUploadRate, true)); #endif m_systrayIcon->setToolTip(html); // tray icon } if (m_displaySpeedInTitle) { setWindowTitle(tr("[D: %1, U: %2] qBittorrent %3", "D = Download; U = Upload; %3 is qBittorrent version") - .arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate(), true)) - .arg(Utils::Misc::friendlyUnit(status.payloadUploadRate(), true)) + .arg(Utils::Misc::friendlyUnit(status.payloadDownloadRate, true)) + .arg(Utils::Misc::friendlyUnit(status.payloadUploadRate, true)) .arg(QBT_VERSION)); } } diff --git a/src/gui/properties/speedwidget.cpp b/src/gui/properties/speedwidget.cpp index 9b159da24..daba84043 100644 --- a/src/gui/properties/speedwidget.cpp +++ b/src/gui/properties/speedwidget.cpp @@ -45,7 +45,8 @@ ComboBoxMenuButton::ComboBoxMenuButton(QWidget *parent, QMenu *menu) : QComboBox(parent) , m_menu(menu) -{} +{ +} void ComboBoxMenuButton::showPopup() { @@ -134,20 +135,20 @@ void SpeedWidget::update() { while (m_isUpdating) { - BitTorrent::SessionStatus btStatus = BitTorrent::Session::instance()->status(); + const BitTorrent::SessionStatus &btStatus = BitTorrent::Session::instance()->status(); SpeedPlotView::PointData point; point.x = QDateTime::currentDateTime().toTime_t(); - point.y[SpeedPlotView::UP] = btStatus.uploadRate(); - point.y[SpeedPlotView::DOWN] = btStatus.downloadRate(); - point.y[SpeedPlotView::PAYLOAD_UP] = btStatus.payloadUploadRate(); - point.y[SpeedPlotView::PAYLOAD_DOWN] = btStatus.payloadDownloadRate(); - point.y[SpeedPlotView::OVERHEAD_UP] = btStatus.ipOverheadUploadRate(); - point.y[SpeedPlotView::OVERHEAD_DOWN] = btStatus.ipOverheadDownloadRate(); - point.y[SpeedPlotView::DHT_UP] = btStatus.dhtUploadRate(); - point.y[SpeedPlotView::DHT_DOWN] = btStatus.dhtDownloadRate(); - point.y[SpeedPlotView::TRACKER_UP] = btStatus.trackerUploadRate(); - point.y[SpeedPlotView::TRACKER_DOWN] = btStatus.trackerDownloadRate(); + point.y[SpeedPlotView::UP] = btStatus.uploadRate; + point.y[SpeedPlotView::DOWN] = btStatus.downloadRate; + point.y[SpeedPlotView::PAYLOAD_UP] = btStatus.payloadUploadRate; + point.y[SpeedPlotView::PAYLOAD_DOWN] = btStatus.payloadDownloadRate; + point.y[SpeedPlotView::OVERHEAD_UP] = btStatus.ipOverheadUploadRate; + point.y[SpeedPlotView::OVERHEAD_DOWN] = btStatus.ipOverheadDownloadRate; + point.y[SpeedPlotView::DHT_UP] = btStatus.dhtUploadRate; + point.y[SpeedPlotView::DHT_DOWN] = btStatus.dhtDownloadRate; + point.y[SpeedPlotView::TRACKER_UP] = btStatus.trackerUploadRate; + point.y[SpeedPlotView::TRACKER_DOWN] = btStatus.trackerDownloadRate; m_plot->pushPoint(point); diff --git a/src/gui/statsdialog.cpp b/src/gui/statsdialog.cpp index e54a1339f..50432db33 100644 --- a/src/gui/statsdialog.cpp +++ b/src/gui/statsdialog.cpp @@ -45,25 +45,21 @@ StatsDialog::StatsDialog(QWidget *parent) connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &StatsDialog::close); update(); - m_timer = new QTimer(this); - m_timer->setInterval(1500); - connect(m_timer, &QTimer::timeout, this, &StatsDialog::update); - m_timer->start(); + connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated + , this, &StatsDialog::update); show(); } StatsDialog::~StatsDialog() { - m_timer->stop(); - delete m_timer; delete m_ui; } void StatsDialog::update() { - BitTorrent::SessionStatus ss = BitTorrent::Session::instance()->status(); - BitTorrent::CacheStatus cs = BitTorrent::Session::instance()->cacheStatus(); + const BitTorrent::SessionStatus &ss = BitTorrent::Session::instance()->status(); + const BitTorrent::CacheStatus &cs = BitTorrent::Session::instance()->cacheStatus(); // Alltime DL/UL quint64 atd = BitTorrent::Session::instance()->getAlltimeDL(); @@ -71,17 +67,17 @@ void StatsDialog::update() m_ui->labelAlltimeDL->setText(Utils::Misc::friendlyUnit(atd)); m_ui->labelAlltimeUL->setText(Utils::Misc::friendlyUnit(atu)); // Total waste (this session) - m_ui->labelWaste->setText(Utils::Misc::friendlyUnit(ss.totalWasted())); + m_ui->labelWaste->setText(Utils::Misc::friendlyUnit(ss.totalWasted)); // Global ratio m_ui->labelGlobalRatio->setText( ((atd > 0) && (atu > 0)) ? Utils::String::fromDouble((qreal)atu / (qreal)atd, 2) : "-"); // Cache hits - qreal readRatio = cs.readRatio(); + qreal readRatio = cs.readRatio; m_ui->labelCacheHits->setText((readRatio >= 0) ? Utils::String::fromDouble(100 * readRatio, 2) : "-"); // Buffers size - m_ui->labelTotalBuf->setText(Utils::Misc::friendlyUnit(cs.totalUsedBuffers() * 16 * 1024)); + m_ui->labelTotalBuf->setText(Utils::Misc::friendlyUnit(cs.totalUsedBuffers * 16 * 1024)); // Disk overload (100%) equivalent // From lt manual: disk_write_queue and disk_read_queue are the number of peers currently waiting on a disk write or disk read // to complete before it receives or sends any more data on the socket. It's a metric of how disk bound you are. @@ -92,18 +88,18 @@ void StatsDialog::update() peers += torrent->peersCount(); m_ui->labelWriteStarve->setText(QString("%1%") - .arg(((ss.diskWriteQueue() > 0) && (peers > 0)) - ? Utils::String::fromDouble((100. * ss.diskWriteQueue()) / peers, 2) + .arg(((ss.diskWriteQueue > 0) && (peers > 0)) + ? Utils::String::fromDouble((100. * ss.diskWriteQueue) / peers, 2) : "0")); m_ui->labelReadStarve->setText(QString("%1%") - .arg(((ss.diskReadQueue() > 0) && (peers > 0)) - ? Utils::String::fromDouble((100. * ss.diskReadQueue()) / peers, 2) + .arg(((ss.diskReadQueue > 0) && (peers > 0)) + ? Utils::String::fromDouble((100. * ss.diskReadQueue) / peers, 2) : "0")); // Disk queues - m_ui->labelQueuedJobs->setText(QString::number(cs.jobQueueLength())); - m_ui->labelJobsTime->setText(tr("%1 ms", "18 milliseconds").arg(cs.averageJobTime())); - m_ui->labelQueuedBytes->setText(Utils::Misc::friendlyUnit(cs.queuedBytes())); + m_ui->labelQueuedJobs->setText(QString::number(cs.jobQueueLength)); + m_ui->labelJobsTime->setText(tr("%1 ms", "18 milliseconds").arg(cs.averageJobTime)); + m_ui->labelQueuedBytes->setText(Utils::Misc::friendlyUnit(cs.queuedBytes)); // Total connected peers - m_ui->labelPeers->setText(QString::number(ss.peersCount())); + m_ui->labelPeers->setText(QString::number(ss.peersCount)); } diff --git a/src/gui/statsdialog.h b/src/gui/statsdialog.h index 9f2602c5c..22652ebda 100644 --- a/src/gui/statsdialog.h +++ b/src/gui/statsdialog.h @@ -30,7 +30,6 @@ #define STATSDIALOG_H #include -#include namespace Ui { @@ -50,7 +49,6 @@ private slots: private: Ui::StatsDialog *m_ui; - QTimer *m_timer; }; #endif // STATSDIALOG_H diff --git a/src/gui/statusbar.cpp b/src/gui/statusbar.cpp index 6f5144c7d..f8d55ed73 100644 --- a/src/gui/statusbar.cpp +++ b/src/gui/statusbar.cpp @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include @@ -135,10 +134,9 @@ StatusBar::StatusBar(QStatusBar *bar) bar->adjustSize(); // Is DHT enabled m_DHTLbl->setVisible(session->isDHTEnabled()); - m_refreshTimer = new QTimer(bar); refreshStatusBar(); - connect(m_refreshTimer, SIGNAL(timeout()), this, SLOT(refreshStatusBar())); - m_refreshTimer->start(1500); + connect(BitTorrent::Session::instance(), &BitTorrent::Session::statsUpdated + , this, &StatusBar::refreshStatusBar); } StatusBar::~StatusBar() @@ -167,19 +165,16 @@ void StatusBar::showRestartRequired() Logger::instance()->addMessage(tr("qBittorrent was just updated and needs to be restarted for the changes to be effective."), Log::CRITICAL); } -void StatusBar::stopTimer() +void StatusBar::updateConnectionStatus() { - m_refreshTimer->stop(); -} + const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status(); -void StatusBar::updateConnectionStatus(const BitTorrent::SessionStatus &sessionStatus) -{ if (!BitTorrent::Session::instance()->isListening()) { m_connecStatusLblIcon->setIcon(QIcon(QLatin1String(":/icons/skin/disconnected.png"))); m_connecStatusLblIcon->setToolTip(QLatin1String("") + tr("Connection Status:") + QLatin1String("
") + tr("Offline. This usually means that qBittorrent failed to listen on the selected port for incoming connections.")); } else { - if (sessionStatus.hasIncomingConnections()) { + if (sessionStatus.hasIncomingConnections) { // Connection OK m_connecStatusLblIcon->setIcon(QIcon(QLatin1String(":/icons/skin/connected.png"))); m_connecStatusLblIcon->setToolTip(QLatin1String("") + tr("Connection Status:") + QLatin1String("
") + tr("Online")); @@ -191,39 +186,41 @@ void StatusBar::updateConnectionStatus(const BitTorrent::SessionStatus &sessionS } } -void StatusBar::updateDHTNodesNumber(const BitTorrent::SessionStatus &sessionStatus) +void StatusBar::updateDHTNodesNumber() { if (BitTorrent::Session::instance()->isDHTEnabled()) { m_DHTLbl->setVisible(true); - m_DHTLbl->setText(tr("DHT: %1 nodes").arg(QString::number(sessionStatus.dhtNodes()))); + m_DHTLbl->setText(tr("DHT: %1 nodes") + .arg(QString::number(BitTorrent::Session::instance()->status().dhtNodes))); } else { m_DHTLbl->setVisible(false); } } -void StatusBar::updateSpeedLabels(const BitTorrent::SessionStatus &sessionStatus) +void StatusBar::updateSpeedLabels() { - QString speedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadDownloadRate(), true); + const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status(); + + QString speedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadDownloadRate, true); int speedLimit = BitTorrent::Session::instance()->downloadSpeedLimit(); if (speedLimit) speedLbl += " [" + Utils::Misc::friendlyUnit(speedLimit, true) + "]"; - speedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadDownload()) + ")"; + speedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadDownload) + ")"; m_dlSpeedLbl->setText(speedLbl); speedLimit = BitTorrent::Session::instance()->uploadSpeedLimit(); - speedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadUploadRate(), true); + speedLbl = Utils::Misc::friendlyUnit(sessionStatus.payloadUploadRate, true); if (speedLimit) speedLbl += " [" + Utils::Misc::friendlyUnit(speedLimit, true) + "]"; - speedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadUpload()) + ")"; + speedLbl += " (" + Utils::Misc::friendlyUnit(sessionStatus.totalPayloadUpload) + ")"; m_upSpeedLbl->setText(speedLbl); } void StatusBar::refreshStatusBar() { - const BitTorrent::SessionStatus sessionStatus = BitTorrent::Session::instance()->status(); - updateConnectionStatus(sessionStatus); - updateDHTNodesNumber(sessionStatus); - updateSpeedLabels(sessionStatus); + updateConnectionStatus(); + updateDHTNodesNumber(); + updateSpeedLabels(); } void StatusBar::updateAltSpeedsBtn(bool alternative) diff --git a/src/gui/statusbar.h b/src/gui/statusbar.h index 5efea8a5d..34eb9a021 100644 --- a/src/gui/statusbar.h +++ b/src/gui/statusbar.h @@ -1,6 +1,6 @@ /* - * Bittorrent Client using Qt4 and libtorrent. - * Copyright (C) 2006 Christophe Dumez + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2006 Christophe Dumez * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -24,8 +24,6 @@ * 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. - * - * Contact : chris@qbittorrent.org */ #ifndef STATUSBAR_H @@ -36,13 +34,12 @@ class QStatusBar; class QFrame; class QLabel; -class QTimer; class QPushButton; class QHBoxLayout; namespace BitTorrent { - class SessionStatus; + struct SessionStatus; } class StatusBar: public QObject @@ -57,7 +54,6 @@ public: public slots: void showRestartRequired(); - void stopTimer(); void refreshStatusBar(); void updateAltSpeedsBtn(bool alternative); void toggleAlternativeSpeeds(); @@ -65,6 +61,10 @@ public slots: void capUploadSpeed(); private: + void updateConnectionStatus(); + void updateDHTNodesNumber(); + void updateSpeedLabels(); + QStatusBar *m_bar; QPushButton *m_dlSpeedLbl; QPushButton *m_upSpeedLbl; @@ -75,13 +75,8 @@ private: QFrame *m_statusSep4; QPushButton *m_connecStatusLblIcon; QPushButton *m_altSpeedsBtn; - QTimer *m_refreshTimer; QWidget *m_container; QHBoxLayout *m_layout; - - void updateConnectionStatus(const BitTorrent::SessionStatus &sessionStatus); - void updateDHTNodesNumber(const BitTorrent::SessionStatus &sessionStatus); - void updateSpeedLabels(const BitTorrent::SessionStatus &sessionStatus); }; #endif // STATUSBAR_H diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 7ad9eb152..404279e39 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -676,12 +676,12 @@ QByteArray btjson::getTransferInfo() QVariantMap getTranserInfoMap() { QVariantMap map; - BitTorrent::SessionStatus sessionStatus = BitTorrent::Session::instance()->status(); - BitTorrent::CacheStatus cacheStatus = BitTorrent::Session::instance()->cacheStatus(); - map[KEY_TRANSFER_DLSPEED] = sessionStatus.payloadDownloadRate(); - map[KEY_TRANSFER_DLDATA] = sessionStatus.totalPayloadDownload(); - map[KEY_TRANSFER_UPSPEED] = sessionStatus.payloadUploadRate(); - map[KEY_TRANSFER_UPDATA] = sessionStatus.totalPayloadUpload(); + const BitTorrent::SessionStatus &sessionStatus = BitTorrent::Session::instance()->status(); + const BitTorrent::CacheStatus &cacheStatus = BitTorrent::Session::instance()->cacheStatus(); + map[KEY_TRANSFER_DLSPEED] = sessionStatus.payloadDownloadRate; + map[KEY_TRANSFER_DLDATA] = sessionStatus.totalPayloadDownload; + map[KEY_TRANSFER_UPSPEED] = sessionStatus.payloadUploadRate; + map[KEY_TRANSFER_UPDATA] = sessionStatus.totalPayloadUpload; map[KEY_TRANSFER_DLRATELIMIT] = BitTorrent::Session::instance()->downloadSpeedLimit(); map[KEY_TRANSFER_UPRATELIMIT] = BitTorrent::Session::instance()->uploadSpeedLimit(); @@ -689,30 +689,30 @@ QVariantMap getTranserInfoMap() quint64 atu = BitTorrent::Session::instance()->getAlltimeUL(); map[KEY_TRANSFER_ALLTIME_DL] = atd; map[KEY_TRANSFER_ALLTIME_UL] = atu; - map[KEY_TRANSFER_TOTAL_WASTE_SESSION] = sessionStatus.totalWasted(); + map[KEY_TRANSFER_TOTAL_WASTE_SESSION] = sessionStatus.totalWasted; map[KEY_TRANSFER_GLOBAL_RATIO] = ( atd > 0 && atu > 0 ) ? Utils::String::fromDouble((qreal)atu / (qreal)atd, 2) : "-"; - map[KEY_TRANSFER_TOTAL_PEER_CONNECTIONS] = sessionStatus.peersCount(); + map[KEY_TRANSFER_TOTAL_PEER_CONNECTIONS] = sessionStatus.peersCount; - qreal readRatio = cacheStatus.readRatio(); + qreal readRatio = cacheStatus.readRatio; map[KEY_TRANSFER_READ_CACHE_HITS] = (readRatio >= 0) ? Utils::String::fromDouble(100 * readRatio, 2) : "-"; - map[KEY_TRANSFER_TOTAL_BUFFERS_SIZE] = cacheStatus.totalUsedBuffers() * 16 * 1024; + map[KEY_TRANSFER_TOTAL_BUFFERS_SIZE] = cacheStatus.totalUsedBuffers * 16 * 1024; // num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake) quint32 peers = 0; foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents()) peers += torrent->peersCount(); - map[KEY_TRANSFER_WRITE_CACHE_OVERLOAD] = ((sessionStatus.diskWriteQueue() > 0) && (peers > 0)) ? Utils::String::fromDouble((100. * sessionStatus.diskWriteQueue()) / peers, 2) : "0"; - map[KEY_TRANSFER_READ_CACHE_OVERLOAD] = ((sessionStatus.diskReadQueue() > 0) && (peers > 0)) ? Utils::String::fromDouble((100. * sessionStatus.diskReadQueue()) / peers, 2) : "0"; + map[KEY_TRANSFER_WRITE_CACHE_OVERLOAD] = ((sessionStatus.diskWriteQueue > 0) && (peers > 0)) ? Utils::String::fromDouble((100. * sessionStatus.diskWriteQueue) / peers, 2) : "0"; + map[KEY_TRANSFER_READ_CACHE_OVERLOAD] = ((sessionStatus.diskReadQueue > 0) && (peers > 0)) ? Utils::String::fromDouble((100. * sessionStatus.diskReadQueue) / peers, 2) : "0"; - map[KEY_TRANSFER_QUEUED_IO_JOBS] = cacheStatus.jobQueueLength(); - map[KEY_TRANSFER_AVERAGE_TIME_QUEUE] = cacheStatus.averageJobTime(); - map[KEY_TRANSFER_TOTAL_QUEUED_SIZE] = cacheStatus.queuedBytes(); + map[KEY_TRANSFER_QUEUED_IO_JOBS] = cacheStatus.jobQueueLength; + map[KEY_TRANSFER_AVERAGE_TIME_QUEUE] = cacheStatus.averageJobTime; + map[KEY_TRANSFER_TOTAL_QUEUED_SIZE] = cacheStatus.queuedBytes; - map[KEY_TRANSFER_DHT_NODES] = sessionStatus.dhtNodes(); + map[KEY_TRANSFER_DHT_NODES] = sessionStatus.dhtNodes; if (!BitTorrent::Session::instance()->isListening()) map[KEY_TRANSFER_CONNECTION_STATUS] = "disconnected"; else - map[KEY_TRANSFER_CONNECTION_STATUS] = sessionStatus.hasIncomingConnections() ? "connected" : "firewalled"; + map[KEY_TRANSFER_CONNECTION_STATUS] = sessionStatus.hasIncomingConnections ? "connected" : "firewalled"; return map; } From bdca55f15c7dd2ed2cd748a35696e99d16a96226 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sat, 29 Apr 2017 14:52:28 +0300 Subject: [PATCH 04/12] Use new Session Statistics feature --- src/base/bittorrent/private/statistics.h | 4 +- src/base/bittorrent/session.cpp | 158 ++++++++++++++++++++++- src/base/bittorrent/session.h | 59 ++++++++- src/base/bittorrent/sessionstatus.h | 6 + src/gui/statusbar.cpp | 2 +- 5 files changed, 219 insertions(+), 10 deletions(-) diff --git a/src/base/bittorrent/private/statistics.h b/src/base/bittorrent/private/statistics.h index 64b4015fc..c012c409e 100644 --- a/src/base/bittorrent/private/statistics.h +++ b/src/base/bittorrent/private/statistics.h @@ -30,8 +30,8 @@ private: // Will overflow at 15.9 EiB quint64 m_alltimeUL; quint64 m_alltimeDL; - qint64 m_sessionUL; - qint64 m_sessionDL; + quint64 m_sessionUL; + quint64 m_sessionDL; mutable qint64 m_lastWrite; mutable bool m_dirty; diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index e4236bf81..2343fa225 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -64,6 +64,9 @@ #endif #include #include +#if LIBTORRENT_VERSION_NUM >= 10100 +#include +#endif #include #include @@ -440,6 +443,11 @@ Session::Session(QObject *parent) // initialize PortForwarder instance Net::PortForwarder::initInstance(m_nativeSession); +#if LIBTORRENT_VERSION_NUM >= 10100 + initMetrics(); + m_statsUpdateTimer.start(); +#endif + qDebug("* BitTorrent Session constructed"); } @@ -920,6 +928,75 @@ void Session::adjustLimits(libt::settings_pack &settingsPack) , maxActive > -1 ? maxActive + m_extraLimit : maxActive); } +void Session::initMetrics() +{ + m_metricIndices.net.hasIncomingConnections = libt::find_metric_idx("net.has_incoming_connections"); + Q_ASSERT(m_metricIndices.net.hasIncomingConnections >= 0); + + m_metricIndices.net.sentPayloadBytes = libt::find_metric_idx("net.sent_payload_bytes"); + Q_ASSERT(m_metricIndices.net.sentPayloadBytes >= 0); + + m_metricIndices.net.recvPayloadBytes = libt::find_metric_idx("net.recv_payload_bytes"); + Q_ASSERT(m_metricIndices.net.recvPayloadBytes >= 0); + + m_metricIndices.net.sentBytes = libt::find_metric_idx("net.sent_bytes"); + Q_ASSERT(m_metricIndices.net.sentBytes >= 0); + + m_metricIndices.net.recvBytes = libt::find_metric_idx("net.recv_bytes"); + Q_ASSERT(m_metricIndices.net.recvBytes >= 0); + + m_metricIndices.net.sentIPOverheadBytes = libt::find_metric_idx("net.sent_ip_overhead_bytes"); + Q_ASSERT(m_metricIndices.net.sentIPOverheadBytes >= 0); + + m_metricIndices.net.recvIPOverheadBytes = libt::find_metric_idx("net.recv_ip_overhead_bytes"); + Q_ASSERT(m_metricIndices.net.recvIPOverheadBytes >= 0); + + m_metricIndices.net.sentTrackerBytes = libt::find_metric_idx("net.sent_tracker_bytes"); + Q_ASSERT(m_metricIndices.net.sentTrackerBytes >= 0); + + m_metricIndices.net.recvTrackerBytes = libt::find_metric_idx("net.recv_tracker_bytes"); + Q_ASSERT(m_metricIndices.net.recvTrackerBytes >= 0); + + m_metricIndices.net.recvRedundantBytes = libt::find_metric_idx("net.recv_redundant_bytes"); + Q_ASSERT(m_metricIndices.net.recvRedundantBytes >= 0); + + m_metricIndices.net.recvFailedBytes = libt::find_metric_idx("net.recv_failed_bytes"); + Q_ASSERT(m_metricIndices.net.recvFailedBytes >= 0); + + m_metricIndices.peer.numPeersConnected = libt::find_metric_idx("peer.num_peers_connected"); + Q_ASSERT(m_metricIndices.peer.numPeersConnected >= 0); + + m_metricIndices.peer.numPeersDownDisk = libt::find_metric_idx("peer.num_peers_down_disk"); + Q_ASSERT(m_metricIndices.peer.numPeersDownDisk >= 0); + + m_metricIndices.peer.numPeersUpDisk = libt::find_metric_idx("peer.num_peers_up_disk"); + Q_ASSERT(m_metricIndices.peer.numPeersUpDisk >= 0); + + m_metricIndices.dht.dhtBytesIn = libt::find_metric_idx("dht.dht_bytes_in"); + Q_ASSERT(m_metricIndices.dht.dhtBytesIn >= 0); + + m_metricIndices.dht.dhtBytesOut = libt::find_metric_idx("dht.dht_bytes_out"); + Q_ASSERT(m_metricIndices.dht.dhtBytesOut >= 0); + + m_metricIndices.dht.dhtNodes = libt::find_metric_idx("dht.dht_nodes"); + Q_ASSERT(m_metricIndices.dht.dhtNodes >= 0); + + m_metricIndices.disk.diskBlocksInUse = libt::find_metric_idx("disk.disk_blocks_in_use"); + Q_ASSERT(m_metricIndices.disk.diskBlocksInUse >= 0); + + m_metricIndices.disk.numBlocksRead = libt::find_metric_idx("disk.num_blocks_read"); + Q_ASSERT(m_metricIndices.disk.numBlocksRead >= 0); + + m_metricIndices.disk.numBlocksCacheHits = libt::find_metric_idx("disk.num_blocks_cache_hits"); + Q_ASSERT(m_metricIndices.disk.numBlocksCacheHits >= 0); + + m_metricIndices.disk.queuedDiskJobs = libt::find_metric_idx("disk.queued_disk_jobs"); + Q_ASSERT(m_metricIndices.disk.queuedDiskJobs >= 0); + + m_metricIndices.disk.diskJobTime = libt::find_metric_idx("disk.disk_job_time"); + Q_ASSERT(m_metricIndices.disk.diskJobTime >= 0); +} + void Session::configure(libtorrent::settings_pack &settingsPack) { Logger* const logger = Logger::instance(); @@ -3196,6 +3273,9 @@ quint64 Session::getAlltimeUL() const void Session::refresh() { m_nativeSession->post_torrent_updates(); +#if LIBTORRENT_VERSION_NUM >= 10100 + m_nativeSession->post_session_stats(); +#endif } void Session::handleIPFilterParsed(int ruleCount) @@ -3304,6 +3384,11 @@ void Session::handleAlert(libt::alert *a) case libt::state_update_alert::alert_type: handleStateUpdateAlert(static_cast(a)); break; +#if LIBTORRENT_VERSION_NUM >= 10100 + case libt::session_stats_alert::alert_type: + handleSessionStatsAlert(static_cast(a)); + break; +#endif case libt::file_error_alert::alert_type: handleFileErrorAlert(static_cast(a)); break; @@ -3581,6 +3666,69 @@ void Session::handleExternalIPAlert(libt::external_ip_alert *p) Logger::instance()->addMessage(tr("External IP: %1", "e.g. External IP: 192.168.0.1").arg(p->external_address.to_string(ec).c_str()), Log::INFO); } +#if LIBTORRENT_VERSION_NUM >= 10100 +void Session::handleSessionStatsAlert(libt::session_stats_alert *p) +{ + qreal interval = m_statsUpdateTimer.restart() / 1000.; + + m_status.hasIncomingConnections = static_cast(p->values[m_metricIndices.net.hasIncomingConnections]); + + const auto ipOverheadDownload = p->values[m_metricIndices.net.recvIPOverheadBytes]; + const auto ipOverheadUpload = p->values[m_metricIndices.net.sentIPOverheadBytes]; + const auto totalDownload = p->values[m_metricIndices.net.recvBytes] + ipOverheadDownload; + const auto totalUpload = p->values[m_metricIndices.net.sentBytes] + ipOverheadUpload; + const auto totalPayloadDownload = p->values[m_metricIndices.net.recvPayloadBytes]; + const auto totalPayloadUpload = p->values[m_metricIndices.net.sentPayloadBytes]; + const auto trackerDownload = p->values[m_metricIndices.net.recvTrackerBytes]; + const auto trackerUpload = p->values[m_metricIndices.net.sentTrackerBytes]; + const auto dhtDownload = p->values[m_metricIndices.dht.dhtBytesIn]; + const auto dhtUpload = p->values[m_metricIndices.dht.dhtBytesOut]; + + auto calcRate = [interval](quint64 previous, quint64 current) + { + Q_ASSERT(current >= previous); + return static_cast((current - previous) / interval); + }; + + m_status.payloadDownloadRate = calcRate(m_status.totalPayloadDownload, totalPayloadDownload); + m_status.payloadUploadRate = calcRate(m_status.totalPayloadUpload, totalPayloadUpload); + m_status.downloadRate = calcRate(m_status.totalDownload, totalDownload); + m_status.uploadRate = calcRate(m_status.totalUpload, totalUpload); + m_status.ipOverheadDownloadRate = calcRate(m_status.ipOverheadDownload, ipOverheadDownload); + m_status.ipOverheadUploadRate = calcRate(m_status.ipOverheadUpload, ipOverheadUpload); + m_status.dhtDownloadRate = calcRate(m_status.dhtDownload, dhtDownload); + m_status.dhtUploadRate = calcRate(m_status.dhtUpload, dhtUpload); + m_status.trackerDownloadRate = calcRate(m_status.trackerDownload, trackerDownload); + m_status.trackerUploadRate = calcRate(m_status.trackerUpload, trackerUpload); + + m_status.totalDownload = totalDownload; + m_status.totalUpload = totalUpload; + m_status.totalPayloadDownload = totalPayloadDownload; + m_status.totalPayloadUpload = totalPayloadUpload; + m_status.ipOverheadDownload = ipOverheadDownload; + m_status.ipOverheadUpload = ipOverheadUpload; + m_status.trackerDownload = trackerDownload; + m_status.trackerUpload = trackerUpload; + m_status.dhtDownload = dhtDownload; + m_status.dhtUpload = dhtUpload; + m_status.totalWasted = p->values[m_metricIndices.net.recvRedundantBytes] + + p->values[m_metricIndices.net.recvFailedBytes]; + m_status.dhtNodes = p->values[m_metricIndices.dht.dhtNodes]; + m_status.diskReadQueue = p->values[m_metricIndices.peer.numPeersUpDisk]; + m_status.diskWriteQueue = p->values[m_metricIndices.peer.numPeersDownDisk]; + m_status.peersCount = p->values[m_metricIndices.peer.numPeersConnected]; + + const auto numBlocksRead = p->values[m_metricIndices.disk.numBlocksRead]; + m_cacheStatus.totalUsedBuffers = p->values[m_metricIndices.disk.diskBlocksInUse]; + m_cacheStatus.readRatio = numBlocksRead > 0 + ? static_cast(p->values[m_metricIndices.disk.numBlocksCacheHits]) / numBlocksRead + : -1; + m_cacheStatus.jobQueueLength = p->values[m_metricIndices.disk.queuedDiskJobs]; + m_cacheStatus.averageJobTime = p->values[m_metricIndices.disk.diskJobTime]; + + emit statsUpdated(); +} +#else void Session::updateStats() { libt::session_status ss = m_nativeSession->status(); @@ -3595,6 +3743,7 @@ void Session::updateStats() m_status.dhtUploadRate = ss.dht_upload_rate; m_status.trackerDownloadRate = ss.tracker_download_rate; m_status.trackerUploadRate = ss.tracker_upload_rate; + m_status.totalDownload = ss.total_download; m_status.totalUpload = ss.total_upload; m_status.totalPayloadDownload = ss.total_payload_download; @@ -3610,20 +3759,19 @@ void Session::updateStats() m_cacheStatus.readRatio = cs.blocks_read > 0 ? static_cast(cs.blocks_read_hit) / cs.blocks_read : -1; -#if LIBTORRENT_VERSION_NUM < 10100 m_cacheStatus.jobQueueLength = cs.job_queue_length; -#else - m_cacheStatus.jobQueueLength = cs.queued_jobs; -#endif m_cacheStatus.averageJobTime = cs.average_job_time; - m_cacheStatus.queuedBytes = cs.queued_bytes; + m_cacheStatus.queuedBytes = cs.queued_bytes; // it seems that it is constantly equal to zero emit statsUpdated(); } +#endif void Session::handleStateUpdateAlert(libt::state_update_alert *p) { +#if LIBTORRENT_VERSION_NUM < 10100 updateStats(); +#endif foreach (const libt::torrent_status &status, p->status) { TorrentHandle *const torrent = m_torrents.value(status.info_hash); diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 6ed134056..352649c8c 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -33,6 +33,9 @@ #include #include +#if LIBTORRENT_VERSION_NUM >= 10100 +#include +#endif #include #include #include @@ -97,6 +100,9 @@ namespace libtorrent struct listen_succeeded_alert; struct listen_failed_alert; struct external_ip_alert; +#if LIBTORRENT_VERSION_NUM >= 10100 + struct session_stats_alert; +#endif } class QThread; @@ -144,6 +150,49 @@ namespace BitTorrent uint nbErrored = 0; }; +#if LIBTORRENT_VERSION_NUM >= 10100 + struct SessionMetricIndices + { + struct + { + int hasIncomingConnections = 0; + int sentPayloadBytes = 0; + int recvPayloadBytes = 0; + int sentBytes = 0; + int recvBytes = 0; + int sentIPOverheadBytes = 0; + int recvIPOverheadBytes = 0; + int sentTrackerBytes = 0; + int recvTrackerBytes = 0; + int recvRedundantBytes = 0; + int recvFailedBytes = 0; + } net; + + struct + { + int numPeersConnected = 0; + int numPeersUpDisk = 0; + int numPeersDownDisk = 0; + } peer; + + struct + { + int dhtBytesIn = 0; + int dhtBytesOut = 0; + int dhtNodes = 0; + } dht; + + struct + { + int diskBlocksInUse = 0; + int numBlocksRead = 0; + int numBlocksCacheHits = 0; + int queuedDiskJobs = 0; + int diskJobTime = 0; + } disk; + }; +#endif + class Session : public QObject { Q_OBJECT @@ -435,6 +484,7 @@ namespace BitTorrent #else void configure(libtorrent::settings_pack &settingsPack); void adjustLimits(libtorrent::settings_pack &settingsPack); + void initMetrics(); #endif void adjustLimits(); void processBannedIPs(libtorrent::ip_filter &filter); @@ -473,6 +523,9 @@ namespace BitTorrent void handleListenSucceededAlert(libtorrent::listen_succeeded_alert *p); void handleListenFailedAlert(libtorrent::listen_failed_alert *p); void handleExternalIPAlert(libtorrent::external_ip_alert *p); +#if LIBTORRENT_VERSION_NUM >= 10100 + void handleSessionStatsAlert(libtorrent::session_stats_alert *p); +#endif void createTorrentHandle(const libtorrent::torrent_handle &nativeHandle); @@ -480,11 +533,10 @@ namespace BitTorrent #if LIBTORRENT_VERSION_NUM < 10100 void dispatchAlerts(libtorrent::alert *alertPtr); + void updateStats(); #endif void getPendingAlerts(std::vector &out, ulong time = 0); - void updateStats(); - // BitTorrent libtorrent::session *m_nativeSession; @@ -598,6 +650,9 @@ namespace BitTorrent QMutex m_alertsMutex; QWaitCondition m_alertsWaitCondition; std::vector m_alerts; +#else + SessionMetricIndices m_metricIndices; + QElapsedTimer m_statsUpdateTimer; #endif SessionStatus m_status; diff --git a/src/base/bittorrent/sessionstatus.h b/src/base/bittorrent/sessionstatus.h index d899b8545..1426786ba 100644 --- a/src/base/bittorrent/sessionstatus.h +++ b/src/base/bittorrent/sessionstatus.h @@ -61,6 +61,12 @@ namespace BitTorrent quint64 totalUpload = 0; quint64 totalPayloadDownload = 0; quint64 totalPayloadUpload = 0; + quint64 ipOverheadUpload = 0; + quint64 ipOverheadDownload = 0; + quint64 dhtUpload = 0; + quint64 dhtDownload = 0; + quint64 trackerUpload = 0; + quint64 trackerDownload = 0; quint64 totalWasted = 0; quint64 diskReadQueue = 0; quint64 diskWriteQueue = 0; diff --git a/src/gui/statusbar.cpp b/src/gui/statusbar.cpp index f8d55ed73..2e523d6d6 100644 --- a/src/gui/statusbar.cpp +++ b/src/gui/statusbar.cpp @@ -191,7 +191,7 @@ void StatusBar::updateDHTNodesNumber() if (BitTorrent::Session::instance()->isDHTEnabled()) { m_DHTLbl->setVisible(true); m_DHTLbl->setText(tr("DHT: %1 nodes") - .arg(QString::number(BitTorrent::Session::instance()->status().dhtNodes))); + .arg(BitTorrent::Session::instance()->status().dhtNodes)); } else { m_DHTLbl->setVisible(false); From 13b04f8d2db237a667be918f97e201410cea0f14 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 1 May 2017 19:19:34 +0300 Subject: [PATCH 05/12] Don't use deprecated settings_pack members --- src/base/bittorrent/session.cpp | 64 +++++++++++++++++++++++++++++---- src/base/bittorrent/session.h | 1 + 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 2343fa225..308c8b851 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -371,6 +371,8 @@ Session::Session(QObject *parent) { QMetaObject::invokeMethod(this, "readAlerts", Qt::QueuedConnection); }); + + configurePeerClasses(); #endif // Enabling plugins @@ -889,6 +891,7 @@ void Session::configure() libt::settings_pack settingsPack = m_nativeSession->get_settings(); configure(settingsPack); m_nativeSession->apply_settings(settingsPack); + configurePeerClasses(); #endif if (m_IPFilteringChanged) { @@ -1136,16 +1139,12 @@ void Session::configure(libtorrent::settings_pack &settingsPack) settingsPack.set_int(libt::settings_pack::outgoing_port, outgoingPortsMin()); settingsPack.set_int(libt::settings_pack::num_outgoing_ports, outgoingPortsMax() - outgoingPortsMin() + 1); - // Ignore limits on LAN - settingsPack.set_bool(libt::settings_pack::ignore_limits_on_local_network, ignoreLimitsOnLAN()); // Include overhead in transfer limits settingsPack.set_bool(libt::settings_pack::rate_limit_ip_overhead, includeOverheadInLimits()); // IP address to announce to trackers settingsPack.set_str(libt::settings_pack::announce_ip, announceIP().toStdString()); // Super seeding settingsPack.set_bool(libt::settings_pack::strict_super_seeding, isSuperSeedingEnabled()); - // * Max Half-open connections - settingsPack.set_int(libt::settings_pack::half_open_limit, maxHalfOpenConnections()); // * Max connections limit settingsPack.set_int(libt::settings_pack::connections_limit, maxConnections()); // * Global max upload slots @@ -1153,8 +1152,6 @@ void Session::configure(libtorrent::settings_pack &settingsPack) // uTP settingsPack.set_bool(libt::settings_pack::enable_incoming_utp, isUTPEnabled()); settingsPack.set_bool(libt::settings_pack::enable_outgoing_utp, isUTPEnabled()); - // uTP rate limiting - settingsPack.set_bool(libt::settings_pack::rate_limit_utp, isUTPRateLimited()); settingsPack.set_int(libt::settings_pack::mixed_mode_algorithm, isUTPRateLimited() ? libt::settings_pack::prefer_tcp : libt::settings_pack::peer_proportional); @@ -1167,6 +1164,61 @@ void Session::configure(libtorrent::settings_pack &settingsPack) settingsPack.set_bool(libt::settings_pack::enable_lsd, isLSDEnabled()); } +void Session::configurePeerClasses() +{ + libt::ip_filter f; + f.add_rule(libt::address_v4::from_string("0.0.0.0") + , libt::address_v4::from_string("255.255.255.255") + , 1 << libt::session::global_peer_class_id); +#if TORRENT_USE_IPV6 + f.add_rule(libt::address_v6::from_string("::0") + , libt::address_v6::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") + , 1 << libt::session::global_peer_class_id); +#endif + if (ignoreLimitsOnLAN()) { + f.add_rule(libt::address_v4::from_string("10.0.0.0") + , libt::address_v4::from_string("10.255.255.255") + , 1 << libt::session::local_peer_class_id); + f.add_rule(libt::address_v4::from_string("172.16.0.0") + , libt::address_v4::from_string("172.16.255.255") + , 1 << libt::session::local_peer_class_id); + f.add_rule(libt::address_v4::from_string("192.168.0.0") + , libt::address_v4::from_string("192.168.255.255") + , 1 << libt::session::local_peer_class_id); + f.add_rule(libt::address_v4::from_string("169.254.0.0") + , libt::address_v4::from_string("169.254.255.255") + , 1 << libt::session::local_peer_class_id); + f.add_rule(libt::address_v4::from_string("127.0.0.0") + , libt::address_v4::from_string("127.255.255.255") + , 1 << libt::session::local_peer_class_id); +#if TORRENT_USE_IPV6 + f.add_rule(libt::address_v6::from_string("fe80::") + , libt::address_v6::from_string("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff") + , 1 << libt::session::local_peer_class_id); + f.add_rule(libt::address_v6::from_string("::1") + , libt::address_v6::from_string("::1") + , 1 << libt::session::local_peer_class_id); +#endif + } + m_nativeSession->set_peer_class_filter(f); + + libt::peer_class_type_filter peerClassTypeFilter; + peerClassTypeFilter.add(libt::peer_class_type_filter::tcp_socket, libt::session::tcp_peer_class_id); + peerClassTypeFilter.add(libt::peer_class_type_filter::ssl_tcp_socket, libt::session::tcp_peer_class_id); + peerClassTypeFilter.add(libt::peer_class_type_filter::i2p_socket, libt::session::tcp_peer_class_id); + if (isUTPRateLimited()) { + peerClassTypeFilter.add(libt::peer_class_type_filter::utp_socket + , libt::session::local_peer_class_id); + peerClassTypeFilter.add(libt::peer_class_type_filter::utp_socket + , libt::session::global_peer_class_id); + peerClassTypeFilter.add(libt::peer_class_type_filter::ssl_utp_socket + , libt::session::local_peer_class_id); + peerClassTypeFilter.add(libt::peer_class_type_filter::ssl_utp_socket + , libt::session::global_peer_class_id); + } + m_nativeSession->set_peer_class_type_filter(peerClassTypeFilter); +} + #else void Session::adjustLimits(libt::session_settings &sessionSettings) diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 352649c8c..6dcd4e988 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -483,6 +483,7 @@ namespace BitTorrent void adjustLimits(libtorrent::session_settings &sessionSettings); #else void configure(libtorrent::settings_pack &settingsPack); + void configurePeerClasses(); void adjustLimits(libtorrent::settings_pack &settingsPack); void initMetrics(); #endif From a2f82be6c258d9aba072153467a093f265beb3c8 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 1 May 2017 19:25:10 +0300 Subject: [PATCH 06/12] Don't use deprecated url_seed_alert fields --- src/base/bittorrent/session.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 308c8b851..90b31b05b 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -3666,7 +3666,13 @@ void Session::handlePeerBanAlert(libt::peer_ban_alert *p) void Session::handleUrlSeedAlert(libt::url_seed_alert *p) { - Logger::instance()->addMessage(tr("URL seed lookup failed for URL: '%1', message: %2").arg(QString::fromStdString(p->url)).arg(QString::fromStdString(p->message())), Log::CRITICAL); + Logger::instance()->addMessage(tr("URL seed lookup failed for URL: '%1', message: %2") +#if LIBTORRENT_VERSION_NUM >= 10100 + .arg(QString::fromStdString(p->server_url())) +#else + .arg(QString::fromStdString(p->url)) +#endif + .arg(QString::fromStdString(p->message())), Log::CRITICAL); } void Session::handleListenSucceededAlert(libt::listen_succeeded_alert *p) From 72a54910e9ad4399b2d531f9c72a80fc882c5a0f Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 1 May 2017 19:29:43 +0300 Subject: [PATCH 07/12] Don't use deprecated peer_info fields --- src/base/bittorrent/peerinfo.cpp | 6 ------ src/base/bittorrent/peerinfo.h | 1 - 2 files changed, 7 deletions(-) diff --git a/src/base/bittorrent/peerinfo.cpp b/src/base/bittorrent/peerinfo.cpp index 29c7686e7..d054e38d7 100644 --- a/src/base/bittorrent/peerinfo.cpp +++ b/src/base/bittorrent/peerinfo.cpp @@ -121,12 +121,6 @@ bool PeerInfo::isConnecting() const return (m_nativeInfo.flags & libt::peer_info::connecting); } -bool PeerInfo::isQueued() const -{ - return (m_nativeInfo.flags & libt::peer_info::queued); -} - - bool PeerInfo::isOnParole() const { return (m_nativeInfo.flags & libt::peer_info::on_parole); diff --git a/src/base/bittorrent/peerinfo.h b/src/base/bittorrent/peerinfo.h index 0c7a06570..0d096c493 100644 --- a/src/base/bittorrent/peerinfo.h +++ b/src/base/bittorrent/peerinfo.h @@ -68,7 +68,6 @@ namespace BitTorrent bool isHandshake() const; bool isConnecting() const; - bool isQueued() const; bool isOnParole() const; bool isSeed() const; From 5dc54aa224638083f3016f92ef7a8f467919d2bb Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 1 May 2017 19:45:08 +0300 Subject: [PATCH 08/12] Don't use deprecated torrent_status fields --- src/base/bittorrent/torrenthandle.cpp | 21 ++++++++++++++++++--- src/base/bittorrent/torrenthandle.h | 2 ++ src/gui/torrentmodel.cpp | 4 ++++ src/gui/transferlistdelegate.cpp | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 0a52254b3..3f4227e61 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -147,8 +147,10 @@ QString TorrentState::toString() const return QLatin1String("checkingDL"); case ForcedDownloading: return QLatin1String("forcedDL"); +#if LIBTORRENT_VERSION_NUM < 10100 case QueuedForChecking: return QLatin1String("queuedForChecking"); +#endif case CheckingResumeData: return QLatin1String("checkingResumeData"); default: @@ -658,9 +660,12 @@ bool TorrentHandle::isQueued() const bool TorrentHandle::isChecking() const { - return ((m_nativeStatus.state == libt::torrent_status::queued_for_checking) - || (m_nativeStatus.state == libt::torrent_status::checking_files) - || (m_nativeStatus.state == libt::torrent_status::checking_resume_data)); + return ((m_nativeStatus.state == libt::torrent_status::checking_files) + || (m_nativeStatus.state == libt::torrent_status::checking_resume_data) +#if LIBTORRENT_VERSION_NUM < 10100 + || (m_nativeStatus.state == libt::torrent_status::queued_for_checking) +#endif + ); } bool TorrentHandle::isDownloading() const @@ -799,9 +804,11 @@ void TorrentHandle::updateState() case libt::torrent_status::allocating: m_state = TorrentState::Allocating; break; +#if LIBTORRENT_VERSION_NUM < 10100 case libt::torrent_status::queued_for_checking: m_state = TorrentState::QueuedForChecking; break; +#endif case libt::torrent_status::checking_resume_data: m_state = TorrentState::CheckingResumeData; break; @@ -837,7 +844,11 @@ bool TorrentHandle::hasMissingFiles() const bool TorrentHandle::hasError() const { +#if LIBTORRENT_VERSION_NUM < 10100 return (m_nativeStatus.paused && !m_nativeStatus.error.empty()); +#else + return (m_nativeStatus.paused && m_nativeStatus.errc); +#endif } bool TorrentHandle::hasFilteredPieces() const @@ -859,7 +870,11 @@ int TorrentHandle::queuePosition() const QString TorrentHandle::error() const { +#if LIBTORRENT_VERSION_NUM < 10100 return QString::fromStdString(m_nativeStatus.error); +#else + return QString::fromStdString(m_nativeStatus.errc.message()); +#endif } qlonglong TorrentHandle::totalDownload() const diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index 8cdcfe02e..0e057f24d 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -140,7 +140,9 @@ namespace BitTorrent CheckingUploading, CheckingDownloading, +#if LIBTORRENT_VERSION_NUM < 10100 QueuedForChecking, +#endif CheckingResumeData, PausedDownloading, diff --git a/src/gui/torrentmodel.cpp b/src/gui/torrentmodel.cpp index d18c3c627..2971f0107 100644 --- a/src/gui/torrentmodel.cpp +++ b/src/gui/torrentmodel.cpp @@ -338,7 +338,9 @@ QIcon getIconByState(BitTorrent::TorrentState state) return getQueuedIcon(); case BitTorrent::TorrentState::CheckingDownloading: case BitTorrent::TorrentState::CheckingUploading: +#if LIBTORRENT_VERSION_NUM < 10100 case BitTorrent::TorrentState::QueuedForChecking: +#endif case BitTorrent::TorrentState::CheckingResumeData: return getCheckingIcon(); case BitTorrent::TorrentState::Unknown: @@ -391,7 +393,9 @@ QColor getColorByState(BitTorrent::TorrentState state) case BitTorrent::TorrentState::QueuedUploading: case BitTorrent::TorrentState::CheckingDownloading: case BitTorrent::TorrentState::CheckingUploading: +#if LIBTORRENT_VERSION_NUM < 10100 case BitTorrent::TorrentState::QueuedForChecking: +#endif case BitTorrent::TorrentState::CheckingResumeData: if (!dark) return QColor(0, 128, 128); // Teal diff --git a/src/gui/transferlistdelegate.cpp b/src/gui/transferlistdelegate.cpp index fc5d404d8..eb1f8adde 100644 --- a/src/gui/transferlistdelegate.cpp +++ b/src/gui/transferlistdelegate.cpp @@ -258,9 +258,11 @@ QString TransferListDelegate::getStatusString(const int state) const case BitTorrent::TorrentState::CheckingUploading: str = tr("Checking", "Torrent local data is being checked"); break; +#if LIBTORRENT_VERSION_NUM < 10100 case BitTorrent::TorrentState::QueuedForChecking: str = tr("Queued for checking", "i.e. torrent is queued for hash checking"); break; +#endif case BitTorrent::TorrentState::CheckingResumeData: str = tr("Checking resume data", "used when loading the torrents from disk after qbt is launched. It checks the correctness of the .fastresume file. Normally it is completed in a fraction of a second, unless loading many many torrents."); break; From 1d3dbcb9d2b2aed4645f9af2cb6521212d2ac533 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 1 May 2017 19:58:40 +0300 Subject: [PATCH 09/12] Don't use deprecated fields of torrent-related alerts --- src/base/bittorrent/torrenthandle.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 3f4227e61..386ca8ba4 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1382,7 +1382,11 @@ void TorrentHandle::handleStorageMovedAlert(libtorrent::storage_moved_alert *p) return; } +#if LIBTORRENT_VERSION_NUM < 10100 const QString newPath = QString::fromStdString(p->path); +#else + const QString newPath(p->storage_path()); +#endif if (newPath != m_newPath) { qWarning() << Q_FUNC_INFO << ": New path doesn't match a path in a queue."; return; @@ -1432,7 +1436,11 @@ void TorrentHandle::handleStorageMovedFailedAlert(libtorrent::storage_moved_fail void TorrentHandle::handleTrackerReplyAlert(libtorrent::tracker_reply_alert *p) { +#if LIBTORRENT_VERSION_NUM < 10100 QString trackerUrl = QString::fromStdString(p->url); +#else + QString trackerUrl(p->tracker_url()); +#endif qDebug("Received a tracker reply from %s (Num_peers = %d)", qPrintable(trackerUrl), p->num_peers); // Connection was successful now. Remove possible old errors m_trackerInfos[trackerUrl].lastMessage.clear(); // Reset error/warning message @@ -1443,8 +1451,13 @@ void TorrentHandle::handleTrackerReplyAlert(libtorrent::tracker_reply_alert *p) void TorrentHandle::handleTrackerWarningAlert(libtorrent::tracker_warning_alert *p) { +#if LIBTORRENT_VERSION_NUM < 10100 QString trackerUrl = QString::fromStdString(p->url); QString message = QString::fromStdString(p->msg); +#else + QString trackerUrl(p->tracker_url()); + QString message = QString::fromStdString(p->message()); +#endif qDebug("Received a tracker warning for %s: %s", qPrintable(trackerUrl), qPrintable(message)); // Connection was successful now but there is a warning message m_trackerInfos[trackerUrl].lastMessage = message; // Store warning message @@ -1454,8 +1467,13 @@ void TorrentHandle::handleTrackerWarningAlert(libtorrent::tracker_warning_alert void TorrentHandle::handleTrackerErrorAlert(libtorrent::tracker_error_alert *p) { +#if LIBTORRENT_VERSION_NUM < 10100 QString trackerUrl = QString::fromStdString(p->url); QString message = QString::fromStdString(p->msg); +#else + QString trackerUrl(p->tracker_url()); + QString message = QString::fromStdString(p->message()); +#endif qDebug("Received a tracker error for %s: %s", qPrintable(trackerUrl), qPrintable(message)); m_trackerInfos[trackerUrl].lastMessage = message; @@ -1592,7 +1610,11 @@ void TorrentHandle::handleFastResumeRejectedAlert(libtorrent::fastresume_rejecte void TorrentHandle::handleFileRenamedAlert(libtorrent::file_renamed_alert *p) { +#if LIBTORRENT_VERSION_NUM < 10100 QString newName = Utils::Fs::fromNativePath(QString::fromStdString(p->name)); +#else + QString newName = Utils::Fs::fromNativePath(p->new_name()); +#endif // TODO: Check this! if (filesCount() > 1) { From a4d2df575e9fef8b53c21566f0d7d85cc94af225 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 1 May 2017 20:27:07 +0300 Subject: [PATCH 10/12] Don't use deprecated torrent_handle::set_tracker_login --- src/base/bittorrent/torrenthandle.cpp | 2 ++ src/base/bittorrent/torrenthandle.h | 2 ++ src/gui/mainwindow.cpp | 6 ++++++ src/gui/trackerlogin.cpp | 6 +++++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 386ca8ba4..b615ba7f9 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -1331,11 +1331,13 @@ void TorrentHandle::moveStorage(const QString &newPath) } } +#if LIBTORRENT_VERSION_NUM < 10100 void TorrentHandle::setTrackerLogin(const QString &username, const QString &password) { m_nativeHandle.set_tracker_login(std::string(username.toLocal8Bit().constData()) , std::string(password.toLocal8Bit().constData())); } +#endif void TorrentHandle::renameFile(int index, const QString &name) { diff --git a/src/base/bittorrent/torrenthandle.h b/src/base/bittorrent/torrenthandle.h index 0e057f24d..b24f633cb 100644 --- a/src/base/bittorrent/torrenthandle.h +++ b/src/base/bittorrent/torrenthandle.h @@ -333,7 +333,9 @@ namespace BitTorrent void forceReannounce(int index = -1); void forceDHTAnnounce(); void forceRecheck(); +#if LIBTORRENT_VERSION_NUM < 10100 void setTrackerLogin(const QString &username, const QString &password); +#endif void renameFile(int index, const QString &name); bool saveTorrentFile(const QString &path); void prioritizeFiles(const QVector &priorities); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 9632e2988..459839f3b 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -91,7 +91,9 @@ #include "rss/rsswidget.h" #include "about_imp.h" #include "optionsdlg.h" +#if LIBTORRENT_VERSION_NUM < 10100 #include "trackerlogin.h" +#endif #include "lineedit.h" #include "executionlog.h" #include "hidabletabwidget.h" @@ -1311,9 +1313,13 @@ void MainWindow::addUnauthenticatedTracker(const QPaircurrentTracker())) < 0) // Tracker login new trackerLogin(this, torrent); +#else + Q_UNUSED(torrent); +#endif } // Check connection status and display right icon diff --git a/src/gui/trackerlogin.cpp b/src/gui/trackerlogin.cpp index 9ff21cad4..74abf1dae 100644 --- a/src/gui/trackerlogin.cpp +++ b/src/gui/trackerlogin.cpp @@ -28,9 +28,11 @@ * Contact : chris@qbittorrent.org */ -#include "base/bittorrent/torrenthandle.h" #include "trackerlogin.h" +#include +#include "base/bittorrent/torrenthandle.h" + trackerLogin::trackerLogin(QWidget *parent, BitTorrent::TorrentHandle *const torrent) : QDialog(parent) , m_torrent(torrent) @@ -47,7 +49,9 @@ trackerLogin::~trackerLogin() {} void trackerLogin::on_loginButton_clicked() { // login +#if LIBTORRENT_VERSION_NUM < 10100 m_torrent->setTrackerLogin(lineUsername->text(), linePasswd->text()); +#endif close(); } From 26f645bd6cb1d615a89917aa4c3b935dad69f3e3 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Tue, 2 May 2017 14:04:11 +0300 Subject: [PATCH 11/12] Don't create redundant "buf" variable --- src/base/bittorrent/session.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 90b31b05b..4642c12e2 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1749,7 +1749,6 @@ bool Session::addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri libt::add_torrent_params p; InfoHash hash; - std::vector buf(fastresumeData.constData(), fastresumeData.constData() + fastresumeData.size()); std::vector filePriorities; QString savePath; @@ -1803,7 +1802,7 @@ bool Session::addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri if (addData.resumed && !fromMagnetUri) { // Set torrent fast resume data - p.resume_data = buf; + p.resume_data = {fastresumeData.constData(), fastresumeData.constData() + fastresumeData.size()}; p.flags |= libt::add_torrent_params::flag_use_resume_save_path; } else { From fbda237cc428a6e126e9d56b6c637d4d6fa83c69 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Tue, 2 May 2017 18:11:23 +0300 Subject: [PATCH 12/12] Fix local IP addresses --- src/base/bittorrent/session.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 4642c12e2..97c377b92 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1176,25 +1176,34 @@ void Session::configurePeerClasses() , 1 << libt::session::global_peer_class_id); #endif if (ignoreLimitsOnLAN()) { + // local networks f.add_rule(libt::address_v4::from_string("10.0.0.0") , libt::address_v4::from_string("10.255.255.255") , 1 << libt::session::local_peer_class_id); f.add_rule(libt::address_v4::from_string("172.16.0.0") - , libt::address_v4::from_string("172.16.255.255") + , libt::address_v4::from_string("172.31.255.255") , 1 << libt::session::local_peer_class_id); f.add_rule(libt::address_v4::from_string("192.168.0.0") , libt::address_v4::from_string("192.168.255.255") , 1 << libt::session::local_peer_class_id); + // link local f.add_rule(libt::address_v4::from_string("169.254.0.0") , libt::address_v4::from_string("169.254.255.255") , 1 << libt::session::local_peer_class_id); + // loopback f.add_rule(libt::address_v4::from_string("127.0.0.0") , libt::address_v4::from_string("127.255.255.255") , 1 << libt::session::local_peer_class_id); #if TORRENT_USE_IPV6 + // link local f.add_rule(libt::address_v6::from_string("fe80::") , libt::address_v6::from_string("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff") , 1 << libt::session::local_peer_class_id); + // unique local addresses + f.add_rule(libt::address_v6::from_string("fc00::") + , libt::address_v6::from_string("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") + , 1 << libt::session::local_peer_class_id); + // loopback f.add_rule(libt::address_v6::from_string("::1") , libt::address_v6::from_string("::1") , 1 << libt::session::local_peer_class_id);