From 45a0c5558ace60d8ba77b035009874787048bb24 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 14 Sep 2017 04:29:54 +0800 Subject: [PATCH] Add comboBox for selecting BitTorrent protocol. Closes #6316. Use unicode string C_UTP in place of "uTP" --- src/base/bittorrent/session.cpp | 72 +++++++++++++++--- src/base/bittorrent/session.h | 15 +++- src/gui/advancedsettings.cpp | 3 +- src/gui/optionsdlg.cpp | 8 +- src/gui/optionsdlg.ui | 74 +++++++++++-------- src/webui/prefjson.cpp | 6 +- src/webui/www/public/preferences_content.html | 21 ++---- 7 files changed, 132 insertions(+), 67 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 8759e3501..2c814b3ad 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -299,7 +299,8 @@ Session::Session(QObject *parent) , m_maxUploads(BITTORRENT_SESSION_KEY("MaxUploads"), -1, lowerLimited(0, -1)) , m_maxConnectionsPerTorrent(BITTORRENT_SESSION_KEY("MaxConnectionsPerTorrent"), 100, lowerLimited(0, -1)) , m_maxUploadsPerTorrent(BITTORRENT_SESSION_KEY("MaxUploadsPerTorrent"), -1, lowerLimited(0, -1)) - , m_isUTPEnabled(BITTORRENT_SESSION_KEY("uTPEnabled"), true) + , m_btProtocol(BITTORRENT_SESSION_KEY("BTProtocol"), BTProtocol::Both + , clampValue(BTProtocol::Both, BTProtocol::UTP)) , m_isUTPRateLimited(BITTORRENT_SESSION_KEY("uTPRateLimited"), true) , m_utpMixedMode(BITTORRENT_SESSION_KEY("uTPMixedMode"), MixedModeAlgorithm::Proportional , clampValue(MixedModeAlgorithm::TCP, MixedModeAlgorithm::Proportional)) @@ -1342,8 +1343,30 @@ void Session::configure(libtorrent::settings_pack &settingsPack) // * Global max upload slots settingsPack.set_int(libt::settings_pack::unchoke_slots_limit, maxUploads()); // uTP - settingsPack.set_bool(libt::settings_pack::enable_incoming_utp, isUTPEnabled()); - settingsPack.set_bool(libt::settings_pack::enable_outgoing_utp, isUTPEnabled()); + switch (btProtocol()) { + case BTProtocol::Both: + default: + settingsPack.set_bool(libt::settings_pack::enable_incoming_tcp, true); + settingsPack.set_bool(libt::settings_pack::enable_outgoing_tcp, true); + settingsPack.set_bool(libt::settings_pack::enable_incoming_utp, true); + settingsPack.set_bool(libt::settings_pack::enable_outgoing_utp, true); + break; + + case BTProtocol::TCP: + settingsPack.set_bool(libt::settings_pack::enable_incoming_tcp, true); + settingsPack.set_bool(libt::settings_pack::enable_outgoing_tcp, true); + settingsPack.set_bool(libt::settings_pack::enable_incoming_utp, false); + settingsPack.set_bool(libt::settings_pack::enable_outgoing_utp, false); + break; + + case BTProtocol::UTP: + settingsPack.set_bool(libt::settings_pack::enable_incoming_tcp, false); + settingsPack.set_bool(libt::settings_pack::enable_outgoing_tcp, false); + settingsPack.set_bool(libt::settings_pack::enable_incoming_utp, true); + settingsPack.set_bool(libt::settings_pack::enable_outgoing_utp, true); + break; + } + switch (utpMixedMode()) { case MixedModeAlgorithm::TCP: default: @@ -1604,8 +1627,30 @@ void Session::configure(libtorrent::session_settings &sessionSettings) // * Global max upload slots sessionSettings.unchoke_slots_limit = maxUploads(); // uTP - sessionSettings.enable_incoming_utp = isUTPEnabled(); - sessionSettings.enable_outgoing_utp = isUTPEnabled(); + switch (btProtocol()) { + case BTProtocol::Both: + default: + sessionSettings.enable_incoming_tcp = true; + sessionSettings.enable_outgoing_tcp = true; + sessionSettings.enable_incoming_utp = true; + sessionSettings.enable_outgoing_utp = true; + break; + + case BTProtocol::TCP: + sessionSettings.enable_incoming_tcp = true; + sessionSettings.enable_outgoing_tcp = true; + sessionSettings.enable_incoming_utp = false; + sessionSettings.enable_outgoing_utp = false; + break; + + case BTProtocol::UTP: + sessionSettings.enable_incoming_tcp = false; + sessionSettings.enable_outgoing_tcp = false; + sessionSettings.enable_incoming_utp = true; + sessionSettings.enable_outgoing_utp = true; + break; + } + // uTP rate limiting sessionSettings.rate_limit_utp = isUTPRateLimited(); switch (utpMixedMode()) { @@ -3246,17 +3291,20 @@ void Session::setMaxUploads(int max) } } -bool Session::isUTPEnabled() const +BTProtocol Session::btProtocol() const { - return m_isUTPEnabled; + return m_btProtocol; } -void Session::setUTPEnabled(bool enabled) +void Session::setBTProtocol(BTProtocol protocol) { - if (enabled != m_isUTPEnabled) { - m_isUTPEnabled = enabled; - configureDeferred(); - } + if ((protocol < BTProtocol::Both) || (BTProtocol::UTP < protocol)) + return; + + if (protocol == m_btProtocol) return; + + m_btProtocol = protocol; + configureDeferred(); } bool Session::isUTPRateLimited() const diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 8ba11b640..b3cd86568 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -178,10 +178,19 @@ namespace BitTorrent Proportional = 1 }; Q_ENUM(MixedModeAlgorithm) + + enum class BTProtocol : int + { + Both = 0, + TCP = 1, + UTP = 2 + }; + Q_ENUM(BTProtocol) }; using ChokingAlgorithm = SessionSettingsEnums::ChokingAlgorithm; using SeedChokingAlgorithm = SessionSettingsEnums::SeedChokingAlgorithm; using MixedModeAlgorithm = SessionSettingsEnums::MixedModeAlgorithm; + using BTProtocol = SessionSettingsEnums::BTProtocol; #if LIBTORRENT_VERSION_NUM >= 10100 struct SessionMetricIndices @@ -413,8 +422,8 @@ namespace BitTorrent void setMaxActiveUploads(int max); int maxActiveTorrents() const; void setMaxActiveTorrents(int max); - bool isUTPEnabled() const; - void setUTPEnabled(bool enabled); + BTProtocol btProtocol() const; + void setBTProtocol(BTProtocol protocol); bool isUTPRateLimited() const; void setUTPRateLimited(bool limited); MixedModeAlgorithm utpMixedMode() const; @@ -648,7 +657,7 @@ namespace BitTorrent CachedSettingValue m_maxUploads; CachedSettingValue m_maxConnectionsPerTorrent; CachedSettingValue m_maxUploadsPerTorrent; - CachedSettingValue m_isUTPEnabled; + CachedSettingValue m_btProtocol; CachedSettingValue m_isUTPRateLimited; CachedSettingValue m_utpMixedMode; CachedSettingValue m_multiConnectionsPerIpEnabled; diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index aa6963640..02528bd12 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -36,6 +36,7 @@ #include "app/application.h" #include "base/bittorrent/session.h" #include "base/preferences.h" +#include "base/unicodestrings.h" #include "gui/mainwindow.h" enum AdvSettingsCols @@ -345,7 +346,7 @@ void AdvancedSettings::loadAdvancedSettings() // uTP-TCP mixed mode comboUtpMixedMode.addItems({"Prefer TCP", "Peer proportional (throttles TCP)"}); comboUtpMixedMode.setCurrentIndex(static_cast(session->utpMixedMode())); - addRow(UTP_MIX_MODE, tr("uTP-TCP mixed mode algorithm"), &comboUtpMixedMode); + addRow(UTP_MIX_MODE, tr("%1-TCP mixed mode algorithm", "uTP-TCP mixed mode algorithm").arg(C_UTP), &comboUtpMixedMode); // multiple connections per IP cbMultiConnectionsPerIp.setChecked(session->multiConnectionsPerIpEnabled()); addRow(MULTI_CONNECTIONS_PER_IP, tr("Allow multiple connections from the same IP address"), &cbMultiConnectionsPerIp); diff --git a/src/gui/optionsdlg.cpp b/src/gui/optionsdlg.cpp index 2ed485218..428e3003b 100644 --- a/src/gui/optionsdlg.cpp +++ b/src/gui/optionsdlg.cpp @@ -272,6 +272,7 @@ OptionsDialog::OptionsDialog(QWidget *parent) m_ui->autoRun_param->setText(autoRunStr); // Connection tab + connect(m_ui->comboProtocol, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton); connect(m_ui->spinPort, qSpinBoxValueChanged, this, &ThisType::enableApplyButton); connect(m_ui->checkRandomPort, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkUPnP, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); @@ -287,8 +288,6 @@ OptionsDialog::OptionsDialog(QWidget *parent) connect(m_ui->schedule_from, &QDateTimeEdit::timeChanged, this, &ThisType::enableApplyButton); connect(m_ui->schedule_to, &QDateTimeEdit::timeChanged, this, &ThisType::enableApplyButton); connect(m_ui->schedule_days, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton); - connect(m_ui->checkuTP, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); - connect(m_ui->checkuTP, &QAbstractButton::toggled, m_ui->checkLimituTPConnections, &QWidget::setEnabled); connect(m_ui->checkLimituTPConnections, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkLimitTransportOverhead, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkLimitLocalPeerRate, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); @@ -573,13 +572,13 @@ void OptionsDialog::saveOptions() // End Downloads preferences // Connection preferences + session->setBTProtocol(static_cast(m_ui->comboProtocol->currentIndex())); session->setPort(getPort()); session->setUseRandomPort(m_ui->checkRandomPort->isChecked()); Net::PortForwarder::instance()->setEnabled(isUPnPEnabled()); const QPair down_up_limit = getGlobalBandwidthLimits(); session->setGlobalDownloadSpeedLimit(down_up_limit.first); session->setGlobalUploadSpeedLimit(down_up_limit.second); - session->setUTPEnabled(m_ui->checkuTP->isChecked()); session->setUTPRateLimited(m_ui->checkLimituTPConnections->isChecked()); session->setIncludeOverheadInLimits(m_ui->checkLimitTransportOverhead->isChecked()); session->setIgnoreLimitsOnLAN(!m_ui->checkLimitLocalPeerRate->isChecked()); @@ -824,6 +823,7 @@ void OptionsDialog::loadOptions() // End Downloads preferences // Connection preferences + m_ui->comboProtocol->setCurrentIndex(static_cast(session->btProtocol())); m_ui->checkUPnP->setChecked(Net::PortForwarder::instance()->isEnabled()); m_ui->checkRandomPort->setChecked(session->useRandomPort()); m_ui->spinPort->setValue(session->port()); @@ -973,8 +973,6 @@ void OptionsDialog::loadOptions() m_ui->spinUploadLimitAlt->setEnabled(false); } - m_ui->checkuTP->setChecked(session->isUTPEnabled()); - m_ui->checkLimituTPConnections->setEnabled(m_ui->checkuTP->isChecked()); m_ui->checkLimituTPConnections->setChecked(session->isUTPRateLimited()); m_ui->checkLimitTransportOverhead->setChecked(session->includeOverheadInLimits()); m_ui->checkLimitLocalPeerRate->setChecked(!session->ignoreLimitsOnLAN()); diff --git a/src/gui/optionsdlg.ui b/src/gui/optionsdlg.ui index 253236ddc..e617eeb98 100644 --- a/src/gui/optionsdlg.ui +++ b/src/gui/optionsdlg.ui @@ -1250,6 +1250,42 @@ + + + + + + + 0 + 0 + + + + Enabled protocol: + + + + + + + + TCP and μTP + + + + + TCP + + + + + μTP + + + + + + @@ -2011,48 +2047,28 @@ Rate Limits Settings - - - + + + - Apply rate limit to peers on LAN + Apply rate limit to µTP protocol - + Apply rate limit to transport overhead - - - - Enable µTP protocol - - - - - + + - Apply rate limit to µTP protocol + Apply rate limit to peers on LAN - - - - Qt::Horizontal - - - - 40 - 20 - - - - @@ -3239,8 +3255,6 @@ Use ';' to split multiple entries. Can use wildcard '*'. spinDownloadLimitAlt checkLimitLocalPeerRate checkLimitTransportOverhead - checkuTP - checkLimituTPConnections scrollArea_4 checkDHT checkPeX diff --git a/src/webui/prefjson.cpp b/src/webui/prefjson.cpp index 6292cfdb9..01390c7ee 100644 --- a/src/webui/prefjson.cpp +++ b/src/webui/prefjson.cpp @@ -120,7 +120,7 @@ QByteArray prefjson::getPreferences() // Global Rate Limits data["dl_limit"] = session->globalDownloadSpeedLimit(); data["up_limit"] = session->globalUploadSpeedLimit(); - data["enable_utp"] = session->isUTPEnabled(); + data["bittorrent_protocol"] = static_cast(session->btProtocol()); data["limit_utp_rate"] = session->isUTPRateLimited(); data["limit_tcp_overhead"] = session->includeOverheadInLimits(); data["alt_dl_limit"] = session->altGlobalDownloadSpeedLimit(); @@ -322,8 +322,8 @@ void prefjson::setPreferences(const QString& json) session->setGlobalDownloadSpeedLimit(m["dl_limit"].toInt()); if (m.contains("up_limit")) session->setGlobalUploadSpeedLimit(m["up_limit"].toInt()); - if (m.contains("enable_utp")) - session->setUTPEnabled(m["enable_utp"].toBool()); + if (m.contains("bittorrent_protocol")) + session->setBTProtocol(static_cast(m["bittorrent_protocol"].toInt())); if (m.contains("limit_utp_rate")) session->setUTPRateLimited(m["limit_utp_rate"].toBool()); if (m.contains("limit_tcp_overhead")) diff --git a/src/webui/www/public/preferences_content.html b/src/webui/www/public/preferences_content.html index a9990eb6f..f452b1991 100644 --- a/src/webui/www/public/preferences_content.html +++ b/src/webui/www/public/preferences_content.html @@ -88,6 +88,12 @@