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
35 func (p *piece) pendingChunk(cs chunkSpec) bool {
36 if p.PendingChunkSpecs == nil {
39 return p.PendingChunkSpecs[chunkIndex(cs)]
42 func (p *piece) numPendingChunks() (ret int) {
43 for _, pending := range p.PendingChunkSpecs {
51 func (p *piece) unpendChunkIndex(i int) {
52 if p.PendingChunkSpecs == nil {
55 p.PendingChunkSpecs[i] = false
58 func chunkIndexSpec(index int, pieceLength pp.Integer) chunkSpec {
59 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
60 if ret.Begin+ret.Length > pieceLength {
61 ret.Length = pieceLength - ret.Begin
66 func (p *piece) shuffledPendingChunkSpecs(pieceLength pp.Integer) (css []chunkSpec) {
67 if p.numPendingChunks() == 0 {
70 css = make([]chunkSpec, 0, p.numPendingChunks())
71 for i, pending := range p.PendingChunkSpecs {
73 css = append(css, chunkIndexSpec(i, pieceLength))
81 css[i], css[j] = css[j], css[i]