7 "github.com/anacrolix/missinggo/v2/bitmap"
9 "github.com/anacrolix/torrent/metainfo"
10 pp "github.com/anacrolix/torrent/peer_protocol"
11 "github.com/anacrolix/torrent/storage"
14 // Describes the importance of obtaining a particular piece.
15 type piecePriority byte
17 func (pp *piecePriority) Raise(maybe piecePriority) bool {
25 // Priority for use in PriorityBitmap
26 func (me piecePriority) BitmapPriority() int {
31 PiecePriorityNone piecePriority = iota // Not wanted. Must be the zero value.
32 PiecePriorityNormal // Wanted.
33 PiecePriorityHigh // Wanted a lot.
34 PiecePriorityReadahead // May be required soon.
35 // Succeeds a piece where a read occurred. Currently the same as Now,
36 // apparently due to issues with caching.
38 PiecePriorityNow // A Reader is reading in this piece. Highest urgency.
42 // The completed piece SHA1 hash, from the metainfo "pieces" field.
47 // Chunks we've written to since the last check. The chunk offset and
48 // length can be determined by the request chunkSize in use.
49 _dirtyChunks bitmap.Bitmap
53 storageCompletionOk bool
55 publicPieceState PieceState
56 priority piecePriority
58 pendingWritesMutex sync.Mutex
60 noPendingWrites sync.Cond
62 // Connections that have written data to this piece since its last check.
63 // This can include connections that have closed.
64 dirtiers map[*connection]struct{}
67 func (p *Piece) String() string {
68 return fmt.Sprintf("%s/%d", p.t.infoHash.HexString(), p.index)
71 func (p *Piece) Info() metainfo.Piece {
72 return p.t.info.Piece(int(p.index))
75 func (p *Piece) Storage() storage.Piece {
76 return p.t.storage.Piece(p.Info())
79 func (p *Piece) pendingChunkIndex(chunkIndex int) bool {
80 return !p._dirtyChunks.Contains(chunkIndex)
83 func (p *Piece) pendingChunk(cs chunkSpec, chunkSize pp.Integer) bool {
84 return p.pendingChunkIndex(chunkIndex(cs, chunkSize))
87 func (p *Piece) hasDirtyChunks() bool {
88 return p._dirtyChunks.Len() != 0
91 func (p *Piece) numDirtyChunks() pp.Integer {
92 return pp.Integer(p._dirtyChunks.Len())
95 func (p *Piece) unpendChunkIndex(i int) {
100 func (p *Piece) pendChunkIndex(i int) {
101 p._dirtyChunks.Remove(i)
104 func (p *Piece) numChunks() pp.Integer {
105 return p.t.pieceNumChunks(p.index)
108 func (p *Piece) undirtiedChunkIndices() (ret bitmap.Bitmap) {
109 ret = p._dirtyChunks.Copy()
110 ret.FlipRange(0, bitmap.BitIndex(p.numChunks()))
114 func (p *Piece) incrementPendingWrites() {
115 p.pendingWritesMutex.Lock()
117 p.pendingWritesMutex.Unlock()
120 func (p *Piece) decrementPendingWrites() {
121 p.pendingWritesMutex.Lock()
122 if p.pendingWrites == 0 {
126 if p.pendingWrites == 0 {
127 p.noPendingWrites.Broadcast()
129 p.pendingWritesMutex.Unlock()
132 func (p *Piece) waitNoPendingWrites() {
133 p.pendingWritesMutex.Lock()
134 for p.pendingWrites != 0 {
135 p.noPendingWrites.Wait()
137 p.pendingWritesMutex.Unlock()
140 func (p *Piece) chunkIndexDirty(chunk pp.Integer) bool {
141 return p._dirtyChunks.Contains(bitmap.BitIndex(chunk))
144 func (p *Piece) chunkIndexSpec(chunk pp.Integer) chunkSpec {
145 return chunkIndexSpec(chunk, p.length(), p.chunkSize())
148 func (p *Piece) chunkIndexRequest(chunkIndex pp.Integer) request {
151 chunkIndexSpec(chunkIndex, p.length(), p.chunkSize()),
155 func (p *Piece) numDirtyBytes() (ret pp.Integer) {
157 // if ret > p.length() {
158 // panic("too many dirty bytes")
161 numRegularDirtyChunks := p.numDirtyChunks()
162 if p.chunkIndexDirty(p.numChunks() - 1) {
163 numRegularDirtyChunks--
164 ret += p.chunkIndexSpec(p.lastChunkIndex()).Length
166 ret += pp.Integer(numRegularDirtyChunks) * p.chunkSize()
170 func (p *Piece) length() pp.Integer {
171 return p.t.pieceLength(p.index)
174 func (p *Piece) chunkSize() pp.Integer {
178 func (p *Piece) lastChunkIndex() pp.Integer {
179 return p.numChunks() - 1
182 func (p *Piece) bytesLeft() (ret pp.Integer) {
183 if p.t.pieceComplete(p.index) {
186 return p.length() - p.numDirtyBytes()
189 // Forces the piece data to be rehashed.
190 func (p *Piece) VerifyData() {
192 defer p.t.cl.unlock()
193 target := p.numVerifies + 1
197 //log.Printf("target: %d", target)
198 p.t.queuePieceCheck(p.index)
200 //log.Printf("got %d verifies", p.numVerifies)
201 if p.numVerifies >= target {
209 func (p *Piece) queuedForHash() bool {
210 return p.t.piecesQueuedForHash.Get(bitmap.BitIndex(p.index))
213 func (p *Piece) torrentBeginOffset() int64 {
214 return int64(p.index) * p.t.info.PieceLength
217 func (p *Piece) torrentEndOffset() int64 {
218 return p.torrentBeginOffset() + int64(p.length())
221 func (p *Piece) SetPriority(prio piecePriority) {
223 defer p.t.cl.unlock()
225 p.t.updatePiecePriority(p.index)
228 func (p *Piece) uncachedPriority() (ret piecePriority) {
229 if p.t.pieceComplete(p.index) || p.t.pieceQueuedForHash(p.index) || p.t.hashingPiece(p.index) {
230 return PiecePriorityNone
232 for _, f := range p.files {
235 if p.t.readerNowPieces().Contains(int(p.index)) {
236 ret.Raise(PiecePriorityNow)
238 // if t._readerNowPieces.Contains(piece - 1) {
239 // return PiecePriorityNext
241 if p.t.readerReadaheadPieces().Contains(bitmap.BitIndex(p.index)) {
242 ret.Raise(PiecePriorityReadahead)
244 ret.Raise(p.priority)
248 func (p *Piece) completion() (ret storage.Completion) {
249 ret.Complete = p.t.pieceComplete(p.index)
250 ret.Ok = p.storageCompletionOk
254 func (p *Piece) allChunksDirty() bool {
255 return p._dirtyChunks.Len() == int(p.numChunks())
258 func (p *Piece) requestStrategyPiece() requestStrategyPiece {
262 func (p *Piece) dirtyChunks() bitmap.Bitmap {
263 return p._dirtyChunks
266 func (rs requestStrategyDuplicateRequestTimeout) wouldDuplicateRecent(r request) bool {
267 // This piece has been requested on another connection, and the duplicate request timer is still
269 _, ok := rs.lastRequested[r]