Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
depler committed Nov 25, 2022
1 parent f306528 commit 7ad015a
Show file tree
Hide file tree
Showing 34 changed files with 370 additions and 309 deletions.
2 changes: 1 addition & 1 deletion lib/libtransmission/announcer-http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ class scrape_data
return log_name_;
}

void invoke_callback()
void invoke_callback() const
{
if (response_func_)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/libtransmission/announcer-udp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct tau_scrape_request
return !!on_response_;
}

void requestFinished()
void requestFinished() const
{
if (on_response_)
{
Expand Down Expand Up @@ -194,7 +194,7 @@ struct tau_announce_request
return !!on_response_;
}

void requestFinished()
void requestFinished() const
{
if (on_response_)
{
Expand Down
25 changes: 19 additions & 6 deletions lib/libtransmission/announcer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ static auto constexpr DefaultAnnounceMinIntervalSec = int{ 60 * 2 };
static auto constexpr Numwant = int{ 80 };

/* how often to announce & scrape */
static auto constexpr UpkeepInterval = 500ms;
static auto constexpr MaxAnnouncesPerUpkeep = int{ 20 };
static auto constexpr MaxScrapesPerUpkeep = int{ 20 };

Expand Down Expand Up @@ -234,6 +233,8 @@ class tr_announcer_impl final : public tr_announcer
std::unique_ptr<libtransmission::Timer> const upkeep_timer_;

std::set<tr_announce_request, StopsCompare> stops_;

static auto constexpr UpkeepInterval = 500ms;
};

std::unique_ptr<tr_announcer> tr_announcer::create(tr_session* session, tr_announcer_udp& announcer_udp)
Expand Down Expand Up @@ -1191,8 +1192,13 @@ static void tierAnnounce(tr_announcer_impl* announcer, tr_tier* tier)

announcer->announce(
req,
[announcer, tier_id, event, is_running_on_success](tr_announce_response const& response)
{ announcer->onAnnounceDone(tier_id, event, is_running_on_success, response); });
[session = announcer->session, announcer, tier_id, event, is_running_on_success](tr_announce_response const& response)
{
if (session->announcer_)
{
announcer->onAnnounceDone(tier_id, event, is_running_on_success, response);
}
});
}

/***
Expand Down Expand Up @@ -1434,7 +1440,15 @@ static void multiscrape(tr_announcer_impl* announcer, std::vector<tr_tier*> cons
/* send the requests we just built */
for (size_t i = 0; i < request_count; ++i)
{
announcer->scrape(requests[i], [announcer](tr_scrape_response const& response) { announcer->onScrapeDone(response); });
announcer->scrape(
requests[i],
[session = announcer->session, announcer](tr_scrape_response const& response)
{
if (session->announcer_)
{
announcer->onScrapeDone(response);
}
});
}
}

Expand Down Expand Up @@ -1545,7 +1559,7 @@ void tr_announcer_impl::upkeep()
****
***/

static tr_tracker_view trackerView(tr_torrent const& tor, int tier_index, tr_tier const& tier, tr_tracker const& tracker)
static tr_tracker_view trackerView(tr_torrent const& tor, size_t tier_index, tr_tier const& tier, tr_tracker const& tracker)
{
auto const now = tr_time();
auto view = tr_tracker_view{};
Expand Down Expand Up @@ -1633,7 +1647,6 @@ static tr_tracker_view trackerView(tr_torrent const& tor, int tier_index, tr_tie
}
}

TR_ASSERT(0 <= view.tier);
return view;
}

Expand Down
17 changes: 8 additions & 9 deletions lib/libtransmission/bandwidth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ void tr_bandwidth::allocateBandwidth(
tr_priority_t const priority = std::max(parent_priority, this->priority_);

/* set the available bandwidth */
auto bandwidth = &this->band_[dir];
if (bandwidth->is_limited_)
if (auto& bandwidth = band_[dir]; bandwidth.is_limited_)
{
uint64_t const next_pulse_speed = bandwidth->desired_speed_bps_;
bandwidth->bytes_left_ = next_pulse_speed * period_msec / 1000U;
auto const next_pulse_speed = bandwidth.desired_speed_bps_;
bandwidth.bytes_left_ = next_pulse_speed * period_msec / 1000U;
}

/* add this bandwidth's peer, if any, to the peer pool */
Expand Down Expand Up @@ -253,7 +252,7 @@ void tr_bandwidth::allocate(tr_direction dir, unsigned int period_msec)
****
***/

unsigned int tr_bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int byte_count) const
size_t tr_bandwidth::clamp(uint64_t now, tr_direction dir, size_t byte_count) const
{
TR_ASSERT(tr_isDirection(dir));

Expand All @@ -272,19 +271,19 @@ unsigned int tr_bandwidth::clamp(uint64_t now, tr_direction dir, unsigned int by

auto const current = this->getRawSpeedBytesPerSecond(now, TR_DOWN);
auto const desired = this->getDesiredSpeedBytesPerSecond(TR_DOWN);
auto const r = desired >= 1 ? double(current) / desired : 0;
auto const r = desired >= 1 ? static_cast<double>(current) / desired : 0.0;

if (r > 1.0)
{
byte_count = 0;
byte_count = 0; // none left
}
else if (r > 0.9)
{
byte_count = static_cast<unsigned int>(byte_count * 0.8);
byte_count -= (byte_count / 5U); // cap at 80%
}
else if (r > 0.8)
{
byte_count = static_cast<unsigned int>(byte_count * 0.9);
byte_count -= (byte_count / 10U); // cap at 90%
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/libtransmission/bandwidth.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct tr_bandwidth
/**
* @brief clamps byte_count down to a number that this bandwidth will allow to be consumed
*/
[[nodiscard]] unsigned int clamp(tr_direction dir, unsigned int byte_count) const noexcept
[[nodiscard]] size_t clamp(tr_direction dir, size_t byte_count) const noexcept
{
return this->clamp(0, dir, byte_count);
}
Expand Down Expand Up @@ -230,7 +230,7 @@ struct tr_bandwidth
{
RateControl raw_;
RateControl piece_;
unsigned int bytes_left_;
size_t bytes_left_;
tr_bytes_per_second_t desired_speed_bps_;
bool is_limited_ = false;
bool honor_parent_limits_ = true;
Expand All @@ -250,7 +250,7 @@ struct tr_bandwidth

static void notifyBandwidthConsumedBytes(uint64_t now, RateControl* r, size_t size);

[[nodiscard]] unsigned int clamp(uint64_t now, tr_direction dir, unsigned int byte_count) const;
[[nodiscard]] size_t clamp(uint64_t now, tr_direction dir, size_t byte_count) const;

static void phaseOne(std::vector<tr_peerIo*>& peer_array, tr_direction dir);

Expand Down
52 changes: 52 additions & 0 deletions lib/libtransmission/bitfield.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,55 @@ void tr_bitfield::setSpan(size_t begin, size_t end, bool value)
decrementTrueCount(old_count);
}
}

tr_bitfield& tr_bitfield::operator|=(tr_bitfield const& that) noexcept
{
TR_ASSERT(size() == std::size(that));

if (hasAll() || that.hasNone())
{
return *this;
}

if (that.hasAll() || hasNone())
{
*this = that;
return *this;
}

flags_.resize(std::max(std::size(flags_), std::size(that.flags_)));

for (size_t i = 0, n = std::size(flags_); i < n; ++i)
{
flags_[i] |= that.flags_[i];
}

rebuildTrueCount();
return *this;
}

tr_bitfield& tr_bitfield::operator&=(tr_bitfield const& that) noexcept
{
TR_ASSERT(size() == std::size(that));

if (hasNone() || that.hasAll())
{
return *this;
}

if (that.hasNone() || hasAll())
{
*this = that;
return *this;
}

flags_.resize(std::min(std::size(flags_), std::size(that.flags_)));

for (size_t i = 0, n = std::size(flags_); i < n; ++i)
{
flags_[i] &= that.flags_[i];
}

rebuildTrueCount();
return *this;
}
18 changes: 18 additions & 0 deletions lib/libtransmission/bitfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ class tr_bitfield

[[nodiscard]] bool isValid() const;

[[nodiscard]] constexpr auto percent() const noexcept
{
if (hasAll())
{
return 1.0F;
}

if (hasNone() || empty())
{
return 0.0F;
}

return static_cast<float>(count()) / size();
}

tr_bitfield& operator|=(tr_bitfield const& that) noexcept;
tr_bitfield& operator&=(tr_bitfield const& that) noexcept;

private:
[[nodiscard]] size_t countFlags() const noexcept;
[[nodiscard]] size_t countFlags(size_t begin, size_t end) const noexcept;
Expand Down
4 changes: 2 additions & 2 deletions lib/libtransmission/crypto-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ std::string tr_sha1_to_string(tr_sha1_digest_t const& digest)
using namespace hex_impl;

auto str = std::string(std::size(digest) * 2, '?');
tr_binary_to_hex(std::data(digest), std::data(str), std::size(digest));
tr_binary_to_hex(digest.data(), str.data(), std::size(digest));
return str;
}

Expand All @@ -232,7 +232,7 @@ std::string tr_sha256_to_string(tr_sha256_digest_t const& digest)
using namespace hex_impl;

auto str = std::string(std::size(digest) * 2, '?');
tr_binary_to_hex(std::data(digest), std::data(str), std::size(digest));
tr_binary_to_hex(digest.data(), str.data(), std::size(digest));
return str;
}

Expand Down
24 changes: 12 additions & 12 deletions lib/libtransmission/handshake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,29 +256,29 @@ static bool buildHandshakeMessage(tr_handshake const* const handshake, uint8_t*

static ReadState tr_handshakeDone(tr_handshake* handshake, bool is_connected);

enum handshake_parse_err_t
enum class ParseResult
{
HANDSHAKE_OK,
HANDSHAKE_ENCRYPTION_WRONG,
HANDSHAKE_BAD_TORRENT,
HANDSHAKE_PEER_IS_SELF,
Ok,
EncryptionWrong,
BadTorrent,
PeerIsSelf,
};

static handshake_parse_err_t parseHandshake(tr_handshake* handshake, tr_peerIo* peer_io)
static ParseResult parseHandshake(tr_handshake* handshake, tr_peerIo* peer_io)
{
tr_logAddTraceHand(handshake, fmt::format("payload: need {}, got {}", HandshakeSize, peer_io->readBufferSize()));

if (peer_io->readBufferSize() < HandshakeSize)
{
return HANDSHAKE_ENCRYPTION_WRONG;
return ParseResult::EncryptionWrong;
}

/* confirm the protocol */
auto name = decltype(HandshakeName){};
peer_io->readBytes(std::data(name), std::size(name));
if (name != HandshakeName)
{
return HANDSHAKE_ENCRYPTION_WRONG;
return ParseResult::EncryptionWrong;
}

/* read the reserved bytes */
Expand All @@ -291,7 +291,7 @@ static handshake_parse_err_t parseHandshake(tr_handshake* handshake, tr_peerIo*
if (info_hash == tr_sha1_digest_t{} || info_hash != peer_io->torrentHash())
{
tr_logAddTraceHand(handshake, "peer returned the wrong hash. wtf?");
return HANDSHAKE_BAD_TORRENT;
return ParseResult::BadTorrent;
}

// peer_id
Expand All @@ -306,7 +306,7 @@ static handshake_parse_err_t parseHandshake(tr_handshake* handshake, tr_peerIo*
if (auto const info = handshake->mediator->torrentInfo(info_hash); info && info->client_peer_id == peer_id)
{
tr_logAddTraceHand(handshake, "streuth! we've connected to ourselves.");
return HANDSHAKE_PEER_IS_SELF;
return ParseResult::PeerIsSelf;
}

/**
Expand All @@ -317,7 +317,7 @@ static handshake_parse_err_t parseHandshake(tr_handshake* handshake, tr_peerIo*
peer_io->enableLTEP(HANDSHAKE_HAS_LTEP(reserved));
peer_io->enableFEXT(HANDSHAKE_HAS_FASTEXT(reserved));

return HANDSHAKE_OK;
return ParseResult::Ok;
}

/***
Expand Down Expand Up @@ -906,7 +906,7 @@ static ReadState readPayloadStream(tr_handshake* handshake, tr_peerIo* peer_io)
auto const i = parseHandshake(handshake, peer_io);
tr_logAddTraceHand(handshake, fmt::format("parseHandshake returned {}", static_cast<int>(i)));

if (i != HANDSHAKE_OK)
if (i != ParseResult::Ok)
{
return tr_handshakeDone(handshake, false);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/libtransmission/inout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,17 @@ std::optional<tr_sha1_digest_t> recalculateHash(tr_torrent* tor, tr_piece_index_

} // namespace

int tr_ioRead(tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t* setme)
int tr_ioRead(tr_torrent* tor, tr_block_info::Location loc, size_t len, uint8_t* setme)
{
return readOrWritePiece(tor, IoMode::Read, loc, setme, len);
}

int tr_ioPrefetch(tr_torrent* tor, tr_block_info::Location loc, uint32_t len)
int tr_ioPrefetch(tr_torrent* tor, tr_block_info::Location loc, size_t len)
{
return readOrWritePiece(tor, IoMode::Prefetch, loc, nullptr, len);
}

int tr_ioWrite(tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t const* writeme)
int tr_ioWrite(tr_torrent* tor, tr_block_info::Location loc, size_t len, uint8_t const* writeme)
{
return readOrWritePiece(tor, IoMode::Write, loc, const_cast<uint8_t*>(writeme), len);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/libtransmission/inout.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ struct tr_torrent;
* Reads the block specified by the piece index, offset, and length.
* @return 0 on success, or an errno value on failure.
*/
int tr_ioRead(struct tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t* setme);
int tr_ioRead(struct tr_torrent* tor, tr_block_info::Location loc, size_t len, uint8_t* setme);

int tr_ioPrefetch(tr_torrent* tor, tr_block_info::Location loc, uint32_t len);
int tr_ioPrefetch(tr_torrent* tor, tr_block_info::Location loc, size_t len);

/**
* Writes the block specified by the piece index, offset, and length.
* @return 0 on success, or an errno value on failure.
*/
int tr_ioWrite(struct tr_torrent* tor, tr_block_info::Location loc, uint32_t len, uint8_t const* writeme);
int tr_ioWrite(struct tr_torrent* tor, tr_block_info::Location loc, size_t len, uint8_t const* writeme);

/**
* @brief Test to see if the piece matches its metainfo's SHA1 checksum.
Expand Down
Loading

0 comments on commit 7ad015a

Please sign in to comment.