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