From: Matt Joiner Date: Thu, 5 May 2022 07:45:17 +0000 (+1000) Subject: Add Torrent.pieceIndexOfRequestIndex X-Git-Tag: v1.43.0~20 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=78c36e4c2f2de5165b1aa050fa11bad32af1ad4d;p=btrtrc.git Add Torrent.pieceIndexOfRequestIndex --- diff --git a/peerconn.go b/peerconn.go index 41b3f113..6364affb 100644 --- a/peerconn.go +++ b/peerconn.go @@ -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") } diff --git a/torrent.go b/torrent.go index ae3d1c05..a0872a88 100644 --- a/torrent.go +++ b/torrent.go @@ -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()) +}