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 PublicPieceState PieceState
35 pendingWritesMutex sync.Mutex
37 noPendingWrites sync.Cond
40 func (p *piece) pendingChunk(cs chunkSpec, chunkSize pp.Integer) bool {
41 if p.PendingChunkSpecs == nil {
44 return p.PendingChunkSpecs[chunkIndex(cs, chunkSize)]
47 func (p *piece) numPendingChunks() (ret int) {
48 for _, pending := range p.PendingChunkSpecs {
56 func (p *piece) unpendChunkIndex(i int) {
57 if p.PendingChunkSpecs == nil {
60 p.PendingChunkSpecs[i] = false
63 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
64 ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
65 if ret.Begin+ret.Length > pieceLength {
66 ret.Length = pieceLength - ret.Begin
71 func (p *piece) shuffledPendingChunkSpecs(pieceLength, chunkSize pp.Integer) (css []chunkSpec) {
72 if p.numPendingChunks() == 0 {
75 css = make([]chunkSpec, 0, p.numPendingChunks())
76 for i, pending := range p.PendingChunkSpecs {
78 css = append(css, chunkIndexSpec(i, pieceLength, chunkSize))
86 css[i], css[j] = css[j], css[i]