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) (ps PieceState) {
79 ps = t.pieceState(piece)
84 // The number of pieces in the torrent. This requires that the info has been
86 func (t *Torrent) NumPieces() pieceIndex {
90 // Get missing bytes count for specific piece.
91 func (t *Torrent) PieceBytesMissing(piece int) int64 {
95 return int64(t.pieces[piece].bytesLeft())
98 // Drop the torrent from the client, and close it. It's always safe to do
99 // this. No data corruption can, or should occur to either the torrent's data,
100 // or connected peers.
101 func (t *Torrent) Drop() {
102 var wg sync.WaitGroup
106 t.cl.dropTorrent(t.infoHash, &wg)
109 // Number of bytes of the entire torrent we have completed. This is the sum of
110 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
111 // for download rate, as it can go down when pieces are lost or fail checks.
112 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
113 func (t *Torrent) BytesCompleted() int64 {
116 return t.bytesCompleted()
119 // The subscription emits as (int) the index of pieces as their state changes.
120 // A state change is when the PieceState for a piece alters in value.
121 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
122 return t.pieceStateChanges.Subscribe()
125 // Returns true if the torrent is currently being seeded. This occurs when the
126 // client is willing to upload without wanting anything in return.
127 func (t *Torrent) Seeding() (ret bool) {
134 // Clobbers the torrent display name if metainfo is unavailable.
135 // The display name is used as the torrent name while the metainfo is unavailable.
136 func (t *Torrent) SetDisplayName(dn string) {
144 // The current working name for the torrent. Either the name in the info dict,
145 // or a display name given such as by the dn value in a magnet link, or "".
146 func (t *Torrent) Name() string {
150 // The completed length of all the torrent data, in all its files. This is
151 // derived from the torrent info, when it is available.
152 func (t *Torrent) Length() int64 {
156 // Returns a run-time generated metainfo for the torrent that includes the
157 // info bytes and announce-list as currently known to the client.
158 func (t *Torrent) Metainfo() metainfo.MetaInfo {
161 return t.newMetaInfo()
164 func (t *Torrent) addReader(r *reader) {
167 if t.readers == nil {
168 t.readers = make(map[*reader]struct{})
170 t.readers[r] = struct{}{}
174 func (t *Torrent) deleteReader(r *reader) {
179 // Raise the priorities of pieces in the range [begin, end) to at least Normal
180 // priority. Piece indexes are not the same as bytes. Requires that the info
181 // has been obtained, see Torrent.Info and Torrent.GotInfo.
182 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
184 t.downloadPiecesLocked(begin, end)
188 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
189 for i := begin; i < end; i++ {
190 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
191 t.updatePiecePriority(i)
196 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
198 t.cancelPiecesLocked(begin, end)
202 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
203 for i := begin; i < end; i++ {
205 if p.priority == PiecePriorityNone {
208 p.priority = PiecePriorityNone
209 t.updatePiecePriority(i)
213 func (t *Torrent) initFiles() {
215 t.files = new([]*File)
216 for _, fi := range t.info.UpvertedFiles() {
218 if len(fi.PathUTF8) != 0 {
224 if len(fi.Path) != 0 {
225 dp = strings.Join(fi.Path, "/")
227 *t.files = append(*t.files, &File{
229 strings.Join(append([]string{t.info.Name}, path...), "/"),
241 // Returns handles to the files in the torrent. This requires that the Info is
243 func (t *Torrent) Files() []*File {
247 func (t *Torrent) AddPeers(pp []PeerInfo) (n int) {
254 // Marks the entire torrent for download. Requires the info first, see
255 // GotInfo. Sets piece priorities for historical reasons.
256 func (t *Torrent) DownloadAll() {
257 t.DownloadPieces(0, t.numPieces())
260 func (t *Torrent) String() string {
263 return t.infoHash.HexString()
265 return strconv.Quote(s)
269 func (t *Torrent) AddTrackers(announceList [][]string) {
272 t.addTrackers(announceList)
275 func (t *Torrent) Piece(i pieceIndex) *Piece {
279 func (t *Torrent) PeerConns() []*PeerConn {
282 ret := make([]*PeerConn, 0, len(t.conns))
283 for c := range t.conns {