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 Hash pieceSum // The completed piece SHA1 hash, from the metainfo "pieces" field.
24 // Chunks we don't have. The offset and length can be determined by the
25 // request chunkSize in use.
26 PendingChunkSpecs []bool
31 Priority piecePriority
34 func (p *piece) pendingChunk(cs chunkSpec) bool {
35 if p.PendingChunkSpecs == nil {
38 return p.PendingChunkSpecs[chunkIndex(cs)]
41 func (p *piece) numPendingChunks() (ret int) {
42 for _, pending := range p.PendingChunkSpecs {
50 func (p *piece) unpendChunkIndex(i int) {
51 if p.PendingChunkSpecs == nil {
54 p.PendingChunkSpecs[i] = false
57 func chunkIndexSpec(index int, pieceLength pp.Integer) chunkSpec {
58 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
59 if ret.Begin+ret.Length > pieceLength {
60 ret.Length = pieceLength - ret.Begin
65 func (p *piece) shuffledPendingChunkSpecs(pieceLength pp.Integer) (css []chunkSpec) {
66 if p.numPendingChunks() == 0 {
69 css = make([]chunkSpec, 0, p.numPendingChunks())
70 for i, pending := range p.PendingChunkSpecs {
72 css = append(css, chunkIndexSpec(i, pieceLength))
80 css[i], css[j] = css[j], css[i]