misc.go | 40 ---------------------------------------- piece.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ diff --git a/misc.go b/misc.go index 873d73adfc0a6f580bf5b7648364113ff31f2e3c..4fbd1ff76825cd6d60ff0ad690eda76cb58fe7e7 100644 --- a/misc.go +++ b/misc.go @@ -4,8 +4,6 @@ import ( "crypto" "errors" "fmt" - "math/rand" - "sync" "time" "github.com/anacrolix/torrent/peer_protocol" @@ -31,44 +29,6 @@ } func (ih *InfoHash) HexString() string { return fmt.Sprintf("%x", ih[:]) -} - -type piecePriority byte - -const ( - piecePriorityNone piecePriority = iota - piecePriorityNormal - piecePriorityReadahead - piecePriorityNext - piecePriorityNow -) - -type piece struct { - Hash pieceSum - PendingChunkSpecs map[chunkSpec]struct{} - Hashing bool - QueuedForHash bool - EverHashed bool - Event sync.Cond - Priority piecePriority -} - -func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) { - if len(p.PendingChunkSpecs) == 0 { - return - } - css = make([]chunkSpec, 0, len(p.PendingChunkSpecs)) - for cs := range p.PendingChunkSpecs { - css = append(css, cs) - } - if len(css) <= 1 { - return - } - for i := range css { - j := rand.Intn(i + 1) - css[i], css[j] = css[j], css[i] - } - return } func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) { diff --git a/piece.go b/piece.go new file mode 100644 index 0000000000000000000000000000000000000000..37b894b91dc11a00ae47fc7c2d54786a31085b43 --- /dev/null +++ b/piece.go @@ -0,0 +1,44 @@ +package torrent + +import ( + "math/rand" + "sync" +) + +type piecePriority byte + +const ( + piecePriorityNone piecePriority = iota + piecePriorityNormal + piecePriorityReadahead + piecePriorityNext + piecePriorityNow +) + +type piece struct { + Hash pieceSum + PendingChunkSpecs map[chunkSpec]struct{} + Hashing bool + QueuedForHash bool + EverHashed bool + Event sync.Cond + Priority piecePriority +} + +func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) { + if len(p.PendingChunkSpecs) == 0 { + return + } + css = make([]chunkSpec, 0, len(p.PendingChunkSpecs)) + for cs := range p.PendingChunkSpecs { + css = append(css, cs) + } + if len(css) <= 1 { + return + } + for i := range css { + j := rand.Intn(i + 1) + css[i], css[j] = css[j], css[i] + } + return +}