]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Dynamic outbound max requests
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "errors"
5         "net"
6
7         "github.com/RoaringBitmap/roaring"
8         "github.com/anacrolix/missinggo/v2"
9         "github.com/anacrolix/torrent/types"
10         "golang.org/x/time/rate"
11
12         "github.com/anacrolix/torrent/metainfo"
13         pp "github.com/anacrolix/torrent/peer_protocol"
14 )
15
16 type (
17         Request       = types.Request
18         ChunkSpec     = types.ChunkSpec
19         piecePriority = types.PiecePriority
20 )
21
22 const (
23         PiecePriorityNormal    = types.PiecePriorityNormal
24         PiecePriorityNone      = types.PiecePriorityNone
25         PiecePriorityNow       = types.PiecePriorityNow
26         PiecePriorityReadahead = types.PiecePriorityReadahead
27         PiecePriorityNext      = types.PiecePriorityNext
28         PiecePriorityHigh      = types.PiecePriorityHigh
29 )
30
31 func newRequest(index, begin, length pp.Integer) Request {
32         return Request{index, ChunkSpec{begin, length}}
33 }
34
35 func newRequestFromMessage(msg *pp.Message) Request {
36         switch msg.Type {
37         case pp.Request, pp.Cancel, pp.Reject:
38                 return newRequest(msg.Index, msg.Begin, msg.Length)
39         case pp.Piece:
40                 return newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
41         default:
42                 panic(msg.Type)
43         }
44 }
45
46 // The size in bytes of a metadata extension piece.
47 func metadataPieceSize(totalSize, piece int) int {
48         ret := totalSize - piece*(1<<14)
49         if ret > 1<<14 {
50                 ret = 1 << 14
51         }
52         return ret
53 }
54
55 // Return the request that would include the given offset into the torrent data.
56 func torrentOffsetRequest(
57         torrentLength, pieceSize, chunkSize, offset int64,
58 ) (
59         r Request, ok bool,
60 ) {
61         if offset < 0 || offset >= torrentLength {
62                 return
63         }
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 {
69                 r.Length = pieceLeft
70         }
71         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
72         if int64(r.Length) > torrentLeft {
73                 r.Length = pp.Integer(torrentLeft)
74         }
75         ok = true
76         return
77 }
78
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")
83         }
84         return
85 }
86
87 func validateInfo(info *metainfo.Info) error {
88         if len(info.Pieces)%20 != 0 {
89                 return errors.New("pieces has invalid length")
90         }
91         if info.PieceLength == 0 {
92                 if info.TotalLength() != 0 {
93                         return errors.New("zero piece length")
94                 }
95         } else {
96                 if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
97                         return errors.New("piece count and file lengths are at odds")
98                 }
99         }
100         return nil
101 }
102
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
107         }
108         return ret
109 }
110
111 func connLessTrusted(l, r *Peer) bool {
112         return l.trust().Less(r.trust())
113 }
114
115 func connIsIpv6(nc interface {
116         LocalAddr() net.Addr
117 }) bool {
118         ra := nc.LocalAddr()
119         rip := addrIpOrNil(ra)
120         return rip.To4() == nil && rip.To16() != nil
121 }
122
123 func clamp(min, value, max int64) int64 {
124         if min > max {
125                 panic("harumph")
126         }
127         if value < min {
128                 value = min
129         }
130         if value > max {
131                 value = max
132         }
133         return value
134 }
135
136 func max(as ...int64) int64 {
137         ret := as[0]
138         for _, a := range as[1:] {
139                 if a > ret {
140                         ret = a
141                 }
142         }
143         return ret
144 }
145
146 func maxInt(as ...int) int {
147         ret := as[0]
148         for _, a := range as[1:] {
149                 if a > ret {
150                         ret = a
151                 }
152         }
153         return ret
154 }
155
156 func min(as ...int64) int64 {
157         ret := as[0]
158         for _, a := range as[1:] {
159                 if a < ret {
160                         ret = a
161                 }
162         }
163         return ret
164 }
165
166 func minInt(as ...int) int {
167         ret := as[0]
168         for _, a := range as[1:] {
169                 if a < ret {
170                         ret = a
171                 }
172         }
173         return ret
174 }
175
176 var unlimited = rate.NewLimiter(rate.Inf, 0)
177
178 type (
179         pieceIndex = int
180         InfoHash   = metainfo.Hash
181         IpPort     = missinggo.IpPort
182 )
183
184 func boolSliceToBitmap(slice []bool) (rb roaring.Bitmap) {
185         for i, b := range slice {
186                 if b {
187                         rb.AddInt(i)
188                 }
189         }
190         return
191 }