forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtorrent-piece-request-order.go
91 lines (79 loc) · 1.96 KB
/
torrent-piece-request-order.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package torrent
import (
g "github.com/anacrolix/generics"
request_strategy "github.com/anacrolix/torrent/request-strategy"
)
func (t *Torrent) updatePieceRequestOrderPiece(pieceIndex int, lock bool) {
if t.storage == nil {
return
}
pro := t.clientPieceRequestOrder
if pro == nil {
return
}
key := t.pieceRequestOrderKey(pieceIndex)
if t.hasStorageCap() {
pro.Update(key, t.requestStrategyPieceOrderState(pieceIndex, lock))
return
}
//pending := !t.ignorePieceForRequests(pieceIndex)
//if pending {
pro.Add(key, t.requestStrategyPieceOrderState(pieceIndex, lock))
//} else {
// pro.Delete(key)
//}
}
func (t *Torrent) clientPieceRequestOrderKey() interface{} {
if t.storage.Capacity == nil {
return t
}
return t.storage.Capacity
}
func (t *Torrent) deletePieceRequestOrder(lockClient bool) {
if t.storage == nil {
return
}
if lockClient {
t.cl.lock()
defer t.cl.unlock()
}
cpro := t.cl.pieceRequestOrder
key := t.clientPieceRequestOrderKey()
pro := cpro[key]
for i := 0; i < t.numPieces(true); i++ {
pro.Delete(t.pieceRequestOrderKey(i))
}
if pro.Len() == 0 {
delete(cpro, key)
}
t.clientPieceRequestOrder = nil
}
func (t *Torrent) initPieceRequestOrder(lockClient bool) {
if t.storage == nil {
return
}
if lockClient {
t.cl.lock()
defer t.cl.unlock()
}
g.MakeMapIfNil(&t.cl.pieceRequestOrder)
key := t.clientPieceRequestOrderKey()
cpro := t.cl.pieceRequestOrder
if cpro[key] == nil {
cpro[key] = request_strategy.NewPieceOrder(request_strategy.NewAjwernerBtree(), t.numPieces(true))
}
t.clientPieceRequestOrder = cpro[key]
}
func (t *Torrent) addRequestOrderPiece(i int, lock bool) {
if t.storage == nil {
return
}
pro := t.getPieceRequestOrder()
key := t.pieceRequestOrderKey(i)
if t.hasStorageCap() || !t.ignorePieceForRequests(i, lock) {
pro.Add(key, t.requestStrategyPieceOrderState(i, lock))
}
}
func (t *Torrent) getPieceRequestOrder() *request_strategy.PieceRequestOrder {
return t.clientPieceRequestOrder
}