2020-01-10 04:09:21 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2021-05-09 14:53:32 +00:00
|
|
|
"unsafe"
|
2020-01-10 04:09:21 +00:00
|
|
|
|
2021-05-12 23:56:58 +00:00
|
|
|
request_strategy "github.com/anacrolix/torrent/request-strategy"
|
|
|
|
"github.com/anacrolix/torrent/types"
|
2020-01-10 04:09:21 +00:00
|
|
|
)
|
|
|
|
|
2021-05-09 04:14:11 +00:00
|
|
|
func (cl *Client) requester() {
|
|
|
|
for {
|
|
|
|
func() {
|
|
|
|
cl.lock()
|
|
|
|
defer cl.unlock()
|
|
|
|
cl.doRequests()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-cl.closed.LockedChan(cl.locker()):
|
|
|
|
return
|
2021-05-10 07:43:34 +00:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2021-05-09 04:14:11 +00:00
|
|
|
}
|
2020-01-10 05:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 06:55:20 +00:00
|
|
|
|
2021-05-09 04:14:11 +00:00
|
|
|
func (cl *Client) doRequests() {
|
2021-05-14 03:06:12 +00:00
|
|
|
ts := make([]request_strategy.Torrent, 0, len(cl.torrents))
|
2021-05-09 04:14:11 +00:00
|
|
|
for _, t := range cl.torrents {
|
2021-05-14 03:06:12 +00:00
|
|
|
rst := request_strategy.Torrent{
|
2021-05-14 03:40:09 +00:00
|
|
|
StableId: uintptr(unsafe.Pointer(t)),
|
2021-05-14 01:50:41 +00:00
|
|
|
}
|
2021-05-12 23:56:58 +00:00
|
|
|
if t.storage != nil {
|
|
|
|
rst.Capacity = t.storage.Capacity
|
2021-05-12 07:45:36 +00:00
|
|
|
}
|
2021-05-12 23:56:58 +00: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 07:45:36 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-12 23:56:58 +00:00
|
|
|
t.iterPeers(func(p *Peer) {
|
|
|
|
if p.closed.IsSet() {
|
|
|
|
return
|
2021-05-09 04:14:11 +00:00
|
|
|
}
|
2021-05-19 22:56:53 +00:00
|
|
|
if p.piecesReceivedSinceLastRequestUpdate > p.maxPiecesReceivedBetweenRequestUpdates {
|
|
|
|
p.maxPiecesReceivedBetweenRequestUpdates = p.piecesReceivedSinceLastRequestUpdate
|
|
|
|
}
|
|
|
|
p.piecesReceivedSinceLastRequestUpdate = 0
|
2021-05-13 01:26:22 +00:00
|
|
|
rst.Peers = append(rst.Peers, request_strategy.Peer{
|
2021-05-12 23:56:58 +00:00
|
|
|
HasPiece: p.peerHasPiece,
|
2021-05-13 01:26:22 +00:00
|
|
|
MaxRequests: p.nominalMaxRequests(),
|
2021-05-12 23:56:58 +00:00
|
|
|
HasExistingRequest: func(r request_strategy.Request) bool {
|
|
|
|
_, ok := p.requests[r]
|
|
|
|
return ok
|
|
|
|
},
|
|
|
|
Choking: p.peerChoking,
|
|
|
|
PieceAllowedFast: func(i pieceIndex) bool {
|
|
|
|
return p.peerAllowedFast.Contains(i)
|
|
|
|
},
|
|
|
|
DownloadRate: p.downloadRate(),
|
|
|
|
Age: time.Since(p.completedHandshake),
|
2021-05-13 01:26:22 +00:00
|
|
|
Id: (*peerId)(p),
|
2021-05-12 07:45:36 +00:00
|
|
|
})
|
2021-05-09 04:14:11 +00:00
|
|
|
})
|
2021-05-12 23:56:58 +00:00
|
|
|
ts = append(ts, rst)
|
2020-01-24 06:55:20 +00:00
|
|
|
}
|
2021-05-14 03:40:09 +00:00
|
|
|
nextPeerStates := request_strategy.Run(request_strategy.Input{
|
|
|
|
Torrents: ts,
|
|
|
|
MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes,
|
|
|
|
})
|
2021-05-12 23:56:58 +00:00
|
|
|
for p, state := range nextPeerStates {
|
|
|
|
applyPeerNextRequestState(p, state)
|
2021-05-12 07:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 01:26:22 +00:00
|
|
|
type peerId Peer
|
|
|
|
|
|
|
|
func (p *peerId) Uintptr() uintptr {
|
|
|
|
return uintptr(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyPeerNextRequestState(_p request_strategy.PeerId, rp request_strategy.PeerNextRequestState) {
|
|
|
|
p := (*Peer)(_p.(*peerId))
|
2021-05-12 23:56:58 +00:00
|
|
|
p.setInterested(rp.Interested)
|
2021-05-12 07:45:36 +00:00
|
|
|
for req := range p.requests {
|
2021-05-12 23:56:58 +00:00
|
|
|
if _, ok := rp.Requests[req]; !ok {
|
2021-05-12 07:45:36 +00:00
|
|
|
p.cancel(req)
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 23:56:58 +00:00
|
|
|
for req := range rp.Requests {
|
2021-05-12 07:45:36 +00:00
|
|
|
err := p.request(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
2021-05-13 03:49:57 +00:00
|
|
|
//log.Print(req)
|
2021-05-12 07:45:36 +00:00
|
|
|
}
|
2020-01-24 06:55:20 +00:00
|
|
|
}
|
|
|
|
}
|