2014-04-03 12:16:59 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-08-28 00:06:36 +00:00
|
|
|
"container/heap"
|
2014-04-03 12:16:59 +00:00
|
|
|
"container/list"
|
|
|
|
"fmt"
|
2014-06-26 07:29:12 +00:00
|
|
|
"io"
|
2014-06-26 14:57:07 +00:00
|
|
|
"log"
|
2014-04-03 12:16:59 +00:00
|
|
|
"net"
|
2014-08-28 00:06:36 +00:00
|
|
|
"sort"
|
2014-07-22 15:54:11 +00:00
|
|
|
"sync"
|
2014-08-21 11:08:56 +00:00
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/mmap_span"
|
|
|
|
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/tracker"
|
|
|
|
"github.com/anacrolix/libtorgo/bencode"
|
|
|
|
"github.com/anacrolix/libtorgo/metainfo"
|
2014-04-03 12:16:59 +00:00
|
|
|
)
|
|
|
|
|
2014-05-23 10:58:11 +00:00
|
|
|
func (t *torrent) PieceNumPendingBytes(index pp.Integer) (count pp.Integer) {
|
2014-04-03 12:16:59 +00:00
|
|
|
pendingChunks := t.Pieces[index].PendingChunkSpecs
|
2014-05-23 10:58:11 +00:00
|
|
|
count = pp.Integer(len(pendingChunks)) * chunkSize
|
2014-04-03 12:16:59 +00:00
|
|
|
_lastChunkSpec := lastChunkSpec(t.PieceLength(index))
|
|
|
|
if _lastChunkSpec.Length != chunkSize {
|
|
|
|
if _, ok := pendingChunks[_lastChunkSpec]; ok {
|
|
|
|
count += _lastChunkSpec.Length - chunkSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-09 14:26:58 +00:00
|
|
|
type pieceBytesLeft struct {
|
|
|
|
Piece, BytesLeft int
|
|
|
|
}
|
|
|
|
|
|
|
|
type torrentPiece struct {
|
|
|
|
piece
|
|
|
|
bytesLeftElement *list.Element
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:10:19 +00:00
|
|
|
type peersKey struct {
|
|
|
|
IPBytes string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
type torrent struct {
|
2014-08-27 23:39:27 +00:00
|
|
|
stateMu sync.Mutex
|
|
|
|
closing chan struct{}
|
|
|
|
ceasingNetworking chan struct{}
|
|
|
|
|
2014-08-24 19:31:34 +00:00
|
|
|
InfoHash InfoHash
|
|
|
|
Pieces []*torrentPiece
|
|
|
|
IncompletePiecesByBytesLeft *OrderedList
|
|
|
|
length int64
|
2014-07-22 15:54:11 +00:00
|
|
|
// Prevent mutations to Data memory maps while in use as they're not safe.
|
2014-07-24 03:42:31 +00:00
|
|
|
dataLock sync.RWMutex
|
2014-08-24 19:31:34 +00:00
|
|
|
Data mmap_span.MMapSpan
|
|
|
|
|
|
|
|
Info *metainfo.Info
|
|
|
|
Conns []*connection
|
|
|
|
Peers map[peersKey]Peer
|
2014-04-03 12:16:59 +00:00
|
|
|
// BEP 12 Multitracker Metadata Extension. The tracker.Client instances
|
|
|
|
// mirror their respective URLs from the announce-list key.
|
2014-07-24 03:42:31 +00:00
|
|
|
Trackers [][]tracker.Client
|
|
|
|
DisplayName string
|
|
|
|
MetaData []byte
|
|
|
|
metadataHave []bool
|
2014-09-25 08:05:52 +00:00
|
|
|
|
|
|
|
gotMetainfo chan *metainfo.MetaInfo
|
|
|
|
GotMetainfo <-chan *metainfo.MetaInfo
|
2014-06-28 09:38:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 10:31:31 +00:00
|
|
|
func (t *torrent) worstConnsHeap() (wcs *worstConns) {
|
2014-09-13 17:40:35 +00:00
|
|
|
wcs = &worstConns{
|
|
|
|
c: append([]*connection{}, t.Conns...),
|
|
|
|
t: t,
|
|
|
|
}
|
2014-08-28 00:06:36 +00:00
|
|
|
heap.Init(wcs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-27 23:39:27 +00:00
|
|
|
func (t *torrent) CeaseNetworking() {
|
|
|
|
t.stateMu.Lock()
|
|
|
|
defer t.stateMu.Unlock()
|
|
|
|
select {
|
|
|
|
case <-t.ceasingNetworking:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(t.ceasingNetworking)
|
|
|
|
for _, c := range t.Conns {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-24 19:31:34 +00:00
|
|
|
func (t *torrent) assertIncompletePiecesByBytesLeftOrdering() {
|
|
|
|
allIndexes := make(map[int]struct{}, t.NumPieces())
|
|
|
|
for i := 0; i < t.NumPieces(); i++ {
|
|
|
|
allIndexes[i] = struct{}{}
|
|
|
|
}
|
|
|
|
var lastBytesLeft int
|
|
|
|
for e := t.IncompletePiecesByBytesLeft.Front(); e != nil; e = e.Next() {
|
|
|
|
i := e.Value.(int)
|
|
|
|
if _, ok := allIndexes[i]; !ok {
|
|
|
|
panic("duplicate entry")
|
|
|
|
}
|
|
|
|
delete(allIndexes, i)
|
|
|
|
if t.Pieces[i].Complete() {
|
|
|
|
panic("complete piece")
|
|
|
|
}
|
|
|
|
bytesLeft := int(t.PieceNumPendingBytes(pp.Integer(i)))
|
|
|
|
if bytesLeft < lastBytesLeft {
|
|
|
|
panic("ordering broken")
|
|
|
|
}
|
|
|
|
lastBytesLeft = bytesLeft
|
|
|
|
}
|
|
|
|
for i := range allIndexes {
|
|
|
|
if !t.Pieces[i].Complete() {
|
|
|
|
panic("leaked incomplete piece")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:10:19 +00:00
|
|
|
func (t *torrent) AddPeers(pp []Peer) {
|
|
|
|
for _, p := range pp {
|
|
|
|
t.Peers[peersKey{string(p.IP), p.Port}] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 09:38:31 +00:00
|
|
|
func (t *torrent) InvalidateMetadata() {
|
2014-07-14 13:12:52 +00:00
|
|
|
t.MetaData = nil
|
|
|
|
t.metadataHave = nil
|
2014-06-28 09:38:31 +00:00
|
|
|
t.Info = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) SaveMetadataPiece(index int, data []byte) {
|
|
|
|
if t.haveInfo() {
|
|
|
|
return
|
|
|
|
}
|
2014-07-14 13:12:52 +00:00
|
|
|
if index >= len(t.metadataHave) {
|
|
|
|
log.Printf("%s: ignoring metadata piece %d", t, index)
|
|
|
|
return
|
|
|
|
}
|
2014-06-28 09:38:31 +00:00
|
|
|
copy(t.MetaData[(1<<14)*index:], data)
|
|
|
|
t.metadataHave[index] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) MetadataPieceCount() int {
|
|
|
|
return (len(t.MetaData) + (1 << 14) - 1) / (1 << 14)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) HaveMetadataPiece(piece int) bool {
|
2014-08-21 17:42:00 +00:00
|
|
|
if t.haveInfo() {
|
|
|
|
return (1<<14)*piece < len(t.MetaData)
|
|
|
|
} else {
|
2014-08-25 12:15:45 +00:00
|
|
|
return piece < len(t.metadataHave) && t.metadataHave[piece]
|
2014-08-21 17:42:00 +00:00
|
|
|
}
|
2014-06-26 14:57:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 09:38:31 +00:00
|
|
|
func (t *torrent) metadataSizeKnown() bool {
|
|
|
|
return t.MetaData != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) metadataSize() int {
|
|
|
|
return len(t.MetaData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func infoPieceHashes(info *metainfo.Info) (ret []string) {
|
|
|
|
for i := 0; i < len(info.Pieces); i += 20 {
|
|
|
|
ret = append(ret, string(info.Pieces[i:i+20]))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-16 07:09:30 +00:00
|
|
|
// Called when metadata for a torrent becomes available.
|
2014-06-28 09:38:31 +00:00
|
|
|
func (t *torrent) setMetadata(md metainfo.Info, dataDir string, infoBytes []byte) (err error) {
|
|
|
|
t.Info = &md
|
|
|
|
t.MetaData = infoBytes
|
|
|
|
t.metadataHave = nil
|
|
|
|
t.Data, err = mmapTorrentData(&md, dataDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-23 17:09:02 +00:00
|
|
|
t.length = t.Data.Size()
|
2014-08-24 19:31:34 +00:00
|
|
|
t.IncompletePiecesByBytesLeft = NewList(func(a, b interface{}) bool {
|
2014-07-09 14:26:58 +00:00
|
|
|
apb := t.PieceNumPendingBytes(pp.Integer(a.(int)))
|
|
|
|
bpb := t.PieceNumPendingBytes(pp.Integer(b.(int)))
|
|
|
|
if apb < bpb {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if apb > bpb {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.(int) < b.(int)
|
|
|
|
})
|
|
|
|
for index, hash := range infoPieceHashes(&md) {
|
|
|
|
piece := &torrentPiece{}
|
2014-08-21 08:24:19 +00:00
|
|
|
util.CopyExact(piece.Hash[:], hash)
|
2014-06-28 09:38:31 +00:00
|
|
|
t.Pieces = append(t.Pieces, piece)
|
2014-08-24 19:31:34 +00:00
|
|
|
piece.bytesLeftElement = t.IncompletePiecesByBytesLeft.Insert(index)
|
2014-07-09 14:26:58 +00:00
|
|
|
t.pendAllChunkSpecs(pp.Integer(index))
|
2014-06-28 09:38:31 +00:00
|
|
|
}
|
2014-08-24 19:31:34 +00:00
|
|
|
t.assertIncompletePiecesByBytesLeftOrdering()
|
2014-07-16 07:09:30 +00:00
|
|
|
for _, conn := range t.Conns {
|
|
|
|
if err := conn.setNumPieces(t.NumPieces()); err != nil {
|
|
|
|
log.Printf("closing connection: %s", err)
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
2014-06-28 09:38:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) HaveAllMetadataPieces() bool {
|
|
|
|
if t.haveInfo() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if t.metadataHave == nil {
|
2014-06-26 14:57:07 +00:00
|
|
|
return false
|
|
|
|
}
|
2014-06-28 09:38:31 +00:00
|
|
|
for _, have := range t.metadataHave {
|
2014-06-26 14:57:07 +00:00
|
|
|
if !have {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-28 09:38:31 +00:00
|
|
|
func (t *torrent) SetMetadataSize(bytes int64) {
|
2014-06-26 14:57:07 +00:00
|
|
|
if t.MetaData != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.MetaData = make([]byte, bytes)
|
2014-06-28 09:38:31 +00:00
|
|
|
t.metadataHave = make([]bool, (bytes+(1<<14)-1)/(1<<14))
|
2014-06-26 14:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) Name() string {
|
2014-06-28 09:38:31 +00:00
|
|
|
if !t.haveInfo() {
|
2014-06-26 14:57:07 +00:00
|
|
|
return t.DisplayName
|
|
|
|
}
|
2014-06-28 09:38:31 +00:00
|
|
|
return t.Info.Name
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 07:29:12 +00:00
|
|
|
func (t *torrent) pieceStatusChar(index int) byte {
|
|
|
|
p := t.Pieces[index]
|
|
|
|
switch {
|
|
|
|
case p.Complete():
|
|
|
|
return 'C'
|
|
|
|
case p.QueuedForHash:
|
|
|
|
return 'Q'
|
|
|
|
case p.Hashing:
|
|
|
|
return 'H'
|
|
|
|
case t.PiecePartiallyDownloaded(index):
|
|
|
|
return 'P'
|
2014-09-13 17:43:11 +00:00
|
|
|
case !p.EverHashed:
|
|
|
|
return '?'
|
2014-06-26 07:29:12 +00:00
|
|
|
default:
|
|
|
|
return '.'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 09:38:31 +00:00
|
|
|
func (t *torrent) metadataPieceSize(piece int) int {
|
|
|
|
return metadataPieceSize(len(t.MetaData), piece)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) NewMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message {
|
|
|
|
d := map[string]int{
|
|
|
|
"msg_type": msgType,
|
|
|
|
"piece": piece,
|
|
|
|
}
|
|
|
|
if data != nil {
|
|
|
|
d["total_size"] = len(t.MetaData)
|
|
|
|
}
|
|
|
|
p, err := bencode.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return pp.Message{
|
|
|
|
Type: pp.Extended,
|
|
|
|
ExtendedID: byte(c.PeerExtensionIDs["ut_metadata"]),
|
|
|
|
ExtendedPayload: append(p, data...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 07:29:12 +00:00
|
|
|
func (t *torrent) WriteStatus(w io.Writer) {
|
2014-07-09 14:26:58 +00:00
|
|
|
fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
|
2014-08-25 12:15:45 +00:00
|
|
|
fmt.Fprintf(w, "Piece length: %s\n", func() string {
|
|
|
|
if t.haveInfo() {
|
|
|
|
return fmt.Sprint(t.UsualPieceSize())
|
|
|
|
} else {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
}())
|
2014-09-13 17:43:11 +00:00
|
|
|
fmt.Fprint(w, "Pieces:")
|
2014-06-26 07:29:12 +00:00
|
|
|
for index := range t.Pieces {
|
2014-09-13 17:43:11 +00:00
|
|
|
if index%100 == 0 {
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
}
|
2014-06-26 07:29:12 +00:00
|
|
|
fmt.Fprintf(w, "%c", t.pieceStatusChar(index))
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w)
|
2014-07-16 07:07:28 +00:00
|
|
|
fmt.Fprintf(w, "Pending peers: %d\n", len(t.Peers))
|
2014-08-22 07:33:17 +00:00
|
|
|
fmt.Fprintf(w, "Active peers: %d\n", len(t.Conns))
|
2014-09-13 17:40:35 +00:00
|
|
|
sort.Sort(&worstConns{
|
|
|
|
c: t.Conns,
|
|
|
|
t: t,
|
|
|
|
})
|
2014-06-26 07:29:12 +00:00
|
|
|
for _, c := range t.Conns {
|
|
|
|
c.WriteStatus(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:52:49 +00:00
|
|
|
func (t *torrent) String() string {
|
2014-06-26 14:57:07 +00:00
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) haveInfo() bool {
|
|
|
|
return t.Info != nil
|
2014-05-20 14:52:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 14:35:24 +00:00
|
|
|
func (t *torrent) BytesLeft() (left int64) {
|
2014-06-26 14:57:07 +00:00
|
|
|
if !t.haveInfo() {
|
|
|
|
return -1
|
|
|
|
}
|
2014-05-23 10:58:11 +00:00
|
|
|
for i := pp.Integer(0); i < pp.Integer(t.NumPieces()); i++ {
|
2014-05-22 14:35:24 +00:00
|
|
|
left += int64(t.PieceNumPendingBytes(i))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-26 07:29:12 +00:00
|
|
|
func (t *torrent) PiecePartiallyDownloaded(index int) bool {
|
|
|
|
return t.PieceNumPendingBytes(pp.Integer(index)) != t.PieceLength(pp.Integer(index))
|
|
|
|
}
|
|
|
|
|
2014-05-23 11:01:05 +00:00
|
|
|
func NumChunksForPiece(chunkSize int, pieceSize int) int {
|
|
|
|
return (pieceSize + chunkSize - 1) / chunkSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) ChunkCount() (num int) {
|
|
|
|
num += (t.NumPieces() - 1) * NumChunksForPiece(chunkSize, int(t.PieceLength(0)))
|
|
|
|
num += NumChunksForPiece(chunkSize, int(t.PieceLength(pp.Integer(t.NumPieces()-1))))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-28 15:32:34 +00:00
|
|
|
func (t *torrent) UsualPieceSize() int {
|
2014-06-28 09:38:31 +00:00
|
|
|
return int(t.Info.PieceLength)
|
2014-05-28 15:32:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 11:01:05 +00:00
|
|
|
func (t *torrent) LastPieceSize() int {
|
|
|
|
return int(t.PieceLength(pp.Integer(t.NumPieces() - 1)))
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) NumPieces() int {
|
2014-06-28 09:38:31 +00:00
|
|
|
return len(t.Info.Pieces) / 20
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) NumPiecesCompleted() (num int) {
|
2014-04-03 12:16:59 +00:00
|
|
|
for _, p := range t.Pieces {
|
|
|
|
if p.Complete() {
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) Length() int64 {
|
2014-08-23 17:09:02 +00:00
|
|
|
if t.Data == nil {
|
|
|
|
// Possibly the length might be available before the data is mmapped,
|
|
|
|
// I defer this decision to such a need arising.
|
|
|
|
panic("torrent length not known?")
|
|
|
|
}
|
|
|
|
return t.length
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 15:54:11 +00:00
|
|
|
func (t *torrent) isClosed() bool {
|
2014-08-24 20:01:05 +00:00
|
|
|
select {
|
|
|
|
case <-t.closing:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) Close() (err error) {
|
2014-08-24 20:01:05 +00:00
|
|
|
if t.isClosed() {
|
|
|
|
return
|
|
|
|
}
|
2014-08-27 23:39:27 +00:00
|
|
|
t.CeaseNetworking()
|
2014-08-24 20:01:05 +00:00
|
|
|
close(t.closing)
|
2014-07-22 15:54:11 +00:00
|
|
|
t.dataLock.Lock()
|
2014-04-03 12:16:59 +00:00
|
|
|
t.Data.Close()
|
2014-07-22 15:54:11 +00:00
|
|
|
t.Data = nil
|
|
|
|
t.dataLock.Unlock()
|
2014-04-03 12:16:59 +00:00
|
|
|
for _, conn := range t.Conns {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-08 06:45:33 +00:00
|
|
|
// Return the request that would include the given offset into the torrent data.
|
|
|
|
func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
|
2014-04-16 11:13:44 +00:00
|
|
|
r request, ok bool) {
|
2014-04-08 06:45:33 +00:00
|
|
|
if offset < 0 || offset >= torrentLength {
|
2014-04-03 12:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
2014-05-23 10:58:11 +00:00
|
|
|
r.Index = pp.Integer(offset / pieceSize)
|
|
|
|
r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
|
2014-04-08 06:45:33 +00:00
|
|
|
left := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
|
|
|
|
if chunkSize < left {
|
2014-05-23 10:58:11 +00:00
|
|
|
r.Length = pp.Integer(chunkSize)
|
2014-04-08 06:45:33 +00:00
|
|
|
} else {
|
2014-05-23 10:58:11 +00:00
|
|
|
r.Length = pp.Integer(left)
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-16 11:13:44 +00:00
|
|
|
func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
|
2014-04-08 15:15:39 +00:00
|
|
|
off = int64(r.Index)*pieceSize + int64(r.Begin)
|
|
|
|
if off < 0 || off >= torrentLength {
|
|
|
|
panic("invalid request")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-16 11:13:44 +00:00
|
|
|
func (t *torrent) requestOffset(r request) int64 {
|
2014-06-28 09:38:31 +00:00
|
|
|
return torrentRequestOffset(t.Length(), int64(t.UsualPieceSize()), r)
|
2014-04-08 15:15:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 06:45:33 +00:00
|
|
|
// Return the request that would include the given offset into the torrent data.
|
2014-04-16 11:13:44 +00:00
|
|
|
func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
|
2014-06-28 09:38:31 +00:00
|
|
|
return torrentOffsetRequest(t.Length(), t.Info.PieceLength, chunkSize, off)
|
2014-04-08 06:45:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) WriteChunk(piece int, begin int64, data []byte) (err error) {
|
2014-06-28 09:38:31 +00:00
|
|
|
_, err = t.Data.WriteAt(data, int64(piece)*t.Info.PieceLength+begin)
|
2014-04-03 12:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) bitfield() (bf []bool) {
|
2014-04-03 12:16:59 +00:00
|
|
|
for _, p := range t.Pieces {
|
|
|
|
bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-21 17:42:38 +00:00
|
|
|
func (t *torrent) pieceChunks(piece int) (css []chunkSpec) {
|
|
|
|
css = make([]chunkSpec, 0, (t.PieceLength(pp.Integer(piece))+chunkSize-1)/chunkSize)
|
|
|
|
var cs chunkSpec
|
|
|
|
for left := t.PieceLength(pp.Integer(piece)); left != 0; left -= cs.Length {
|
|
|
|
cs.Length = left
|
|
|
|
if cs.Length > chunkSize {
|
|
|
|
cs.Length = chunkSize
|
|
|
|
}
|
|
|
|
css = append(css, cs)
|
|
|
|
cs.Begin += cs.Length
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-23 10:58:11 +00:00
|
|
|
func (t *torrent) pendAllChunkSpecs(index pp.Integer) {
|
2014-04-03 12:16:59 +00:00
|
|
|
piece := t.Pieces[index]
|
|
|
|
if piece.PendingChunkSpecs == nil {
|
|
|
|
piece.PendingChunkSpecs = make(
|
2014-04-08 16:36:05 +00:00
|
|
|
map[chunkSpec]struct{},
|
2014-08-21 17:42:38 +00:00
|
|
|
(t.PieceLength(index)+chunkSize-1)/chunkSize)
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
2014-08-21 17:42:38 +00:00
|
|
|
pcss := piece.PendingChunkSpecs
|
|
|
|
for _, cs := range t.pieceChunks(int(index)) {
|
|
|
|
pcss[cs] = struct{}{}
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
2014-08-24 19:31:34 +00:00
|
|
|
t.IncompletePiecesByBytesLeft.ValueChanged(piece.bytesLeftElement)
|
2014-04-03 12:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-24 19:31:34 +00:00
|
|
|
func (t *torrent) PieceBytesLeftChanged(index int) {
|
|
|
|
p := t.Pieces[index]
|
|
|
|
if p.Complete() {
|
|
|
|
t.IncompletePiecesByBytesLeft.Remove(p.bytesLeftElement)
|
|
|
|
} else {
|
|
|
|
t.IncompletePiecesByBytesLeft.ValueChanged(p.bytesLeftElement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 12:16:59 +00:00
|
|
|
type Peer struct {
|
2014-07-16 07:06:18 +00:00
|
|
|
Id [20]byte
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
Source peerSource
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 10:58:11 +00:00
|
|
|
func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) {
|
2014-04-03 12:16:59 +00:00
|
|
|
if int(piece) == t.NumPieces()-1 {
|
2014-08-23 17:09:02 +00:00
|
|
|
len_ = pp.Integer(t.Length() % t.Info.PieceLength)
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
if len_ == 0 {
|
2014-06-28 09:38:31 +00:00
|
|
|
len_ = pp.Integer(t.Info.PieceLength)
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-23 10:58:11 +00:00
|
|
|
func (t *torrent) HashPiece(piece pp.Integer) (ps pieceSum) {
|
2014-04-08 16:36:05 +00:00
|
|
|
hash := pieceHash.New()
|
2014-07-22 15:54:11 +00:00
|
|
|
t.dataLock.RLock()
|
2014-06-28 09:38:31 +00:00
|
|
|
n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
|
2014-07-22 15:54:11 +00:00
|
|
|
t.dataLock.RUnlock()
|
2014-04-03 12:16:59 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-23 10:58:11 +00:00
|
|
|
if pp.Integer(n) != t.PieceLength(piece) {
|
2014-07-22 15:54:11 +00:00
|
|
|
// log.Print(t.Info)
|
2014-04-03 12:16:59 +00:00
|
|
|
panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceLength(piece), n, piece))
|
|
|
|
}
|
2014-08-21 08:24:19 +00:00
|
|
|
util.CopyExact(ps[:], hash.Sum(nil))
|
2014-04-03 12:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) haveAllPieces() bool {
|
2014-06-27 08:57:35 +00:00
|
|
|
if !t.haveInfo() {
|
2014-06-26 14:57:07 +00:00
|
|
|
return false
|
|
|
|
}
|
2014-04-03 12:16:59 +00:00
|
|
|
for _, piece := range t.Pieces {
|
|
|
|
if !piece.Complete() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (me *torrent) haveAnyPieces() bool {
|
2014-04-03 12:16:59 +00:00
|
|
|
for _, piece := range me.Pieces {
|
|
|
|
if piece.Complete() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-13 17:50:15 +00:00
|
|
|
func (t *torrent) havePiece(index int) bool {
|
|
|
|
return t.haveInfo() && t.Pieces[index].Complete()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) haveChunk(r request) bool {
|
|
|
|
p := t.Pieces[r.Index]
|
|
|
|
if !p.EverHashed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := p.PendingChunkSpecs[r.chunkSpec]
|
|
|
|
return !ok
|
|
|
|
}
|
|
|
|
|
2014-07-24 03:42:31 +00:00
|
|
|
func (t *torrent) wantChunk(r request) bool {
|
|
|
|
if !t.wantPiece(int(r.Index)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := t.Pieces[r.Index].PendingChunkSpecs[r.chunkSpec]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (t *torrent) wantPiece(index int) bool {
|
2014-06-26 14:57:07 +00:00
|
|
|
if !t.haveInfo() {
|
|
|
|
return false
|
|
|
|
}
|
2014-04-03 12:16:59 +00:00
|
|
|
p := t.Pieces[index]
|
|
|
|
return p.EverHashed && len(p.PendingChunkSpecs) != 0
|
|
|
|
}
|
2014-09-13 18:06:17 +00:00
|
|
|
|
|
|
|
func (t *torrent) connHasWantedPieces(c *connection) bool {
|
|
|
|
for p := range t.Pieces {
|
|
|
|
if t.wantPiece(p) && c.PeerHasPiece(pp.Integer(p)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) extentPieces(off, _len int64) (pieces []int) {
|
|
|
|
for i := off / int64(t.UsualPieceSize()); i*int64(t.UsualPieceSize()) < off+_len; i++ {
|
|
|
|
pieces = append(pieces, int(i))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|