FedP2P/misc.go

81 lines
1.6 KiB
Go
Raw Normal View History

package torrent
import (
"crypto"
"errors"
2014-12-02 06:34:45 +08:00
"fmt"
"time"
2014-08-21 19:08:56 +08:00
"github.com/anacrolix/torrent/peer_protocol"
)
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-08-21 16:24:19 +08:00
type (
InfoHash [20]byte
pieceSum [20]byte
)
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[:])
}
func lastChunkSpec(pieceLength peer_protocol.Integer) (cs chunkSpec) {
cs.Begin = (pieceLength - 1) / chunkSize * chunkSize
cs.Length = pieceLength - cs.Begin
return
}
type chunkSpec struct {
Begin, Length peer_protocol.Integer
}
2014-04-16 19:13:44 +08:00
type request struct {
Index peer_protocol.Integer
chunkSpec
}
2014-04-16 19:13:44 +08:00
func newRequest(index, begin, length peer_protocol.Integer) request {
return request{index, chunkSpec{begin, length}}
2014-04-16 15:33:33 +08:00
}
var (
// Requested data not yet available.
2015-03-08 14:28:14 +08:00
errDataNotReady = errors.New("data not ready")
)
2014-11-19 11:53:00 +08:00
// The size in bytes of a metadata extension piece.
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
}