6 "github.com/anacrolix/missinggo/pubsub"
7 "github.com/anacrolix/torrent/metainfo"
10 // The torrent's infohash. This is fixed and cannot change. It uniquely
11 // identifies a torrent.
12 func (t *Torrent) InfoHash() metainfo.Hash {
16 // Returns a channel that is closed when the info (.Info()) for the torrent
17 // 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
32 // the data requested is actually available.
33 func (t *Torrent) NewReader() Reader {
37 readahead: 5 * 1024 * 1024,
44 // Returns the state of pieces of the torrent. They are grouped into runs of
45 // same state. The sum of the state run lengths is the number of pieces
47 func (t *Torrent) PieceStateRuns() []PieceStateRun {
50 return t.pieceStateRuns()
53 func (t *Torrent) PieceState(piece pieceIndex) PieceState {
56 return t.pieceState(piece)
59 // The number of pieces in the torrent. This requires that the info has been
61 func (t *Torrent) NumPieces() pieceIndex {
65 // Get missing bytes count for specific piece.
66 func (t *Torrent) PieceBytesMissing(piece int) int64 {
70 return int64(t.pieces[piece].bytesLeft())
73 // Drop the torrent from the client, and close it. It's always safe to do
74 // this. No data corruption can, or should occur to either the torrent's data,
75 // or connected peers.
76 func (t *Torrent) Drop() {
78 t.cl.dropTorrent(t.infoHash)
82 // Number of bytes of the entire torrent we have completed. This is the sum of
83 // completed pieces, and dirtied chunks of incomplete pieces. Do not use this
84 // for download rate, as it can go down when pieces are lost or fail checks.
85 // Sample Torrent.Stats.DataBytesRead for actual file data download rate.
86 func (t *Torrent) BytesCompleted() int64 {
89 return t.bytesCompleted()
92 // The subscription emits as (int) the index of pieces as their state changes.
93 // A state change is when the PieceState for a piece alters in value.
94 func (t *Torrent) SubscribePieceStateChanges() *pubsub.Subscription {
95 return t.pieceStateChanges.Subscribe()
98 // Returns true if the torrent is currently being seeded. This occurs when the
99 // client is willing to upload without wanting anything in return.
100 func (t *Torrent) Seeding() bool {
106 // Clobbers the torrent display name. The display name is used as the torrent
107 // name if the metainfo is not available.
108 func (t *Torrent) SetDisplayName(dn string) {
114 // The current working name for the torrent. Either the name in the info dict,
115 // or a display name given such as by the dn value in a magnet link, or "".
116 func (t *Torrent) Name() string {
122 // The completed length of all the torrent data, in all its files. This is
123 // derived from the torrent info, when it is available.
124 func (t *Torrent) Length() int64 {
128 // Returns a run-time generated metainfo for the torrent that includes the
129 // info bytes and announce-list as currently known to the client.
130 func (t *Torrent) Metainfo() metainfo.MetaInfo {
133 return t.newMetaInfo()
136 func (t *Torrent) addReader(r *reader) {
139 if t.readers == nil {
140 t.readers = make(map[*reader]struct{})
142 t.readers[r] = struct{}{}
146 func (t *Torrent) deleteReader(r *reader) {
151 // Raise the priorities of pieces in the range [begin, end) to at least Normal
152 // priority. Piece indexes are not the same as bytes. Requires that the info
153 // has been obtained, see Torrent.Info and Torrent.GotInfo.
154 func (t *Torrent) DownloadPieces(begin, end pieceIndex) {
157 t.downloadPiecesLocked(begin, end)
160 func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) {
161 for i := begin; i < end; i++ {
162 if t.pieces[i].priority.Raise(PiecePriorityNormal) {
163 t.updatePiecePriority(i)
168 func (t *Torrent) CancelPieces(begin, end pieceIndex) {
171 t.cancelPiecesLocked(begin, end)
174 func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) {
175 for i := begin; i < end; i++ {
177 if p.priority == PiecePriorityNone {
180 p.priority = PiecePriorityNone
181 t.updatePiecePriority(i)
185 func (t *Torrent) initFiles() {
187 t.files = new([]*File)
188 for _, fi := range t.info.UpvertedFiles() {
189 *t.files = append(*t.files, &File{
191 strings.Join(append([]string{t.info.Name}, fi.Path...), "/"),
202 // Returns handles to the files in the torrent. This requires that the Info is
204 func (t *Torrent) Files() []*File {
208 func (t *Torrent) AddPeers(pp []Peer) {
215 // Marks the entire torrent for download. Requires the info first, see
216 // GotInfo. Sets piece priorities for historical reasons.
217 func (t *Torrent) DownloadAll() {
218 t.DownloadPieces(0, t.numPieces())
221 func (t *Torrent) String() string {
224 s = t.infoHash.HexString()
229 func (t *Torrent) AddTrackers(announceList [][]string) {
232 t.addTrackers(announceList)
235 func (t *Torrent) Piece(i pieceIndex) *Piece {