2021-12-01 11:38:47 +08:00
|
|
|
package torrent
|
|
|
|
|
2021-12-11 10:46:29 +08:00
|
|
|
import (
|
|
|
|
request_strategy "github.com/anacrolix/torrent/request-strategy"
|
|
|
|
)
|
|
|
|
|
2021-12-01 11:38:47 +08:00
|
|
|
func (t *Torrent) updatePieceRequestOrder(pieceIndex int) {
|
2021-12-15 16:09:22 +08:00
|
|
|
if t.storage == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-11 10:46:29 +08:00
|
|
|
t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()].Update(
|
2021-12-01 11:38:47 +08:00
|
|
|
t.pieceRequestOrderKey(pieceIndex),
|
|
|
|
t.requestStrategyPieceOrderState(pieceIndex))
|
|
|
|
}
|
2021-12-11 10:46:29 +08:00
|
|
|
|
|
|
|
func (t *Torrent) clientPieceRequestOrderKey() interface{} {
|
|
|
|
if t.storage.Capacity == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return t.storage.Capacity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) deletePieceRequestOrder() {
|
2021-12-15 16:09:22 +08:00
|
|
|
if t.storage == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-11 10:46:29 +08:00
|
|
|
cpro := t.cl.pieceRequestOrder
|
|
|
|
key := t.clientPieceRequestOrderKey()
|
|
|
|
pro := cpro[key]
|
|
|
|
for i := 0; i < t.numPieces(); i++ {
|
|
|
|
pro.Delete(t.pieceRequestOrderKey(i))
|
|
|
|
}
|
|
|
|
if pro.Len() == 0 {
|
|
|
|
delete(cpro, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) initPieceRequestOrder() {
|
2021-12-15 16:09:22 +08:00
|
|
|
if t.storage == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-11 10:46:29 +08:00
|
|
|
if t.cl.pieceRequestOrder == nil {
|
|
|
|
t.cl.pieceRequestOrder = make(map[interface{}]*request_strategy.PieceRequestOrder)
|
|
|
|
}
|
|
|
|
key := t.clientPieceRequestOrderKey()
|
|
|
|
cpro := t.cl.pieceRequestOrder
|
|
|
|
if cpro[key] == nil {
|
2021-12-20 09:10:26 +08:00
|
|
|
cpro[key] = request_strategy.NewPieceOrder(request_strategy.NewAjwernerBtree(), t.numPieces())
|
2021-12-11 10:46:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) addRequestOrderPiece(i int) {
|
2021-12-15 16:09:22 +08:00
|
|
|
if t.storage == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-12-11 10:46:29 +08:00
|
|
|
t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()].Add(
|
|
|
|
t.pieceRequestOrderKey(i),
|
|
|
|
t.requestStrategyPieceOrderState(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Torrent) getPieceRequestOrder() *request_strategy.PieceRequestOrder {
|
|
|
|
return t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()]
|
|
|
|
}
|