]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Remove deadcode
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "crypto"
5         "errors"
6         "time"
7
8         "github.com/anacrolix/torrent/metainfo"
9         pp "github.com/anacrolix/torrent/peer_protocol"
10 )
11
12 const (
13         pieceHash        = crypto.SHA1
14         maxRequests      = 250    // Maximum pending requests we allow peers to send us.
15         defaultChunkSize = 0x4000 // 16KiB
16         // Peer ID client identifier prefix. We'll update this occasionally to
17         // reflect changes to client behaviour that other clients may depend on.
18         // Also see `extendedHandshakeClientVersion`.
19         bep20              = "-GT0001-"
20         nominalDialTimeout = time.Second * 30
21         minDialTimeout     = 5 * time.Second
22 )
23
24 type chunkSpec struct {
25         Begin, Length pp.Integer
26 }
27
28 type request struct {
29         Index pp.Integer
30         chunkSpec
31 }
32
33 func newRequest(index, begin, length pp.Integer) request {
34         return request{index, chunkSpec{begin, length}}
35 }
36
37 // The size in bytes of a metadata extension piece.
38 func metadataPieceSize(totalSize int, piece int) int {
39         ret := totalSize - piece*(1<<14)
40         if ret > 1<<14 {
41                 ret = 1 << 14
42         }
43         return ret
44 }
45
46 type superer interface {
47         Super() interface{}
48 }
49
50 // Return the request that would include the given offset into the torrent data.
51 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
52         r request, ok bool) {
53         if offset < 0 || offset >= torrentLength {
54                 return
55         }
56         r.Index = pp.Integer(offset / pieceSize)
57         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
58         r.Length = pp.Integer(chunkSize)
59         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
60         if r.Length > pieceLeft {
61                 r.Length = pieceLeft
62         }
63         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
64         if int64(r.Length) > torrentLeft {
65                 r.Length = pp.Integer(torrentLeft)
66         }
67         ok = true
68         return
69 }
70
71 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
72         off = int64(r.Index)*pieceSize + int64(r.Begin)
73         if off < 0 || off >= torrentLength {
74                 panic("invalid request")
75         }
76         return
77 }
78
79 func validateInfo(info *metainfo.Info) error {
80         if len(info.Pieces)%20 != 0 {
81                 return errors.New("pieces has invalid length")
82         }
83         if int((info.TotalLength()+info.PieceLength-1)/info.PieceLength) != info.NumPieces() {
84                 return errors.New("piece count and file lengths are at odds")
85         }
86         return nil
87 }
88
89 func chunkIndexSpec(index int, pieceLength, chunkSize pp.Integer) chunkSpec {
90         ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
91         if ret.Begin+ret.Length > pieceLength {
92                 ret.Length = pieceLength - ret.Begin
93         }
94         return ret
95 }