7 "github.com/anacrolix/missinggo/pubsub"
8 "github.com/anacrolix/sync"
10 "github.com/anacrolix/torrent/metainfo"
13 // The Torrent's infohash. This is fixed and cannot change. It uniquely identifies a torrent.
14 func (t *Torrent) InfoHash() metainfo.Hash {
18 // Returns a channel that is closed when the info (.Info()) for the torrent has become available.
19 func (t *Torrent) GotInfo() <-chan struct{} {
20 // TODO: We shouldn't need to lock to take a channel here, if the event is only ever set.
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 the data requested is
34 // actually available. Note that you probably want to ensure the Torrent Info is available first.
35 func (t *Torrent) NewReader() Reader {
36 return t.newReader(0, *t.length)
39 func (t *Torrent) newReader(offset, length int64) Reader {
46 r.readaheadFunc = r.defaultReadaheadFunc
51 type PieceStateRuns []PieceStateRun
53 func (me PieceStateRuns) String() (s string) {
55 var sb strings.Builder
56 sb.WriteString(me[0].String())
57 for i := 1; i < len(me); i += 1 {
59 sb.WriteString(me[i].String())
66 // Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of
67 // the state run-lengths is the number of pieces in the torrent.
68 func (t *Torrent) PieceStateRuns() (runs PieceStateRuns) {
70 runs = t.pieceStateRuns()
75 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
78 return t.pieceState(piece)
81 // The number of pieces in the torrent. This requires that the info has been
83 func (t *Torrent) NumPieces() pieceIndex {
87 // Get missing bytes count for specific piece.
88 func (t *Torrent) PieceBytesMissing(piece int) int64 {
92 return int64(t.pieces[piece].bytesLeft())
95 // Drop the torrent from the client, and close it. It's always safe to do
96 // this. No data corruption can, or should occur to either the torrent's data,
97 // or connected peers.
98 func (t *Torrent) Drop() {
103 t.cl.dropTorrent(t.infoHash, &wg)
106 // Number of bytes of the entire torrent we have completed. This is the sum of
107 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
108 // for download rate, as it can go down when pieces are lost or fail checks.
109 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
110 func (t *Torrent) BytesCompleted() int64 {
113 return t.bytesCompleted()
116 // The subscription emits as (int) the index of pieces as their state changes.
117 // A state change is when the PieceState for a piece alters in value.
118 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
119 return t.pieceStateChanges.Subscribe()
122 // Returns true if the torrent is currently being seeded. This occurs when the
123 // client is willing to upload without wanting anything in return.
124 func (t *Torrent) Seeding() bool {
130 // Clobbers the torrent display name. The display name is used as the torrent
131 // name if the metainfo is not available.
132 func (t *Torrent) SetDisplayName(dn string) {
134 defer t.nameMu.Unlock()
141 // The current working name for the torrent. Either the name in the info dict,
142 // or a display name given such as by the dn value in a magnet link, or "".
143 func (t *Torrent) Name() string {
147 // The completed length of all the torrent data, in all its files. This is
148 // derived from the torrent info, when it is available.
149 func (t *Torrent) Length() int64 {
153 // Returns a run-time generated metainfo for the torrent that includes the
154 // info bytes and announce-list as currently known to the client.
155 func (t *Torrent) Metainfo() metainfo.MetaInfo {
158 return t.newMetaInfo()
161 func (t *Torrent) addReader(r *reader) {
164 if t.readers == nil {
165 t.readers = make(map[*reader]struct{})
167 t.readers[r] = struct{}{}
171 func (t *Torrent) deleteReader(r *reader) {
176 // Raise the priorities of pieces in the range [begin, end) to at least Normal
177 // priority. Piece indexes are not the same as bytes. Requires that the info
178 // has been obtained, see Torrent.Info and Torrent.GotInfo.
179 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
182 t.downloadPiecesLocked(begin, end)
185 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
186 for i := begin; i < end; i++ {
187 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
188 t.updatePiecePriority(i)
193 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
196 t.cancelPiecesLocked(begin, end)
199 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
200 for i := begin; i < end; i++ {
202 if p.priority == PiecePriorityNone {
205 p.priority = PiecePriorityNone
206 t.updatePiecePriority(i)
210 func (t *Torrent) initFiles() {
212 t.files = new([]*File)
213 for _, fi := range t.info.UpvertedFiles() {
215 if len(fi.PathUTF8) != 0 {
221 if len(fi.Path) != 0 {
222 dp = strings.Join(fi.Path, "/")
224 *t.files = append(*t.files, &File{
226 strings.Join(append([]string{t.info.Name}, path...), "/"),
238 // Returns handles to the files in the torrent. This requires that the Info is
240 func (t *Torrent) Files() []*File {
244 func (t *Torrent) AddPeers(pp []PeerInfo) int {
248 return t.addPeers(pp)
251 // Marks the entire torrent for download. Requires the info first, see
252 // GotInfo. Sets piece priorities for historical reasons.
253 func (t *Torrent) DownloadAll() {
254 t.DownloadPieces(0, t.numPieces())
257 func (t *Torrent) String() string {
260 return t.infoHash.HexString()
262 return strconv.Quote(s)
266 func (t *Torrent) AddTrackers(announceList [][]string) {
269 t.addTrackers(announceList)
272 func (t *Torrent) Piece(i pieceIndex) *Piece {
276 func (t *Torrent) PeerConns() []*PeerConn {
279 ret := make([]*PeerConn, 0, len(t.conns))
280 for c := range t.conns {