2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-08-28 08:06:36 +08:00
|
|
|
"container/heap"
|
2014-04-03 20:16:59 +08:00
|
|
|
"fmt"
|
2014-06-26 15:29:12 +08:00
|
|
|
"io"
|
2014-06-26 22:57:07 +08:00
|
|
|
"log"
|
2014-04-03 20:16:59 +08:00
|
|
|
"net"
|
2014-08-28 08:06:36 +08:00
|
|
|
"sort"
|
2014-07-22 23:54:11 +08:00
|
|
|
"sync"
|
2014-12-02 06:40:18 +08:00
|
|
|
"time"
|
2014-08-21 19:08:56 +08:00
|
|
|
|
|
|
|
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/tracker"
|
2014-12-28 09:51:09 +08:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
2014-08-21 19:08:56 +08:00
|
|
|
"github.com/anacrolix/libtorgo/bencode"
|
|
|
|
"github.com/anacrolix/libtorgo/metainfo"
|
2014-04-03 20:16:59 +08:00
|
|
|
)
|
|
|
|
|
2014-05-23 18:58:11 +08:00
|
|
|
func (t *torrent) PieceNumPendingBytes(index pp.Integer) (count pp.Integer) {
|
2014-12-09 14:22:05 +08:00
|
|
|
piece := t.Pieces[index]
|
|
|
|
if !piece.EverHashed {
|
|
|
|
return t.PieceLength(index)
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
pendingChunks := t.Pieces[index].PendingChunkSpecs
|
2014-05-23 18:58:11 +08:00
|
|
|
count = pp.Integer(len(pendingChunks)) * chunkSize
|
2014-04-03 20:16:59 +08:00
|
|
|
_lastChunkSpec := lastChunkSpec(t.PieceLength(index))
|
|
|
|
if _lastChunkSpec.Length != chunkSize {
|
|
|
|
if _, ok := pendingChunks[_lastChunkSpec]; ok {
|
|
|
|
count += _lastChunkSpec.Length - chunkSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-21 19:10:19 +08:00
|
|
|
type peersKey struct {
|
|
|
|
IPBytes string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2015-02-26 22:43:37 +08:00
|
|
|
type Data interface {
|
2015-02-09 21:14:52 +08:00
|
|
|
ReadAt(p []byte, off int64) (n int, err error)
|
|
|
|
Close()
|
|
|
|
WriteAt(p []byte, off int64) (n int, err error)
|
|
|
|
WriteSectionTo(w io.Writer, off, n int64) (written int64, err error)
|
|
|
|
}
|
|
|
|
|
2015-02-24 22:34:57 +08:00
|
|
|
// Is not aware of Client. Maintains state of torrent for with-in a Client.
|
2014-04-09 00:36:05 +08:00
|
|
|
type torrent struct {
|
2014-11-21 14:09:55 +08:00
|
|
|
stateMu sync.Mutex
|
|
|
|
closing chan struct{}
|
|
|
|
|
|
|
|
// Closed when no more network activity is desired. This includes
|
|
|
|
// announcing, and communicating with peers.
|
2014-08-28 07:39:27 +08:00
|
|
|
ceasingNetworking chan struct{}
|
|
|
|
|
2014-12-26 14:15:17 +08:00
|
|
|
InfoHash InfoHash
|
2014-12-28 09:51:09 +08:00
|
|
|
Pieces []*piece
|
2014-12-26 14:15:17 +08:00
|
|
|
length int64
|
2015-02-09 21:14:52 +08:00
|
|
|
|
2015-02-26 22:43:37 +08:00
|
|
|
data Data
|
2014-08-25 03:31:34 +08:00
|
|
|
|
2015-02-26 19:17:58 +08:00
|
|
|
Info *metainfo.Info
|
2014-11-17 03:30:44 +08:00
|
|
|
// Active peer connections.
|
2014-08-25 03:31:34 +08:00
|
|
|
Conns []*connection
|
2014-11-17 03:30:44 +08:00
|
|
|
// Set of addrs to which we're attempting to connect.
|
|
|
|
HalfOpen map[string]struct{}
|
2014-11-21 14:09:55 +08:00
|
|
|
|
2014-11-17 03:30:44 +08:00
|
|
|
// Reserve of peers to connect to. A peer can be both here and in the
|
|
|
|
// active connections if were told about the peer after connecting with
|
|
|
|
// them. That encourages us to reconnect to peers that are well known.
|
2014-11-21 14:09:55 +08:00
|
|
|
Peers map[peersKey]Peer
|
|
|
|
wantPeers sync.Cond
|
2014-11-17 03:30:44 +08:00
|
|
|
|
2014-04-03 20:16:59 +08:00
|
|
|
// BEP 12 Multitracker Metadata Extension. The tracker.Client instances
|
2015-02-24 22:34:57 +08:00
|
|
|
// mirror their respective URLs from the announce-list metainfo key.
|
2014-07-24 11:42:31 +08:00
|
|
|
Trackers [][]tracker.Client
|
|
|
|
DisplayName string
|
|
|
|
MetaData []byte
|
|
|
|
metadataHave []bool
|
2014-09-25 16:05:52 +08:00
|
|
|
|
2014-12-02 06:37:40 +08:00
|
|
|
gotMetainfo chan struct{}
|
|
|
|
GotMetainfo <-chan struct{}
|
2015-02-25 11:48:39 +08:00
|
|
|
|
|
|
|
pruneTimer *time.Timer
|
2014-06-28 17:38:31 +08:00
|
|
|
}
|
|
|
|
|
2015-01-10 21:16:57 +08:00
|
|
|
func (t *torrent) numConnsUnchoked() (num int) {
|
|
|
|
for _, c := range t.Conns {
|
|
|
|
if !c.PeerChoked {
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-17 03:30:44 +08:00
|
|
|
func (t *torrent) addrActive(addr string) bool {
|
|
|
|
if _, ok := t.HalfOpen[addr]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, c := range t.Conns {
|
|
|
|
if c.Socket.RemoteAddr().String() == addr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-11 18:31:31 +08:00
|
|
|
func (t *torrent) worstConnsHeap() (wcs *worstConns) {
|
2014-09-14 01:40:35 +08:00
|
|
|
wcs = &worstConns{
|
|
|
|
c: append([]*connection{}, t.Conns...),
|
|
|
|
t: t,
|
|
|
|
}
|
2014-08-28 08:06:36 +08:00
|
|
|
heap.Init(wcs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-09 21:12:29 +08:00
|
|
|
func (t *torrent) ceaseNetworking() {
|
2014-08-28 07:39:27 +08:00
|
|
|
t.stateMu.Lock()
|
|
|
|
defer t.stateMu.Unlock()
|
|
|
|
select {
|
|
|
|
case <-t.ceasingNetworking:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(t.ceasingNetworking)
|
|
|
|
for _, c := range t.Conns {
|
|
|
|
c.Close()
|
|
|
|
}
|
2015-02-25 11:48:39 +08:00
|
|
|
t.pruneTimer.Stop()
|
2014-08-28 07:39:27 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 19:10:19 +08:00
|
|
|
func (t *torrent) AddPeers(pp []Peer) {
|
|
|
|
for _, p := range pp {
|
|
|
|
t.Peers[peersKey{string(p.IP), p.Port}] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) invalidateMetadata() {
|
2014-07-14 21:12:52 +08:00
|
|
|
t.MetaData = nil
|
|
|
|
t.metadataHave = nil
|
2014-06-28 17:38:31 +08:00
|
|
|
t.Info = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) SaveMetadataPiece(index int, data []byte) {
|
|
|
|
if t.haveInfo() {
|
|
|
|
return
|
|
|
|
}
|
2014-07-14 21:12:52 +08:00
|
|
|
if index >= len(t.metadataHave) {
|
|
|
|
log.Printf("%s: ignoring metadata piece %d", t, index)
|
|
|
|
return
|
|
|
|
}
|
2014-06-28 17:38:31 +08:00
|
|
|
copy(t.MetaData[(1<<14)*index:], data)
|
|
|
|
t.metadataHave[index] = true
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) metadataPieceCount() int {
|
2014-06-28 17:38:31 +08:00
|
|
|
return (len(t.MetaData) + (1 << 14) - 1) / (1 << 14)
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) haveMetadataPiece(piece int) bool {
|
2014-08-22 01:42:00 +08:00
|
|
|
if t.haveInfo() {
|
|
|
|
return (1<<14)*piece < len(t.MetaData)
|
|
|
|
} else {
|
2014-08-25 20:15:45 +08:00
|
|
|
return piece < len(t.metadataHave) && t.metadataHave[piece]
|
2014-08-22 01:42:00 +08:00
|
|
|
}
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-28 17:38:31 +08: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 15:09:30 +08:00
|
|
|
// Called when metadata for a torrent becomes available.
|
2015-02-25 11:48:39 +08:00
|
|
|
func (t *torrent) setMetadata(md metainfo.Info, infoBytes []byte, eventLocker sync.Locker) (err error) {
|
2015-02-26 19:17:58 +08:00
|
|
|
t.Info = &md
|
2015-02-09 21:14:52 +08:00
|
|
|
t.length = 0
|
|
|
|
for _, f := range t.Info.UpvertedFiles() {
|
|
|
|
t.length += f.Length
|
|
|
|
}
|
2014-06-28 17:38:31 +08:00
|
|
|
t.MetaData = infoBytes
|
|
|
|
t.metadataHave = nil
|
2014-12-26 14:15:17 +08:00
|
|
|
for _, hash := range infoPieceHashes(&md) {
|
2014-12-28 09:51:09 +08:00
|
|
|
piece := &piece{}
|
2014-12-03 15:07:50 +08:00
|
|
|
piece.Event.L = eventLocker
|
2014-08-21 16:24:19 +08:00
|
|
|
util.CopyExact(piece.Hash[:], hash)
|
2014-06-28 17:38:31 +08:00
|
|
|
t.Pieces = append(t.Pieces, piece)
|
|
|
|
}
|
2014-07-16 15:09:30 +08:00
|
|
|
for _, conn := range t.Conns {
|
2014-12-03 08:42:22 +08:00
|
|
|
t.initRequestOrdering(conn)
|
2015-02-09 21:12:29 +08:00
|
|
|
if err := conn.setNumPieces(t.numPieces()); err != nil {
|
2014-07-16 15:09:30 +08:00
|
|
|
log.Printf("closing connection: %s", err)
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 11:48:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-26 22:43:37 +08:00
|
|
|
func (t *torrent) setStorage(td Data) (err error) {
|
2015-02-25 11:48:39 +08:00
|
|
|
if t.data != nil {
|
|
|
|
t.data.Close()
|
2015-02-09 21:14:52 +08:00
|
|
|
}
|
2015-02-25 11:48:39 +08:00
|
|
|
t.data = td
|
2014-06-28 17:38:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) haveAllMetadataPieces() bool {
|
2014-06-28 17:38:31 +08:00
|
|
|
if t.haveInfo() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if t.metadataHave == nil {
|
2014-06-26 22:57:07 +08:00
|
|
|
return false
|
|
|
|
}
|
2014-06-28 17:38:31 +08:00
|
|
|
for _, have := range t.metadataHave {
|
2014-06-26 22:57:07 +08:00
|
|
|
if !have {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:38:31 +08:00
|
|
|
func (t *torrent) SetMetadataSize(bytes int64) {
|
2014-06-26 22:57:07 +08:00
|
|
|
if t.MetaData != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-02-06 11:54:59 +08:00
|
|
|
if bytes > 10000000 { // 10MB, pulled from my ass.
|
|
|
|
return
|
|
|
|
}
|
2014-06-26 22:57:07 +08:00
|
|
|
t.MetaData = make([]byte, bytes)
|
2014-06-28 17:38:31 +08:00
|
|
|
t.metadataHave = make([]bool, (bytes+(1<<14)-1)/(1<<14))
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) Name() string {
|
2015-02-06 11:54:59 +08:00
|
|
|
if t.haveInfo() {
|
|
|
|
return t.Info.Name
|
|
|
|
}
|
|
|
|
if t.DisplayName != "" {
|
2014-06-26 22:57:07 +08:00
|
|
|
return t.DisplayName
|
|
|
|
}
|
2015-02-06 11:54:59 +08:00
|
|
|
return t.InfoHash.HexString()
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 15:29:12 +08: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'
|
2014-09-14 01:43:11 +08:00
|
|
|
case !p.EverHashed:
|
|
|
|
return '?'
|
2015-02-25 12:42:47 +08:00
|
|
|
case t.piecePartiallyDownloaded(index):
|
2014-12-05 14:56:28 +08:00
|
|
|
switch p.Priority {
|
|
|
|
case piecePriorityNone:
|
|
|
|
return 'F' // Forgotten
|
|
|
|
default:
|
|
|
|
return 'P'
|
|
|
|
}
|
2014-06-26 15:29:12 +08:00
|
|
|
default:
|
2014-12-05 14:56:28 +08:00
|
|
|
switch p.Priority {
|
|
|
|
case piecePriorityNone:
|
|
|
|
return 'z'
|
|
|
|
case piecePriorityNow:
|
|
|
|
return '!'
|
|
|
|
case piecePriorityReadahead:
|
|
|
|
return 'R'
|
|
|
|
case piecePriorityNext:
|
|
|
|
return 'N'
|
|
|
|
default:
|
|
|
|
return '.'
|
|
|
|
}
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 17:38:31 +08:00
|
|
|
func (t *torrent) metadataPieceSize(piece int) int {
|
|
|
|
return metadataPieceSize(len(t.MetaData), piece)
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) newMetadataExtensionMessage(c *connection, msgType int, piece int, data []byte) pp.Message {
|
2014-06-28 17:38:31 +08:00
|
|
|
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...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 17:52:59 +08:00
|
|
|
type PieceStatusCharSequence struct {
|
|
|
|
Char byte
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) PieceStatusCharSequences() []PieceStatusCharSequence {
|
|
|
|
t.stateMu.Lock()
|
|
|
|
defer t.stateMu.Unlock()
|
|
|
|
return t.pieceStatusCharSequences()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the length of sequences of identical piece status chars.
|
|
|
|
func (t *torrent) pieceStatusCharSequences() (ret []PieceStatusCharSequence) {
|
|
|
|
var (
|
|
|
|
char byte
|
|
|
|
count int
|
|
|
|
)
|
|
|
|
writeSequence := func() {
|
|
|
|
ret = append(ret, PieceStatusCharSequence{char, count})
|
|
|
|
}
|
|
|
|
if len(t.Pieces) != 0 {
|
|
|
|
char = t.pieceStatusChar(0)
|
|
|
|
}
|
|
|
|
for index := range t.Pieces {
|
|
|
|
char1 := t.pieceStatusChar(index)
|
|
|
|
if char1 == char {
|
|
|
|
count++
|
|
|
|
} else {
|
|
|
|
writeSequence()
|
|
|
|
char = char1
|
|
|
|
count = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
writeSequence()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-26 15:29:12 +08:00
|
|
|
func (t *torrent) WriteStatus(w io.Writer) {
|
2014-07-09 22:26:58 +08:00
|
|
|
fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
|
2014-08-25 20:15:45 +08:00
|
|
|
fmt.Fprintf(w, "Piece length: %s\n", func() string {
|
|
|
|
if t.haveInfo() {
|
2015-02-25 12:42:47 +08:00
|
|
|
return fmt.Sprint(t.usualPieceSize())
|
2014-08-25 20:15:45 +08:00
|
|
|
} else {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
}())
|
2014-12-07 11:16:02 +08:00
|
|
|
if t.haveInfo() {
|
|
|
|
fmt.Fprint(w, "Pieces: ")
|
2015-01-26 17:52:59 +08:00
|
|
|
for _, seq := range t.pieceStatusCharSequences() {
|
|
|
|
fmt.Fprintf(w, "%d%c ", seq.Count, seq.Char)
|
2014-09-14 01:43:11 +08:00
|
|
|
}
|
2014-12-07 11:16:02 +08:00
|
|
|
fmt.Fprintln(w)
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
2014-11-21 14:07:42 +08:00
|
|
|
fmt.Fprintf(w, "Trackers: ")
|
|
|
|
for _, tier := range t.Trackers {
|
|
|
|
for _, tr := range tier {
|
|
|
|
fmt.Fprintf(w, "%q ", tr.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\n")
|
2014-07-16 15:07:28 +08:00
|
|
|
fmt.Fprintf(w, "Pending peers: %d\n", len(t.Peers))
|
2014-11-17 03:30:44 +08:00
|
|
|
fmt.Fprintf(w, "Half open: %d\n", len(t.HalfOpen))
|
2014-08-22 15:33:17 +08:00
|
|
|
fmt.Fprintf(w, "Active peers: %d\n", len(t.Conns))
|
2014-09-14 01:40:35 +08:00
|
|
|
sort.Sort(&worstConns{
|
|
|
|
c: t.Conns,
|
|
|
|
t: t,
|
|
|
|
})
|
2014-06-26 15:29:12 +08:00
|
|
|
for _, c := range t.Conns {
|
|
|
|
c.WriteStatus(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 22:52:49 +08:00
|
|
|
func (t *torrent) String() string {
|
2014-12-02 04:29:30 +08:00
|
|
|
s := t.Name()
|
|
|
|
if s == "" {
|
|
|
|
s = fmt.Sprintf("%x", t.InfoHash)
|
|
|
|
}
|
|
|
|
return s
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) haveInfo() bool {
|
|
|
|
return t.Info != nil
|
2014-05-20 22:52:49 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 06:37:40 +08:00
|
|
|
// TODO: Include URIs that weren't converted to tracker clients.
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) announceList() (al [][]string) {
|
2014-12-02 06:37:40 +08:00
|
|
|
for _, tier := range t.Trackers {
|
|
|
|
var l []string
|
|
|
|
for _, tr := range tier {
|
|
|
|
l = append(l, tr.URL())
|
|
|
|
}
|
|
|
|
al = append(al, l)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) MetaInfo() *metainfo.MetaInfo {
|
2014-12-02 13:33:38 +08:00
|
|
|
if t.MetaData == nil {
|
|
|
|
panic("info bytes not set")
|
|
|
|
}
|
2014-12-02 06:37:40 +08:00
|
|
|
return &metainfo.MetaInfo{
|
|
|
|
Info: metainfo.InfoEx{
|
2015-02-26 19:17:58 +08:00
|
|
|
Info: *t.Info,
|
2014-12-02 13:33:38 +08:00
|
|
|
Bytes: t.MetaData,
|
2014-12-02 06:37:40 +08:00
|
|
|
},
|
|
|
|
CreationDate: time.Now().Unix(),
|
|
|
|
Comment: "dynamic metainfo from client",
|
|
|
|
CreatedBy: "go.torrent",
|
2015-02-25 12:42:47 +08:00
|
|
|
AnnounceList: t.announceList(),
|
2014-12-02 06:37:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) bytesLeft() (left int64) {
|
2014-06-26 22:57:07 +08:00
|
|
|
if !t.haveInfo() {
|
|
|
|
return -1
|
|
|
|
}
|
2015-02-09 21:12:29 +08:00
|
|
|
for i := pp.Integer(0); i < pp.Integer(t.numPieces()); i++ {
|
2014-05-22 22:35:24 +08:00
|
|
|
left += int64(t.PieceNumPendingBytes(i))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) piecePartiallyDownloaded(index int) bool {
|
2014-06-26 15:29:12 +08:00
|
|
|
return t.PieceNumPendingBytes(pp.Integer(index)) != t.PieceLength(pp.Integer(index))
|
|
|
|
}
|
|
|
|
|
2014-05-23 19:01:05 +08:00
|
|
|
func NumChunksForPiece(chunkSize int, pieceSize int) int {
|
|
|
|
return (pieceSize + chunkSize - 1) / chunkSize
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) usualPieceSize() int {
|
2014-06-28 17:38:31 +08:00
|
|
|
return int(t.Info.PieceLength)
|
2014-05-28 23:32:34 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) lastPieceSize() int {
|
2015-02-09 21:12:29 +08:00
|
|
|
return int(t.PieceLength(pp.Integer(t.numPieces() - 1)))
|
2014-05-23 19:01:05 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 21:12:29 +08:00
|
|
|
func (t *torrent) numPieces() int {
|
2014-06-28 17:38:31 +08:00
|
|
|
return len(t.Info.Pieces) / 20
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) numPiecesCompleted() (num int) {
|
2014-04-03 20:16:59 +08:00
|
|
|
for _, p := range t.Pieces {
|
|
|
|
if p.Complete() {
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (t *torrent) Length() int64 {
|
2014-08-24 01:09:02 +08:00
|
|
|
return t.length
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2014-07-22 23:54:11 +08:00
|
|
|
func (t *torrent) isClosed() bool {
|
2014-08-25 04:01:05 +08:00
|
|
|
select {
|
|
|
|
case <-t.closing:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2014-07-22 23:54:11 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 21:12:29 +08:00
|
|
|
func (t *torrent) close() (err error) {
|
2014-08-25 04:01:05 +08:00
|
|
|
if t.isClosed() {
|
|
|
|
return
|
|
|
|
}
|
2015-02-09 21:12:29 +08:00
|
|
|
t.ceaseNetworking()
|
2014-08-25 04:01:05 +08:00
|
|
|
close(t.closing)
|
2015-02-25 11:48:39 +08:00
|
|
|
if t.data != nil {
|
|
|
|
t.data.Close()
|
2014-12-05 14:54:55 +08:00
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
for _, conn := range t.Conns {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-08 14:45:33 +08:00
|
|
|
// Return the request that would include the given offset into the torrent data.
|
|
|
|
func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
|
2014-04-16 19:13:44 +08:00
|
|
|
r request, ok bool) {
|
2014-04-08 14:45:33 +08:00
|
|
|
if offset < 0 || offset >= torrentLength {
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
2014-05-23 18:58:11 +08:00
|
|
|
r.Index = pp.Integer(offset / pieceSize)
|
|
|
|
r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
|
2014-04-08 14:45:33 +08:00
|
|
|
left := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
|
|
|
|
if chunkSize < left {
|
2014-05-23 18:58:11 +08:00
|
|
|
r.Length = pp.Integer(chunkSize)
|
2014-04-08 14:45:33 +08:00
|
|
|
} else {
|
2014-05-23 18:58:11 +08:00
|
|
|
r.Length = pp.Integer(left)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-16 19:13:44 +08:00
|
|
|
func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
|
2014-04-08 23:15:39 +08:00
|
|
|
off = int64(r.Index)*pieceSize + int64(r.Begin)
|
|
|
|
if off < 0 || off >= torrentLength {
|
|
|
|
panic("invalid request")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-16 19:13:44 +08:00
|
|
|
func (t *torrent) requestOffset(r request) int64 {
|
2015-02-25 12:42:47 +08:00
|
|
|
return torrentRequestOffset(t.Length(), int64(t.usualPieceSize()), r)
|
2014-04-08 23:15:39 +08:00
|
|
|
}
|
|
|
|
|
2014-04-08 14:45:33 +08:00
|
|
|
// Return the request that would include the given offset into the torrent data.
|
2014-04-16 19:13:44 +08:00
|
|
|
func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
|
2014-06-28 17:38:31 +08:00
|
|
|
return torrentOffsetRequest(t.Length(), t.Info.PieceLength, chunkSize, off)
|
2014-04-08 14:45:33 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) writeChunk(piece int, begin int64, data []byte) (err error) {
|
2015-02-25 11:48:39 +08:00
|
|
|
_, err = t.data.WriteAt(data, int64(piece)*t.Info.PieceLength+begin)
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (t *torrent) bitfield() (bf []bool) {
|
2014-04-03 20:16:59 +08:00
|
|
|
for _, p := range t.Pieces {
|
|
|
|
bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-22 01:42:38 +08: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 18:58:11 +08:00
|
|
|
func (t *torrent) pendAllChunkSpecs(index pp.Integer) {
|
2014-04-03 20:16:59 +08:00
|
|
|
piece := t.Pieces[index]
|
|
|
|
if piece.PendingChunkSpecs == nil {
|
|
|
|
piece.PendingChunkSpecs = make(
|
2014-04-09 00:36:05 +08:00
|
|
|
map[chunkSpec]struct{},
|
2014-08-22 01:42:38 +08:00
|
|
|
(t.PieceLength(index)+chunkSize-1)/chunkSize)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
2014-08-22 01:42:38 +08:00
|
|
|
pcss := piece.PendingChunkSpecs
|
|
|
|
for _, cs := range t.pieceChunks(int(index)) {
|
|
|
|
pcss[cs] = struct{}{}
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type Peer struct {
|
2014-07-16 15:06:18 +08:00
|
|
|
Id [20]byte
|
|
|
|
IP net.IP
|
|
|
|
Port int
|
|
|
|
Source peerSource
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2014-05-23 18:58:11 +08:00
|
|
|
func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) {
|
2015-02-09 21:12:29 +08:00
|
|
|
if int(piece) == t.numPieces()-1 {
|
2014-08-24 01:09:02 +08:00
|
|
|
len_ = pp.Integer(t.Length() % t.Info.PieceLength)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
if len_ == 0 {
|
2014-06-28 17:38:31 +08:00
|
|
|
len_ = pp.Integer(t.Info.PieceLength)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
|
2014-04-09 00:36:05 +08:00
|
|
|
hash := pieceHash.New()
|
2015-02-25 11:48:39 +08:00
|
|
|
t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
|
2014-08-21 16:24:19 +08:00
|
|
|
util.CopyExact(ps[:], hash.Sum(nil))
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
2014-04-09 00:36:05 +08:00
|
|
|
func (t *torrent) haveAllPieces() bool {
|
2014-06-27 16:57:35 +08:00
|
|
|
if !t.haveInfo() {
|
2014-06-26 22:57:07 +08:00
|
|
|
return false
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
for _, piece := range t.Pieces {
|
|
|
|
if !piece.Complete() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (me *torrent) haveAnyPieces() bool {
|
2014-04-03 20:16:59 +08:00
|
|
|
for _, piece := range me.Pieces {
|
|
|
|
if piece.Complete() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-14 01:50:15 +08: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 11:42:31 +08: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-09 00:36:05 +08:00
|
|
|
func (t *torrent) wantPiece(index int) bool {
|
2014-06-26 22:57:07 +08:00
|
|
|
if !t.haveInfo() {
|
|
|
|
return false
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
p := t.Pieces[index]
|
2014-12-03 15:07:50 +08:00
|
|
|
return p.EverHashed && len(p.PendingChunkSpecs) != 0 && p.Priority != piecePriorityNone
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
2014-09-14 02:06:17 +08: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) {
|
2015-02-25 12:42:47 +08:00
|
|
|
for i := off / int64(t.usualPieceSize()); i*int64(t.usualPieceSize()) < off+_len; i++ {
|
2014-09-14 02:06:17 +08:00
|
|
|
pieces = append(pieces, int(i))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|