11 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
15 pieceHash = crypto.SHA1
16 maxRequests = 250 // Maximum pending requests we allow peers to send us.
17 chunkSize = 0x4000 // 16KiB
18 BEP20 = "-GT0000-" // Peer ID client identifier prefix
19 nominalDialTimeout = time.Second * 30
20 minDialTimeout = 5 * time.Second
28 func (ih *InfoHash) AsString() string {
32 func (ih *InfoHash) HexString() string {
33 return fmt.Sprintf("%x", ih[:])
36 type piecePriority byte
39 piecePriorityNone piecePriority = iota
41 piecePriorityReadahead
49 PendingChunkSpecs map[chunkSpec]struct{}
54 Priority piecePriority
57 func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) {
58 if len(p.PendingChunkSpecs) == 0 {
61 css = make([]chunkSpec, 0, len(p.PendingChunkSpecs))
62 for cs := range p.PendingChunkSpecs {
70 css[i], css[j] = css[j], css[i]
75 func (p *piece) Complete() bool {
79 func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
80 cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
81 cs.Length = pieceLength - cs.Begin
85 type chunkSpec struct {
86 Begin, Length peer_protocol.Integer
90 Index peer_protocol.Integer
94 func newRequest(index, begin, length peer_protocol.Integer) request {
95 return request{index, chunkSpec{begin, length}}
99 // Requested data not yet available.
100 ErrDataNotReady = errors.New("data not ready")
103 // The size in bytes of a metadata extension piece.
104 func metadataPieceSize(totalSize int, piece int) int {
105 ret := totalSize - piece*(1<<14)