6 "github.com/anacrolix/missinggo/pubsub"
8 "github.com/anacrolix/torrent/metainfo"
11 // The torrent's infohash. This is fixed and cannot change. It uniquely
12 // identifies a torrent.
13 func (t *Torrent) InfoHash() metainfo.Hash {
17 // Returns a channel that is closed when the info (.Info()) for the torrent
18 // has become available.
19 func (t *Torrent) GotInfo() <-chan struct{} {
21 defer t.cl.mu.Unlock()
22 return t.gotMetainfo.C()
25 // Returns the metainfo info dictionary, or nil if it's not yet available.
26 func (t *Torrent) Info() *metainfo.Info {
28 defer t.cl.mu.Unlock()
32 // Returns a Reader bound to the torrent's data. All read calls block until
33 // the data requested is actually available.
34 func (t *Torrent) NewReader() Reader {
38 readahead: 5 * 1024 * 1024,
45 // Returns the state of pieces of the torrent. They are grouped into runs of
46 // same state. The sum of the state run lengths is the number of pieces
48 func (t *Torrent) PieceStateRuns() []PieceStateRun {
50 defer t.cl.mu.Unlock()
51 return t.pieceStateRuns()
54 func (t *Torrent) PieceState(piece int) PieceState {
56 defer t.cl.mu.Unlock()
57 return t.pieceState(piece)
60 // The number of pieces in the torrent. This requires that the info has been
62 func (t *Torrent) NumPieces() int {
66 // Get missing bytes count for specific piece.
67 func (t *Torrent) PieceBytesMissing(piece int) int64 {
69 defer t.cl.mu.Unlock()
71 return int64(t.pieces[piece].bytesLeft())
74 // Drop the torrent from the client, and close it. It's always safe to do
75 // this. No data corruption can, or should occur to either the torrent's data,
76 // or connected peers.
77 func (t *Torrent) Drop() {
79 t.cl.dropTorrent(t.infoHash)
83 // Number of bytes of the entire torrent we have completed. This is the sum of
84 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
85 // for download rate, as it can go down when pieces are lost or fail checks.
86 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
87 func (t *Torrent) BytesCompleted() int64 {
89 defer t.cl.mu.RUnlock()
90 return t.bytesCompleted()
93 // The subscription emits as (int) the index of pieces as their state changes.
94 // A state change is when the PieceState for a piece alters in value.
95 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
96 return t.pieceStateChanges.Subscribe()
99 // Returns true if the torrent is currently being seeded. This occurs when the
100 // client is willing to upload without wanting anything in return.
101 func (t *Torrent) Seeding() bool {
103 defer t.cl.mu.Unlock()
107 // Clobbers the torrent display name. The display name is used as the torrent
108 // name if the metainfo is not available.
109 func (t *Torrent) SetDisplayName(dn string) {
111 defer t.cl.mu.Unlock()
115 // The current working name for the torrent. Either the name in the info dict,
116 // or a display name given such as by the dn value in a magnet link, or "".
117 func (t *Torrent) Name() string {
119 defer t.cl.mu.Unlock()
123 // The completed length of all the torrent data, in all its files. This is
124 // derived from the torrent info, when it is available.
125 func (t *Torrent) Length() int64 {
129 // Returns a run-time generated metainfo for the torrent that includes the
130 // info bytes and announce-list as currently known to the client.
131 func (t *Torrent) Metainfo() metainfo.MetaInfo {
133 defer t.cl.mu.Unlock()
134 return t.newMetaInfo()
137 func (t *Torrent) addReader(r *reader) {
139 defer t.cl.mu.Unlock()
140 if t.readers == nil {
141 t.readers = make(map[*reader]struct{})
143 t.readers[r] = struct{}{}
147 func (t *Torrent) deleteReader(r *reader) {
152 func (t *Torrent) DownloadPieces(begin, end int) {
154 defer t.cl.mu.Unlock()
155 for i := begin; i < end; i++ {
156 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
157 t.updatePiecePriority(i)
162 func (t *Torrent) CancelPieces(begin, end int) {
164 defer t.cl.mu.Unlock()
165 for i := begin; i < end; i++ {
167 if p.priority == PiecePriorityNone {
170 p.priority = PiecePriorityNone
171 t.updatePiecePriority(i)
175 func (t *Torrent) initFiles() {
177 t.files = new([]*File)
178 for _, fi := range t.info.UpvertedFiles() {
179 *t.files = append(*t.files, &File{
181 strings.Join(append([]string{t.info.Name}, fi.Path...), "/"),
192 // Returns handles to the files in the torrent. This requires that the Info is
194 func (t *Torrent) Files() []*File {
198 func (t *Torrent) AddPeers(pp []Peer) {
205 // Marks the entire torrent for download. Requires the info first, see
206 // GotInfo. Sets piece priorities for historical reasons.
207 func (t *Torrent) DownloadAll() {
208 t.DownloadPieces(0, t.numPieces())
211 func (t *Torrent) String() string {
214 s = t.infoHash.HexString()
219 func (t *Torrent) AddTrackers(announceList [][]string) {
221 defer t.cl.mu.Unlock()
222 t.addTrackers(announceList)
225 func (t *Torrent) Piece(i int) *Piece {
227 defer t.cl.mu.Unlock()