7 "github.com/anacrolix/chansync/events"
8 "github.com/anacrolix/missinggo/pubsub"
9 "github.com/anacrolix/sync"
11 "github.com/anacrolix/torrent/metainfo"
14 // The Torrent's infohash. This is fixed and cannot change. It uniquely identifies a torrent.
15 func (t *Torrent) InfoHash() metainfo.Hash {
19 // Returns a channel that is closed when the info (.Info()) for the torrent has become available.
20 func (t *Torrent) GotInfo() events.Done {
24 // Returns the metainfo info dictionary, or nil if it's not yet available.
25 func (t *Torrent) Info() (info *metainfo.Info) {
32 // Returns a Reader bound to the torrent's data. All read calls block until the data requested is
33 // actually available. Note that you probably want to ensure the Torrent Info is available first.
34 func (t *Torrent) NewReader() Reader {
35 return t.newReader(0, *t.length)
38 func (t *Torrent) newReader(offset, length int64) Reader {
45 r.readaheadFunc = r.defaultReadaheadFunc
50 type PieceStateRuns []PieceStateRun
52 func (me PieceStateRuns) String() (s string) {
54 var sb strings.Builder
55 sb.WriteString(me[0].String())
56 for i := 1; i < len(me); i += 1 {
58 sb.WriteString(me[i].String())
65 // Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of
66 // the state run-lengths is the number of pieces in the torrent.
67 func (t *Torrent) PieceStateRuns() (runs PieceStateRuns) {
69 runs = t.pieceStateRuns()
74 func (t *Torrent) PieceState(piece pieceIndex) (ps PieceState) {
76 ps = 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() (ret bool) {
131 // Clobbers the torrent display name if metainfo is unavailable.
132 // The display name is used as the torrent name while the metainfo is unavailable.
133 func (t *Torrent) SetDisplayName(dn string) {
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) {
181 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, "Torrent.DownloadPieces")
193 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
195 t.cancelPiecesLocked(begin, end, "Torrent.CancelPieces")
199 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason string) {
200 for i := begin; i < end; i++ {
202 if p.priority == PiecePriorityNone {
205 p.priority = PiecePriorityNone
206 t.updatePiecePriority(i, reason)
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...), "/"),
237 // Returns handles to the files in the torrent. This requires that the Info is
239 func (t *Torrent) Files() []*File {
243 func (t *Torrent) AddPeers(pp []PeerInfo) (n int) {
250 // Marks the entire torrent for download. Requires the info first, see
251 // GotInfo. Sets piece priorities for historical reasons.
252 func (t *Torrent) DownloadAll() {
253 t.DownloadPieces(0, t.numPieces())
256 func (t *Torrent) String() string {
259 return t.infoHash.HexString()
261 return strconv.Quote(s)
265 func (t *Torrent) AddTrackers(announceList [][]string) {
268 t.addTrackers(announceList)
271 func (t *Torrent) Piece(i pieceIndex) *Piece {
275 func (t *Torrent) PeerConns() []*PeerConn {
278 ret := make([]*PeerConn, 0, len(t.conns))
279 for c := range t.conns {