2020-01-10 12:09:21 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2021-10-05 17:06:23 +08:00
|
|
|
"encoding/gob"
|
|
|
|
"reflect"
|
2020-01-10 12:09:21 +08:00
|
|
|
"time"
|
2021-05-09 22:53:32 +08:00
|
|
|
"unsafe"
|
2020-01-10 12:09:21 +08:00
|
|
|
|
2021-09-20 13:24:24 +08:00
|
|
|
"github.com/RoaringBitmap/roaring"
|
2021-10-05 17:06:23 +08:00
|
|
|
"github.com/anacrolix/log"
|
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"
|
2020-01-10 12:09:21 +08:00
|
|
|
)
|
|
|
|
|
2021-09-20 13:09:28 +08:00
|
|
|
// Calculate requests individually for each peer.
|
2021-09-21 14:07:08 +08:00
|
|
|
const peerRequesting = false
|
2021-09-20 13:09:28 +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-09-15 22:12:58 +08:00
|
|
|
minWait := time.After(100 * time.Millisecond)
|
|
|
|
maxWait := time.After(1000 * time.Millisecond)
|
2021-05-09 12:14:11 +08:00
|
|
|
select {
|
2021-08-01 20:52:51 +08:00
|
|
|
case <-cl.closed.Done():
|
2021-05-09 12:14:11 +08:00
|
|
|
return
|
2021-09-15 22:12:58 +08:00
|
|
|
case <-minWait:
|
|
|
|
case <-maxWait:
|
2021-05-09 12:14:11 +08:00
|
|
|
}
|
2021-08-01 20:52:51 +08:00
|
|
|
select {
|
|
|
|
case <-cl.closed.Done():
|
|
|
|
return
|
|
|
|
case <-update:
|
2021-09-15 22:12:58 +08:00
|
|
|
case <-maxWait:
|
2021-08-01 20:52:51 +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-09-18 16:57:50 +08:00
|
|
|
func (cl *Client) getRequestStrategyInput() request_strategy.Input {
|
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-09-19 13:16:37 +08:00
|
|
|
if !t.haveInfo() {
|
2021-09-20 13:09:28 +08:00
|
|
|
// This would be removed if metadata is handled here. We have to guard against not
|
|
|
|
// knowing the piece size. If we have no info, we have no pieces too, so the end result
|
|
|
|
// is the same.
|
2021-09-19 13:16:37 +08:00
|
|
|
continue
|
|
|
|
}
|
2021-05-14 11:06:12 +08:00
|
|
|
rst := request_strategy.Torrent{
|
2021-09-19 13:16:37 +08:00
|
|
|
InfoHash: t.infoHash,
|
|
|
|
ChunksPerPiece: (t.usualPieceSize() + int(t.chunkSize) - 1) / int(t.chunkSize),
|
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-09-11 19:17:31 +08:00
|
|
|
rst.Pieces = make([]request_strategy.Piece, 0, len(t.pieces))
|
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{
|
2021-09-11 19:17:47 +08:00
|
|
|
Request: !t.ignorePieceForRequests(i),
|
|
|
|
Priority: p.purePriority(),
|
|
|
|
Partial: t.piecePartiallyDownloaded(i),
|
|
|
|
Availability: p.availability,
|
|
|
|
Length: int64(p.length()),
|
|
|
|
NumPendingChunks: int(t.pieceNumPendingChunks(i)),
|
2021-10-05 17:06:23 +08:00
|
|
|
IterPendingChunks: p.undirtiedChunksIter(),
|
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-10-05 17:06:23 +08:00
|
|
|
Pieces: *p.newPeerPieces(),
|
|
|
|
MaxRequests: p.nominalMaxRequests(),
|
|
|
|
ExistingRequests: p.actualRequestState.Requests,
|
|
|
|
Choking: p.peerChoking,
|
|
|
|
PieceAllowedFast: p.peerAllowedFast,
|
|
|
|
DownloadRate: p.downloadRate(),
|
|
|
|
Age: time.Since(p.completedHandshake),
|
2021-09-10 21:02:20 +08:00
|
|
|
Id: peerId{
|
|
|
|
Peer: p,
|
|
|
|
ptr: uintptr(unsafe.Pointer(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-09-18 16:57:50 +08:00
|
|
|
return request_strategy.Input{
|
2021-05-14 11:40:09 +08:00
|
|
|
Torrents: ts,
|
|
|
|
MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes,
|
2021-09-18 16:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cl *Client) doRequests() {
|
2021-10-05 17:06:23 +08:00
|
|
|
input := cl.getRequestStrategyInput()
|
|
|
|
nextPeerStates := request_strategy.Run(input)
|
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-10-05 17:06:23 +08:00
|
|
|
func init() {
|
|
|
|
gob.Register(peerId{})
|
|
|
|
}
|
|
|
|
|
2021-09-10 21:02:20 +08:00
|
|
|
type peerId struct {
|
|
|
|
*Peer
|
|
|
|
ptr uintptr
|
|
|
|
}
|
2021-05-13 09:26:22 +08:00
|
|
|
|
2021-09-10 21:02:20 +08:00
|
|
|
func (p peerId) Uintptr() uintptr {
|
|
|
|
return p.ptr
|
2021-05-13 09:26:22 +08:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:06:23 +08:00
|
|
|
func (p peerId) GobEncode() (b []byte, _ error) {
|
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(&b)) = reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(&p.ptr)),
|
|
|
|
Len: int(unsafe.Sizeof(p.ptr)),
|
|
|
|
Cap: int(unsafe.Sizeof(p.ptr)),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peerId) GobDecode(b []byte) error {
|
|
|
|
if uintptr(len(b)) != unsafe.Sizeof(p.ptr) {
|
|
|
|
panic(len(b))
|
|
|
|
}
|
|
|
|
ptr := unsafe.Pointer(&b[0])
|
|
|
|
p.ptr = *(*uintptr)(ptr)
|
|
|
|
log.Printf("%p", ptr)
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(&p.Peer)),
|
|
|
|
Len: int(unsafe.Sizeof(p.Peer)),
|
|
|
|
Cap: int(unsafe.Sizeof(p.Peer)),
|
|
|
|
}
|
|
|
|
copy(*(*[]byte)(unsafe.Pointer(&dst)), b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:23:45 +08:00
|
|
|
func setPeerNextRequestState(_p request_strategy.PeerId, rp request_strategy.PeerNextRequestState) {
|
2021-09-10 21:02:20 +08:00
|
|
|
p := _p.(peerId).Peer
|
2021-05-20 18:23:45 +08:00
|
|
|
p.nextRequestState = rp
|
|
|
|
p.onNextRequestStateChanged()
|
|
|
|
}
|
|
|
|
|
2021-09-19 13:16:37 +08:00
|
|
|
type RequestIndex = request_strategy.RequestIndex
|
|
|
|
type chunkIndexType = request_strategy.ChunkIndex
|
|
|
|
|
2021-05-20 18:23:45 +08:00
|
|
|
func (p *Peer) applyNextRequestState() bool {
|
2021-09-20 13:09:28 +08:00
|
|
|
if peerRequesting {
|
|
|
|
if p.actualRequestState.Requests.GetCardinality() > uint64(p.nominalMaxRequests()/2) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
type piece struct {
|
|
|
|
index int
|
|
|
|
endGame bool
|
|
|
|
}
|
|
|
|
var pieceOrder []piece
|
|
|
|
request_strategy.GetRequestablePieces(
|
|
|
|
p.t.cl.getRequestStrategyInput(),
|
|
|
|
func(t *request_strategy.Torrent, rsp *request_strategy.Piece, pieceIndex int) {
|
|
|
|
if t.InfoHash != p.t.infoHash {
|
2021-09-18 18:34:14 +08:00
|
|
|
return
|
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
if !p.peerHasPiece(pieceIndex) {
|
2021-09-18 18:34:14 +08:00
|
|
|
return
|
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
pieceOrder = append(pieceOrder, piece{
|
|
|
|
index: pieceIndex,
|
|
|
|
endGame: rsp.Priority == PiecePriorityNow,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
more := true
|
|
|
|
interested := false
|
|
|
|
for _, endGameIter := range []bool{false, true} {
|
|
|
|
for _, piece := range pieceOrder {
|
|
|
|
tp := p.t.piece(piece.index)
|
|
|
|
tp.iterUndirtiedChunks(func(cs chunkIndexType) {
|
|
|
|
req := cs + tp.requestIndexOffset()
|
|
|
|
if !piece.endGame && !endGameIter && p.t.pendingRequests[req] > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
interested = true
|
|
|
|
more = p.setInterested(true)
|
|
|
|
if !more {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxRequests(p.actualRequestState.Requests.GetCardinality()) >= p.nominalMaxRequests() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.peerChoking && !p.peerAllowedFast.Contains(bitmap.BitIndex(piece.index)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
more, err = p.request(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if interested && maxRequests(p.actualRequestState.Requests.GetCardinality()) >= p.nominalMaxRequests() {
|
|
|
|
break
|
2021-09-18 18:34:14 +08:00
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
if !more {
|
|
|
|
break
|
2021-09-18 18:34:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !more {
|
|
|
|
break
|
2021-05-20 18:23:45 +08:00
|
|
|
}
|
2021-05-12 15:45:36 +08:00
|
|
|
}
|
2021-05-20 18:23:45 +08:00
|
|
|
if !more {
|
2021-09-20 13:09:28 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !interested {
|
|
|
|
p.setInterested(false)
|
2021-05-20 18:23:45 +08:00
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
return more
|
2020-01-24 14:55:20 +08:00
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
|
|
|
|
next := p.nextRequestState
|
|
|
|
current := p.actualRequestState
|
|
|
|
if !p.setInterested(next.Interested) {
|
2021-09-18 18:34:14 +08:00
|
|
|
return false
|
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
more := true
|
2021-09-20 13:24:24 +08:00
|
|
|
cancel := roaring.AndNot(¤t.Requests, &next.Requests)
|
|
|
|
cancel.Iterate(func(req uint32) bool {
|
|
|
|
more = p.cancel(req)
|
|
|
|
return more
|
2021-09-20 13:09:28 +08:00
|
|
|
})
|
|
|
|
if !more {
|
|
|
|
return false
|
2021-09-18 18:34:14 +08:00
|
|
|
}
|
2021-09-20 13:09:28 +08:00
|
|
|
next.Requests.Iterate(func(req uint32) bool {
|
|
|
|
// This could happen if the peer chokes us between the next state being generated, and us
|
|
|
|
// trying to transmit the state.
|
|
|
|
if p.peerChoking && !p.peerAllowedFast.Contains(bitmap.BitIndex(req/p.t.chunksPerRegularPiece())) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
more, err = p.request(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} /* else {
|
|
|
|
log.Print(req)
|
|
|
|
} */
|
|
|
|
return more
|
|
|
|
})
|
2021-09-18 18:34:14 +08:00
|
|
|
return more
|
2020-01-24 14:55:20 +08:00
|
|
|
}
|