7 "github.com/anacrolix/missinggo/pubsub"
9 "github.com/anacrolix/torrent/metainfo"
12 // The torrent's infohash. This is fixed and cannot change. It uniquely
13 // identifies a torrent.
14 func (t *Torrent) InfoHash() metainfo.Hash {
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{} {
23 return t.gotMetainfo.C()
26 // Returns the metainfo info dictionary, or nil if it's not yet available.
27 func (t *Torrent) Info() *metainfo.Info {
33 // Returns a Reader bound to the torrent's data. All read calls block until
34 // the data requested is actually available.
35 func (t *Torrent) NewReader() Reader {
39 readahead: 5 * 1024 * 1024,
46 // Returns the state of pieces of the torrent. They are grouped into runs of
47 // same state. The sum of the state run lengths is the number of pieces
49 func (t *Torrent) PieceStateRuns() []PieceStateRun {
52 return t.pieceStateRuns()
55 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
58 return t.pieceState(piece)
61 // The number of pieces in the torrent. This requires that the info has been
63 func (t *Torrent) NumPieces() pieceIndex {
67 // Get missing bytes count for specific piece.
68 func (t *Torrent) PieceBytesMissing(piece int) int64 {
72 return int64(t.pieces[piece].bytesLeft())
75 // Drop the torrent from the client, and close it. It's always safe to do
76 // this. No data corruption can, or should occur to either the torrent's data,
77 // or connected peers.
78 func (t *Torrent) Drop() {
80 t.cl.dropTorrent(t.infoHash)
84 // Number of bytes of the entire torrent we have completed. This is the sum of
85 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
86 // for download rate, as it can go down when pieces are lost or fail checks.
87 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
88 func (t *Torrent) BytesCompleted() int64 {
91 return t.bytesCompleted()
94 // The subscription emits as (int) the index of pieces as their state changes.
95 // A state change is when the PieceState for a piece alters in value.
96 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
97 return t.pieceStateChanges.Subscribe()
100 // Returns true if the torrent is currently being seeded. This occurs when the
101 // client is willing to upload without wanting anything in return.
102 func (t *Torrent) Seeding() bool {
108 // Clobbers the torrent display name. The display name is used as the torrent
109 // name if the metainfo is not available.
110 func (t *Torrent) SetDisplayName(dn string) {
112 defer t.nameMu.Unlock()
119 // The current working name for the torrent. Either the name in the info dict,
120 // or a display name given such as by the dn value in a magnet link, or "".
121 func (t *Torrent) Name() string {
125 // The completed length of all the torrent data, in all its files. This is
126 // derived from the torrent info, when it is available.
127 func (t *Torrent) Length() int64 {
131 // Returns a run-time generated metainfo for the torrent that includes the
132 // info bytes and announce-list as currently known to the client.
133 func (t *Torrent) Metainfo() metainfo.MetaInfo {
136 return t.newMetaInfo()
139 func (t *Torrent) addReader(r *reader) {
142 if t.readers == nil {
143 t.readers = make(map[*reader]struct{})
145 t.readers[r] = struct{}{}
149 func (t *Torrent) deleteReader(r *reader) {
154 // Raise the priorities of pieces in the range [begin, end) to at least Normal
155 // priority. Piece indexes are not the same as bytes. Requires that the info
156 // has been obtained, see Torrent.Info and Torrent.GotInfo.
157 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
160 t.downloadPiecesLocked(begin, end)
163 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
164 for i := begin; i < end; i++ {
165 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
166 t.updatePiecePriority(i)
171 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
174 t.cancelPiecesLocked(begin, end)
177 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
178 for i := begin; i < end; i++ {
180 if p.priority == PiecePriorityNone {
183 p.priority = PiecePriorityNone
184 t.updatePiecePriority(i)
188 func (t *Torrent) initFiles() {
190 t.files = new([]*File)
191 for _, fi := range t.info.UpvertedFiles() {
192 *t.files = append(*t.files, &File{
194 strings.Join(append([]string{t.info.Name}, fi.Path...), "/"),
205 // Returns handles to the files in the torrent. This requires that the Info is
207 func (t *Torrent) Files() []*File {
211 func (t *Torrent) AddPeers(pp []Peer) {
218 // Marks the entire torrent for download. Requires the info first, see
219 // GotInfo. Sets piece priorities for historical reasons.
220 func (t *Torrent) DownloadAll() {
221 t.DownloadPieces(0, t.numPieces())
224 func (t *Torrent) String() string {
227 return t.infoHash.HexString()
229 return strconv.Quote(s)
233 func (t *Torrent) AddTrackers(announceList [][]string) {
236 t.addTrackers(announceList)
239 func (t *Torrent) Piece(i pieceIndex) *Piece {