]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Move piece into its own file
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "crypto"
5         "errors"
6         "fmt"
7         "time"
8
9         "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 peer_protocol.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 peer_protocol.Integer
42 }
43
44 type request struct {
45         Index peer_protocol.Integer
46         chunkSpec
47 }
48
49 func newRequest(index, begin, length peer_protocol.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 }