2014-04-03 12:16:59 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"errors"
|
2014-12-01 22:34:45 +00:00
|
|
|
"fmt"
|
2014-05-28 15:32:34 +00:00
|
|
|
"math/rand"
|
2014-12-03 07:07:50 +00:00
|
|
|
"sync"
|
2014-04-03 12:16:59 +00:00
|
|
|
"time"
|
2014-08-21 11:08:56 +00:00
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
2014-04-03 12:16:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-11-19 03:53:00 +00:00
|
|
|
pieceHash = crypto.SHA1
|
|
|
|
maxRequests = 250 // Maximum pending requests we allow peers to send us.
|
|
|
|
chunkSize = 0x4000 // 16KiB
|
2015-03-08 06:28:14 +00:00
|
|
|
bep20 = "-GT0000-" // Peer ID client identifier prefix
|
2014-11-19 03:53:00 +00:00
|
|
|
nominalDialTimeout = time.Second * 30
|
|
|
|
minDialTimeout = 5 * time.Second
|
2014-04-03 12:16:59 +00:00
|
|
|
)
|
|
|
|
|
2014-08-21 08:24:19 +00:00
|
|
|
type (
|
|
|
|
InfoHash [20]byte
|
|
|
|
pieceSum [20]byte
|
|
|
|
)
|
2014-04-03 12:16:59 +00:00
|
|
|
|
2014-11-16 19:18:08 +00:00
|
|
|
func (ih *InfoHash) AsString() string {
|
|
|
|
return string(ih[:])
|
|
|
|
}
|
|
|
|
|
2014-12-01 22:34:45 +00:00
|
|
|
func (ih *InfoHash) HexString() string {
|
|
|
|
return fmt.Sprintf("%x", ih[:])
|
|
|
|
}
|
|
|
|
|
2014-12-03 07:07:50 +00:00
|
|
|
type piecePriority byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
piecePriorityNone piecePriority = iota
|
|
|
|
piecePriorityNormal
|
2014-12-05 06:56:28 +00:00
|
|
|
piecePriorityReadahead
|
|
|
|
piecePriorityNext
|
|
|
|
piecePriorityNow
|
2014-12-03 07:07:50 +00:00
|
|
|
)
|
|
|
|
|
2014-04-03 12:16:59 +00:00
|
|
|
type piece struct {
|
|
|
|
Hash pieceSum
|
2014-04-08 16:36:05 +00:00
|
|
|
PendingChunkSpecs map[chunkSpec]struct{}
|
2014-04-03 12:16:59 +00:00
|
|
|
Hashing bool
|
|
|
|
QueuedForHash bool
|
|
|
|
EverHashed bool
|
2014-12-03 07:07:50 +00:00
|
|
|
Event sync.Cond
|
|
|
|
Priority piecePriority
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 15:32:34 +00:00
|
|
|
func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) {
|
2014-08-22 07:37:18 +00:00
|
|
|
if len(p.PendingChunkSpecs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-05-28 15:32:34 +00:00
|
|
|
css = make([]chunkSpec, 0, len(p.PendingChunkSpecs))
|
|
|
|
for cs := range p.PendingChunkSpecs {
|
|
|
|
css = append(css, cs)
|
|
|
|
}
|
2014-08-22 07:37:18 +00:00
|
|
|
if len(css) <= 1 {
|
|
|
|
return
|
|
|
|
}
|
2014-05-28 15:32:34 +00:00
|
|
|
for i := range css {
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
css[i], css[j] = css[j], css[i]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
|
2014-04-03 12:16:59 +00:00
|
|
|
cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
|
|
|
|
cs.Length = pieceLength - cs.Begin
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
type chunkSpec struct {
|
2014-04-03 12:16:59 +00:00
|
|
|
Begin, Length peer_protocol.Integer
|
|
|
|
}
|
|
|
|
|
2014-04-16 11:13:44 +00:00
|
|
|
type request struct {
|
2014-04-03 12:16:59 +00:00
|
|
|
Index peer_protocol.Integer
|
2014-04-08 16:36:05 +00:00
|
|
|
chunkSpec
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 11:13:44 +00:00
|
|
|
func newRequest(index, begin, length peer_protocol.Integer) request {
|
|
|
|
return request{index, chunkSpec{begin, length}}
|
2014-04-16 07:33:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 12:16:59 +00:00
|
|
|
var (
|
2014-04-08 16:36:05 +00:00
|
|
|
// Requested data not yet available.
|
2015-03-08 06:28:14 +00:00
|
|
|
errDataNotReady = errors.New("data not ready")
|
2014-04-03 12:16:59 +00:00
|
|
|
)
|
|
|
|
|
2014-11-19 03:53:00 +00:00
|
|
|
// The size in bytes of a metadata extension piece.
|
2014-06-26 14:57:07 +00:00
|
|
|
func metadataPieceSize(totalSize int, piece int) int {
|
|
|
|
ret := totalSize - piece*(1<<14)
|
|
|
|
if ret > 1<<14 {
|
|
|
|
ret = 1 << 14
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2015-03-10 15:41:21 +00:00
|
|
|
|
|
|
|
type Super interface {
|
|
|
|
Super() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns ok if there's a parent, and it's not nil.
|
|
|
|
func super(child interface{}) (parent interface{}, ok bool) {
|
|
|
|
s, ok := child.(Super)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parent = s.Super()
|
|
|
|
ok = parent != nil
|
|
|
|
return
|
|
|
|
}
|