forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest-strategy-impls.go
112 lines (86 loc) · 3.05 KB
/
request-strategy-impls.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package torrent
import (
g "github.com/anacrolix/generics"
"github.com/anacrolix/torrent/metainfo"
request_strategy "github.com/anacrolix/torrent/request-strategy"
"github.com/anacrolix/torrent/storage"
)
type requestStrategyInputCommon struct {
maxUnverifiedBytes int64
}
func (r requestStrategyInputCommon) MaxUnverifiedBytes() int64 {
return r.maxUnverifiedBytes
}
type requestStrategyInputMultiTorrent struct {
requestStrategyInputCommon
torrents map[metainfo.Hash]*Torrent
capFunc storage.TorrentCapacity
}
func (r requestStrategyInputMultiTorrent) Torrent(ih metainfo.Hash) request_strategy.Torrent {
return requestStrategyTorrent{g.MapMustGet(r.torrents, ih)}
}
func (r requestStrategyInputMultiTorrent) Capacity() (int64, bool) {
return (*r.capFunc)()
}
type requestStrategyInputSingleTorrent struct {
requestStrategyInputCommon
t *Torrent
}
func (r requestStrategyInputSingleTorrent) Torrent(_ metainfo.Hash) request_strategy.Torrent {
return requestStrategyTorrent{r.t}
}
func (r requestStrategyInputSingleTorrent) Capacity() (cap int64, capped bool) {
return 0, false
}
var _ request_strategy.Input = requestStrategyInputSingleTorrent{}
func (cl *Client) getRequestStrategyInputCommon() requestStrategyInputCommon {
return requestStrategyInputCommon{cl.config.MaxUnverifiedBytes}
}
// Returns what is necessary to run request_strategy.GetRequestablePieces for primaryTorrent.
func (cl *Client) getRequestStrategyInput(primaryTorrent *Torrent) (input request_strategy.Input) {
if primaryTorrent.storage.Capacity == nil {
return requestStrategyInputSingleTorrent{
requestStrategyInputCommon: cl.getRequestStrategyInputCommon(),
t: primaryTorrent,
}
} else {
return requestStrategyInputMultiTorrent{
requestStrategyInputCommon: cl.getRequestStrategyInputCommon(),
torrents: cl.torrents,
capFunc: primaryTorrent.storage.Capacity,
}
}
}
func (t *Torrent) getRequestStrategyInput() request_strategy.Input {
return t.cl.getRequestStrategyInput(t)
}
type requestStrategyTorrent struct {
t *Torrent
}
func (r requestStrategyTorrent) Piece(i int, lockTorrent bool) request_strategy.Piece {
return (*requestStrategyPiece)(r.t.piece(i, lockTorrent))
}
func (r requestStrategyTorrent) PieceLength() int64 {
return r.t.info.PieceLength
}
func (r requestStrategyTorrent) GetPieceRequestOrder() *request_strategy.PieceRequestOrder {
return r.t.getPieceRequestOrder()
}
func (r requestStrategyTorrent) RLock() {
r.t.mu.RLock()
}
func (r requestStrategyTorrent) RUnlock() {
r.t.mu.RUnlock()
}
func (r requestStrategyTorrent) NumPieces() pieceIndex {
return r.t.NumPieces()
}
var _ request_strategy.Torrent = requestStrategyTorrent{}
type requestStrategyPiece Piece
func (r *requestStrategyPiece) Request(lockTorrent bool) bool {
return !r.t.ignorePieceForRequests(r.index, lockTorrent)
}
func (r *requestStrategyPiece) NumPendingChunks(lockTorrent bool) int {
return int(r.t.pieceNumPendingChunks((*Piece)(r), lockTorrent))
}
var _ request_strategy.Piece = (*requestStrategyPiece)(nil)