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