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 the data requested is
32 // actually available. Note that you probably want to ensure the Torrent Info is available first.
33 func (t *Torrent) NewReader() Reader {
37 readahead: 5 * 1024 * 1024,
44 type PieceStateRuns []PieceStateRun
46 func (me PieceStateRuns) String() string {
47 ss := make([]string, 0, len(me))
48 for _, psr := range me {
49 ss = append(ss, psr.String())
51 return strings.Join(ss, " ")
54 // Returns the state of pieces of the torrent. They are grouped into runs of same state. The sum of
55 // the state run-lengths is the number of pieces in the torrent.
56 func (t *Torrent) PieceStateRuns() PieceStateRuns {
59 return t.pieceStateRuns()
62 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
65 return t.pieceState(piece)
68 // The number of pieces in the torrent. This requires that the info has been
70 func (t *Torrent) NumPieces() pieceIndex {
74 // Get missing bytes count for specific piece.
75 func (t *Torrent) PieceBytesMissing(piece int) int64 {
79 return int64(t.pieces[piece].bytesLeft())
82 // Drop the torrent from the client, and close it. It's always safe to do
83 // this. No data corruption can, or should occur to either the torrent's data,
84 // or connected peers.
85 func (t *Torrent) Drop() {
87 t.cl.dropTorrent(t.infoHash)
91 // Number of bytes of the entire torrent we have completed. This is the sum of
92 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
93 // for download rate, as it can go down when pieces are lost or fail checks.
94 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
95 func (t *Torrent) BytesCompleted() int64 {
98 return t.bytesCompleted()
101 // The subscription emits as (int) the index of pieces as their state changes.
102 // A state change is when the PieceState for a piece alters in value.
103 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
104 return t.pieceStateChanges.Subscribe()
107 // Returns true if the torrent is currently being seeded. This occurs when the
108 // client is willing to upload without wanting anything in return.
109 func (t *Torrent) Seeding() bool {
115 // Clobbers the torrent display name. The display name is used as the torrent
116 // name if the metainfo is not available.
117 func (t *Torrent) SetDisplayName(dn string) {
119 defer t.nameMu.Unlock()
126 // The current working name for the torrent. Either the name in the info dict,
127 // or a display name given such as by the dn value in a magnet link, or "".
128 func (t *Torrent) Name() string {
132 // The completed length of all the torrent data, in all its files. This is
133 // derived from the torrent info, when it is available.
134 func (t *Torrent) Length() int64 {
138 // Returns a run-time generated metainfo for the torrent that includes the
139 // info bytes and announce-list as currently known to the client.
140 func (t *Torrent) Metainfo() metainfo.MetaInfo {
143 return t.newMetaInfo()
146 func (t *Torrent) addReader(r *reader) {
149 if t.readers == nil {
150 t.readers = make(map[*reader]struct{})
152 t.readers[r] = struct{}{}
156 func (t *Torrent) deleteReader(r *reader) {
161 // Raise the priorities of pieces in the range [begin, end) to at least Normal
162 // priority. Piece indexes are not the same as bytes. Requires that the info
163 // has been obtained, see Torrent.Info and Torrent.GotInfo.
164 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
167 t.downloadPiecesLocked(begin, end)
170 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
171 for i := begin; i < end; i++ {
172 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
173 t.updatePiecePriority(i)
178 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
181 t.cancelPiecesLocked(begin, end)
184 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
185 for i := begin; i < end; i++ {
187 if p.priority == PiecePriorityNone {
190 p.priority = PiecePriorityNone
191 t.updatePiecePriority(i)
195 func (t *Torrent) initFiles() {
197 t.files = new([]*File)
198 for _, fi := range t.info.UpvertedFiles() {
200 if len(fi.PathUTF8) != 0 {
205 *t.files = append(*t.files, &File{
207 strings.Join(append([]string{t.info.Name}, path...), "/"),
218 // Returns handles to the files in the torrent. This requires that the Info is
220 func (t *Torrent) Files() []*File {
224 func (t *Torrent) AddPeers(pp []Peer) int {
228 return t.addPeers(pp)
231 // Marks the entire torrent for download. Requires the info first, see
232 // GotInfo. Sets piece priorities for historical reasons.
233 func (t *Torrent) DownloadAll() {
234 t.DownloadPieces(0, t.numPieces())
237 func (t *Torrent) String() string {
240 return t.infoHash.HexString()
242 return strconv.Quote(s)
246 func (t *Torrent) AddTrackers(announceList [][]string) {
249 t.addTrackers(announceList)
252 func (t *Torrent) Piece(i pieceIndex) *Piece {
256 func (t *Torrent) PeerConns() []*PeerConn {
257 ret := make([]*PeerConn, 0, len(t.conns))
258 for c := range t.conns {