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 identifies a torrent.
13 func (t *Torrent) InfoHash() metainfo.Hash {
17 // Returns a channel that is closed when the info (.Info()) for the torrent has become available.
18 func (t *Torrent) GotInfo() <-chan struct{} {
21 return t.gotMetainfo.C()
24 // Returns the metainfo info dictionary, or nil if it's not yet available.
25 func (t *Torrent) Info() *metainfo.Info {
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() Reader {
37 readahead: 5 * 1024 * 1024,
44 // Returns the state of pieces of the torrent. They are grouped into runs of
45 // same state. The sum of the state run lengths is the number of pieces
47 func (t *Torrent) PieceStateRuns() []PieceStateRun {
50 return t.pieceStateRuns()
53 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
56 return t.pieceState(piece)
59 // The number of pieces in the torrent. This requires that the info has been
61 func (t *Torrent) NumPieces() pieceIndex {
65 // Get missing bytes count for specific piece.
66 func (t *Torrent) PieceBytesMissing(piece int) int64 {
70 return int64(t.pieces[piece].bytesLeft())
73 // Drop the torrent from the client, and close it. It's always safe to do
74 // this. No data corruption can, or should occur to either the torrent's data,
75 // or connected peers.
76 func (t *Torrent) Drop() {
78 t.cl.dropTorrent(t.infoHash)
82 // Number of bytes of the entire torrent we have completed. This is the sum of
83 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
84 // for download rate, as it can go down when pieces are lost or fail checks.
85 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
86 func (t *Torrent) BytesCompleted() int64 {
89 return t.bytesCompleted()
92 // The subscription emits as (int) the index of pieces as their state changes.
93 // A state change is when the PieceState for a piece alters in value.
94 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
95 return t.pieceStateChanges.Subscribe()
98 // Returns true if the torrent is currently being seeded. This occurs when the
99 // client is willing to upload without wanting anything in return.
100 func (t *Torrent) Seeding() bool {
106 // Clobbers the torrent display name. The display name is used as the torrent
107 // name if the metainfo is not available.
108 func (t *Torrent) SetDisplayName(dn string) {
110 defer t.nameMu.Unlock()
117 // The current working name for the torrent. Either the name in the info dict,
118 // or a display name given such as by the dn value in a magnet link, or "".
119 func (t *Torrent) Name() string {
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 {
134 return t.newMetaInfo()
137 func (t *Torrent) addReader(r *reader) {
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 // Raise the priorities of pieces in the range [begin, end) to at least Normal
153 // priority. Piece indexes are not the same as bytes. Requires that the info
154 // has been obtained, see Torrent.Info and Torrent.GotInfo.
155 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
158 t.downloadPiecesLocked(begin, end)
161 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
162 for i := begin; i < end; i++ {
163 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
164 t.updatePiecePriority(i)
169 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
172 t.cancelPiecesLocked(begin, end)
175 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
176 for i := begin; i < end; i++ {
178 if p.priority == PiecePriorityNone {
181 p.priority = PiecePriorityNone
182 t.updatePiecePriority(i)
186 func (t *Torrent) initFiles() {
188 t.files = new([]*File)
189 for _, fi := range t.info.UpvertedFiles() {
191 if len(fi.PathUTF8) != 0 {
196 *t.files = append(*t.files, &File{
198 strings.Join(append([]string{t.info.Name}, path...), "/"),
209 // Returns handles to the files in the torrent. This requires that the Info is
211 func (t *Torrent) Files() []*File {
215 func (t *Torrent) AddPeers(pp []Peer) {
222 // Marks the entire torrent for download. Requires the info first, see
223 // GotInfo. Sets piece priorities for historical reasons.
224 func (t *Torrent) DownloadAll() {
225 t.DownloadPieces(0, t.numPieces())
228 func (t *Torrent) String() string {
231 return t.infoHash.HexString()
233 return strconv.Quote(s)
237 func (t *Torrent) AddTrackers(announceList [][]string) {
240 t.addTrackers(announceList)
243 func (t *Torrent) Piece(i pieceIndex) *Piece {
247 func (t *Torrent) PeerConns() []*PeerConn {
248 ret := make([]*PeerConn, 0, len(t.conns))
249 for c := range t.conns {