2020-02-27 06:45:13 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/anacrolix/missinggo/iter"
|
|
|
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Provides default implementations for requestStrategy methods. Could be embedded, or delegated to.
|
|
|
|
type requestStrategyDefaults struct{}
|
|
|
|
|
|
|
|
func (requestStrategyDefaults) hooks() requestStrategyHooks {
|
|
|
|
return requestStrategyHooks{
|
2021-01-28 11:23:22 +08:00
|
|
|
sentRequest: func(Request) {},
|
|
|
|
deletedRequest: func(Request) {},
|
2020-02-27 06:45:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 11:23:22 +08:00
|
|
|
func (requestStrategyDefaults) iterUndirtiedChunks(p requestStrategyPiece, f func(ChunkSpec) bool) bool {
|
2020-02-27 06:45:13 +08:00
|
|
|
chunkIndices := p.dirtyChunks().Copy()
|
|
|
|
chunkIndices.FlipRange(0, bitmap.BitIndex(p.numChunks()))
|
|
|
|
return iter.ForPerm(chunkIndices.Len(), func(i int) bool {
|
|
|
|
ci, err := chunkIndices.RB.Select(uint32(i))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-28 11:23:22 +08:00
|
|
|
return f(p.chunkIndexRequest(pp.Integer(ci)).ChunkSpec)
|
2020-02-27 06:45:13 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (requestStrategyDefaults) nominalMaxRequests(cn requestStrategyConnection) int {
|
|
|
|
return int(
|
2020-04-19 09:30:48 +08:00
|
|
|
max(
|
|
|
|
64,
|
|
|
|
cn.stats().ChunksReadUseful.Int64()-
|
|
|
|
(cn.stats().ChunksRead.Int64()-cn.stats().ChunksReadUseful.Int64())))
|
2020-02-27 06:45:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (requestStrategyDefaults) piecePriority(cn requestStrategyConnection, piece pieceIndex, tpp piecePriority, prio int) int {
|
|
|
|
return prio
|
|
|
|
}
|
|
|
|
|
|
|
|
func (requestStrategyDefaults) shouldRequestWithoutBias(cn requestStrategyConnection) bool {
|
|
|
|
return false
|
|
|
|
}
|