@@ -1271,7 +1271,7 @@ namespace libtorrent {
12711271 return p.piece >= piece_index_t (0 )
12721272 && p.piece < ti.end_piece ()
12731273 && p.start >= 0
1274- && p.start < ti.piece_length ( )
1274+ && p.start < ti.piece_size_for_req (p. piece )
12751275 && t->to_req (piece_block (p.piece , p.start / t->block_size ())) == p;
12761276 }
12771277
@@ -1599,9 +1599,9 @@ namespace libtorrent {
15991599 if (r.piece < piece_index_t {}
16001600 || r.piece >= t->torrent_file ().files ().end_piece ()
16011601 || r.start < 0
1602- || r.start >= t->torrent_file ().piece_length ( )
1602+ || r.start >= t->torrent_file ().piece_size_for_req (r. piece )
16031603 || (r.start % block_size) != 0
1604- || r.length != std::min (t->torrent_file ().piece_size (r.piece ) - r.start , block_size))
1604+ || r.length != std::min (t->torrent_file ().piece_size_for_req (r.piece ) - r.start , block_size))
16051605 {
16061606#ifndef TORRENT_DISABLE_LOGGING
16071607 peer_log (peer_log_alert::info, " REJECT_PIECE" , " invalid reject message (%d, %d, %d)"
@@ -3717,25 +3717,15 @@ namespace libtorrent {
37173717 {
37183718 piece_block const b = pb.block ;
37193719
3720- int const block_offset = b.block_index * t->block_size ();
3721- int const block_size
3722- = std::min (t->torrent_file ().piece_size (b.piece_index )-block_offset,
3723- t->block_size ());
3724- TORRENT_ASSERT (block_size > 0 );
3725- TORRENT_ASSERT (block_size <= t->block_size ());
3726-
37273720 // we can't cancel the piece if we've started receiving it
37283721 if (m_receiving_block == b) continue ;
37293722
3730- peer_request r;
3731- r.piece = b.piece_index ;
3732- r.start = block_offset;
3733- r.length = block_size;
3723+ peer_request const r = t->to_req (b);
37343724
37353725#ifndef TORRENT_DISABLE_LOGGING
37363726 peer_log (peer_log_alert::outgoing_message, " CANCEL"
37373727 , " piece: %d s: %d l: %d b: %d"
3738- , static_cast <int >(b. piece_index ), block_offset, block_size , b.block_index );
3728+ , static_cast <int >(r. piece ), r. start , r. length , b.block_index );
37393729#endif
37403730 write_cancel (r);
37413731 }
@@ -3784,28 +3774,18 @@ namespace libtorrent {
37843774 return ;
37853775 }
37863776
3787- int const block_offset = block.block_index * t->block_size ();
3788- int const block_size
3789- = std::min (t->torrent_file ().piece_size (block.piece_index ) - block_offset,
3790- t->block_size ());
3791- TORRENT_ASSERT (block_size > 0 );
3792- TORRENT_ASSERT (block_size <= t->block_size ());
3793-
37943777 it->not_wanted = true ;
37953778
37963779 if (force) t->picker ().abort_download (block, peer_info_struct ());
37973780
3798- if (m_outstanding_bytes < block_size) return ;
3781+ peer_request const r = t-> to_req (block) ;
37993782
3800- peer_request r;
3801- r.piece = block.piece_index ;
3802- r.start = block_offset;
3803- r.length = block_size;
3783+ if (m_outstanding_bytes < r.length ) return ;
38043784
38053785#ifndef TORRENT_DISABLE_LOGGING
38063786 peer_log (peer_log_alert::outgoing_message, " CANCEL"
38073787 , " piece: %d s: %d l: %d b: %d"
3808- , static_cast <int >(block. piece_index ), block_offset, block_size , block.block_index );
3788+ , static_cast <int >(r. piece ), r. start , r. length , block.block_index );
38093789#endif
38103790 write_cancel (r);
38113791 }
@@ -4081,24 +4061,14 @@ namespace libtorrent {
40814061 continue ;
40824062 }
40834063
4084- int block_offset = block.block .block_index * t->block_size ();
4085- int bs = std::min (t->torrent_file ().piece_size (
4086- block.block .piece_index ) - block_offset, t->block_size ());
4087- TORRENT_ASSERT (bs > 0 );
4088- TORRENT_ASSERT (bs <= t->block_size ());
4089-
4090- peer_request r;
4091- r.piece = block.block .piece_index ;
4092- r.start = block_offset;
4093- r.length = bs;
4094-
4064+ peer_request r = t->to_req (block.block );
40954065 if (m_download_queue.empty ())
40964066 m_counters.inc_stats_counter (counters::num_peers_down_requests);
40974067
40984068 TORRENT_ASSERT (validate_piece_request (t->to_req (block.block )));
40994069 block.send_buffer_offset = aux::numeric_cast<std::uint32_t >(m_send_buffer.size ());
41004070 m_download_queue.push_back (block);
4101- m_outstanding_bytes += bs ;
4071+ m_outstanding_bytes += r. length ;
41024072#if TORRENT_USE_INVARIANT_CHECKS
41034073 check_invariant ();
41044074#endif
@@ -4128,9 +4098,10 @@ namespace libtorrent {
41284098 m_download_queue.push_back (block);
41294099 if (m_queued_time_critical) --m_queued_time_critical;
41304100
4131- block_offset = block.block .block_index * t->block_size ();
4132- bs = std::min (t->torrent_file ().piece_size (
4133- block.block .piece_index ) - block_offset, t->block_size ());
4101+ int const block_offset = block.block .block_index * t->block_size ();
4102+ int const bs =
4103+ std::min (t->torrent_file ().piece_size_for_req (block.block .piece_index )
4104+ - block_offset, t->block_size ());
41344105 TORRENT_ASSERT (bs > 0 );
41354106 TORRENT_ASSERT (bs <= t->block_size ());
41364107
@@ -6607,28 +6578,20 @@ namespace libtorrent {
66076578 // if the piece is fully downloaded, we might have popped it from the
66086579 // download queue already
66096580 int outstanding_bytes = 0 ;
6610- // bool in_download_queue = false;
66116581 int const bs = t->block_size ();
66126582 piece_block last_block (ti.last_piece ()
6613- , (ti.piece_size (ti.last_piece ()) + bs - 1 ) / bs);
6583+ , (ti.piece_size_for_req (ti.last_piece ()) + bs - 1 ) / bs);
6584+
66146585 for (std::vector<pending_block>::const_iterator i = m_download_queue.begin ()
66156586 , end (m_download_queue.end ()); i != end; ++i)
66166587 {
66176588 TORRENT_ASSERT (i->block .piece_index <= last_block.piece_index );
66186589 TORRENT_ASSERT (i->block .piece_index < last_block.piece_index
66196590 || i->block .block_index <= last_block.block_index );
6591+
6592+ outstanding_bytes += t->to_req (i->block ).length ;
66206593 if (m_received_in_piece && i == m_download_queue.begin ())
6621- {
6622- // in_download_queue = true;
6623- // this assert is not correct since block may have different sizes
6624- // and may not be returned in the order they were requested
6625- // TORRENT_ASSERT(t->to_req(i->block).length >= m_received_in_piece);
6626- outstanding_bytes += t->to_req (i->block ).length - m_received_in_piece;
6627- }
6628- else
6629- {
6630- outstanding_bytes += t->to_req (i->block ).length ;
6631- }
6594+ outstanding_bytes -= m_received_in_piece;
66326595 }
66336596 // if (p && p->bytes_downloaded < p->full_block_bytes) TORRENT_ASSERT(in_download_queue);
66346597
0 commit comments