9 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
10 "bitbucket.org/anacrolix/go.torrent/tracker"
11 metainfo "github.com/nsf/libtorgo/torrent"
14 func (t *Torrent) PieceNumPendingBytes(index peer_protocol.Integer) (count peer_protocol.Integer) {
15 pendingChunks := t.Pieces[index].PendingChunkSpecs
16 count = peer_protocol.Integer(len(pendingChunks)) * chunkSize
17 _lastChunkSpec := lastChunkSpec(t.PieceLength(index))
18 if _lastChunkSpec.Length != chunkSize {
19 if _, ok := pendingChunks[_lastChunkSpec]; ok {
20 count += _lastChunkSpec.Length - chunkSize
30 MetaInfo *metainfo.MetaInfo
34 // BEP 12 Multitracker Metadata Extension. The tracker.Client instances
35 // mirror their respective URLs from the announce-list key.
36 Trackers [][]tracker.Client
39 func (t *Torrent) NumPieces() int {
40 return len(t.MetaInfo.Pieces) / PieceHash.Size()
43 func (t *Torrent) NumPiecesCompleted() (num int) {
44 for _, p := range t.Pieces {
52 func (t *Torrent) Length() int64 {
53 return int64(t.PieceLength(peer_protocol.Integer(len(t.Pieces)-1))) + int64(len(t.Pieces)-1)*int64(t.PieceLength(0))
56 func (t *Torrent) Close() (err error) {
58 for _, conn := range t.Conns {
64 func (t *Torrent) piecesByPendingBytesDesc() (indices []peer_protocol.Integer) {
65 slice := pieceByBytesPendingSlice{
66 Pending: make([]peer_protocol.Integer, 0, len(t.Pieces)),
67 Indices: make([]peer_protocol.Integer, 0, len(t.Pieces)),
69 for i := range t.Pieces {
70 slice.Pending = append(slice.Pending, t.PieceNumPendingBytes(peer_protocol.Integer(i)))
71 slice.Indices = append(slice.Indices, peer_protocol.Integer(i))
73 sort.Sort(sort.Reverse(slice))
77 // Return the request that would include the given offset into the torrent data.
78 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
80 if offset < 0 || offset >= torrentLength {
83 r.Index = peer_protocol.Integer(offset / pieceSize)
84 r.Begin = peer_protocol.Integer(offset % pieceSize / chunkSize * chunkSize)
85 left := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
87 r.Length = peer_protocol.Integer(chunkSize)
89 r.Length = peer_protocol.Integer(left)
95 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
96 off = int64(r.Index)*pieceSize + int64(r.Begin)
97 if off < 0 || off >= torrentLength {
98 panic("invalid request")
103 func (t *Torrent) requestOffset(r Request) int64 {
104 return torrentRequestOffset(t.Length(), t.MetaInfo.PieceLength, r)
107 // Return the request that would include the given offset into the torrent data.
108 func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) {
109 return torrentOffsetRequest(t.Length(), t.MetaInfo.PieceLength, chunkSize, off)
112 func (t *Torrent) WriteChunk(piece int, begin int64, data []byte) (err error) {
113 _, err = t.Data.WriteAt(data, int64(piece)*t.MetaInfo.PieceLength+begin)
117 func (t *Torrent) bitfield() (bf []bool) {
118 for _, p := range t.Pieces {
119 bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
124 func (t *Torrent) pendAllChunkSpecs(index peer_protocol.Integer) {
125 piece := t.Pieces[index]
126 if piece.PendingChunkSpecs == nil {
127 piece.PendingChunkSpecs = make(
128 map[ChunkSpec]struct{},
129 (t.MetaInfo.PieceLength+chunkSize-1)/chunkSize)
134 cs := piece.PendingChunkSpecs
135 for left := peer_protocol.Integer(t.PieceLength(index)); left != 0; left -= c.Length {
137 if c.Length > chunkSize {
146 func (t *Torrent) requestHeat() (ret map[Request]int) {
147 ret = make(map[Request]int)
148 for _, conn := range t.Conns {
149 for req, _ := range conn.Requests {
162 func (t *Torrent) PieceLength(piece peer_protocol.Integer) (len_ peer_protocol.Integer) {
163 if int(piece) == t.NumPieces()-1 {
164 len_ = peer_protocol.Integer(t.Data.Size() % t.MetaInfo.PieceLength)
167 len_ = peer_protocol.Integer(t.MetaInfo.PieceLength)
172 func (t *Torrent) HashPiece(piece peer_protocol.Integer) (ps pieceSum) {
173 hash := PieceHash.New()
174 n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength)
178 if peer_protocol.Integer(n) != t.PieceLength(piece) {
179 panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceLength(piece), n, piece))
181 copyHashSum(ps[:], hash.Sum(nil))
184 func (t *Torrent) haveAllPieces() bool {
185 for _, piece := range t.Pieces {
186 if !piece.Complete() {
193 func (me *Torrent) haveAnyPieces() bool {
194 for _, piece := range me.Pieces {
195 if piece.Complete() {
202 func (t *Torrent) wantPiece(index int) bool {
204 return p.EverHashed && len(p.PendingChunkSpecs) != 0