fixup! refactor: remove tr_piece struct (#2059) (#2115)

yet another fixup of the tr_piece struct removal
This commit is contained in:
Charles Kerr 2021-11-07 18:25:45 -06:00 committed by GitHub
parent 2bd328c082
commit af4a953cd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 8 additions and 13 deletions

View File

@ -292,18 +292,13 @@ void tr_bitfield::setHasAll()
TR_ASSERT(assertValid());
}
void tr_bitfield::setRaw(uint8_t const* raw, size_t byte_count, bool bounded)
void tr_bitfield::setRaw(uint8_t const* raw, size_t byte_count)
{
if (bounded)
{
byte_count = std::min(byte_count, getBytesNeeded(bit_count_));
}
flags_ = std::vector<uint8_t>(raw, raw + byte_count);
if (bounded)
// ensure any excess bits at the end of the array are set to '0'.
if (byte_count == getBytesNeeded(bit_count_))
{
/* ensure the excess bits are set to '0' */
int const excess_bit_count = byte_count * 8 - bit_count_;
TR_ASSERT(excess_bit_count >= 0);

View File

@ -63,7 +63,7 @@ public:
// "raw" here is in BEP0003 format: "The first byte of the bitfield
// corresponds to indices 0 - 7 from high bit to low bit, respectively.
// The next one 8-15, etc. Spare bits at the end are set to zero.
void setRaw(uint8_t const* bits, size_t byte_count, bool bounded);
void setRaw(uint8_t const* bits, size_t byte_count);
std::vector<uint8_t> raw() const;
[[nodiscard]] constexpr bool hasAll() const

View File

@ -1715,7 +1715,7 @@ static ReadState readBtMessage(tr_peerMsgsImpl* msgs, struct evbuffer* inbuf, si
uint8_t* tmp = tr_new(uint8_t, msglen);
dbgmsg(msgs, "got a bitfield");
tr_peerIoReadBytes(msgs->io, inbuf, tmp, msglen);
msgs->have.setRaw(tmp, msglen, tr_torrentHasMetadata(msgs->torrent));
msgs->have.setRaw(tmp, msglen);
msgs->publishClientGotBitfield(&msgs->have);
updatePeerProgress(msgs);
tr_free(tmp);

View File

@ -485,7 +485,7 @@ static void rawToBitfield(tr_bitfield& bitfield, uint8_t const* raw, size_t rawl
}
else
{
bitfield.setRaw(raw, rawlen, true);
bitfield.setRaw(raw, rawlen);
}
}
@ -648,7 +648,7 @@ static uint64_t loadProgress(tr_variant* dict, tr_torrent* tor)
}
else if (tr_variantDictFindRaw(prog, TR_KEY_bitfield, &raw, &rawlen))
{
blocks.setRaw(raw, rawlen, true);
blocks.setRaw(raw, rawlen);
}
else
{

View File

@ -99,7 +99,7 @@ TEST(Bitfield, setRaw)
auto const raw = std::vector<uint8_t>(100, TestByte);
auto bf = tr_bitfield(std::size(raw) * 8);
bf.setRaw(std::data(raw), std::size(raw), true);
bf.setRaw(std::data(raw), std::size(raw));
EXPECT_EQ(TestByteTrueBits * std::size(raw), bf.count());
// The first byte of the bitfield corresponds to indices 0 - 7