2009-06-15 00:11:06 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-06-15 00:11:06 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-06-15 00:11:06 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2021-10-16 14:04:19 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
#include "tr-assert.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/**
|
|
|
|
* @brief Implementation of the BitTorrent spec's Bitfield array of bits.
|
|
|
|
*
|
|
|
|
* This is for tracking the pieces a peer has. Its functionality is like
|
|
|
|
* a bitset or vector<bool> with some added use cases:
|
|
|
|
*
|
|
|
|
* - It needs to be able to read/write the left-to-right bitfield format
|
|
|
|
* specified in the bittorrent spec. This is what raw() and getRaw()
|
|
|
|
* are for.
|
|
|
|
*
|
|
|
|
* - "Have all" is a special case where we know the peer has all the
|
|
|
|
* pieces and don't need to check the bit array. This is useful since
|
|
|
|
* (a) it's very common (i.e. seeds) and saves memory and work of
|
|
|
|
* allocating a bit array and doing lookups, and (b) if we have a
|
|
|
|
* magnet link and haven't gotten the metainfo yet, we may not know
|
|
|
|
* how many pieces there are -- but we can still know "this peer has
|
|
|
|
* all of them".
|
|
|
|
*
|
|
|
|
* - "Have none" is another special case that has the same advantages
|
|
|
|
* and motivations as "Have all".
|
|
|
|
*/
|
|
|
|
class tr_bitfield
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2021-10-11 17:29:14 +00:00
|
|
|
public:
|
2021-10-24 20:43:36 +00:00
|
|
|
explicit tr_bitfield(size_t bit_count);
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void setHasAll();
|
|
|
|
void setHasNone();
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
// set one or more bits
|
|
|
|
void set(size_t bit, bool value = true);
|
|
|
|
void setRange(size_t begin, size_t end, bool value = true);
|
|
|
|
void unset(size_t bit)
|
2021-10-16 14:04:19 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
set(bit, false);
|
2021-10-16 14:04:19 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
void unsetRange(size_t begin, size_t end)
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
setRange(begin, end, false);
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
2021-10-24 20:43:36 +00:00
|
|
|
void setFromBools(bool const* bytes, size_t n);
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
// "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.
|
2021-11-08 00:25:45 +00:00
|
|
|
void setRaw(uint8_t const* bits, size_t byte_count);
|
2021-10-24 20:43:36 +00:00
|
|
|
std::vector<uint8_t> raw() const;
|
2021-10-11 17:29:14 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool hasAll() const
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return have_all_hint_ || (bit_count_ > 0 && bit_count_ == true_count_);
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool hasNone() const
|
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return have_none_hint_ || (bit_count_ > 0 && true_count_ == 0);
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
[[nodiscard]] bool test(size_t bit) const
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return hasAll() || (!hasNone() && testFlag(bit));
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
[[nodiscard]] constexpr size_t count() const
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return true_count_;
|
2021-10-16 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
[[nodiscard]] size_t count(size_t begin, size_t end) const;
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
[[nodiscard]] constexpr size_t size() const
|
2021-10-16 14:04:19 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return bit_count_;
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2021-10-11 17:29:14 +00:00
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
2021-10-24 20:43:36 +00:00
|
|
|
bool assertValid() const;
|
2021-10-11 17:29:14 +00:00
|
|
|
#endif
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
private:
|
|
|
|
std::vector<uint8_t> flags_;
|
|
|
|
[[nodiscard]] size_t countFlags() const;
|
|
|
|
[[nodiscard]] size_t countFlags(size_t begin, size_t end) const;
|
|
|
|
[[nodiscard]] bool testFlag(size_t bit) const;
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void ensureBitsAlloced(size_t n);
|
|
|
|
[[nodiscard]] bool ensureNthBitAlloced(size_t nth);
|
|
|
|
void freeArray();
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
void setTrueCount(size_t n);
|
|
|
|
void rebuildTrueCount();
|
|
|
|
void incrementTrueCount(size_t inc);
|
|
|
|
void decrementTrueCount(size_t dec);
|
2021-10-16 14:04:19 +00:00
|
|
|
|
|
|
|
size_t bit_count_ = 0;
|
|
|
|
size_t true_count_ = 0;
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
/* Special cases for when full or empty but we don't know the bitCount.
|
|
|
|
This occurs when a magnet link's peers send have all / have none */
|
|
|
|
bool have_all_hint_ = false;
|
|
|
|
bool have_none_hint_ = false;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|