2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"errors"
|
2014-12-02 06:34:45 +08:00
|
|
|
"fmt"
|
2014-04-03 20:16:59 +08:00
|
|
|
"time"
|
2014-08-21 19:08:56 +08:00
|
|
|
|
2015-04-08 00:17:15 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2014-04-03 20:16:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-11-19 11:53:00 +08:00
|
|
|
pieceHash = crypto.SHA1
|
|
|
|
maxRequests = 250 // Maximum pending requests we allow peers to send us.
|
|
|
|
chunkSize = 0x4000 // 16KiB
|
2015-03-08 14:28:14 +08:00
|
|
|
bep20 = "-GT0000-" // Peer ID client identifier prefix
|
2014-11-19 11:53:00 +08:00
|
|
|
nominalDialTimeout = time.Second * 30
|
|
|
|
minDialTimeout = 5 * time.Second
|
2014-04-03 20:16:59 +08:00
|
|
|
)
|
|
|
|
|
2014-08-21 16:24:19 +08:00
|
|
|
type (
|
|
|
|
InfoHash [20]byte
|
|
|
|
pieceSum [20]byte
|
|
|
|
)
|
2014-04-03 20:16:59 +08:00
|
|
|
|
2014-11-17 03:18:08 +08:00
|
|
|
func (ih *InfoHash) AsString() string {
|
|
|
|
return string(ih[:])
|
|
|
|
}
|
|
|
|
|
2014-12-02 06:34:45 +08:00
|
|
|
func (ih *InfoHash) HexString() string {
|
|
|
|
return fmt.Sprintf("%x", ih[:])
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:17:15 +08:00
|
|
|
func lastChunkSpec(pieceLength pp.Integer) (cs chunkSpec) {
|
2014-04-03 20:16:59 +08:00
|
|
|
cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
|
|
|
|
cs.Length = pieceLength - cs.Begin
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
type chunkSpec struct {
|
2015-04-08 00:17:15 +08:00
|
|
|
Begin, Length pp.Integer
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2014-04-16 19:13:44 +08:00
|
|
|
type request struct {
|
2015-04-08 00:17:15 +08:00
|
|
|
Index pp.Integer
|
2014-04-09 00:36:05 +08:00
|
|
|
chunkSpec
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:17:15 +08:00
|
|
|
func newRequest(index, begin, length pp.Integer) request {
|
2014-04-16 19:13:44 +08:00
|
|
|
return request{index, chunkSpec{begin, length}}
|
2014-04-16 15:33:33 +08:00
|
|
|
}
|
|
|
|
|
2014-04-03 20:16:59 +08:00
|
|
|
var (
|
2014-04-09 00:36:05 +08:00
|
|
|
// Requested data not yet available.
|
2015-03-08 14:28:14 +08:00
|
|
|
errDataNotReady = errors.New("data not ready")
|
2014-04-03 20:16:59 +08:00
|
|
|
)
|
|
|
|
|
2014-11-19 11:53:00 +08:00
|
|
|
// The size in bytes of a metadata extension piece.
|
2014-06-26 22:57:07 +08: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 23:41:21 +08:00
|
|
|
|
2015-03-20 20:52:53 +08:00
|
|
|
type superer interface {
|
2015-03-10 23:41:21 +08:00
|
|
|
Super() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns ok if there's a parent, and it's not nil.
|
|
|
|
func super(child interface{}) (parent interface{}, ok bool) {
|
2015-03-20 20:52:53 +08:00
|
|
|
s, ok := child.(superer)
|
2015-03-10 23:41:21 +08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
parent = s.Super()
|
|
|
|
ok = parent != nil
|
|
|
|
return
|
|
|
|
}
|