From a4c1a23faeafec0a7d869a7ea1291b00b1c3f748 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Aug 2022 19:33:28 -0500 Subject: [PATCH] refactor: make tr_session.is_incomplete_file_naming_enabled_ private (#3642) * refactor: make tr_session.is_incomplete_file_naming_enabled_ private * refactor: make session callback setters constexpr * refactor: make tr_session.is_port_random_ private * refactor: make tr_session.desired_ratio_ private * refactor: make tr_session.is_pex_enabled_ private * refactor: make tr_session.is_idle_limited_ private --- libtransmission/inout.cc | 2 +- libtransmission/rpcimpl.cc | 14 ++++----- libtransmission/session.cc | 34 +++++++++++----------- libtransmission/session.h | 53 +++++++++++++++++++++++++++------- libtransmission/torrent.cc | 6 ++-- libtransmission/torrent.h | 2 +- libtransmission/transmission.h | 6 ++-- 7 files changed, 74 insertions(+), 43 deletions(-) diff --git a/libtransmission/inout.cc b/libtransmission/inout.cc index 8e96d8813..bbfea09ce 100644 --- a/libtransmission/inout.cc +++ b/libtransmission/inout.cc @@ -88,7 +88,7 @@ bool getFilename(tr_pathbuf& setme, tr_torrent const* tor, tr_file_index_t file_ // We didn't find the file that we want to write to. // Let's figure out where it goes so that we can create it. auto const base = tor->currentDir(); - auto const suffix = tor->session->isIncompleteFileNamingEnabled ? tr_torrent_files::PartialFileSuffix : ""sv; + auto const suffix = tor->session->isIncompleteFileNamingEnabled() ? tr_torrent_files::PartialFileSuffix : ""sv; setme.assign(base, '/', tor->fileSubpath(file_index), suffix); return true; } diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index 532aab612..ad0d30a38 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -2184,7 +2184,7 @@ static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_pex_enabled: - tr_variantDictAddBool(d, key, tr_sessionIsPexEnabled(s)); + tr_variantDictAddBool(d, key, s->allowsPEX()); break; case TR_KEY_utp_enabled: @@ -2192,11 +2192,11 @@ static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_dht_enabled: - tr_variantDictAddBool(d, key, tr_sessionIsDHTEnabled(s)); + tr_variantDictAddBool(d, key, s->allowsDHT()); break; case TR_KEY_lpd_enabled: - tr_variantDictAddBool(d, key, tr_sessionIsLPDEnabled(s)); + tr_variantDictAddBool(d, key, s->allowsLPD()); break; case TR_KEY_peer_port: @@ -2204,7 +2204,7 @@ static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_peer_port_random_on_start: - tr_variantDictAddBool(d, key, tr_sessionGetPeerPortRandomOnStart(s)); + tr_variantDictAddBool(d, key, s->isPortRandom()); break; case TR_KEY_port_forwarding_enabled: @@ -2212,7 +2212,7 @@ static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_rename_partial_files: - tr_variantDictAddBool(d, key, tr_sessionIsIncompleteFileNamingEnabled(s)); + tr_variantDictAddBool(d, key, s->isIncompleteFileNamingEnabled()); break; case TR_KEY_rpc_version: @@ -2228,7 +2228,7 @@ static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_seedRatioLimit: - tr_variantDictAddReal(d, key, tr_sessionGetRatioLimit(s)); + tr_variantDictAddReal(d, key, s->desiredRatio()); break; case TR_KEY_seedRatioLimited: @@ -2240,7 +2240,7 @@ static void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_idle_seeding_limit_enabled: - tr_variantDictAddBool(d, key, tr_sessionIsIdleLimited(s)); + tr_variantDictAddBool(d, key, s->isIdleLimited()); break; case TR_KEY_seed_queue_enabled: diff --git a/libtransmission/session.cc b/libtransmission/session.cc index f16128037..c45518320 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -406,21 +406,21 @@ void tr_sessionGetSettings(tr_session const* s, tr_variant* setme_dictionary) tr_variantDictAddInt(d, TR_KEY_peer_limit_global, s->peerLimit()); tr_variantDictAddInt(d, TR_KEY_peer_limit_per_torrent, s->peerLimitPerTorrent()); tr_variantDictAddInt(d, TR_KEY_peer_port, s->peerPort().host()); - tr_variantDictAddBool(d, TR_KEY_peer_port_random_on_start, s->isPortRandom); + tr_variantDictAddBool(d, TR_KEY_peer_port_random_on_start, s->isPortRandom()); tr_variantDictAddInt(d, TR_KEY_peer_port_random_low, s->random_port_low_.host()); tr_variantDictAddInt(d, TR_KEY_peer_port_random_high, s->random_port_high_.host()); tr_variantDictAddStr(d, TR_KEY_peer_socket_tos, tr_netTosToName(s->peer_socket_tos_)); tr_variantDictAddStr(d, TR_KEY_peer_congestion_algorithm, s->peerCongestionAlgorithm()); - tr_variantDictAddBool(d, TR_KEY_pex_enabled, s->isPexEnabled); + tr_variantDictAddBool(d, TR_KEY_pex_enabled, s->allowsPEX()); tr_variantDictAddBool(d, TR_KEY_port_forwarding_enabled, tr_sessionIsPortForwardingEnabled(s)); tr_variantDictAddInt(d, TR_KEY_preallocation, s->preallocationMode()); tr_variantDictAddBool(d, TR_KEY_prefetch_enabled, s->isPrefetchEnabled); tr_variantDictAddInt(d, TR_KEY_peer_id_ttl_hours, s->peer_id_ttl_hours); tr_variantDictAddBool(d, TR_KEY_queue_stalled_enabled, s->queueStalledEnabled()); tr_variantDictAddInt(d, TR_KEY_queue_stalled_minutes, s->queueStalledMinutes()); - tr_variantDictAddReal(d, TR_KEY_ratio_limit, s->desiredRatio); + tr_variantDictAddReal(d, TR_KEY_ratio_limit, s->desiredRatio()); tr_variantDictAddBool(d, TR_KEY_ratio_limit_enabled, s->isRatioLimited); - tr_variantDictAddBool(d, TR_KEY_rename_partial_files, tr_sessionIsIncompleteFileNamingEnabled(s)); + tr_variantDictAddBool(d, TR_KEY_rename_partial_files, s->isIncompleteFileNamingEnabled()); tr_variantDictAddBool(d, TR_KEY_rpc_authentication_required, tr_sessionIsRPCPasswordEnabled(s)); tr_variantDictAddStr(d, TR_KEY_rpc_bind_address, s->rpc_server_->getBindAddress()); tr_variantDictAddBool(d, TR_KEY_rpc_enabled, tr_sessionIsRPCEnabled(s)); @@ -1138,18 +1138,18 @@ char const* tr_sessionGetConfigDir(tr_session const* session) **** ***/ -void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool b) +void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool enabled) { TR_ASSERT(session != nullptr); - session->isIncompleteFileNamingEnabled = b; + session->is_incomplete_file_naming_enabled_ = enabled; } bool tr_sessionIsIncompleteFileNamingEnabled(tr_session const* session) { TR_ASSERT(session != nullptr); - return session->isIncompleteFileNamingEnabled; + return session->isIncompleteFileNamingEnabled(); } /*** @@ -1234,14 +1234,14 @@ void tr_sessionSetPeerPortRandomOnStart(tr_session* session, bool random) { TR_ASSERT(session != nullptr); - session->isPortRandom = random; + session->is_port_random_ = random; } bool tr_sessionGetPeerPortRandomOnStart(tr_session const* session) { TR_ASSERT(session != nullptr); - return session->isPortRandom; + return session->isPortRandom(); } tr_port_forwarding tr_sessionGetPortForwarding(tr_session const* session) @@ -1262,11 +1262,11 @@ void tr_sessionSetRatioLimited(tr_session* session, bool isLimited) session->isRatioLimited = isLimited; } -void tr_sessionSetRatioLimit(tr_session* session, double desiredRatio) +void tr_sessionSetRatioLimit(tr_session* session, double desired_ratio) { TR_ASSERT(session != nullptr); - session->desiredRatio = desiredRatio; + session->desired_ratio_ = desired_ratio; } bool tr_sessionIsRatioLimited(tr_session const* session) @@ -1280,18 +1280,18 @@ double tr_sessionGetRatioLimit(tr_session const* session) { TR_ASSERT(session != nullptr); - return session->desiredRatio; + return session->desiredRatio(); } /*** **** ***/ -void tr_sessionSetIdleLimited(tr_session* session, bool isLimited) +void tr_sessionSetIdleLimited(tr_session* session, bool is_limited) { TR_ASSERT(session != nullptr); - session->isIdleLimited = isLimited; + session->is_idle_limited_ = is_limited; } void tr_sessionSetIdleLimit(tr_session* session, uint16_t idleMinutes) @@ -1305,7 +1305,7 @@ bool tr_sessionIsIdleLimited(tr_session const* session) { TR_ASSERT(session != nullptr); - return session->isIdleLimited; + return session->isIdleLimited(); } uint16_t tr_sessionGetIdleLimit(tr_session const* session) @@ -1990,14 +1990,14 @@ void tr_sessionSetPexEnabled(tr_session* session, bool enabled) { TR_ASSERT(session != nullptr); - session->isPexEnabled = enabled; + session->is_pex_enabled_ = enabled; } bool tr_sessionIsPexEnabled(tr_session const* session) { TR_ASSERT(session != nullptr); - return session->isPexEnabled; + return session->allowsPEX(); } bool tr_sessionIsDHTEnabled(tr_session const* session) diff --git a/libtransmission/session.h b/libtransmission/session.h index 93879fa87..86c448665 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -354,7 +354,7 @@ public: using queue_start_callback_t = void (*)(tr_session*, tr_torrent*, void* user_data); - void setQueueStartCallback(queue_start_callback_t cb, void* user_data) + constexpr void setQueueStartCallback(queue_start_callback_t cb, void* user_data) { queue_start_callback_ = cb; queue_start_user_data_ = user_data; @@ -368,7 +368,7 @@ public: } } - void setIdleLimitHitCallback(tr_session_idle_limit_hit_func cb, void* user_data) + constexpr void setIdleLimitHitCallback(tr_session_idle_limit_hit_func cb, void* user_data) { idle_limit_hit_callback_ = cb; idle_limit_hit_user_data_ = user_data; @@ -382,7 +382,7 @@ public: } } - void setRatioLimitHitCallback(tr_session_ratio_limit_hit_func cb, void* user_data) + constexpr void setRatioLimitHitCallback(tr_session_ratio_limit_hit_func cb, void* user_data) { ratio_limit_hit_cb_ = cb; ratio_limit_hit_user_data_ = user_data; @@ -396,7 +396,7 @@ public: } } - void setMetadataCallback(tr_session_metadata_func cb, void* user_data) + constexpr void setMetadataCallback(tr_session_metadata_func cb, void* user_data) { got_metadata_cb_ = cb; got_metadata_user_data_ = user_data; @@ -410,7 +410,7 @@ public: } } - void setTorrentCompletenessCallback(tr_torrent_completeness_func cb, void* user_data) + constexpr void setTorrentCompletenessCallback(tr_torrent_completeness_func cb, void* user_data) { completeness_func_ = cb; completeness_func_user_data_ = user_data; @@ -460,13 +460,9 @@ public: TR_SCRIPT_ON_TORRENT_DONE_SEEDING } } }; - bool isPortRandom = false; - bool isPexEnabled = false; bool isUTPEnabled = false; bool isPrefetchEnabled = false; bool isRatioLimited = false; - bool isIdleLimited = false; - bool isIncompleteFileNamingEnabled = false; uint8_t peer_id_ttl_hours = 0; @@ -553,8 +549,6 @@ public: std::vector>> bandwidth_groups_; - float desiredRatio; - uint16_t idleLimitMinutes; tr_bindinfo bind_ipv4 = tr_bindinfo{ tr_inaddr_any }; @@ -644,6 +638,16 @@ public: return is_lpd_enabled_; } + [[nodiscard]] auto constexpr allowsPEX() const noexcept + { + return is_pex_enabled_; + } + + [[nodiscard]] auto constexpr isIdleLimited() const noexcept + { + return is_idle_limited_; + } + [[nodiscard]] std::vector getAllTorrents() const { return std::vector{ std::begin(torrents()), std::end(torrents()) }; @@ -687,6 +691,21 @@ public: [[nodiscard]] std::optional activeSpeedLimitBps(tr_direction dir) const noexcept; + [[nodiscard]] auto isIncompleteFileNamingEnabled() const noexcept + { + return is_incomplete_file_naming_enabled_; + } + + [[nodiscard]] constexpr auto isPortRandom() const noexcept + { + return is_port_random_; + } + + [[nodiscard]] constexpr auto desiredRatio() const noexcept + { + return desired_ratio_; + } + private: [[nodiscard]] tr_port randomPort() const; @@ -698,19 +717,27 @@ private: friend void tr_sessionSetDHTEnabled(tr_session* session, bool enabled); friend void tr_sessionSetDeleteSource(tr_session* session, bool delete_source); friend void tr_sessionSetEncryption(tr_session* session, tr_encryption_mode mode); + friend void tr_sessionSetIdleLimited(tr_session* session, bool is_limited); + friend void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool enabled); friend void tr_sessionSetLPDEnabled(tr_session* session, bool enabled); friend void tr_sessionSetPaused(tr_session* session, bool is_paused); friend void tr_sessionSetPeerLimit(tr_session* session, uint16_t max_global_peers); friend void tr_sessionSetPeerLimitPerTorrent(tr_session* session, uint16_t max_peers); + friend void tr_sessionSetPeerPortRandomOnStart(tr_session* session, bool random); + friend void tr_sessionSetPexEnabled(tr_session* session, bool enabled); friend void tr_sessionSetQueueEnabled(tr_session* session, tr_direction dir, bool do_limit_simultaneous_seed_torrents); friend void tr_sessionSetQueueSize(tr_session* session, tr_direction dir, int max_simultaneous_seed_torrents); friend void tr_sessionSetQueueStalledEnabled(tr_session* session, bool is_enabled); friend void tr_sessionSetQueueStalledMinutes(tr_session* session, int minutes); friend void tr_sessionSetRPCCallback(tr_session* session, tr_rpc_func func, void* user_data); + friend void tr_sessionSetRatioLimit(tr_session* session, double desired_ratio); + bool is_pex_enabled_ = false; bool is_dht_enabled_ = false; bool is_lpd_enabled_ = false; + bool is_idle_limited_ = false; + struct init_data; void initImpl(init_data&); void setImpl(init_data&); @@ -724,9 +751,12 @@ private: tr_rpc_func rpc_func_ = nullptr; void* rpc_func_user_data_ = nullptr; + float desired_ratio_ = 2.0F; + bool should_pause_added_torrents_ = false; bool should_delete_source_torrents_ = false; bool should_scrape_paused_torrents_ = false; + bool is_incomplete_file_naming_enabled_ = false; tr_encryption_mode encryption_mode_ = TR_ENCRYPTION_PREFERRED; @@ -734,6 +764,7 @@ private: bool is_closing_ = false; bool is_closed_ = false; + bool is_port_random_ = false; uint16_t peer_count_ = 0; uint16_t peer_limit_ = 200; diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 630e1d3ec..791655c5a 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -319,7 +319,7 @@ bool tr_torrentGetSeedRatio(tr_torrent const* tor, double* ratio) if (isLimited && ratio != nullptr) { - *ratio = tr_sessionGetRatioLimit(tor->session); + *ratio = tor->session->desiredRatio(); } break; @@ -428,7 +428,7 @@ bool tr_torrentGetSeedIdle(tr_torrent const* tor, uint16_t* idleMinutes) break; case TR_IDLELIMIT_GLOBAL: - isLimited = tr_sessionIsIdleLimited(tor->session); + isLimited = tor->session->isIdleLimited(); if (isLimited && idleMinutes != nullptr) { @@ -764,7 +764,7 @@ static void torrentInit(tr_torrent* tor, tr_ctor const* ctor) if ((loaded & tr_resume::Ratiolimit) == 0) { tr_torrentSetRatioMode(tor, TR_RATIOLIMIT_GLOBAL); - tr_torrentSetRatioLimit(tor, tr_sessionGetRatioLimit(tor->session)); + tr_torrentSetRatioLimit(tor, tor->session->desiredRatio()); } if ((loaded & tr_resume::Idlelimit) == 0) diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 8527844c2..686c6b15a 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -518,7 +518,7 @@ public: [[nodiscard]] auto allowsPex() const noexcept { - return this->isPublic() && this->session->isPexEnabled; + return this->isPublic() && this->session->allowsPEX(); } [[nodiscard]] auto allowsDht() const diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 1f725e819..68060525f 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -309,7 +309,7 @@ bool tr_sessionIsIncompleteDirEnabled(tr_session const* session); * * @see tr_sessionIsIncompleteFileNamingEnabled() */ -void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool); +void tr_sessionSetIncompleteFileNamingEnabled(tr_session* session, bool enabled); /** @brief return true if files will end in ".part" until they're complete */ bool tr_sessionIsIncompleteFileNamingEnabled(tr_session const* session); @@ -585,10 +585,10 @@ double tr_sessionGetRawSpeed_KBps(tr_session const*, tr_direction); void tr_sessionSetRatioLimited(tr_session*, bool isLimited); bool tr_sessionIsRatioLimited(tr_session const*); -void tr_sessionSetRatioLimit(tr_session*, double desiredRatio); +void tr_sessionSetRatioLimit(tr_session*, double desired_ratio); double tr_sessionGetRatioLimit(tr_session const*); -void tr_sessionSetIdleLimited(tr_session*, bool isLimited); +void tr_sessionSetIdleLimited(tr_session*, bool is_limited); bool tr_sessionIsIdleLimited(tr_session const*); void tr_sessionSetIdleLimit(tr_session*, uint16_t idleMinutes);