diff --git a/libtransmission/peer-msgs.cc b/libtransmission/peer-msgs.cc index 48bee9bec..c0153dc7e 100644 --- a/libtransmission/peer-msgs.cc +++ b/libtransmission/peer-msgs.cc @@ -568,10 +568,9 @@ private: // honor the session limits, if enabled if (torrent->uses_session_limits()) { - if (auto const irate_bytes_per_second = torrent->session->activeSpeedLimitBps(TR_PEER_TO_CLIENT); - irate_bytes_per_second) + if (auto const limit = torrent->session->active_speed_limit(TR_PEER_TO_CLIENT); limit) { - rate_bytes_per_second = std::min(rate_bytes_per_second, uint64_t{ *irate_bytes_per_second }); + rate_bytes_per_second = std::min(rate_bytes_per_second, limit->base_quantity()); } } diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc index d74f711a4..ddb0c247a 100644 --- a/libtransmission/rpcimpl.cc +++ b/libtransmission/rpcimpl.cc @@ -1868,7 +1868,7 @@ char const* sessionSet(tr_session* session, tr_variant* args_in, tr_variant* /*a if (tr_variantDictFindInt(args_in, TR_KEY_speed_limit_down, &i)) { - tr_sessionSetSpeedLimit_KBps(session, TR_DOWN, static_cast(i)); + session->set_speed_limit(TR_DOWN, Speed{ i, Speed::Units::KByps }); } if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_speed_limit_down_enabled, &val)) @@ -1878,7 +1878,7 @@ char const* sessionSet(tr_session* session, tr_variant* args_in, tr_variant* /*a if (tr_variantDictFindInt(args_in, TR_KEY_speed_limit_up, &i)) { - tr_sessionSetSpeedLimit_KBps(session, TR_UP, static_cast(i)); + session->set_speed_limit(TR_UP, Speed{ i, Speed::Units::KByps }); } if (auto val = bool{}; tr_variantDictFindBool(args_in, TR_KEY_speed_limit_up_enabled, &val)) @@ -1927,10 +1927,10 @@ char const* sessionStats(tr_session* session, tr_variant* /*args_in*/, tr_varian [](auto const* tor) { return tor->is_running(); }); tr_variantDictAddInt(args_out, TR_KEY_activeTorrentCount, running); - tr_variantDictAddReal(args_out, TR_KEY_downloadSpeed, session->pieceSpeedBps(TR_DOWN)); + tr_variantDictAddInt(args_out, TR_KEY_downloadSpeed, session->piece_speed(TR_DOWN).base_quantity()); tr_variantDictAddInt(args_out, TR_KEY_pausedTorrentCount, total - running); tr_variantDictAddInt(args_out, TR_KEY_torrentCount, total); - tr_variantDictAddReal(args_out, TR_KEY_uploadSpeed, session->pieceSpeedBps(TR_UP)); + tr_variantDictAddInt(args_out, TR_KEY_uploadSpeed, session->piece_speed(TR_UP).base_quantity()); auto stats = session->stats().cumulative(); tr_variant* d = tr_variantDictAddDict(args_out, TR_KEY_cumulative_stats, 5); @@ -2142,19 +2142,19 @@ void addSessionField(tr_session const* s, tr_variant* d, tr_quark key) break; case TR_KEY_speed_limit_up: - tr_variantDictAddInt(d, key, tr_sessionGetSpeedLimit_KBps(s, TR_UP)); + tr_variantDictAddInt(d, key, s->speed_limit(TR_UP).count(Speed::Units::KByps)); break; case TR_KEY_speed_limit_up_enabled: - tr_variantDictAddBool(d, key, s->isSpeedLimited(TR_UP)); + tr_variantDictAddBool(d, key, s->is_speed_limited(TR_UP)); break; case TR_KEY_speed_limit_down: - tr_variantDictAddInt(d, key, tr_sessionGetSpeedLimit_KBps(s, TR_DOWN)); + tr_variantDictAddInt(d, key, s->speed_limit(TR_DOWN).count(Speed::Units::KByps)); break; case TR_KEY_speed_limit_down_enabled: - tr_variantDictAddBool(d, key, s->isSpeedLimited(TR_DOWN)); + tr_variantDictAddBool(d, key, s->is_speed_limited(TR_DOWN)); break; case TR_KEY_script_torrent_added_filename: diff --git a/libtransmission/session-alt-speeds.h b/libtransmission/session-alt-speeds.h index 4471f6d55..dc1ee7500 100644 --- a/libtransmission/session-alt-speeds.h +++ b/libtransmission/session-alt-speeds.h @@ -17,12 +17,13 @@ #include "libtransmission/transmission.h" // for TR_SCHED_ALL #include "libtransmission/quark.h" +#include "libtransmission/values.h" struct tr_variant; #define ALT_SPEEDS_FIELDS(V) \ - V(TR_KEY_alt_speed_up, speed_up_kilobytes_per_second_, size_t, 50U, "") \ - V(TR_KEY_alt_speed_down, speed_down_kilobytes_per_second_, size_t, 50U, "") \ + V(TR_KEY_alt_speed_up, speed_up_kbyps_, size_t, 50U, "") \ + V(TR_KEY_alt_speed_down, speed_down_kbyps_, size_t, 50U, "") \ V(TR_KEY_alt_speed_time_enabled, scheduler_enabled_, bool, false, "whether alt speeds toggle on and off on schedule") \ V(TR_KEY_alt_speed_time_day, use_on_these_weekdays_, size_t, TR_SCHED_ALL, "days of the week") \ V(TR_KEY_alt_speed_time_begin, minute_begin_, size_t, 540U, "minutes past midnight; 9AM") \ @@ -31,6 +32,8 @@ struct tr_variant; /** Manages alternate speed limits and a scheduler to auto-toggle them. */ class tr_session_alt_speeds { + using Speed = libtransmission::Values::Speed; + public: enum class ChangeReason { @@ -110,20 +113,21 @@ public: return static_cast(use_on_these_weekdays_); } - [[nodiscard]] constexpr auto limit_kbps(tr_direction dir) const noexcept + [[nodiscard]] auto speed_limit(tr_direction const dir) const noexcept { - return dir == TR_DOWN ? speed_down_kilobytes_per_second_ : speed_up_kilobytes_per_second_; + auto const kbyps = dir == TR_DOWN ? speed_down_kbyps_ : speed_up_kbyps_; + return Speed{ kbyps, Speed::Units::KByps }; } - constexpr void set_limit_kbps(tr_direction dir, size_t limit) noexcept + constexpr void set_speed_limit(tr_direction dir, Speed const limit) noexcept { if (dir == TR_DOWN) { - speed_down_kilobytes_per_second_ = limit; + speed_down_kbyps_ = limit.count(Speed::Units::KByps); } else { - speed_up_kilobytes_per_second_ = limit; + speed_up_kbyps_ = limit.count(Speed::Units::KByps); } } diff --git a/libtransmission/session.cc b/libtransmission/session.cc index 2056875a9..64fed0868 100644 --- a/libtransmission/session.cc +++ b/libtransmission/session.cc @@ -151,20 +151,20 @@ void bandwidthGroupWrite(tr_session const* session, std::string_view config_dir) tr_pathbuf{ config_dir, '/', BandwidthGroupsFilename }); } } // namespace bandwidth_group_helpers +} // namespace -void update_bandwidth(tr_session* session, tr_direction dir) +void tr_session::update_bandwidth(tr_direction const dir) { - if (auto const limit_byps = session->activeSpeedLimitBps(dir); limit_byps) + if (auto const limit = active_speed_limit(dir); limit) { - session->top_bandwidth_.set_limited(dir, *limit_byps > 0U); - session->top_bandwidth_.set_desired_speed(dir, Speed{ *limit_byps, Speed::Units::Byps }); + top_bandwidth_.set_limited(dir, limit->base_quantity() > 0U); + top_bandwidth_.set_desired_speed(dir, *limit); } else { - session->top_bandwidth_.set_limited(dir, false); + top_bandwidth_.set_limited(dir, false); } } -} // namespace tr_port tr_session::randomPort() const { @@ -869,8 +869,8 @@ void tr_session::setSettings(tr_session_settings&& settings_in, bool force) // We need to update bandwidth if speed settings changed. // It's a harmless call, so just call it instead of checking for settings changes - update_bandwidth(this, TR_UP); - update_bandwidth(this, TR_DOWN); + update_bandwidth(TR_UP); + update_bandwidth(TR_DOWN); } void tr_sessionSet(tr_session* session, tr_variant const& settings) @@ -1077,16 +1077,16 @@ uint16_t tr_sessionGetIdleLimit(tr_session const* session) // --- Speed limits -std::optional tr_session::activeSpeedLimitBps(tr_direction dir) const noexcept +std::optional tr_session::active_speed_limit(tr_direction dir) const noexcept { if (tr_sessionUsesAltSpeed(this)) { - return tr_toSpeedBytes(tr_sessionGetAltSpeed_KBps(this, dir)); + return alt_speeds_.speed_limit(dir); } - if (this->isSpeedLimited(dir)) + if (is_speed_limited(dir)) { - return tr_toSpeedBytes(tr_sessionGetSpeedLimit_KBps(this, dir)); + return speed_limit(dir); } return {}; @@ -1101,8 +1101,8 @@ void tr_session::AltSpeedMediator::is_active_changed(bool is_active, tr_session_ { auto const in_session_thread = [session = &session_, is_active, reason]() { - update_bandwidth(session, TR_UP); - update_bandwidth(session, TR_DOWN); + session->update_bandwidth(TR_UP); + session->update_bandwidth(TR_DOWN); if (session->alt_speed_active_changed_func_ != nullptr) { @@ -1119,32 +1119,23 @@ void tr_session::AltSpeedMediator::is_active_changed(bool is_active, tr_session_ // --- Session primary speed limits -void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit) +void tr_sessionSetSpeedLimit_KBps(tr_session* const session, tr_direction const dir, size_t const limit_kbyps) { TR_ASSERT(session != nullptr); TR_ASSERT(tr_isDirection(dir)); - if (dir == TR_DOWN) - { - session->settings_.speed_limit_down = limit; - } - else - { - session->settings_.speed_limit_up = limit; - } - - update_bandwidth(session, dir); + session->set_speed_limit(dir, Speed{ limit_kbyps, Speed::Units::KByps }); } -tr_kilobytes_per_second_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir) +size_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir) { TR_ASSERT(session != nullptr); TR_ASSERT(tr_isDirection(dir)); - return dir == TR_DOWN ? session->settings_.speed_limit_down : session->settings_.speed_limit_up; + return session->speed_limit(dir).count(Speed::Units::KByps); } -void tr_sessionLimitSpeed(tr_session* session, tr_direction dir, bool limited) +void tr_sessionLimitSpeed(tr_session* session, tr_direction const dir, bool limited) { TR_ASSERT(session != nullptr); TR_ASSERT(tr_isDirection(dir)); @@ -1158,34 +1149,34 @@ void tr_sessionLimitSpeed(tr_session* session, tr_direction dir, bool limited) session->settings_.speed_limit_up_enabled = limited; } - update_bandwidth(session, dir); + session->update_bandwidth(dir); } -bool tr_sessionIsSpeedLimited(tr_session const* session, tr_direction dir) +bool tr_sessionIsSpeedLimited(tr_session const* session, tr_direction const dir) { TR_ASSERT(session != nullptr); TR_ASSERT(tr_isDirection(dir)); - return session->isSpeedLimited(dir); + return session->is_speed_limited(dir); } // --- Session alt speed limits -void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit) +void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction const dir, size_t limit_kbyps) { TR_ASSERT(session != nullptr); TR_ASSERT(tr_isDirection(dir)); - session->alt_speeds_.set_limit_kbps(dir, limit); - update_bandwidth(session, dir); + session->alt_speeds_.set_speed_limit(dir, Speed{ limit_kbyps, Speed::Units::KByps }); + session->update_bandwidth(dir); } -tr_kilobytes_per_second_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir) +size_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir) { TR_ASSERT(session != nullptr); TR_ASSERT(tr_isDirection(dir)); - return session->alt_speeds_.limit_kbps(dir); + return session->alt_speeds_.speed_limit(dir).count(Speed::Units::KByps); } void tr_sessionUseAltSpeedTime(tr_session* session, bool enabled) diff --git a/libtransmission/session.h b/libtransmission/session.h index 2db7c4b71..92fcae8ef 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -83,6 +83,8 @@ class SessionTest; /** @brief handle to an active libtransmission session */ struct tr_session { + using Speed = libtransmission::Values::Speed; + private: class BoundSocket { @@ -837,22 +839,30 @@ public: return global_ip_cache_->global_source_addr(type); } - [[nodiscard]] constexpr auto speedLimitKBps(tr_direction dir) const noexcept + [[nodiscard]] auto speed_limit(tr_direction const dir) const noexcept { - return dir == TR_DOWN ? settings_.speed_limit_down : settings_.speed_limit_up; + auto const kbyps = dir == TR_DOWN ? settings_.speed_limit_down : settings_.speed_limit_up; + return Speed{ kbyps, Speed::Units::KByps }; } - [[nodiscard]] constexpr auto isSpeedLimited(tr_direction dir) const noexcept + void set_speed_limit(tr_direction dir, Speed limit) noexcept + { + auto& tgt = dir == TR_DOWN ? settings_.speed_limit_down : settings_.speed_limit_up; + tgt = limit.count(Speed::Units::KByps); + update_bandwidth(dir); + } + + [[nodiscard]] constexpr auto is_speed_limited(tr_direction dir) const noexcept { return dir == TR_DOWN ? settings_.speed_limit_down_enabled : settings_.speed_limit_up_enabled; } - [[nodiscard]] auto pieceSpeedBps(tr_direction dir) const noexcept + [[nodiscard]] auto piece_speed(tr_direction dir) const noexcept { - return top_bandwidth_.get_piece_speed(0, dir).base_quantity(); + return top_bandwidth_.get_piece_speed(0, dir); } - [[nodiscard]] std::optional activeSpeedLimitBps(tr_direction dir) const noexcept; + [[nodiscard]] std::optional active_speed_limit(tr_direction dir) const noexcept; [[nodiscard]] constexpr auto isIncompleteFileNamingEnabled() const noexcept { @@ -933,6 +943,8 @@ private: return settings_.script_torrent_done_seeding_filename; } + void update_bandwidth(tr_direction dir); + [[nodiscard]] tr_port randomPort() const; void onAdvertisedPeerPortChanged(); @@ -969,8 +981,7 @@ private: friend size_t tr_sessionGetAltSpeedBegin(tr_session const* session); friend size_t tr_sessionGetAltSpeedEnd(tr_session const* session); friend size_t tr_sessionGetCacheLimit_MB(tr_session const* session); - friend tr_kilobytes_per_second_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir); - friend tr_kilobytes_per_second_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir); + friend size_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir); friend tr_port_forwarding_state tr_sessionGetPortForwarding(tr_session const* session); friend tr_sched_day tr_sessionGetAltSpeedDay(tr_session const* session); friend tr_session* tr_sessionInit(char const* config_dir, bool message_queueing_enabled, tr_variant const& client_settings); @@ -986,7 +997,7 @@ private: friend void tr_sessionSetAltSpeedDay(tr_session* session, tr_sched_day days); friend void tr_sessionSetAltSpeedEnd(tr_session* session, size_t minutes_since_midnight); friend void tr_sessionSetAltSpeedFunc(tr_session* session, tr_altSpeedFunc func, void* user_data); - friend void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, tr_bytes_per_second_t limit); + friend void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, size_t limit_kbyps); friend void tr_sessionSetAntiBruteForceEnabled(tr_session* session, bool is_enabled); friend void tr_sessionSetAntiBruteForceThreshold(tr_session* session, int max_bad_requests); friend void tr_sessionSetCacheLimit_MB(tr_session* session, size_t mb); @@ -1016,7 +1027,6 @@ private: friend void tr_sessionSetRPCUsername(tr_session* session, char const* username); friend void tr_sessionSetRatioLimit(tr_session* session, double desired_ratio); friend void tr_sessionSetRatioLimited(tr_session* session, bool is_limited); - friend void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit); friend void tr_sessionSetUTPEnabled(tr_session* session, bool enabled); friend void tr_sessionUseAltSpeed(tr_session* session, bool enabled); friend void tr_sessionUseAltSpeedTime(tr_session* session, bool enabled); diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 833cb9c77..c8d2fe03a 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -212,12 +212,12 @@ bool tr_torrentIsSeedRatioDone(tr_torrent const* tor) // --- PER-TORRENT UL / DL SPEEDS -void tr_torrentSetSpeedLimit_KBps(tr_torrent* tor, tr_direction dir, tr_kilobytes_per_second_t kbyps) +void tr_torrentSetSpeedLimit_KBps(tr_torrent* const tor, tr_direction const dir, size_t const limit_kbyps) { - tor->set_speed_limit(dir, Speed{ kbyps, Speed::Units::KByps }); + tor->set_speed_limit(dir, Speed{ limit_kbyps, Speed::Units::KByps }); } -tr_kilobytes_per_second_t tr_torrentGetSpeedLimit_KBps(tr_torrent const* tor, tr_direction dir) +size_t tr_torrentGetSpeedLimit_KBps(tr_torrent const* const tor, tr_direction const dir) { TR_ASSERT(tr_isTorrent(tor)); TR_ASSERT(tr_isDirection(dir)); @@ -225,7 +225,7 @@ tr_kilobytes_per_second_t tr_torrentGetSpeedLimit_KBps(tr_torrent const* tor, tr return tor->speed_limit(dir).count(Speed::Units::KByps); } -void tr_torrentUseSpeedLimit(tr_torrent* tor, tr_direction dir, bool enabled) +void tr_torrentUseSpeedLimit(tr_torrent* const tor, tr_direction const dir, bool const enabled) { TR_ASSERT(tr_isTorrent(tor)); TR_ASSERT(tr_isDirection(dir)); @@ -233,14 +233,14 @@ void tr_torrentUseSpeedLimit(tr_torrent* tor, tr_direction dir, bool enabled) tor->use_speed_limit(dir, enabled); } -bool tr_torrentUsesSpeedLimit(tr_torrent const* tor, tr_direction dir) +bool tr_torrentUsesSpeedLimit(tr_torrent const* const tor, tr_direction const dir) { TR_ASSERT(tr_isTorrent(tor)); return tor->uses_speed_limit(dir); } -void tr_torrentUseSessionLimits(tr_torrent* tor, bool enabled) +void tr_torrentUseSessionLimits(tr_torrent* const tor, bool const enabled) { TR_ASSERT(tr_isTorrent(tor)); @@ -1022,9 +1022,9 @@ void tr_torrent::init(tr_ctor const* const ctor) if ((loaded & tr_resume::Speedlimit) == 0) { use_speed_limit(TR_UP, false); - set_speed_limit(TR_UP, Speed{ session->speedLimitKBps(TR_UP), Speed::Units::KByps }); + set_speed_limit(TR_UP, session->speed_limit(TR_UP)); use_speed_limit(TR_DOWN, false); - set_speed_limit(TR_DOWN, Speed{ session->speedLimitKBps(TR_DOWN), Speed::Units::KByps }); + set_speed_limit(TR_DOWN, session->speed_limit(TR_DOWN)); tr_torrentUseSessionLimits(this, true); } diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 15ecc536b..16aaeb0f7 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -1120,7 +1120,7 @@ private: if (uses_session_limits()) { - if (auto const limit = session->activeSpeedLimitBps(direction); limit && *limit == 0U) + if (auto const limit = session->active_speed_limit(direction); limit && limit->base_quantity() == 0U) { return false; } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 113cb45c5..2c8aec356 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -33,7 +33,6 @@ using tr_tracker_tier_t = uint32_t; using tr_tracker_id_t = uint32_t; using tr_torrent_id_t = int; using tr_bytes_per_second_t = size_t; -using tr_kilobytes_per_second_t = size_t; using tr_mode_t = uint16_t; struct tr_block_span_t @@ -489,16 +488,16 @@ enum tr_direction // --- Session primary speed limits -tr_kilobytes_per_second_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir); -void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit); +size_t tr_sessionGetSpeedLimit_KBps(tr_session const* session, tr_direction dir); +void tr_sessionSetSpeedLimit_KBps(tr_session* session, tr_direction dir, size_t limit_kbyps); bool tr_sessionIsSpeedLimited(tr_session const* session, tr_direction dir); void tr_sessionLimitSpeed(tr_session* session, tr_direction dir, bool limited); // --- Session alt speed limits -tr_kilobytes_per_second_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir); -void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, tr_kilobytes_per_second_t limit); +size_t tr_sessionGetAltSpeed_KBps(tr_session const* session, tr_direction dir); +void tr_sessionSetAltSpeed_KBps(tr_session* session, tr_direction dir, size_t limit_kbyps); bool tr_sessionUsesAltSpeed(tr_session const* session); void tr_sessionUseAltSpeed(tr_session* session, bool enabled); @@ -968,8 +967,8 @@ size_t tr_torrentFindFileToBuf(tr_torrent const* tor, tr_file_index_t file_num, // --- Torrent speed limits -tr_kilobytes_per_second_t tr_torrentGetSpeedLimit_KBps(tr_torrent const* tor, tr_direction dir); -void tr_torrentSetSpeedLimit_KBps(tr_torrent* tor, tr_direction dir, tr_kilobytes_per_second_t kbyps); +size_t tr_torrentGetSpeedLimit_KBps(tr_torrent const* tor, tr_direction dir); +void tr_torrentSetSpeedLimit_KBps(tr_torrent* tor, tr_direction dir, size_t limit_kbyps); bool tr_torrentUsesSpeedLimit(tr_torrent const* tor, tr_direction dir); void tr_torrentUseSpeedLimit(tr_torrent* tor, tr_direction dir, bool enabled);