]> Sergey Matveev's repositories - btrtrc.git/blob - misc.go
Extend the dial timeout to 30s
[btrtrc.git] / misc.go
1 package torrent
2
3 import (
4         "crypto"
5         "errors"
6         "math/rand"
7         "os"
8         "path/filepath"
9         "time"
10
11         "bitbucket.org/anacrolix/go.torrent/mmap_span"
12         "bitbucket.org/anacrolix/go.torrent/peer_protocol"
13         "github.com/anacrolix/libtorgo/metainfo"
14         "launchpad.net/gommap"
15 )
16
17 const (
18         pieceHash   = crypto.SHA1
19         maxRequests = 250        // Maximum pending requests we allow peers to send us.
20         chunkSize   = 0x4000     // 16KiB
21         BEP20       = "-GT0000-" // Peer ID client identifier prefix
22         dialTimeout = time.Second * 30
23 )
24
25 type (
26         InfoHash [20]byte
27         pieceSum [20]byte
28 )
29
30 type piece struct {
31         Hash              pieceSum
32         PendingChunkSpecs map[chunkSpec]struct{}
33         Hashing           bool
34         QueuedForHash     bool
35         EverHashed        bool
36 }
37
38 func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) {
39         if len(p.PendingChunkSpecs) == 0 {
40                 return
41         }
42         css = make([]chunkSpec, 0, len(p.PendingChunkSpecs))
43         for cs := range p.PendingChunkSpecs {
44                 css = append(css, cs)
45         }
46         if len(css) <= 1 {
47                 return
48         }
49         for i := range css {
50                 j := rand.Intn(i + 1)
51                 css[i], css[j] = css[j], css[i]
52         }
53         return
54 }
55
56 func (p *piece) Complete() bool {
57         return len(p.PendingChunkSpecs) == 0 && p.EverHashed
58 }
59
60 func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
61         cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
62         cs.Length = pieceLength - cs.Begin
63         return
64 }
65
66 type chunkSpec struct {
67         Begin, Length peer_protocol.Integer
68 }
69
70 type request struct {
71         Index peer_protocol.Integer
72         chunkSpec
73 }
74
75 func newRequest(index, begin, length peer_protocol.Integer) request {
76         return request{index, chunkSpec{begin, length}}
77 }
78
79 type pieceByBytesPendingSlice struct {
80         Pending, Indices []peer_protocol.Integer
81 }
82
83 func (pcs pieceByBytesPendingSlice) Len() int {
84         return len(pcs.Indices)
85 }
86
87 func (me pieceByBytesPendingSlice) Less(i, j int) bool {
88         return me.Pending[me.Indices[i]] < me.Pending[me.Indices[j]]
89 }
90
91 func (me pieceByBytesPendingSlice) Swap(i, j int) {
92         me.Indices[i], me.Indices[j] = me.Indices[j], me.Indices[i]
93 }
94
95 var (
96         // Requested data not yet available.
97         ErrDataNotReady = errors.New("data not ready")
98 )
99
100 func upvertedSingleFileInfoFiles(info *metainfo.Info) []metainfo.FileInfo {
101         if len(info.Files) != 0 {
102                 return info.Files
103         }
104         return []metainfo.FileInfo{{Length: info.Length, Path: nil}}
105 }
106
107 func mmapTorrentData(md *metainfo.Info, location string) (mms mmap_span.MMapSpan, err error) {
108         defer func() {
109                 if err != nil {
110                         mms.Close()
111                         mms = nil
112                 }
113         }()
114         for _, miFile := range upvertedSingleFileInfoFiles(md) {
115                 fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
116                 err = os.MkdirAll(filepath.Dir(fileName), 0777)
117                 if err != nil {
118                         return
119                 }
120                 var file *os.File
121                 file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
122                 if err != nil {
123                         return
124                 }
125                 func() {
126                         defer file.Close()
127                         var fi os.FileInfo
128                         fi, err = file.Stat()
129                         if err != nil {
130                                 return
131                         }
132                         if fi.Size() < miFile.Length {
133                                 err = file.Truncate(miFile.Length)
134                                 if err != nil {
135                                         return
136                                 }
137                         }
138                         var mMap gommap.MMap
139                         mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
140                         if err != nil {
141                                 return
142                         }
143                         if int64(len(mMap)) != miFile.Length {
144                                 panic("mmap has wrong length")
145                         }
146                         mms = append(mms, mMap)
147                 }()
148                 if err != nil {
149                         return
150                 }
151         }
152         return
153 }
154
155 func metadataPieceSize(totalSize int, piece int) int {
156         ret := totalSize - piece*(1<<14)
157         if ret > 1<<14 {
158                 ret = 1 << 14
159         }
160         return ret
161 }