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 func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) {
78 req.Index = peer_protocol.Integer(off / t.MetaInfo.PieceLength)
79 if req.Index < 0 || int(req.Index) >= len(t.Pieces) {
82 off %= t.MetaInfo.PieceLength
83 pieceLeft := t.PieceLength(req.Index) - peer_protocol.Integer(off)
87 req.Begin = chunkSize * (peer_protocol.Integer(off) / chunkSize)
88 req.Length = chunkSize
89 if req.Length > pieceLeft {
90 req.Length = pieceLeft
96 func (t *Torrent) WriteChunk(piece int, begin int64, data []byte) (err error) {
97 _, err = t.Data.WriteAt(data, int64(piece)*t.MetaInfo.PieceLength+begin)
101 func (t *Torrent) bitfield() (bf []bool) {
102 for _, p := range t.Pieces {
103 bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
108 func (t *Torrent) pendAllChunkSpecs(index peer_protocol.Integer) {
109 piece := t.Pieces[index]
110 if piece.PendingChunkSpecs == nil {
111 piece.PendingChunkSpecs = make(
112 map[ChunkSpec]struct{},
113 (t.MetaInfo.PieceLength+chunkSize-1)/chunkSize)
118 cs := piece.PendingChunkSpecs
119 for left := peer_protocol.Integer(t.PieceLength(index)); left != 0; left -= c.Length {
121 if c.Length > chunkSize {
130 func (t *Torrent) requestHeat() (ret map[Request]int) {
131 ret = make(map[Request]int)
132 for _, conn := range t.Conns {
133 for req, _ := range conn.Requests {
146 func (t *Torrent) PieceLength(piece peer_protocol.Integer) (len_ peer_protocol.Integer) {
147 if int(piece) == t.NumPieces()-1 {
148 len_ = peer_protocol.Integer(t.Data.Size() % t.MetaInfo.PieceLength)
151 len_ = peer_protocol.Integer(t.MetaInfo.PieceLength)
156 func (t *Torrent) HashPiece(piece peer_protocol.Integer) (ps pieceSum) {
157 hash := PieceHash.New()
158 n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength)
162 if peer_protocol.Integer(n) != t.PieceLength(piece) {
163 panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceLength(piece), n, piece))
165 copyHashSum(ps[:], hash.Sum(nil))
168 func (t *Torrent) haveAllPieces() bool {
169 for _, piece := range t.Pieces {
170 if !piece.Complete() {
177 func (me *Torrent) haveAnyPieces() bool {
178 for _, piece := range me.Pieces {
179 if piece.Complete() {
186 func (t *Torrent) wantPiece(index int) bool {
188 return p.EverHashed && len(p.PendingChunkSpecs) != 0