From 2da2054ccf0ed7a17a798b9147634027087b0b61 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 16 Aug 2017 02:23:07 +0800 Subject: [PATCH] Use enums for settings --- src/base/bittorrent/session.cpp | 77 +++++++++++++++++++++++++-------- src/base/bittorrent/session.h | 50 +++++++++++++++++---- src/gui/advancedsettings.cpp | 12 ++--- 3 files changed, 107 insertions(+), 32 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index c6bd56dbb..041db4c47 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -301,7 +301,8 @@ Session::Session(QObject *parent) , m_maxUploadsPerTorrent(BITTORRENT_SESSION_KEY("MaxUploadsPerTorrent"), -1, lowerLimited(0, -1)) , m_isUTPEnabled(BITTORRENT_SESSION_KEY("uTPEnabled"), true) , m_isUTPRateLimited(BITTORRENT_SESSION_KEY("uTPRateLimited"), true) - , m_utpMixedMode(BITTORRENT_SESSION_KEY("uTPMixedMode"), m_isUTPEnabled, clampValue(0, 1)) + , m_utpMixedMode(BITTORRENT_SESSION_KEY("uTPMixedMode"), MixedModeAlgorithm::Proportional + , clampValue(MixedModeAlgorithm::TCP, MixedModeAlgorithm::Proportional)) , m_multiConnectionsPerIpEnabled(BITTORRENT_SESSION_KEY("MultiConnectionsPerIp"), false) , m_isAddTrackersEnabled(BITTORRENT_SESSION_KEY("AddTrackersEnabled"), false) , m_additionalTrackers(BITTORRENT_SESSION_KEY("AdditionalTrackers")) @@ -330,8 +331,10 @@ Session::Session(QObject *parent) , m_encryption(BITTORRENT_SESSION_KEY("Encryption"), 0) , m_isForceProxyEnabled(BITTORRENT_SESSION_KEY("ForceProxy"), true) , m_isProxyPeerConnectionsEnabled(BITTORRENT_SESSION_KEY("ProxyPeerConnections"), false) - , m_chokingAlgorithm(BITTORRENT_SESSION_KEY("ChokingAlgorithm"), 0, clampValue(0, 2)) - , m_seedChokingAlgorithm(BITTORRENT_SESSION_KEY("SeedChokingAlgorithm"), 1, clampValue(0, 2)) + , m_chokingAlgorithm(BITTORRENT_SESSION_KEY("ChokingAlgorithm"), ChokingAlgorithm::FixedSlots + , clampValue(ChokingAlgorithm::FixedSlots, ChokingAlgorithm::RateBased)) + , m_seedChokingAlgorithm(BITTORRENT_SESSION_KEY("SeedChokingAlgorithm"), SeedChokingAlgorithm::FastestUpload + , clampValue(SeedChokingAlgorithm::RoundRobin, SeedChokingAlgorithm::AntiLeech)) , m_storedCategories(BITTORRENT_SESSION_KEY("Categories")) , m_storedTags(BITTORRENT_SESSION_KEY("Tags")) , m_maxRatioAction(BITTORRENT_SESSION_KEY("MaxRatioAction"), Pause) @@ -1324,7 +1327,15 @@ 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()); - settingsPack.set_int(libt::settings_pack::mixed_mode_algorithm, utpMixedMode()); + switch (utpMixedMode()) { + case MixedModeAlgorithm::TCP: + default: + settingsPack.set_int(libt::settings_pack::mixed_mode_algorithm, libt::settings_pack::prefer_tcp); + break; + case MixedModeAlgorithm::Proportional: + settingsPack.set_int(libt::settings_pack::mixed_mode_algorithm, libt::settings_pack::peer_proportional); + break; + } settingsPack.set_bool(libt::settings_pack::allow_multiple_connections_per_ip, multiConnectionsPerIpEnabled()); @@ -1336,15 +1347,27 @@ void Session::configure(libtorrent::settings_pack &settingsPack) settingsPack.set_bool(libt::settings_pack::enable_lsd, isLSDEnabled()); switch (chokingAlgorithm()) { - case 0: + case ChokingAlgorithm::FixedSlots: default: settingsPack.set_int(libt::settings_pack::choking_algorithm, libt::settings_pack::fixed_slots_choker); break; - case 1: + case ChokingAlgorithm::RateBased: settingsPack.set_int(libt::settings_pack::choking_algorithm, libt::settings_pack::rate_based_choker); break; } - settingsPack.set_int(libt::settings_pack::seed_choking_algorithm, seedChokingAlgorithm()); + + switch (seedChokingAlgorithm()) { + case SeedChokingAlgorithm::RoundRobin: + settingsPack.set_int(libt::settings_pack::seed_choking_algorithm, libt::settings_pack::round_robin); + break; + case SeedChokingAlgorithm::FastestUpload: + default: + settingsPack.set_int(libt::settings_pack::seed_choking_algorithm, libt::settings_pack::fastest_upload); + break; + case SeedChokingAlgorithm::AntiLeech: + settingsPack.set_int(libt::settings_pack::seed_choking_algorithm, libt::settings_pack::anti_leech); + break; + } } void Session::configurePeerClasses() @@ -1562,7 +1585,15 @@ void Session::configure(libtorrent::session_settings &sessionSettings) sessionSettings.enable_outgoing_utp = isUTPEnabled(); // uTP rate limiting sessionSettings.rate_limit_utp = isUTPRateLimited(); - sessionSettings.mixed_mode_algorithm = utpMixedMode(); + switch (utpMixedMode()) { + case MixedModeAlgorithm::TCP: + default: + sessionSettings.mixed_mode_algorithm = libt::session_settings::prefer_tcp; + break; + case MixedModeAlgorithm::Proportional: + sessionSettings.mixed_mode_algorithm = libt::session_settings::peer_proportional; + break; + } sessionSettings.allow_multiple_connections_per_ip = multiConnectionsPerIpEnabled(); @@ -1587,15 +1618,27 @@ void Session::configure(libtorrent::session_settings &sessionSettings) m_nativeSession->stop_lsd(); switch (chokingAlgorithm()) { - case 0: + case ChokingAlgorithm::FixedSlots: default: sessionSettings.choking_algorithm = libt::session_settings::fixed_slots_choker; break; - case 1: + case ChokingAlgorithm::RateBased: sessionSettings.choking_algorithm = libt::session_settings::rate_based_choker; break; } - sessionSettings.seed_choking_algorithm = seedChokingAlgorithm(); + + switch (seedChokingAlgorithm()) { + case SeedChokingAlgorithm::RoundRobin: + sessionSettings.seed_choking_algorithm = libt::session_settings::round_robin; + break; + case SeedChokingAlgorithm::FastestUpload: + default: + sessionSettings.seed_choking_algorithm = libt::session_settings::fastest_upload; + break; + case SeedChokingAlgorithm::AntiLeech: + sessionSettings.seed_choking_algorithm = libt::session_settings::anti_leech; + break; + } } #endif @@ -2697,12 +2740,12 @@ void Session::setProxyPeerConnectionsEnabled(bool enabled) } } -int Session::chokingAlgorithm() const +ChokingAlgorithm Session::chokingAlgorithm() const { return m_chokingAlgorithm; } -void Session::setChokingAlgorithm(int mode) +void Session::setChokingAlgorithm(ChokingAlgorithm mode) { if (mode == m_chokingAlgorithm) return; @@ -2710,12 +2753,12 @@ void Session::setChokingAlgorithm(int mode) configureDeferred(); } -int Session::seedChokingAlgorithm() const +SeedChokingAlgorithm Session::seedChokingAlgorithm() const { return m_seedChokingAlgorithm; } -void Session::setSeedChokingAlgorithm(int mode) +void Session::setSeedChokingAlgorithm(SeedChokingAlgorithm mode) { if (mode == m_seedChokingAlgorithm) return; @@ -3220,12 +3263,12 @@ void Session::setUTPRateLimited(bool limited) } } -int Session::utpMixedMode() const +MixedModeAlgorithm Session::utpMixedMode() const { return m_utpMixedMode; } -void Session::setUtpMixedMode(int mode) +void Session::setUtpMixedMode(MixedModeAlgorithm mode) { if (mode == m_utpMixedMode) return; diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 7f4cee61f..7e21d3541 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -151,6 +151,38 @@ namespace BitTorrent uint nbErrored = 0; }; + class SessionSettingsEnums + { + Q_GADGET + + public: + // TODO: remove `SessionSettingsEnums` wrapper when we can use `Q_ENUM_NS` directly (QT >= 5.8 only) + enum class ChokingAlgorithm : int + { + FixedSlots = 0, + RateBased = 1 + }; + Q_ENUM(ChokingAlgorithm) + + enum class SeedChokingAlgorithm : int + { + RoundRobin = 0, + FastestUpload = 1, + AntiLeech = 2 + }; + Q_ENUM(SeedChokingAlgorithm) + + enum class MixedModeAlgorithm : int + { + TCP = 0, + Proportional = 1 + }; + Q_ENUM(MixedModeAlgorithm) + }; + using ChokingAlgorithm = SessionSettingsEnums::ChokingAlgorithm; + using SeedChokingAlgorithm = SessionSettingsEnums::SeedChokingAlgorithm; + using MixedModeAlgorithm = SessionSettingsEnums::MixedModeAlgorithm; + #if LIBTORRENT_VERSION_NUM >= 10100 struct SessionMetricIndices { @@ -317,10 +349,10 @@ namespace BitTorrent void setForceProxyEnabled(bool enabled); bool isProxyPeerConnectionsEnabled() const; void setProxyPeerConnectionsEnabled(bool enabled); - int chokingAlgorithm() const; - void setChokingAlgorithm(int mode); - int seedChokingAlgorithm() const; - void setSeedChokingAlgorithm(int mode); + ChokingAlgorithm chokingAlgorithm() const; + void setChokingAlgorithm(ChokingAlgorithm mode); + SeedChokingAlgorithm seedChokingAlgorithm() const; + void setSeedChokingAlgorithm(SeedChokingAlgorithm mode); bool isAddTrackersEnabled() const; void setAddTrackersEnabled(bool enabled); QString additionalTrackers() const; @@ -385,8 +417,8 @@ namespace BitTorrent void setUTPEnabled(bool enabled); bool isUTPRateLimited() const; void setUTPRateLimited(bool limited); - int utpMixedMode() const; - void setUtpMixedMode(int mode); + MixedModeAlgorithm utpMixedMode() const; + void setUtpMixedMode(MixedModeAlgorithm mode); bool multiConnectionsPerIpEnabled() const; void setMultiConnectionsPerIpEnabled(bool enabled); bool isTrackerFilteringEnabled() const; @@ -617,7 +649,7 @@ namespace BitTorrent CachedSettingValue m_maxUploadsPerTorrent; CachedSettingValue m_isUTPEnabled; CachedSettingValue m_isUTPRateLimited; - CachedSettingValue m_utpMixedMode; + CachedSettingValue m_utpMixedMode; CachedSettingValue m_multiConnectionsPerIpEnabled; CachedSettingValue m_isAddTrackersEnabled; CachedSettingValue m_additionalTrackers; @@ -646,8 +678,8 @@ namespace BitTorrent CachedSettingValue m_encryption; CachedSettingValue m_isForceProxyEnabled; CachedSettingValue m_isProxyPeerConnectionsEnabled; - CachedSettingValue m_chokingAlgorithm; - CachedSettingValue m_seedChokingAlgorithm; + CachedSettingValue m_chokingAlgorithm; + CachedSettingValue m_seedChokingAlgorithm; CachedSettingValue m_storedCategories; CachedSettingValue m_storedTags; CachedSettingValue m_maxRatioAction; diff --git a/src/gui/advancedsettings.cpp b/src/gui/advancedsettings.cpp index 0e2b350cc..8f38eb099 100644 --- a/src/gui/advancedsettings.cpp +++ b/src/gui/advancedsettings.cpp @@ -150,7 +150,7 @@ void AdvancedSettings::saveAdvancedSettings() session->setOutgoingPortsMin(outgoing_ports_min.value()); session->setOutgoingPortsMax(outgoing_ports_max.value()); // uTP-TCP mixed mode - session->setUtpMixedMode(comboUtpMixedMode.currentIndex()); + session->setUtpMixedMode(static_cast(comboUtpMixedMode.currentIndex())); // multiple connections per IP session->setMultiConnectionsPerIpEnabled(cbMultiConnectionsPerIp.isChecked()); // Recheck torrents on completion @@ -200,9 +200,9 @@ void AdvancedSettings::saveAdvancedSettings() session->setTrackerEnabled(cb_tracker_status.isChecked()); pref->setTrackerPort(spin_tracker_port.value()); // Choking algorithm - session->setChokingAlgorithm(comboChokingAlgorithm.currentIndex()); + session->setChokingAlgorithm(static_cast(comboChokingAlgorithm.currentIndex())); // Seed choking algorithm - session->setSeedChokingAlgorithm(comboSeedChokingAlgorithm.currentIndex()); + session->setSeedChokingAlgorithm(static_cast(comboSeedChokingAlgorithm.currentIndex())); #if defined(Q_OS_WIN) || defined(Q_OS_MAC) pref->setUpdateCheckEnabled(cb_update_check.isChecked()); @@ -344,7 +344,7 @@ void AdvancedSettings::loadAdvancedSettings() addRow(OUTGOING_PORT_MAX, tr("Outgoing ports (Max) [0: Disabled]"), &outgoing_ports_max); // uTP-TCP mixed mode comboUtpMixedMode.addItems({"Prefer TCP", "Peer proportional (throttles TCP)"}); - comboUtpMixedMode.setCurrentIndex(session->utpMixedMode()); + comboUtpMixedMode.setCurrentIndex(static_cast(session->utpMixedMode())); addRow(UTP_MIX_MODE, tr("uTP-TCP mixed mode algorithm"), &comboUtpMixedMode); // multiple connections per IP cbMultiConnectionsPerIp.setChecked(session->multiConnectionsPerIpEnabled()); @@ -429,11 +429,11 @@ void AdvancedSettings::loadAdvancedSettings() addRow(TRACKER_PORT, tr("Embedded tracker port"), &spin_tracker_port); // Choking algorithm comboChokingAlgorithm.addItems({"Fixed slots", "Upload rate based"}); - comboChokingAlgorithm.setCurrentIndex(session->chokingAlgorithm()); + comboChokingAlgorithm.setCurrentIndex(static_cast(session->chokingAlgorithm())); addRow(CHOKING_ALGORITHM, tr("Upload slots behavior"), &comboChokingAlgorithm); // Seed choking algorithm comboSeedChokingAlgorithm.addItems({"Round-robin", "Fastest upload", "Anti-leech"}); - comboSeedChokingAlgorithm.setCurrentIndex(session->seedChokingAlgorithm()); + comboSeedChokingAlgorithm.setCurrentIndex(static_cast(session->seedChokingAlgorithm())); addRow(SEED_CHOKING_ALGORITHM, tr("Upload choking algorithm"), &comboSeedChokingAlgorithm); #if defined(Q_OS_WIN) || defined(Q_OS_MAC)