13 "bitbucket.org/anacrolix/go.torrent/mmap_span"
14 "bitbucket.org/anacrolix/go.torrent/peer_protocol"
15 "github.com/anacrolix/libtorgo/metainfo"
16 "launchpad.net/gommap"
20 pieceHash = crypto.SHA1
21 maxRequests = 250 // Maximum pending requests we allow peers to send us.
22 chunkSize = 0x4000 // 16KiB
23 BEP20 = "-GT0000-" // Peer ID client identifier prefix
24 nominalDialTimeout = time.Second * 30
25 minDialTimeout = 5 * time.Second
33 func (ih *InfoHash) AsString() string {
37 func (ih *InfoHash) HexString() string {
38 return fmt.Sprintf("%x", ih[:])
41 type piecePriority byte
44 piecePriorityNone piecePriority = iota
51 PendingChunkSpecs map[chunkSpec]struct{}
56 Priority piecePriority
59 func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) {
60 if len(p.PendingChunkSpecs) == 0 {
63 css = make([]chunkSpec, 0, len(p.PendingChunkSpecs))
64 for cs := range p.PendingChunkSpecs {
72 css[i], css[j] = css[j], css[i]
77 func (p *piece) Complete() bool {
78 return len(p.PendingChunkSpecs) == 0 && p.EverHashed
81 func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
82 cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
83 cs.Length = pieceLength - cs.Begin
87 type chunkSpec struct {
88 Begin, Length peer_protocol.Integer
92 Index peer_protocol.Integer
96 func newRequest(index, begin, length peer_protocol.Integer) request {
97 return request{index, chunkSpec{begin, length}}
100 type pieceByBytesPendingSlice struct {
101 Pending, Indices []peer_protocol.Integer
104 func (pcs pieceByBytesPendingSlice) Len() int {
105 return len(pcs.Indices)
108 func (me pieceByBytesPendingSlice) Less(i, j int) bool {
109 return me.Pending[me.Indices[i]] < me.Pending[me.Indices[j]]
112 func (me pieceByBytesPendingSlice) Swap(i, j int) {
113 me.Indices[i], me.Indices[j] = me.Indices[j], me.Indices[i]
117 // Requested data not yet available.
118 ErrDataNotReady = errors.New("data not ready")
121 func upvertedSingleFileInfoFiles(info *metainfo.Info) []metainfo.FileInfo {
122 if len(info.Files) != 0 {
125 return []metainfo.FileInfo{{Length: info.Length, Path: nil}}
128 func mmapTorrentData(md *metainfo.Info, location string) (mms mmap_span.MMapSpan, err error) {
135 for _, miFile := range upvertedSingleFileInfoFiles(md) {
136 fileName := filepath.Join(append([]string{location, md.Name}, miFile.Path...)...)
137 err = os.MkdirAll(filepath.Dir(fileName), 0777)
139 err = fmt.Errorf("error creating data directory %q: %s", filepath.Dir(fileName), err)
143 file, err = os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
150 fi, err = file.Stat()
154 if fi.Size() < miFile.Length {
155 err = file.Truncate(miFile.Length)
160 if miFile.Length == 0 {
161 // Can't mmap() regions with length 0.
165 mMap, err = gommap.MapRegion(file.Fd(), 0, miFile.Length, gommap.PROT_READ|gommap.PROT_WRITE, gommap.MAP_SHARED)
167 err = fmt.Errorf("error mapping file %q, length %d: %s", file.Name(), miFile.Length, err)
170 if int64(len(mMap)) != miFile.Length {
171 panic("mmap has wrong length")
173 mms = append(mms, mMap)
182 // The size in bytes of a metadata extension piece.
183 func metadataPieceSize(totalSize int, piece int) int {
184 ret := totalSize - piece*(1<<14)