mirror of
https://github.com/transmission/transmission.git
synced 2025-12-24 04:18:39 +00:00
refactor: rename Bandwidth as tr_bandwidth (#3379)
This commit is contained in:
@@ -4,30 +4,25 @@
|
||||
// License text can be found in the licenses/ folder.
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility> // std::swap()
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#include "transmission.h"
|
||||
|
||||
#include "bandwidth.h"
|
||||
#include "crypto-utils.h" /* tr_rand_int_weak() */
|
||||
#include "error.h"
|
||||
#include "crypto-utils.h" // tr_rand_int_weak()
|
||||
#include "log.h"
|
||||
#include "peer-io.h"
|
||||
#include "platform.h"
|
||||
#include "quark.h"
|
||||
#include "session.h"
|
||||
#include "tr-assert.h"
|
||||
#include "utils.h"
|
||||
#include "variant.h"
|
||||
#include "utils.h" // tr_time_msec()
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
||||
unsigned int Bandwidth::getSpeedBytesPerSecond(RateControl& r, unsigned int interval_msec, uint64_t now)
|
||||
unsigned int tr_bandwidth::getSpeedBytesPerSecond(RateControl& r, unsigned int interval_msec, uint64_t now)
|
||||
{
|
||||
if (now == 0)
|
||||
{
|
||||
@@ -61,7 +56,7 @@ unsigned int Bandwidth::getSpeedBytesPerSecond(RateControl& r, unsigned int inte
|
||||
return r.cache_val_;
|
||||
}
|
||||
|
||||
void Bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, RateControl* r, size_t size)
|
||||
void tr_bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, RateControl* r, size_t size)
|
||||
{
|
||||
if (r->date_[r->newest_] + GranularityMSec >= now)
|
||||
{
|
||||
@@ -86,7 +81,7 @@ void Bandwidth::notifyBandwidthConsumedBytes(uint64_t const now, RateControl* r,
|
||||
****
|
||||
***/
|
||||
|
||||
Bandwidth::Bandwidth(Bandwidth* parent)
|
||||
tr_bandwidth::tr_bandwidth(tr_bandwidth* parent)
|
||||
{
|
||||
this->setParent(parent);
|
||||
}
|
||||
@@ -95,7 +90,7 @@ Bandwidth::Bandwidth(Bandwidth* parent)
|
||||
****
|
||||
***/
|
||||
|
||||
static void remove_child(std::vector<Bandwidth*>& v, Bandwidth* remove_me)
|
||||
static void remove_child(std::vector<tr_bandwidth*>& v, tr_bandwidth* remove_me)
|
||||
{
|
||||
auto it = std::find(std::begin(v), std::end(v), remove_me);
|
||||
if (it == std::end(v))
|
||||
@@ -109,7 +104,7 @@ static void remove_child(std::vector<Bandwidth*>& v, Bandwidth* remove_me)
|
||||
v.resize(v.size() - 1);
|
||||
}
|
||||
|
||||
void Bandwidth::setParent(Bandwidth* new_parent)
|
||||
void tr_bandwidth::setParent(tr_bandwidth* new_parent)
|
||||
{
|
||||
TR_ASSERT(this != new_parent);
|
||||
|
||||
@@ -136,7 +131,7 @@ void Bandwidth::setParent(Bandwidth* new_parent)
|
||||
****
|
||||
***/
|
||||
|
||||
void Bandwidth::allocateBandwidth(
|
||||
void tr_bandwidth::allocateBandwidth(
|
||||
tr_priority_t parent_priority,
|
||||
tr_direction dir,
|
||||
unsigned int period_msec,
|
||||
@@ -165,7 +160,7 @@ void Bandwidth::allocateBandwidth(
|
||||
}
|
||||
}
|
||||
|
||||
void Bandwidth::phaseOne(std::vector<tr_peerIo*>& peerArray, tr_direction dir)
|
||||
void tr_bandwidth::phaseOne(std::vector<tr_peerIo*>& peerArray, tr_direction dir)
|
||||
{
|
||||
/* First phase of IO. Tries to distribute bandwidth fairly to keep faster
|
||||
* peers from starving the others. Loop through the peers, giving each a
|
||||
@@ -196,7 +191,7 @@ void Bandwidth::phaseOne(std::vector<tr_peerIo*>& peerArray, tr_direction dir)
|
||||
}
|
||||
}
|
||||
|
||||
void Bandwidth::allocate(tr_direction dir, unsigned int period_msec)
|
||||
void tr_bandwidth::allocate(tr_direction dir, unsigned int period_msec)
|
||||
{
|
||||
TR_ASSERT(tr_isDirection(dir));
|
||||
|
||||
@@ -241,7 +236,7 @@ void Bandwidth::allocate(tr_direction dir, unsigned int period_msec)
|
||||
/* Second phase of IO. To help us scale in high bandwidth situations,
|
||||
* enable on-demand IO for peers with bandwidth left to burn.
|
||||
* This on-demand IO is enabled until (1) the peer runs out of bandwidth,
|
||||
* or (2) the next Bandwidth::allocate () call, when we start over again. */
|
||||
* or (2) the next tr_bandwidth::allocate () call, when we start over again. */
|
||||
for (auto* io : tmp)
|
||||
{
|
||||
tr_peerIoSetEnabled(io, dir, io->hasBandwidthLeft(dir));
|
||||
@@ -257,7 +252,7 @@ void Bandwidth::allocate(tr_direction dir, unsigned int period_msec)
|
||||
****
|
||||
***/
|
||||
|
||||
unsigned int Bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byte_count) const
|
||||
unsigned int tr_bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byte_count) const
|
||||
{
|
||||
TR_ASSERT(tr_isDirection(dir));
|
||||
|
||||
@@ -301,7 +296,7 @@ unsigned int Bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byte_
|
||||
return byte_count;
|
||||
}
|
||||
|
||||
void Bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byte_count, bool is_piece_data, uint64_t now)
|
||||
void tr_bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byte_count, bool is_piece_data, uint64_t now)
|
||||
{
|
||||
TR_ASSERT(tr_isDirection(dir));
|
||||
|
||||
@@ -345,7 +340,7 @@ void Bandwidth::notifyBandwidthConsumed(tr_direction dir, size_t byte_count, boo
|
||||
****
|
||||
***/
|
||||
|
||||
tr_bandwidth_limits Bandwidth::getLimits() const
|
||||
tr_bandwidth_limits tr_bandwidth::getLimits() const
|
||||
{
|
||||
tr_bandwidth_limits limits;
|
||||
limits.up_limit_KBps = tr_toSpeedKBps(this->getDesiredSpeedBytesPerSecond(TR_UP));
|
||||
@@ -355,7 +350,7 @@ tr_bandwidth_limits Bandwidth::getLimits() const
|
||||
return limits;
|
||||
}
|
||||
|
||||
void Bandwidth::setLimits(tr_bandwidth_limits const* limits)
|
||||
void tr_bandwidth::setLimits(tr_bandwidth_limits const* limits)
|
||||
{
|
||||
this->setDesiredSpeedBytesPerSecond(TR_UP, tr_toSpeedBytes(limits->up_limit_KBps));
|
||||
this->setDesiredSpeedBytesPerSecond(TR_DOWN, tr_toSpeedBytes(limits->down_limit_KBps));
|
||||
|
||||
@@ -34,9 +34,9 @@ struct tr_bandwidth_limits
|
||||
};
|
||||
|
||||
/**
|
||||
* Bandwidth is an object for measuring and constraining bandwidth speeds.
|
||||
* tr_bandwidth is an object for measuring and constraining bandwidth speeds.
|
||||
*
|
||||
* Bandwidth objects can be "stacked" so that a peer can be made to obey
|
||||
* tr_bandwidth objects can be "stacked" so that a peer can be made to obey
|
||||
* multiple constraints (for example, obeying the global speed limit and a
|
||||
* per-torrent speed limit).
|
||||
*
|
||||
@@ -60,42 +60,48 @@ struct tr_bandwidth_limits
|
||||
*
|
||||
* CONSTRAINING
|
||||
*
|
||||
* Call Bandwidth::allocate() periodically. tr_bandwidth knows its current
|
||||
* Call tr_bandwidth::allocate() periodically. tr_bandwidth knows its current
|
||||
* speed and will decide how many bytes to make available over the
|
||||
* user-specified period to reach the user-specified desired speed.
|
||||
* If appropriate, it notifies its peer-ios that new bandwidth is available.
|
||||
*
|
||||
* Bandwidth::allocate() operates on the tr_bandwidth subtree, so usually
|
||||
* tr_bandwidth::allocate() operates on the tr_bandwidth subtree, so usually
|
||||
* you'll only need to invoke it for the top-level tr_session bandwidth.
|
||||
*
|
||||
* The peer-ios all have a pointer to their associated tr_bandwidth object,
|
||||
* and call Bandwidth::clamp() before performing I/O to see how much
|
||||
* and call tr_bandwidth::clamp() before performing I/O to see how much
|
||||
* bandwidth they can safely use.
|
||||
*/
|
||||
struct Bandwidth
|
||||
struct tr_bandwidth
|
||||
{
|
||||
public:
|
||||
explicit Bandwidth(Bandwidth* newParent);
|
||||
private:
|
||||
static constexpr size_t HistoryMSec = 2000U;
|
||||
static constexpr size_t IntervalMSec = HistoryMSec;
|
||||
static constexpr size_t GranularityMSec = 250;
|
||||
static constexpr size_t HistorySize = (IntervalMSec / GranularityMSec);
|
||||
|
||||
Bandwidth()
|
||||
: Bandwidth(nullptr)
|
||||
public:
|
||||
explicit tr_bandwidth(tr_bandwidth* newParent);
|
||||
|
||||
tr_bandwidth()
|
||||
: tr_bandwidth(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
~Bandwidth()
|
||||
~tr_bandwidth()
|
||||
{
|
||||
this->setParent(nullptr);
|
||||
}
|
||||
|
||||
Bandwidth& operator=(Bandwidth&&) = delete;
|
||||
Bandwidth& operator=(Bandwidth) = delete;
|
||||
Bandwidth(Bandwidth&&) = delete;
|
||||
Bandwidth(Bandwidth&) = delete;
|
||||
tr_bandwidth& operator=(tr_bandwidth&&) = delete;
|
||||
tr_bandwidth& operator=(tr_bandwidth) = delete;
|
||||
tr_bandwidth(tr_bandwidth&&) = delete;
|
||||
tr_bandwidth(tr_bandwidth&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Sets new peer, nullptr is allowed.
|
||||
*/
|
||||
constexpr void setPeer(tr_peerIo* peer)
|
||||
constexpr void setPeer(tr_peerIo* peer) noexcept
|
||||
{
|
||||
this->peer_ = peer;
|
||||
}
|
||||
@@ -111,7 +117,7 @@ public:
|
||||
*/
|
||||
void allocate(tr_direction dir, unsigned int period_msec);
|
||||
|
||||
void setParent(Bandwidth* newParent);
|
||||
void setParent(tr_bandwidth* newParent);
|
||||
|
||||
[[nodiscard]] constexpr tr_priority_t getPriority() const noexcept
|
||||
{
|
||||
@@ -149,8 +155,8 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Set the desired speed for this bandwidth subtree.
|
||||
* @see Bandwidth::allocate
|
||||
* @see Bandwidth::getDesiredSpeed
|
||||
* @see tr_bandwidth::allocate
|
||||
* @see tr_bandwidth::getDesiredSpeed
|
||||
*/
|
||||
constexpr bool setDesiredSpeedBytesPerSecond(tr_direction dir, unsigned int desired_speed)
|
||||
{
|
||||
@@ -162,7 +168,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Get the desired speed for the bandwidth subtree.
|
||||
* @see Bandwidth::setDesiredSpeed
|
||||
* @see tr_bandwidth::setDesiredSpeed
|
||||
*/
|
||||
[[nodiscard]] constexpr double getDesiredSpeedBytesPerSecond(tr_direction dir) const
|
||||
{
|
||||
@@ -209,11 +215,6 @@ public:
|
||||
return this->band_[direction].honor_parent_limits_;
|
||||
}
|
||||
|
||||
static constexpr size_t HistoryMSec = 2000U;
|
||||
static constexpr size_t IntervalMSec = HistoryMSec;
|
||||
static constexpr size_t GranularityMSec = 250;
|
||||
static constexpr size_t HistorySize = (IntervalMSec / GranularityMSec);
|
||||
|
||||
struct RateControl
|
||||
{
|
||||
std::array<uint64_t, HistorySize> date_;
|
||||
@@ -233,7 +234,8 @@ public:
|
||||
bool honor_parent_limits_ = true;
|
||||
};
|
||||
|
||||
tr_bandwidth_limits getLimits() const;
|
||||
[[nodiscard]] tr_bandwidth_limits getLimits() const;
|
||||
|
||||
void setLimits(tr_bandwidth_limits const* limits);
|
||||
|
||||
[[nodiscard]] constexpr auto* parent() noexcept
|
||||
@@ -257,8 +259,8 @@ private:
|
||||
std::vector<tr_peerIo*>& peer_pool);
|
||||
|
||||
mutable std::array<Band, 2> band_ = {};
|
||||
std::vector<Bandwidth*> children_;
|
||||
Bandwidth* parent_ = nullptr;
|
||||
std::vector<tr_bandwidth*> children_;
|
||||
tr_bandwidth* parent_ = nullptr;
|
||||
tr_peerIo* peer_ = nullptr;
|
||||
tr_priority_t priority_ = 0;
|
||||
};
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
|
||||
class tr_peer;
|
||||
class tr_swarm;
|
||||
struct Bandwidth;
|
||||
struct peer_atom;
|
||||
struct tr_bandwidth;
|
||||
|
||||
/**
|
||||
*** Peer Publish / Subscribe
|
||||
@@ -82,7 +82,7 @@ public:
|
||||
|
||||
[[nodiscard]] virtual bool hasPiece(tr_piece_index_t piece) const noexcept = 0;
|
||||
|
||||
[[nodiscard]] virtual Bandwidth* bandwidth() noexcept = 0;
|
||||
[[nodiscard]] virtual tr_bandwidth& bandwidth() noexcept = 0;
|
||||
|
||||
// requests that have been made but haven't been fulfilled yet
|
||||
[[nodiscard]] virtual size_t activeReqCount(tr_direction) const noexcept = 0;
|
||||
|
||||
@@ -153,11 +153,11 @@ static void didWriteWrapper(tr_peerIo* io, unsigned int bytes_transferred)
|
||||
unsigned int const overhead = io->socket.type == TR_PEER_SOCKET_TYPE_TCP ? guessPacketOverhead(payload) : 0;
|
||||
uint64_t const now = tr_time_msec();
|
||||
|
||||
io->bandwidth->notifyBandwidthConsumed(TR_UP, payload, next->isPieceData, now);
|
||||
io->bandwidth().notifyBandwidthConsumed(TR_UP, payload, next->isPieceData, now);
|
||||
|
||||
if (overhead > 0)
|
||||
{
|
||||
io->bandwidth->notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
||||
io->bandwidth().notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
||||
}
|
||||
|
||||
if (io->didWrite != nullptr)
|
||||
@@ -207,18 +207,18 @@ static void canReadWrapper(tr_peerIo* io)
|
||||
{
|
||||
if (piece != 0)
|
||||
{
|
||||
io->bandwidth->notifyBandwidthConsumed(TR_DOWN, piece, true, now);
|
||||
io->bandwidth().notifyBandwidthConsumed(TR_DOWN, piece, true, now);
|
||||
}
|
||||
|
||||
if (used != piece)
|
||||
{
|
||||
io->bandwidth->notifyBandwidthConsumed(TR_DOWN, used - piece, false, now);
|
||||
io->bandwidth().notifyBandwidthConsumed(TR_DOWN, used - piece, false, now);
|
||||
}
|
||||
}
|
||||
|
||||
if (overhead > 0)
|
||||
{
|
||||
io->bandwidth->notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
||||
io->bandwidth().notifyBandwidthConsumed(TR_UP, overhead, false, now);
|
||||
}
|
||||
|
||||
switch (ret)
|
||||
@@ -263,7 +263,7 @@ static void event_read_cb(evutil_socket_t fd, short /*event*/, void* vio)
|
||||
|
||||
unsigned int const curlen = evbuffer_get_length(io->inbuf.get());
|
||||
unsigned int howmuch = curlen >= max ? 0 : max - curlen;
|
||||
howmuch = io->bandwidth->clamp(TR_DOWN, howmuch);
|
||||
howmuch = io->bandwidth().clamp(TR_DOWN, howmuch);
|
||||
|
||||
tr_logAddTraceIo(io, "libevent says this peer is ready to read");
|
||||
|
||||
@@ -342,7 +342,7 @@ static void event_write_cb(evutil_socket_t fd, short /*event*/, void* vio)
|
||||
|
||||
/* Write as much as possible, since the socket is non-blocking, write() will
|
||||
* return if it can't write any more data without blocking */
|
||||
size_t const howmuch = io->bandwidth->clamp(dir, evbuffer_get_length(io->outbuf.get()));
|
||||
size_t const howmuch = io->bandwidth().clamp(dir, evbuffer_get_length(io->outbuf.get()));
|
||||
|
||||
/* if we don't have any bandwidth left, stop writing */
|
||||
if (howmuch < 1)
|
||||
@@ -462,7 +462,7 @@ static size_t utp_get_rb_size(void* vio)
|
||||
|
||||
TR_ASSERT(tr_isPeerIo(io));
|
||||
|
||||
size_t bytes = io->bandwidth->clamp(TR_DOWN, UtpReadBufferSize);
|
||||
size_t bytes = io->bandwidth().clamp(TR_DOWN, UtpReadBufferSize);
|
||||
|
||||
tr_logAddTraceIo(io, fmt::format("utp_get_rb_size is saying it's ready to read {} bytes", bytes));
|
||||
return UtpReadBufferSize - bytes;
|
||||
@@ -539,7 +539,7 @@ static void utp_on_overhead(void* vio, bool send, size_t count, int /*type*/)
|
||||
|
||||
tr_logAddTraceIo(io, fmt::format("utp_on_overhead -- count is {}", count));
|
||||
|
||||
io->bandwidth->notifyBandwidthConsumed(send ? TR_UP : TR_DOWN, count, false, tr_time_msec());
|
||||
io->bandwidth().notifyBandwidthConsumed(send ? TR_UP : TR_DOWN, count, false, tr_time_msec());
|
||||
}
|
||||
|
||||
static auto utp_function_table = UTPFunctionTable{
|
||||
@@ -589,7 +589,7 @@ static auto dummy_utp_function_table = UTPFunctionTable{
|
||||
|
||||
static tr_peerIo* tr_peerIoNew(
|
||||
tr_session* session,
|
||||
Bandwidth* parent,
|
||||
tr_bandwidth* parent,
|
||||
tr_address const* addr,
|
||||
tr_port port,
|
||||
time_t current_time,
|
||||
@@ -614,11 +614,10 @@ static tr_peerIo* tr_peerIoNew(
|
||||
maybeSetCongestionAlgorithm(socket.handle.tcp, session->peerCongestionAlgorithm());
|
||||
}
|
||||
|
||||
auto* io = new tr_peerIo{ session, torrent_hash, is_incoming, *addr, port, is_seed, current_time };
|
||||
auto* io = new tr_peerIo{ session, torrent_hash, is_incoming, *addr, port, is_seed, current_time, parent };
|
||||
io->socket = socket;
|
||||
io->bandwidth = new Bandwidth(parent);
|
||||
io->bandwidth->setPeer(io);
|
||||
tr_logAddTraceIo(io, fmt::format("bandwidth is {}; its parent is {}", fmt::ptr(&io->bandwidth), fmt::ptr(parent)));
|
||||
io->bandwidth().setPeer(io);
|
||||
tr_logAddTraceIo(io, fmt::format("bandwidth is {}; its parent is {}", fmt::ptr(&io->bandwidth()), fmt::ptr(parent)));
|
||||
|
||||
switch (socket.type)
|
||||
{
|
||||
@@ -655,7 +654,7 @@ static tr_peerIo* tr_peerIoNew(
|
||||
|
||||
tr_peerIo* tr_peerIoNewIncoming(
|
||||
tr_session* session,
|
||||
Bandwidth* parent,
|
||||
tr_bandwidth* parent,
|
||||
tr_address const* addr,
|
||||
tr_port port,
|
||||
time_t current_time,
|
||||
@@ -669,7 +668,7 @@ tr_peerIo* tr_peerIoNewIncoming(
|
||||
|
||||
tr_peerIo* tr_peerIoNewOutgoing(
|
||||
tr_session* session,
|
||||
Bandwidth* parent,
|
||||
tr_bandwidth* parent,
|
||||
tr_address const* addr,
|
||||
tr_port port,
|
||||
time_t current_time,
|
||||
@@ -855,7 +854,6 @@ static void io_dtor(tr_peerIo* const io)
|
||||
|
||||
tr_logAddTraceIo(io, "in tr_peerIo destructor");
|
||||
event_disable(io, EV_READ | EV_WRITE);
|
||||
delete io->bandwidth;
|
||||
io_close_socket(io);
|
||||
|
||||
while (io->outbuf_datatypes != nullptr)
|
||||
@@ -982,7 +980,7 @@ static unsigned int getDesiredOutputBufferSize(tr_peerIo const* io, uint64_t now
|
||||
* being large enough to hold the next 20 seconds' worth of input,
|
||||
* or a few blocks, whichever is bigger.
|
||||
* It's okay to tweak this as needed */
|
||||
unsigned int const currentSpeed_Bps = io->bandwidth->getPieceSpeedBytesPerSecond(now, TR_UP);
|
||||
unsigned int const currentSpeed_Bps = io->bandwidth().getPieceSpeedBytesPerSecond(now, TR_UP);
|
||||
unsigned int const period = 15U; /* arbitrary */
|
||||
/* the 3 is arbitrary; the .5 is to leave room for messages */
|
||||
static auto const ceiling = (unsigned int)(tr_block_info::BlockSize * 3.5);
|
||||
@@ -1177,7 +1175,7 @@ void tr_peerIoDrain(tr_peerIo* io, struct evbuffer* inbuf, size_t byteCount)
|
||||
|
||||
static int tr_peerIoTryRead(tr_peerIo* io, size_t howmuch)
|
||||
{
|
||||
howmuch = io->bandwidth->clamp(TR_DOWN, howmuch);
|
||||
howmuch = io->bandwidth().clamp(TR_DOWN, howmuch);
|
||||
if (howmuch == 0)
|
||||
{
|
||||
return 0;
|
||||
@@ -1242,7 +1240,7 @@ static int tr_peerIoTryWrite(tr_peerIo* io, size_t howmuch)
|
||||
|
||||
tr_logAddTraceIo(io, fmt::format("in tr_peerIoTryWrite {}", howmuch));
|
||||
howmuch = std::min(howmuch, old_len);
|
||||
howmuch = io->bandwidth->clamp(TR_UP, howmuch);
|
||||
howmuch = io->bandwidth().clamp(TR_UP, howmuch);
|
||||
if (howmuch == 0)
|
||||
{
|
||||
return 0;
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
#include "tr-assert.h"
|
||||
|
||||
class tr_peerIo;
|
||||
struct Bandwidth;
|
||||
struct tr_datatype;
|
||||
|
||||
/**
|
||||
@@ -82,10 +81,12 @@ public:
|
||||
tr_address const& addr,
|
||||
tr_port port,
|
||||
bool is_seed,
|
||||
time_t current_time)
|
||||
time_t current_time,
|
||||
tr_bandwidth* parent_bandwidth)
|
||||
: crypto{ torrent_hash, is_incoming }
|
||||
, session{ session_in }
|
||||
, time_created{ current_time }
|
||||
, bandwidth_{ parent_bandwidth }
|
||||
, addr_{ addr }
|
||||
, port_{ port }
|
||||
, is_seed_{ is_seed }
|
||||
@@ -116,12 +117,12 @@ public:
|
||||
|
||||
[[nodiscard]] auto hasBandwidthLeft(tr_direction dir) noexcept
|
||||
{
|
||||
return bandwidth->clamp(dir, 1024) > 0;
|
||||
return bandwidth_.clamp(dir, 1024) > 0;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto getPieceSpeed_Bps(uint64_t now, tr_direction dir) noexcept
|
||||
{
|
||||
return bandwidth->getPieceSpeedBytesPerSecond(now, dir);
|
||||
return bandwidth_.getPieceSpeedBytesPerSecond(now, dir);
|
||||
}
|
||||
|
||||
constexpr void enableFEXT(bool flag) noexcept
|
||||
@@ -164,6 +165,21 @@ public:
|
||||
return is_seed_;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto const& bandwidth() const noexcept
|
||||
{
|
||||
return bandwidth_;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto& bandwidth() noexcept
|
||||
{
|
||||
return bandwidth_;
|
||||
}
|
||||
|
||||
void setParent(tr_bandwidth* parent)
|
||||
{
|
||||
bandwidth_.setParent(parent);
|
||||
}
|
||||
|
||||
tr_crypto crypto;
|
||||
|
||||
// TODO(ckerr): yikes, unlike other class' magic_numbers it looks
|
||||
@@ -183,10 +199,6 @@ public:
|
||||
tr_net_error_cb gotError = nullptr;
|
||||
void* userData = nullptr;
|
||||
|
||||
// Changed to non-owning pointer temporarily till tr_peerIo becomes C++-constructible and destructible
|
||||
// TODO: change tr_bandwidth* to owning pointer to the bandwidth, or remove * and own the value
|
||||
Bandwidth* bandwidth = nullptr;
|
||||
|
||||
tr_evbuffer_ptr const inbuf = tr_evbuffer_ptr{ evbuffer_new() };
|
||||
tr_evbuffer_ptr const outbuf = tr_evbuffer_ptr{ evbuffer_new() };
|
||||
|
||||
@@ -208,6 +220,8 @@ public:
|
||||
bool utp_supported_ = false;
|
||||
|
||||
private:
|
||||
tr_bandwidth bandwidth_;
|
||||
|
||||
tr_address const addr_;
|
||||
tr_port const port_;
|
||||
|
||||
@@ -225,7 +239,7 @@ private:
|
||||
// TODO: 8 constructor args is too many; maybe a builder object?
|
||||
tr_peerIo* tr_peerIoNewOutgoing(
|
||||
tr_session* session,
|
||||
Bandwidth* parent,
|
||||
tr_bandwidth* parent,
|
||||
struct tr_address const* addr,
|
||||
tr_port port,
|
||||
time_t current_time,
|
||||
@@ -235,7 +249,7 @@ tr_peerIo* tr_peerIoNewOutgoing(
|
||||
|
||||
tr_peerIo* tr_peerIoNewIncoming(
|
||||
tr_session* session,
|
||||
Bandwidth* parent,
|
||||
tr_bandwidth* parent,
|
||||
struct tr_address const* addr,
|
||||
tr_port port,
|
||||
time_t current_time,
|
||||
@@ -344,13 +358,6 @@ void tr_peerIoDrain(tr_peerIo* io, struct evbuffer* inbuf, size_t byteCount);
|
||||
|
||||
size_t tr_peerIoGetWriteBufferSpace(tr_peerIo const* io, uint64_t now);
|
||||
|
||||
static inline void tr_peerIoSetParent(tr_peerIo* io, Bandwidth* parent)
|
||||
{
|
||||
TR_ASSERT(tr_isPeerIo(io));
|
||||
|
||||
io->bandwidth->setParent(parent);
|
||||
}
|
||||
|
||||
void tr_peerIoBandwidthUsed(tr_peerIo* io, tr_direction direction, size_t byteCount, int isPieceData);
|
||||
|
||||
/**
|
||||
|
||||
@@ -1165,7 +1165,7 @@ static bool on_handshake_done(tr_handshake_result const& result)
|
||||
|
||||
/* this steals its refcount too, which is balanced by our unref in peerDelete() */
|
||||
tr_peerIo* stolen = tr_handshakeStealIO(result.handshake);
|
||||
tr_peerIoSetParent(stolen, &s->tor->bandwidth_);
|
||||
stolen->setParent(&s->tor->bandwidth_);
|
||||
createBitTorrentPeer(s->tor, stolen, atom, client);
|
||||
|
||||
success = true;
|
||||
@@ -1990,7 +1990,7 @@ void rechokeDownloads(tr_swarm* s)
|
||||
***
|
||||
**/
|
||||
|
||||
[[nodiscard]] static inline bool isBandwidthMaxedOut(Bandwidth const& b, uint64_t const now_msec, tr_direction dir)
|
||||
[[nodiscard]] static inline bool isBandwidthMaxedOut(tr_bandwidth const& b, uint64_t const now_msec, tr_direction dir)
|
||||
{
|
||||
if (!b.isLimited(dir))
|
||||
{
|
||||
|
||||
@@ -359,9 +359,9 @@ public:
|
||||
return io->isIncoming();
|
||||
}
|
||||
|
||||
[[nodiscard]] Bandwidth* bandwidth() noexcept override
|
||||
[[nodiscard]] tr_bandwidth& bandwidth() noexcept override
|
||||
{
|
||||
return io->bandwidth;
|
||||
return io->bandwidth();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_active(tr_direction direction) const override
|
||||
|
||||
@@ -2277,7 +2277,7 @@ void tr_sessionSetDefaultTrackers(tr_session* session, char const* trackers)
|
||||
****
|
||||
***/
|
||||
|
||||
Bandwidth& tr_session::getBandwidthGroup(std::string_view name)
|
||||
tr_bandwidth& tr_session::getBandwidthGroup(std::string_view name)
|
||||
{
|
||||
auto& groups = this->bandwidth_groups_;
|
||||
|
||||
@@ -2289,7 +2289,7 @@ Bandwidth& tr_session::getBandwidthGroup(std::string_view name)
|
||||
}
|
||||
}
|
||||
|
||||
auto& [group_name, group] = groups.emplace_back(name, std::make_unique<Bandwidth>(new Bandwidth(&top_bandwidth_)));
|
||||
auto& [group_name, group] = groups.emplace_back(name, std::make_unique<tr_bandwidth>(new tr_bandwidth(&top_bandwidth_)));
|
||||
return *group;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ struct evdns_base;
|
||||
class tr_bitfield;
|
||||
class tr_rpc_server;
|
||||
class tr_web;
|
||||
struct Bandwidth;
|
||||
struct tr_bandwidth;
|
||||
struct tr_address;
|
||||
struct tr_announcer;
|
||||
struct tr_announcer_udp;
|
||||
@@ -275,7 +275,7 @@ public:
|
||||
|
||||
// bandwidth
|
||||
|
||||
[[nodiscard]] Bandwidth& getBandwidthGroup(std::string_view name);
|
||||
[[nodiscard]] tr_bandwidth& getBandwidthGroup(std::string_view name);
|
||||
|
||||
//
|
||||
|
||||
@@ -423,9 +423,9 @@ public:
|
||||
struct event* saveTimer;
|
||||
|
||||
// monitors the "global pool" speeds
|
||||
Bandwidth top_bandwidth_;
|
||||
tr_bandwidth top_bandwidth_;
|
||||
|
||||
std::vector<std::pair<tr_interned_string, std::unique_ptr<Bandwidth>>> bandwidth_groups_;
|
||||
std::vector<std::pair<tr_interned_string, std::unique_ptr<tr_bandwidth>>> bandwidth_groups_;
|
||||
|
||||
float desiredRatio;
|
||||
|
||||
|
||||
@@ -614,7 +614,7 @@ public:
|
||||
|
||||
tr_torrent_metainfo metainfo_;
|
||||
|
||||
Bandwidth bandwidth_;
|
||||
tr_bandwidth bandwidth_;
|
||||
|
||||
tr_stat stats = {};
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
// Return the number of bytes that should be allowed. See Bandwidth::clamp()
|
||||
// Return the number of bytes that should be allowed. See tr_bandwidth::clamp()
|
||||
[[nodiscard]] virtual unsigned int clamp([[maybe_unused]] int bandwidth_tag, unsigned int byte_count) const
|
||||
{
|
||||
return byte_count;
|
||||
|
||||
@@ -196,9 +196,9 @@ public:
|
||||
return is_active;
|
||||
}
|
||||
|
||||
[[nodiscard]] Bandwidth* bandwidth() noexcept override
|
||||
[[nodiscard]] tr_bandwidth& bandwidth() noexcept override
|
||||
{
|
||||
return &bandwidth_;
|
||||
return bandwidth_;
|
||||
}
|
||||
|
||||
[[nodiscard]] size_t activeReqCount(tr_direction dir) const noexcept override
|
||||
@@ -302,7 +302,7 @@ private:
|
||||
webseed->startTimer();
|
||||
}
|
||||
|
||||
Bandwidth bandwidth_;
|
||||
tr_bandwidth bandwidth_;
|
||||
std::shared_ptr<event> const pulse_timer;
|
||||
static int constexpr IdleTimerMsec = 2000;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user