7 pp "github.com/anacrolix/torrent/peer_protocol"
10 // Piece priority describes the importance of obtaining a particular piece.
12 type piecePriority byte
15 PiecePriorityNone piecePriority = iota // Not wanted.
16 PiecePriorityNormal // Wanted.
17 PiecePriorityReadahead // May be required soon.
18 PiecePriorityNext // Succeeds a piece where a read occurred.
19 PiecePriorityNow // A read occurred in this piece.
23 // The completed piece SHA1 hash, from the metainfo "pieces" field.
25 // Chunks we don't have. The offset and length can be determined by the
26 // request chunkSize in use.
27 PendingChunkSpecs []bool
32 Priority piecePriority
33 pendingWrites sync.WaitGroup
36 func (p *piece) pendingChunk(cs chunkSpec, chunkSize pp.Integer) bool {
37 if p.PendingChunkSpecs == nil {
40 return p.PendingChunkSpecs[chunkIndex(cs, chunkSize)]
43 func (p *piece) numPendingChunks() (ret int) {
44 for _, pending := range p.PendingChunkSpecs {
52 func (p *piece) unpendChunkIndex(i int) {
53 if p.PendingChunkSpecs == nil {
56 p.PendingChunkSpecs[i] = false
59 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
60 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
61 if ret.Begin+ret.Length > pieceLength {
62 ret.Length = pieceLength - ret.Begin
67 func (p *piece) shuffledPendingChunkSpecs(pieceLength, chunkSize pp.Integer) (css []chunkSpec) {
68 if p.numPendingChunks() == 0 {
71 css = make([]chunkSpec, 0, p.numPendingChunks())
72 for i, pending := range p.PendingChunkSpecs {
74 css = append(css, chunkIndexSpec(i, pieceLength, chunkSize))
82 css[i], css[j] = css[j], css[i]