minor simplification of bytes_left(), remove quantized_bytes_done()

This commit is contained in:
arvidn 2018-08-09 00:55:02 +02:00 committed by Arvid Norberg
parent 7be9547817
commit d55200af9d
4 changed files with 21 additions and 53 deletions

View File

@ -391,9 +391,9 @@ namespace libtorrent {
int num_passed() const { return m_num_passed; }
// return true if we have all the pieces we wanted
bool is_finished() const { return m_num_have - m_num_have_filtered == int(m_piece_map.size()) - m_num_filtered; }
bool is_finished() const { return m_num_have - m_num_have_filtered == num_pieces() - m_num_filtered; }
bool is_seeding() const { return m_num_have == int(m_piece_map.size()); }
bool is_seeding() const { return m_num_have == num_pieces(); }
// the number of pieces we want and don't have
int num_want_left() const { return num_pieces() - m_num_have - m_num_filtered + m_num_have_filtered; }

View File

@ -479,7 +479,6 @@ namespace libtorrent {
boost::optional<std::int64_t> bytes_left() const;
int block_bytes_wanted(piece_block const& p) const;
void bytes_done(torrent_status& st, bool accurate) const;
std::int64_t quantized_bytes_done() const;
void sent_bytes(int bytes_payload, int bytes_protocol);
void received_bytes(int bytes_payload, int bytes_protocol);

View File

@ -364,7 +364,7 @@ namespace libtorrent {
void piece_picker::verify_pick(std::vector<piece_block> const& picked
, typed_bitfield<piece_index_t> const& bits) const
{
TORRENT_ASSERT(bits.size() == int(m_piece_map.size()));
TORRENT_ASSERT(bits.size() == num_pieces());
for (piece_block const& pb : picked)
{
TORRENT_ASSERT(bits[pb.piece_index]);
@ -486,7 +486,7 @@ namespace libtorrent {
}
if (t != nullptr)
TORRENT_ASSERT(int(m_piece_map.size()) == t->torrent_file().num_pieces());
TORRENT_ASSERT(num_pieces() == t->torrent_file().num_pieces());
for (download_queue_t j{}; j != piece_pos::num_download_categories; ++j)
{
@ -588,9 +588,8 @@ namespace libtorrent {
, end(m_piece_map.end()); i != end && (i->have() || i->filtered());
++i, ++index);
TORRENT_ASSERT(m_cursor == index);
int const num_pieces = int(m_piece_map.size());
index = m_piece_map.end_index();
if (num_pieces > 0)
if (num_pieces() > 0)
{
for (auto i = m_piece_map.rend() - static_cast<int>(index); index > piece_index_t(0)
&& (i->have() || i->filtered()); ++i, --index);
@ -730,9 +729,9 @@ namespace libtorrent {
std::pair<int, int> piece_picker::distributed_copies() const
{
TORRENT_ASSERT(m_seeds >= 0);
const int num_pieces = int(m_piece_map.size());
const int npieces = num_pieces();
if (num_pieces == 0) return std::make_pair(1, 0);
if (npieces == 0) return std::make_pair(1, 0);
int min_availability = piece_pos::max_peer_count;
// find the lowest availability count
// count the number of pieces that have that availability
@ -761,8 +760,8 @@ namespace libtorrent {
++fraction_part;
}
}
TORRENT_ASSERT(integer_part + fraction_part == num_pieces);
return std::make_pair(min_availability + m_seeds, fraction_part * 1000 / num_pieces);
TORRENT_ASSERT(integer_part + fraction_part == npieces);
return std::make_pair(min_availability + m_seeds, fraction_part * 1000 / npieces);
}
prio_index_t piece_picker::priority_begin(int const prio) const

View File

@ -3439,40 +3439,24 @@ bool is_downloading_state(int const st)
// if we don't have the metadata yet, we
// cannot tell how big the torrent is.
if (!valid_metadata()) return {};
return m_torrent_file->total_size()
- quantized_bytes_done();
}
TORRENT_ASSERT(m_torrent_file->num_pieces() > 0);
if (m_seed_mode) return std::int64_t(0);
if (!has_picker()) return m_seed_mode ? std::int64_t(0) : m_torrent_file->total_size();
std::int64_t torrent::quantized_bytes_done() const
{
// INVARIANT_CHECK;
if (!valid_metadata()) return 0;
if (m_torrent_file->num_pieces() == 0)
return 0;
// if any piece hash fails, we'll be taken out of seed mode
// and m_seed_mode will be false
if (m_seed_mode) return m_torrent_file->total_size();
if (!has_picker()) return m_have_all ? m_torrent_file->total_size() : 0;
std::int64_t left
= m_torrent_file->total_size()
- std::int64_t(m_picker->num_passed()) * m_torrent_file->piece_length();
// if we have the last piece, we may have subtracted too much, as it can
// be smaller than the normal piece size.
// we have to correct it
piece_index_t const last_piece = prev(m_torrent_file->end_piece());
std::int64_t total_done
= std::int64_t(m_picker->num_passed()) * m_torrent_file->piece_length();
// if we have the last piece, we have to correct
// the amount we have, since the first calculation
// assumed all pieces were of equal size
if (m_picker->has_piece_passed(last_piece))
{
int const corr = m_torrent_file->piece_size(last_piece)
- m_torrent_file->piece_length();
total_done += corr;
left += m_torrent_file->piece_length() - m_torrent_file->piece_size(last_piece);
}
return total_done;
return left;
}
// returns the number of bytes we are interested
@ -7987,20 +7971,6 @@ bool is_downloading_state(int const st)
}
#endif
std::int64_t total_done = quantized_bytes_done();
if (m_torrent_file->is_valid())
{
if (is_seed())
TORRENT_ASSERT(total_done == m_torrent_file->total_size());
else
TORRENT_ASSERT(total_done != m_torrent_file->total_size() || !m_files_checked);
TORRENT_ASSERT(block_size() <= m_torrent_file->piece_length());
}
else
{
TORRENT_ASSERT(total_done == 0);
}
/*
if (m_picker && !m_abort)
{