From: Matt Joiner Date: Tue, 7 Apr 2015 16:14:35 +0000 (+1000) Subject: Move piece into its own file X-Git-Tag: v1.0.0~1208 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=6c48d59adb7b32511311acb3ad5f45948e215f49;p=btrtrc.git Move piece into its own file --- diff --git a/misc.go b/misc.go index 873d73ad..4fbd1ff7 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" @@ -33,44 +31,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) { cs.Begin = (pieceLength - 1) / chunkSize * chunkSize cs.Length = pieceLength - cs.Begin diff --git a/piece.go b/piece.go new file mode 100644 index 00000000..37b894b9 --- /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 +}