2020-01-10 12:09:21 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2021-05-09 22:53:32 +08:00
|
|
|
"unsafe"
|
2020-01-10 12:09:21 +08:00
|
|
|
|
2021-05-20 09:16:54 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
2021-05-20 18:23:45 +08:00
|
|
|
|
2021-05-24 15:36:39 +08:00
|
|
|
"github.com/anacrolix/chansync"
|
2021-05-13 07:56:58 +08:00
|
|
|
request_strategy "github.com/anacrolix/torrent/request-strategy"
|
|
|
|
"github.com/anacrolix/torrent/types"
|
2020-01-10 12:09:21 +08:00
|
|
|
)
|
|
|
|
|
2021-05-09 12:14:11 +08:00
|
|
|
func (cl *Client) requester() {
|
|
|
|
for {
|
2021-05-20 18:23:45 +08:00
|
|
|
update := func() chansync.Signaled {
|
2021-05-09 12:14:11 +08:00
|
|
|
cl.lock()
|
|
|
|
defer cl.unlock()
|
|
|
|
cl.doRequests()
|
2021-05-20 18:23:45 +08:00
|
|
|
return cl.updateRequests.Signaled()
|
2021-05-09 12:14:11 +08:00
|
|
|
}()
|
2021-05-20 18:23:45 +08:00
|
|
|
// We can probably tune how often to heed this signal. TODO: Currently disabled to retain
|
|
|
|
// existing behaviour, while the signalling is worked out.
|
|
|
|
update = nil
|
2021-05-09 12:14:11 +08:00
|
|
|
select {
|
|
|
|
case <-cl.closed.LockedChan(cl.locker()):
|
|
|
|
return
|
2021-05-20 18:23:45 +08:00
|
|
|
case <-update:
|
2021-05-10 15:43:34 +08:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2021-05-09 12:14:11 +08:00
|
|
|
}
|
2020-01-10 13:18:55 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 14:55:20 +08:00
|
|
|
|
2021-05-20 18:23:45 +08:00
|
|
|
func (cl *Client) tickleRequester() {
|
|
|
|
cl.updateRequests.Broadcast()
|
|
|
|
}
|
|
|
|
|
2021-05-09 12:14:11 +08:00
|
|
|
func (cl *Client) doRequests() {
|
2021-05-14 11:06:12 +08:00
|
|
|
ts := make([]request_strategy.Torrent, 0, len(cl.torrents))
|
2021-05-09 12:14:11 +08:00
|
|
|
for _, t := range cl.torrents {
|
2021-05-14 11:06:12 +08:00
|
|
|
rst := request_strategy.Torrent{
|
2021-05-14 11:40:09 +08:00
|
|
|
StableId: uintptr(unsafe.Pointer(t)),
|
2021-05-14 09:50:41 +08:00
|
|
|
}
|
2021-05-13 07:56:58 +08:00
|
|
|
if t.storage != nil {
|
|
|
|
rst.Capacity = t.storage.Capacity
|
2021-05-12 15:45:36 +08:00
|
|
|
}
|
2021-05-13 07:56:58 +08:00
|
|
|
for i := range t.pieces {
|
|
|
|
p := &t.pieces[i]
|
|
|
|
rst.Pieces = append(rst.Pieces, request_strategy.Piece{
|
|
|
|
Request: !t.ignorePieceForRequests(i),
|
|
|
|
Priority: p.purePriority(),
|
|
|
|
Partial: t.piecePartiallyDownloaded(i),
|
|
|
|
Availability: p.availability,
|
|
|
|
Length: int64(p.length()),
|
|
|
|
NumPendingChunks: int(t.pieceNumPendingChunks(i)),
|
|
|
|
IterPendingChunks: func(f func(types.ChunkSpec)) {
|
|
|
|
p.iterUndirtiedChunks(func(cs ChunkSpec) bool {
|
|
|
|
f(cs)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
},
|
2021-05-12 15:45:36 +08:00
|
|
|
})
|
|
|
|
}
|
2021-05-13 07:56:58 +08:00
|
|
|
t.iterPeers(func(p *Peer) {
|
|
|
|
if p.closed.IsSet() {
|
|
|
|
return
|
2021-05-09 12:14:11 +08:00
|
|
|
}
|
2021-05-20 06:56:53 +08:00
|
|
|
if p.piecesReceivedSinceLastRequestUpdate > p.maxPiecesReceivedBetweenRequestUpdates {
|
|
|
|
p.maxPiecesReceivedBetweenRequestUpdates = p.piecesReceivedSinceLastRequestUpdate
|
|
|
|
}
|
|
|
|
p.piecesReceivedSinceLastRequestUpdate = 0
|
2021-05-13 09:26:22 +08:00
|
|
|
rst.Peers = append(rst.Peers, request_strategy.Peer{
|
2021-05-13 07:56:58 +08:00
|
|
|
HasPiece: p.peerHasPiece,
|
2021-05-13 09:26:22 +08:00
|
|
|
MaxRequests: p.nominalMaxRequests(),
|
2021-05-13 07:56:58 +08:00
|
|
|
HasExistingRequest: func(r request_strategy.Request) bool {
|
2021-05-20 18:23:45 +08:00
|
|
|
_, ok := p.actualRequestState.Requests[r]
|
2021-05-13 07:56:58 +08:00
|
|
|
return ok
|
|
|
|
},
|
|
|
|
Choking: p.peerChoking,
|
|
|
|
PieceAllowedFast: func(i pieceIndex) bool {
|
2021-05-20 09:16:54 +08:00
|
|
|
return p.peerAllowedFast.Contains(bitmap.BitIndex(i))
|
2021-05-13 07:56:58 +08:00
|
|
|
},
|
|
|
|
DownloadRate: p.downloadRate(),
|
|
|
|
Age: time.Since(p.completedHandshake),
|
2021-05-13 09:26:22 +08:00
|
|
|
Id: (*peerId)(p),
|
2021-05-12 15:45:36 +08:00
|
|
|
})
|
2021-05-09 12:14:11 +08:00
|
|
|
})
|
2021-05-13 07:56:58 +08:00
|
|
|
ts = append(ts, rst)
|
2020-01-24 14:55:20 +08:00
|
|
|
}
|
2021-05-14 11:40:09 +08:00
|
|
|
nextPeerStates := request_strategy.Run(request_strategy.Input{
|
|
|
|
Torrents: ts,
|
|
|
|
MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes,
|
|
|
|
})
|
2021-05-13 07:56:58 +08:00
|
|
|
for p, state := range nextPeerStates {
|
2021-05-20 18:23:45 +08:00
|
|
|
setPeerNextRequestState(p, state)
|
2021-05-12 15:45:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:26:22 +08:00
|
|
|
type peerId Peer
|
|
|
|
|
|
|
|
func (p *peerId) Uintptr() uintptr {
|
|
|
|
return uintptr(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:23:45 +08:00
|
|
|
func setPeerNextRequestState(_p request_strategy.PeerId, rp request_strategy.PeerNextRequestState) {
|
2021-05-13 09:26:22 +08:00
|
|
|
p := (*Peer)(_p.(*peerId))
|
2021-05-20 18:23:45 +08:00
|
|
|
p.nextRequestState = rp
|
|
|
|
p.onNextRequestStateChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) applyNextRequestState() bool {
|
|
|
|
next := p.nextRequestState
|
|
|
|
current := p.actualRequestState
|
|
|
|
if !p.setInterested(next.Interested) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for req := range current.Requests {
|
|
|
|
if _, ok := next.Requests[req]; !ok {
|
|
|
|
if !p.cancel(req) {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-12 15:45:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 18:23:45 +08:00
|
|
|
for req := range next.Requests {
|
|
|
|
more, err := p.request(req)
|
2021-05-12 15:45:36 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
2021-05-13 11:49:57 +08:00
|
|
|
//log.Print(req)
|
2021-05-12 15:45:36 +08:00
|
|
|
}
|
2021-05-20 18:23:45 +08:00
|
|
|
if !more {
|
|
|
|
return false
|
|
|
|
}
|
2020-01-24 14:55:20 +08:00
|
|
|
}
|
2021-05-20 18:23:45 +08:00
|
|
|
return true
|
2020-01-24 14:55:20 +08:00
|
|
|
}
|