7 "github.com/RoaringBitmap/roaring"
8 "github.com/anacrolix/missinggo/v2"
9 "golang.org/x/time/rate"
11 "github.com/anacrolix/torrent/metainfo"
12 pp "github.com/anacrolix/torrent/peer_protocol"
13 "github.com/anacrolix/torrent/types"
14 "github.com/anacrolix/torrent/types/infohash"
18 Request = types.Request
19 ChunkSpec = types.ChunkSpec
20 piecePriority = types.PiecePriority
24 PiecePriorityNormal = types.PiecePriorityNormal
25 PiecePriorityNone = types.PiecePriorityNone
26 PiecePriorityNow = types.PiecePriorityNow
27 PiecePriorityReadahead = types.PiecePriorityReadahead
28 PiecePriorityNext = types.PiecePriorityNext
29 PiecePriorityHigh = types.PiecePriorityHigh
32 func newRequest(index, begin, length pp.Integer) Request {
33 return Request{index, ChunkSpec{begin, length}}
36 func newRequestFromMessage(msg *pp.Message) Request {
38 case pp.Request, pp.Cancel, pp.Reject:
39 return newRequest(msg.Index, msg.Begin, msg.Length)
41 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
47 // The size in bytes of a metadata extension piece.
48 func metadataPieceSize(totalSize, piece int) int {
49 ret := totalSize - piece*(1<<14)
56 // Return the request that would include the given offset into the torrent data.
57 func torrentOffsetRequest(
58 torrentLength, pieceSize, chunkSize, offset int64,
62 if offset < 0 || offset >= torrentLength {
65 r.Index = pp.Integer(offset / pieceSize)
66 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
67 r.Length = pp.Integer(chunkSize)
68 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
69 if r.Length > pieceLeft {
72 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
73 if int64(r.Length) > torrentLeft {
74 r.Length = pp.Integer(torrentLeft)
80 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
81 off = int64(r.Index)*pieceSize + int64(r.Begin)
82 if off < 0 || off >= torrentLength {
83 panic("invalid Request")
88 func validateInfo(info *metainfo.Info) error {
89 if len(info.Pieces)%20 != 0 {
90 return errors.New("pieces has invalid length")
92 if info.PieceLength == 0 {
93 if info.TotalLength() != 0 {
94 return errors.New("zero piece length")
97 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
98 return errors.New("piece count and file lengths are at odds")
104 func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
105 ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
106 if ret.Begin+ret.Length > pieceLength {
107 ret.Length = pieceLength - ret.Begin
112 func connLessTrusted(l, r *Peer) bool {
113 return l.trust().Less(r.trust())
116 func connIsIpv6(nc interface {
121 rip := addrIpOrNil(ra)
122 return rip.To4() == nil && rip.To16() != nil
125 func clamp(min, value, max int64) int64 {
138 func max(as ...int64) int64 {
140 for _, a := range as[1:] {
148 func maxInt(as ...int) int {
150 for _, a := range as[1:] {
158 func min(as ...int64) int64 {
160 for _, a := range as[1:] {
168 func minInt(as ...int) int {
170 for _, a := range as[1:] {
178 var unlimited = rate.NewLimiter(rate.Inf, 0)
182 // Deprecated: Use infohash.T directly to avoid unnecessary imports.
183 InfoHash = infohash.T
184 IpPort = missinggo.IpPort
187 func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
188 for i, b := range slice {