-package request_strategy
+package requestStrategy
import (
- "time"
-
- "github.com/RoaringBitmap/roaring"
+ typedRoaring "github.com/anacrolix/torrent/typed-roaring"
)
-type PeerNextRequestState struct {
+type PeerRequestState struct {
Interested bool
- Requests roaring.Bitmap
-}
-
-type PeerId interface {
- Uintptr() uintptr
-}
-
-type Peer struct {
- HasPiece func(i pieceIndex) bool
- MaxRequests int
- HasExistingRequest func(r RequestIndex) bool
- Choking bool
- PieceAllowedFast func(pieceIndex) bool
- DownloadRate float64
- Age time.Duration
- // This is passed back out at the end, so must support equality. Could be a type-param later.
- Id PeerId
-}
-
-func (p *Peer) pieceAllowedFastOrDefault(i pieceIndex) bool {
- if f := p.PieceAllowedFast; f != nil {
- return f(i)
- }
- return false
+ Requests PeerRequests
+ // Cancelled and waiting response
+ Cancelled typedRoaring.Bitmap[RequestIndex]
}
-// TODO: This might be used in more places I think.
-func (p *Peer) canRequestPiece(i pieceIndex) bool {
- return (!p.Choking || p.pieceAllowedFastOrDefault(i)) && p.HasPiece(i)
+// A set of request indices iterable by order added.
+type PeerRequests interface {
+ // Can be more efficient than GetCardinality.
+ IsEmpty() bool
+ // See roaring.Bitmap.GetCardinality.
+ GetCardinality() uint64
+ Contains(RequestIndex) bool
+ // Should not adjust iteration order if item already exists, although I don't think that usage
+ // exists.
+ Add(RequestIndex)
+ // See roaring.Bitmap.Rank.
+ Rank(RequestIndex) uint64
+ // Must yield in order items were added.
+ Iterate(func(RequestIndex) bool)
+ // See roaring.Bitmap.CheckedRemove.
+ CheckedRemove(RequestIndex) bool
+ // Iterate a snapshot of the values. It is safe to mutate the underlying data structure.
+ IterateSnapshot(func(RequestIndex) bool)
}