2021-05-13 07:56:58 +08:00
|
|
|
package request_strategy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PeerNextRequestState struct {
|
|
|
|
Interested bool
|
|
|
|
Requests map[Request]struct{}
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:26:22 +08:00
|
|
|
type PeerId interface {
|
|
|
|
Uintptr() uintptr
|
|
|
|
}
|
2021-05-13 07:56:58 +08:00
|
|
|
|
|
|
|
type Peer struct {
|
2021-05-13 09:26:22 +08:00
|
|
|
HasPiece func(i pieceIndex) bool
|
|
|
|
MaxRequests int
|
|
|
|
HasExistingRequest func(r Request) bool
|
2021-05-13 07:56:58 +08:00
|
|
|
Choking bool
|
|
|
|
PieceAllowedFast func(pieceIndex) bool
|
|
|
|
DownloadRate float64
|
|
|
|
Age time.Duration
|
2021-05-13 09:26:22 +08:00
|
|
|
// 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
|
2021-05-13 07:56:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This might be used in more places I think.
|
|
|
|
func (p *Peer) canRequestPiece(i pieceIndex) bool {
|
2021-05-13 09:26:22 +08:00
|
|
|
return p.HasPiece(i) && (!p.Choking || (p.PieceAllowedFast != nil && p.PieceAllowedFast(i)))
|
2021-05-13 07:56:58 +08:00
|
|
|
}
|