]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add Torrent.pieceIndexOfRequestIndex
authorMatt Joiner <anacrolix@gmail.com>
Thu, 5 May 2022 07:45:17 +0000 (17:45 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 9 May 2022 02:05:12 +0000 (12:05 +1000)
peerconn.go
torrent.go

index 41b3f113f297e2c23d797f0b7e533003732a694a..6364affbc0973e936de2a245f997086fbce57973 100644 (file)
@@ -578,7 +578,7 @@ type messageWriter func(pp.Message) bool
 // This function seems to only used by Peer.request. It's all logic checks, so maybe we can no-op it
 // when we want to go fast.
 func (cn *Peer) shouldRequest(r RequestIndex) error {
-       pi := pieceIndex(r / cn.t.chunksPerRegularPiece())
+       pi := cn.t.pieceIndexOfRequestIndex(r)
        if cn.requestState.Cancelled.Contains(r) {
                return errors.New("request is cancelled and waiting acknowledgement")
        }
index ae3d1c05a6d578a755e84a4a580b80a77ccc69c2..a0872a88c89eefb2a3eb86146238c72b004218a3 100644 (file)
@@ -1257,7 +1257,7 @@ func (t *Torrent) piecePriority(piece pieceIndex) piecePriority {
 }
 
 func (t *Torrent) pendRequest(req RequestIndex) {
-       t.piece(int(req / t.chunksPerRegularPiece())).pendChunkIndex(req % t.chunksPerRegularPiece())
+       t.piece(t.pieceIndexOfRequestIndex(req)).pendChunkIndex(req % t.chunksPerRegularPiece())
 }
 
 func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason string) {
@@ -2437,10 +2437,10 @@ func (t *Torrent) peerIsActive(p *Peer) (active bool) {
 }
 
 func (t *Torrent) requestIndexToRequest(ri RequestIndex) Request {
-       index := ri / t.chunksPerRegularPiece()
+       index := t.pieceIndexOfRequestIndex(ri)
        return Request{
                pp.Integer(index),
-               t.piece(int(index)).chunkIndexSpec(ri % t.chunksPerRegularPiece()),
+               t.piece(index).chunkIndexSpec(ri % t.chunksPerRegularPiece()),
        }
 }
 
@@ -2494,3 +2494,7 @@ func (t *Torrent) hasStorageCap() bool {
        _, ok := (*f)()
        return ok
 }
+
+func (t *Torrent) pieceIndexOfRequestIndex(ri RequestIndex) pieceIndex {
+       return pieceIndex(ri / t.chunksPerRegularPiece())
+}