7 "github.com/RoaringBitmap/roaring"
8 "github.com/anacrolix/missinggo/v2"
9 "github.com/anacrolix/torrent/types"
10 "golang.org/x/time/rate"
12 "github.com/anacrolix/torrent/metainfo"
13 pp "github.com/anacrolix/torrent/peer_protocol"
17 Request = types.Request
18 ChunkSpec = types.ChunkSpec
19 piecePriority = types.PiecePriority
23 PiecePriorityNormal = types.PiecePriorityNormal
24 PiecePriorityNone = types.PiecePriorityNone
25 PiecePriorityNow = types.PiecePriorityNow
26 PiecePriorityReadahead = types.PiecePriorityReadahead
27 PiecePriorityNext = types.PiecePriorityNext
28 PiecePriorityHigh = types.PiecePriorityHigh
31 func newRequest(index, begin, length pp.Integer) Request {
32 return Request{index, ChunkSpec{begin, length}}
35 func newRequestFromMessage(msg *pp.Message) Request {
37 case pp.Request, pp.Cancel, pp.Reject:
38 return newRequest(msg.Index, msg.Begin, msg.Length)
40 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
46 // The size in bytes of a metadata extension piece.
47 func metadataPieceSize(totalSize, piece int) int {
48 ret := totalSize - piece*(1<<14)
55 // Return the request that would include the given offset into the torrent data.
56 func torrentOffsetRequest(
57 torrentLength, pieceSize, chunkSize, offset int64,
61 if offset < 0 || offset >= torrentLength {
64 r.Index = pp.Integer(offset / pieceSize)
65 r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
66 r.Length = pp.Integer(chunkSize)
67 pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
68 if r.Length > pieceLeft {
71 torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
72 if int64(r.Length) > torrentLeft {
73 r.Length = pp.Integer(torrentLeft)
79 func torrentRequestOffset(torrentLength, pieceSize int64, r Request) (off int64) {
80 off = int64(r.Index)*pieceSize + int64(r.Begin)
81 if off < 0 || off >= torrentLength {
82 panic("invalid Request")
87 func validateInfo(info *metainfo.Info) error {
88 if len(info.Pieces)%20 != 0 {
89 return errors.New("pieces has invalid length")
91 if info.PieceLength == 0 {
92 if info.TotalLength() != 0 {
93 return errors.New("zero piece length")
96 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
97 return errors.New("piece count and file lengths are at odds")
103 func chunkIndexSpec(index, pieceLength, chunkSize pp.Integer) ChunkSpec {
104 ret := ChunkSpec{pp.Integer(index) * chunkSize, chunkSize}
105 if ret.Begin+ret.Length > pieceLength {
106 ret.Length = pieceLength - ret.Begin
111 func connLessTrusted(l, r *Peer) bool {
112 return l.trust().Less(r.trust())
115 func connIsIpv6(nc interface {
119 rip := addrIpOrNil(ra)
120 return rip.To4() == nil && rip.To16() != nil
123 func clamp(min, value, max int64) int64 {
136 func max(as ...int64) int64 {
138 for _, a := range as[1:] {
146 func min(as ...int64) int64 {
148 for _, a := range as[1:] {
156 func minInt(as ...int) int {
158 for _, a := range as[1:] {
166 var unlimited = rate.NewLimiter(rate.Inf, 0)
170 InfoHash = metainfo.Hash
171 IpPort = missinggo.IpPort
174 func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
175 for i, b := range slice {