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() (ret <-chan struct{}) {
20 // TODO: We shouldn't need to lock to take a channel here, if the event is only ever set.
27 // Returns the metainfo info dictionary, or nil if it's not yet available.
28 func (t *Torrent) Info() (info *metainfo.Info) {
35 // Returns a Reader bound to the torrent's data. All read calls block until the data requested is
36 // actually available. Note that you probably want to ensure the Torrent Info is available first.
37 func (t *Torrent) NewReader() Reader {
38 return t.newReader(0, *t.length)
41 func (t *Torrent) newReader(offset, length int64) Reader {
48 r.readaheadFunc = r.defaultReadaheadFunc
53 type PieceStateRuns []PieceStateRun
55 func (me PieceStateRuns) String() (s string) {
57 var sb strings.Builder
58 sb.WriteString(me[0].String())
59 for i := 1; i < len(me); i += 1 {
61 sb.WriteString(me[i].String())
68 // Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of
69 // the state run-lengths is the number of pieces in the torrent.
70 func (t *Torrent) PieceStateRuns() (runs PieceStateRuns) {
72 runs = t.pieceStateRuns()
77 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
80 return t.pieceState(piece)
83 // The number of pieces in the torrent. This requires that the info has been
85 func (t *Torrent) NumPieces() pieceIndex {
89 // Get missing bytes count for specific piece.
90 func (t *Torrent) PieceBytesMissing(piece int) int64 {
94 return int64(t.pieces[piece].bytesLeft())
97 // Drop the torrent from the client, and close it. It's always safe to do
98 // this. No data corruption can, or should occur to either the torrent's data,
99 // or connected peers.
100 func (t *Torrent) Drop() {
101 var wg sync.WaitGroup
105 t.cl.dropTorrent(t.infoHash, &wg)
108 // Number of bytes of the entire torrent we have completed. This is the sum of
109 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
110 // for download rate, as it can go down when pieces are lost or fail checks.
111 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
112 func (t *Torrent) BytesCompleted() int64 {
115 return t.bytesCompleted()
118 // The subscription emits as (int) the index of pieces as their state changes.
119 // A state change is when the PieceState for a piece alters in value.
120 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
121 return t.pieceStateChanges.Subscribe()
124 // Returns true if the torrent is currently being seeded. This occurs when the
125 // client is willing to upload without wanting anything in return.
126 func (t *Torrent) Seeding() bool {
132 // Clobbers the torrent display name. The display name is used as the torrent
133 // name if the metainfo is not available.
134 func (t *Torrent) SetDisplayName(dn string) {
136 defer t.nameMu.Unlock()
143 // The current working name for the torrent. Either the name in the info dict,
144 // or a display name given such as by the dn value in a magnet link, or "".
145 func (t *Torrent) Name() string {
149 // The completed length of all the torrent data, in all its files. This is
150 // derived from the torrent info, when it is available.
151 func (t *Torrent) Length() int64 {
155 // Returns a run-time generated metainfo for the torrent that includes the
156 // info bytes and announce-list as currently known to the client.
157 func (t *Torrent) Metainfo() metainfo.MetaInfo {
160 return t.newMetaInfo()
163 func (t *Torrent) addReader(r *reader) {
166 if t.readers == nil {
167 t.readers = make(map[*reader]struct{})
169 t.readers[r] = struct{}{}
173 func (t *Torrent) deleteReader(r *reader) {
178 // Raise the priorities of pieces in the range [begin, end) to at least Normal
179 // priority. Piece indexes are not the same as bytes. Requires that the info
180 // has been obtained, see Torrent.Info and Torrent.GotInfo.
181 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
183 t.downloadPiecesLocked(begin, end)
187 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
188 for i := begin; i < end; i++ {
189 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
190 t.updatePiecePriority(i)
195 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
197 t.cancelPiecesLocked(begin, end)
201 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
202 for i := begin; i < end; i++ {
204 if p.priority == PiecePriorityNone {
207 p.priority = PiecePriorityNone
208 t.updatePiecePriority(i)
212 func (t *Torrent) initFiles() {
214 t.files = new([]*File)
215 for _, fi := range t.info.UpvertedFiles() {
217 if len(fi.PathUTF8) != 0 {
223 if len(fi.Path) != 0 {
224 dp = strings.Join(fi.Path, "/")
226 *t.files = append(*t.files, &File{
228 strings.Join(append([]string{t.info.Name}, path...), "/"),
240 // Returns handles to the files in the torrent. This requires that the Info is
242 func (t *Torrent) Files() []*File {
246 func (t *Torrent) AddPeers(pp []PeerInfo) int {
250 return t.addPeers(pp)
253 // Marks the entire torrent for download. Requires the info first, see
254 // GotInfo. Sets piece priorities for historical reasons.
255 func (t *Torrent) DownloadAll() {
256 t.DownloadPieces(0, t.numPieces())
259 func (t *Torrent) String() string {
262 return t.infoHash.HexString()
264 return strconv.Quote(s)
268 func (t *Torrent) AddTrackers(announceList [][]string) {
271 t.addTrackers(announceList)
274 func (t *Torrent) Piece(i pieceIndex) *Piece {
278 func (t *Torrent) PeerConns() []*PeerConn {
281 ret := make([]*PeerConn, 0, len(t.conns))
282 for c := range t.conns {