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.
84 func (t *Torrent) BytesCompleted() int64 {
86 defer t.cl.mu.RUnlock()
87 return t.bytesCompleted()
90 // The subscription emits as (int) the index of pieces as their state changes.
91 // A state change is when the PieceState for a piece alters in value.
92 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
93 return t.pieceStateChanges.Subscribe()
96 // Returns true if the torrent is currently being seeded. This occurs when the
97 // client is willing to upload without wanting anything in return.
98 func (t *Torrent) Seeding() bool {
100 defer t.cl.mu.Unlock()
104 // Clobbers the torrent display name. The display name is used as the torrent
105 // name if the metainfo is not available.
106 func (t *Torrent) SetDisplayName(dn string) {
108 defer t.cl.mu.Unlock()
112 // The current working name for the torrent. Either the name in the info dict,
113 // or a display name given such as by the dn value in a magnet link, or "".
114 func (t *Torrent) Name() string {
116 defer t.cl.mu.Unlock()
120 // The completed length of all the torrent data, in all its files. This is
121 // derived from the torrent info, when it is available.
122 func (t *Torrent) Length() int64 {
126 // Returns a run-time generated metainfo for the torrent that includes the
127 // info bytes and announce-list as currently known to the client.
128 func (t *Torrent) Metainfo() metainfo.MetaInfo {
130 defer t.cl.mu.Unlock()
131 return t.newMetaInfo()
134 func (t *Torrent) addReader(r *reader) {
136 defer t.cl.mu.Unlock()
137 if t.readers == nil {
138 t.readers = make(map[*reader]struct{})
140 t.readers[r] = struct{}{}
144 func (t *Torrent) deleteReader(r *reader) {
149 func (t *Torrent) DownloadPieces(begin, end int) {
151 defer t.cl.mu.Unlock()
152 t.pendPieceRange(begin, end)
155 func (t *Torrent) CancelPieces(begin, end int) {
157 defer t.cl.mu.Unlock()
158 t.unpendPieceRange(begin, end)
161 // Returns handles to the files in the torrent. This requires the metainfo is
163 func (t *Torrent) Files() (ret []File) {
169 for _, fi := range info.UpvertedFiles() {
170 ret = append(ret, File{
172 strings.Join(append([]string{info.Name}, fi.Path...), "/"),
182 func (t *Torrent) AddPeers(pp []Peer) {
189 // Marks the entire torrent for download. Requires the info first, see
191 func (t *Torrent) DownloadAll() {
193 defer t.cl.mu.Unlock()
194 t.pendPieceRange(0, t.numPieces())
197 func (t *Torrent) String() string {
200 s = t.infoHash.HexString()
205 func (t *Torrent) AddTrackers(announceList [][]string) {
207 defer t.cl.mu.Unlock()
208 t.addTrackers(announceList)
211 func (t *Torrent) Piece(i int) *Piece {
213 defer t.cl.mu.Unlock()