]> Sergey Matveev's repositories - btrtrc.git/blob - t.go
Massively reduce CPU when Readers are blocked on unavailable data
[btrtrc.git] / t.go
1 package torrent
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/anacrolix/missinggo/pubsub"
8
9         "github.com/anacrolix/torrent/metainfo"
10 )
11
12 // The torrent's infohash. This is fixed and cannot change. It uniquely
13 // identifies a torrent.
14 func (t *Torrent) InfoHash() metainfo.Hash {
15         return t.infoHash
16 }
17
18 // Returns a channel that is closed when the info (.Info()) for the torrent
19 // has become available.
20 func (t *Torrent) GotInfo() <-chan struct{} {
21         t.cl.mu.Lock()
22         defer t.cl.mu.Unlock()
23         return t.gotMetainfo.C()
24 }
25
26 // Returns the metainfo info dictionary, or nil if it's not yet available.
27 func (t *Torrent) Info() *metainfo.Info {
28         return t.info
29 }
30
31 // Returns a Reader bound to the torrent's data. All read calls block until
32 // the data requested is actually available.
33 func (t *Torrent) NewReader() (ret *Reader) {
34         ret = &Reader{
35                 mu:        &t.cl.mu,
36                 t:         t,
37                 readahead: 5 * 1024 * 1024,
38         }
39         t.addReader(ret)
40         return
41 }
42
43 // Returns the state of pieces of the torrent. They are grouped into runs of
44 // same state. The sum of the state run lengths is the number of pieces
45 // in the torrent.
46 func (t *Torrent) PieceStateRuns() []PieceStateRun {
47         t.cl.mu.Lock()
48         defer t.cl.mu.Unlock()
49         return t.pieceStateRuns()
50 }
51
52 func (t *Torrent) PieceState(piece int) PieceState {
53         t.cl.mu.Lock()
54         defer t.cl.mu.Unlock()
55         return t.pieceState(piece)
56 }
57
58 // The number of pieces in the torrent. This requires that the info has been
59 // obtained first.
60 func (t *Torrent) NumPieces() int {
61         return t.numPieces()
62 }
63
64 // Drop the torrent from the client, and close it. It's always safe to do
65 // this. No data corruption can, or should occur to either the torrent's data,
66 // or connected peers.
67 func (t *Torrent) Drop() {
68         t.cl.mu.Lock()
69         t.cl.dropTorrent(t.infoHash)
70         t.cl.mu.Unlock()
71 }
72
73 // Number of bytes of the entire torrent we have completed.
74 func (t *Torrent) BytesCompleted() int64 {
75         t.cl.mu.RLock()
76         defer t.cl.mu.RUnlock()
77         return t.bytesCompleted()
78 }
79
80 // The subscription emits as (int) the index of pieces as their state changes.
81 // A state change is when the PieceState for a piece alters in value.
82 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
83         return t.pieceStateChanges.Subscribe()
84 }
85
86 // Returns true if the torrent is currently being seeded. This occurs when the
87 // client is willing to upload without wanting anything in return.
88 func (t *Torrent) Seeding() bool {
89         t.cl.mu.Lock()
90         defer t.cl.mu.Unlock()
91         return t.seeding()
92 }
93
94 // Clobbers the torrent display name. The display name is used as the torrent
95 // name if the metainfo is not available.
96 func (t *Torrent) SetDisplayName(dn string) {
97         t.cl.mu.Lock()
98         defer t.cl.mu.Unlock()
99         t.setDisplayName(dn)
100 }
101
102 // The current working name for the torrent. Either the name in the info dict,
103 // or a display name given such as by the dn value in a magnet link, or "".
104 func (t *Torrent) Name() string {
105         t.cl.mu.Lock()
106         defer t.cl.mu.Unlock()
107         return t.name()
108 }
109
110 // The completed length of all the torrent data, in all its files. This is
111 // derived from the torrent info, when it is available.
112 func (t *Torrent) Length() int64 {
113         if t.info == nil {
114                 panic("not valid until info obtained")
115         }
116         return t.length
117 }
118
119 // Returns a run-time generated metainfo for the torrent that includes the
120 // info bytes and announce-list as currently known to the client.
121 func (t *Torrent) Metainfo() metainfo.MetaInfo {
122         t.cl.mu.Lock()
123         defer t.cl.mu.Unlock()
124         return t.newMetaInfo()
125 }
126
127 func (t *Torrent) addReader(r *Reader) {
128         t.cl.mu.Lock()
129         defer t.cl.mu.Unlock()
130         if t.readers == nil {
131                 t.readers = make(map[*Reader]struct{})
132         }
133         t.readers[r] = struct{}{}
134         r.posChanged()
135 }
136
137 func (t *Torrent) deleteReader(r *Reader) {
138         delete(t.readers, r)
139         t.readersChanged()
140 }
141
142 func (t *Torrent) DownloadPieces(begin, end int) {
143         t.cl.mu.Lock()
144         defer t.cl.mu.Unlock()
145         t.pendPieceRange(begin, end)
146 }
147
148 func (t *Torrent) CancelPieces(begin, end int) {
149         t.cl.mu.Lock()
150         defer t.cl.mu.Unlock()
151         t.unpendPieceRange(begin, end)
152 }
153
154 // Returns handles to the files in the torrent. This requires the metainfo is
155 // available first.
156 func (t *Torrent) Files() (ret []File) {
157         t.cl.mu.Lock()
158         info := t.Info()
159         t.cl.mu.Unlock()
160         if info == nil {
161                 return
162         }
163         var offset int64
164         for _, fi := range info.UpvertedFiles() {
165                 ret = append(ret, File{
166                         t,
167                         strings.Join(append([]string{info.Name}, fi.Path...), "/"),
168                         offset,
169                         fi.Length,
170                         fi,
171                 })
172                 offset += fi.Length
173         }
174         return
175 }
176
177 func (t *Torrent) AddPeers(pp []Peer) {
178         cl := t.cl
179         cl.mu.Lock()
180         defer cl.mu.Unlock()
181         t.addPeers(pp)
182 }
183
184 // Marks the entire torrent for download. Requires the info first, see
185 // GotInfo.
186 func (t *Torrent) DownloadAll() {
187         t.cl.mu.Lock()
188         defer t.cl.mu.Unlock()
189         t.pendPieceRange(0, t.numPieces())
190 }
191
192 func (t *Torrent) String() string {
193         s := t.name()
194         if s == "" {
195                 s = fmt.Sprintf("%x", t.infoHash)
196         }
197         return s
198 }
199
200 func (t *Torrent) AddTrackers(announceList [][]string) {
201         t.cl.mu.Lock()
202         defer t.cl.mu.Unlock()
203         t.addTrackers(announceList)
204 }