Add Torrent.pieceIndexOfRequestIndex

This commit is contained in:
Matt Joiner 2022-05-05 17:45:17 +10:00
parent 9b9a37eee6
commit 78c36e4c2f
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
2 changed files with 8 additions and 4 deletions

View 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 // 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. // when we want to go fast.
func (cn *Peer) shouldRequest(r RequestIndex) error { func (cn *Peer) shouldRequest(r RequestIndex) error {
pi := pieceIndex(r / cn.t.chunksPerRegularPiece()) pi := cn.t.pieceIndexOfRequestIndex(r)
if cn.requestState.Cancelled.Contains(r) { if cn.requestState.Cancelled.Contains(r) {
return errors.New("request is cancelled and waiting acknowledgement") return errors.New("request is cancelled and waiting acknowledgement")
} }

View File

@ -1257,7 +1257,7 @@ func (t *Torrent) piecePriority(piece pieceIndex) piecePriority {
} }
func (t *Torrent) pendRequest(req RequestIndex) { 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) { 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 { func (t *Torrent) requestIndexToRequest(ri RequestIndex) Request {
index := ri / t.chunksPerRegularPiece() index := t.pieceIndexOfRequestIndex(ri)
return Request{ return Request{
pp.Integer(index), 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)() _, ok := (*f)()
return ok return ok
} }
func (t *Torrent) pieceIndexOfRequestIndex(ri RequestIndex) pieceIndex {
return pieceIndex(ri / t.chunksPerRegularPiece())
}