]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Merge remote-tracking branch 'libtorgo/master' into HEAD
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "crypto"
5         "errors"
6         "fmt"
7         "time"
8
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         chunkSize          = 0x4000     // 16KiB
16         bep20              = "-GT0000-" // Peer ID client identifier prefix
17         nominalDialTimeout = time.Second * 30
18         minDialTimeout     = 5 * time.Second
19 )
20
21 type (
22         InfoHash [20]byte
23         pieceSum [20]byte
24 )
25
26 func (ih *InfoHash) AsString() string {
27         return string(ih[:])
28 }
29
30 func (ih *InfoHash) HexString() string {
31         return fmt.Sprintf("%x", ih[:])
32 }
33
34 func lastChunkSpec(pieceLength pp.Integer) (cs chunkSpec) {
35         cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
36         cs.Length = pieceLength - cs.Begin
37         return
38 }
39
40 type chunkSpec struct {
41         Begin, Length pp.Integer
42 }
43
44 type request struct {
45         Index pp.Integer
46         chunkSpec
47 }
48
49 func newRequest(index, begin, length pp.Integer) request {
50         return request{index, chunkSpec{begin, length}}
51 }
52
53 var (
54         // Requested data not yet available.
55         errDataNotReady = errors.New("data not ready")
56 )
57
58 // The size in bytes of a metadata extension piece.
59 func metadataPieceSize(totalSize int, piece int) int {
60         ret := totalSize - piece*(1<<14)
61         if ret > 1<<14 {
62                 ret = 1 << 14
63         }
64         return ret
65 }
66
67 type superer interface {
68         Super() interface{}
69 }
70
71 // Returns ok if there's a parent, and it's not nil.
72 func super(child interface{}) (parent interface{}, ok bool) {
73         s, ok := child.(superer)
74         if !ok {
75                 return
76         }
77         parent = s.Super()
78         ok = parent != nil
79         return
80 }
81
82 // Return the request that would include the given offset into the torrent data.
83 func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
84         r request, ok bool) {
85         if offset < 0 || offset >= torrentLength {
86                 return
87         }
88         r.Index = pp.Integer(offset / pieceSize)
89         r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
90         r.Length = pp.Integer(chunkSize)
91         pieceLeft := pp.Integer(pieceSize - int64(r.Begin))
92         if r.Length > pieceLeft {
93                 r.Length = pieceLeft
94         }
95         torrentLeft := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
96         if int64(r.Length) > torrentLeft {
97                 r.Length = pp.Integer(torrentLeft)
98         }
99         ok = true
100         return
101 }
102
103 func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
104         off = int64(r.Index)*pieceSize + int64(r.Begin)
105         if off < 0 || off >= torrentLength {
106                 panic("invalid request")
107         }
108         return
109 }