2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-08-28 08:06:36 +08:00
|
|
|
"container/heap"
|
2015-09-26 15:27:35 +08:00
|
|
|
"expvar"
|
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"
|
2015-09-26 15:27:35 +08:00
|
|
|
"math/rand"
|
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
|
|
|
|
2015-06-01 16:22:12 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2015-09-06 10:33:22 +08:00
|
|
|
"github.com/anacrolix/missinggo/pubsub"
|
2015-03-26 14:18:08 +08:00
|
|
|
"github.com/bradfitz/iter"
|
|
|
|
|
2015-04-28 13:24:17 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2015-03-20 13:37:44 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
"github.com/anacrolix/torrent/tracker"
|
2014-04-03 20:16:59 +08:00
|
|
|
)
|
|
|
|
|
2015-03-20 07:52:01 +08:00
|
|
|
func (t *torrent) pieceNumPendingBytes(index int) (count pp.Integer) {
|
2015-03-10 23:41:21 +08:00
|
|
|
if t.pieceComplete(index) {
|
2015-03-01 11:32:54 +08:00
|
|
|
return 0
|
|
|
|
}
|
2015-10-16 19:10:03 +08:00
|
|
|
piece := &t.Pieces[index]
|
2015-05-16 08:51:48 +08:00
|
|
|
pieceLength := t.pieceLength(index)
|
2014-12-09 14:22:05 +08:00
|
|
|
if !piece.EverHashed {
|
2015-05-16 08:51:48 +08:00
|
|
|
return pieceLength
|
|
|
|
}
|
|
|
|
for i, pending := range piece.PendingChunkSpecs {
|
|
|
|
if pending {
|
2015-07-15 13:31:18 +08:00
|
|
|
count += chunkIndexSpec(i, pieceLength, t.chunkSize).Length
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-06 09:19:49 +08:00
|
|
|
type peersKey struct {
|
2014-08-21 19:10:19 +08:00
|
|
|
IPBytes string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
|
2015-09-06 10:33:22 +08:00
|
|
|
InfoHash InfoHash
|
2015-10-16 19:10:03 +08:00
|
|
|
Pieces []piece
|
2015-09-06 10:33:22 +08:00
|
|
|
// Values are the piece indices that changed.
|
|
|
|
pieceStateChanges *pubsub.PubSub
|
|
|
|
chunkSize pp.Integer
|
2015-04-14 21:59:41 +08:00
|
|
|
// Chunks that are wanted before all others. This is for
|
|
|
|
// responsive/streaming readers that want to unblock ASAP.
|
|
|
|
urgent map[request]struct{}
|
2015-03-18 15:28:13 +08:00
|
|
|
// Total length of the torrent in bytes. Stored because it's not O(1) to
|
|
|
|
// get this from the info dict.
|
|
|
|
length int64
|
2015-02-09 21:14:52 +08:00
|
|
|
|
2015-10-01 22:09:04 +08:00
|
|
|
data Data
|
2014-08-25 03:31:34 +08:00
|
|
|
|
2015-04-28 13:24:17 +08:00
|
|
|
// The info dict. Nil if we don't have it (yet).
|
2015-02-26 19:17:58 +08:00
|
|
|
Info *metainfo.Info
|
2015-03-18 15:28:13 +08:00
|
|
|
// Active peer connections, running message stream loops.
|
2014-08-25 03:31:34 +08:00
|
|
|
Conns []*connection
|
2015-03-18 15:28:13 +08:00
|
|
|
// Set of addrs to which we're attempting to connect. Connections are
|
|
|
|
// half-open until all handshakes are completed.
|
2014-11-17 03:30:44 +08:00
|
|
|
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.
|
2016-01-06 09:19:49 +08:00
|
|
|
Peers map[peersKey]Peer
|
2014-11-21 14:09:55 +08:00
|
|
|
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.
|
2015-03-18 15:28:13 +08:00
|
|
|
Trackers [][]tracker.Client
|
|
|
|
// Name used if the info name isn't available.
|
2015-11-22 15:44:08 +08:00
|
|
|
displayName string
|
2015-03-18 15:28:13 +08:00
|
|
|
// The bencoded bytes of the info dict.
|
|
|
|
MetaData []byte
|
|
|
|
// Each element corresponds to the 16KiB metadata pieces. If true, we have
|
|
|
|
// received that piece.
|
2014-07-24 11:42:31 +08:00
|
|
|
metadataHave []bool
|
2014-09-25 16:05:52 +08:00
|
|
|
|
2015-03-18 15:28:13 +08:00
|
|
|
// Closed when .Info is set.
|
2014-12-02 06:37:40 +08:00
|
|
|
gotMetainfo chan struct{}
|
2015-09-26 15:27:35 +08:00
|
|
|
|
|
|
|
connPiecePriorites sync.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
piecePrioritiesReused = expvar.NewInt("piecePrioritiesReused")
|
|
|
|
piecePrioritiesNew = expvar.NewInt("piecePrioritiesNew")
|
|
|
|
)
|
|
|
|
|
2015-12-12 11:03:04 +08:00
|
|
|
func (t *torrent) setDisplayName(dn string) {
|
|
|
|
t.displayName = dn
|
|
|
|
}
|
|
|
|
|
2015-09-26 15:27:35 +08:00
|
|
|
func (t *torrent) newConnPiecePriorities() []int {
|
|
|
|
_ret := t.connPiecePriorites.Get()
|
|
|
|
if _ret != nil {
|
|
|
|
piecePrioritiesReused.Add(1)
|
|
|
|
return _ret.([]int)
|
|
|
|
}
|
|
|
|
piecePrioritiesNew.Add(1)
|
|
|
|
return rand.Perm(t.numPieces())
|
2014-06-28 17:38:31 +08:00
|
|
|
}
|
|
|
|
|
2015-03-10 23:41:21 +08:00
|
|
|
func (t *torrent) pieceComplete(piece int) bool {
|
|
|
|
// TODO: This is called when setting metadata, and before storage is
|
|
|
|
// assigned, which doesn't seem right.
|
|
|
|
return t.data != nil && t.data.PieceComplete(piece)
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:16:57 +08:00
|
|
|
func (t *torrent) numConnsUnchoked() (num int) {
|
|
|
|
for _, c := range t.Conns {
|
|
|
|
if !c.PeerChoked {
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:28:13 +08:00
|
|
|
// There's a connection to that address already.
|
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 {
|
2015-03-13 03:21:13 +08:00
|
|
|
if c.remoteAddr().String() == addr {
|
2014-11-17 03:30:44 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-29 22:45:26 +08:00
|
|
|
func (t *torrent) worstConns(cl *Client) (wcs *worstConns) {
|
2014-09-14 01:40:35 +08:00
|
|
|
wcs = &worstConns{
|
2015-06-29 22:45:26 +08:00
|
|
|
c: make([]*connection, 0, len(t.Conns)),
|
2015-06-16 14:57:47 +08:00
|
|
|
t: t,
|
|
|
|
cl: cl,
|
2014-09-14 01:40:35 +08:00
|
|
|
}
|
2015-06-29 22:45:26 +08:00
|
|
|
for _, c := range t.Conns {
|
|
|
|
select {
|
|
|
|
case <-c.closing:
|
|
|
|
default:
|
|
|
|
wcs.c = append(wcs.c, c)
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 08:06:36 +08:00
|
|
|
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-09-28 13:30:13 +08:00
|
|
|
func (t *torrent) addPeer(p Peer, cl *Client) {
|
|
|
|
cl.openNewConns(t)
|
|
|
|
if len(t.Peers) >= torrentPeersHighWater {
|
|
|
|
return
|
|
|
|
}
|
2016-01-06 09:19:49 +08:00
|
|
|
key := peersKey{string(p.IP), p.Port}
|
2015-09-28 13:30:13 +08:00
|
|
|
if _, ok := t.Peers[key]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Peers[key] = p
|
|
|
|
peersAddedBySource.Add(string(p.Source), 1)
|
|
|
|
cl.openNewConns(t)
|
|
|
|
|
2014-08-21 19:10:19 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-20 07:52:01 +08:00
|
|
|
func (t *torrent) saveMetadataPiece(index int, data []byte) {
|
2014-06-28 17:38:31 +08:00
|
|
|
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-11-05 21:40:16 +08:00
|
|
|
func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte) (err error) {
|
2015-06-02 22:17:58 +08:00
|
|
|
err = validateInfo(md)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("bad info: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2015-03-18 15:32:31 +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
|
2015-10-16 19:10:03 +08:00
|
|
|
hashes := infoPieceHashes(md)
|
|
|
|
t.Pieces = make([]piece, len(hashes))
|
|
|
|
for i, hash := range hashes {
|
|
|
|
piece := &t.Pieces[i]
|
2015-08-05 00:40:46 +08:00
|
|
|
piece.noPendingWrites.L = &piece.pendingWritesMutex
|
2015-08-03 22:29:01 +08:00
|
|
|
missinggo.CopyExact(piece.Hash[:], hash)
|
2014-06-28 17:38:31 +08:00
|
|
|
}
|
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-10-01 22:09:04 +08:00
|
|
|
func (t *torrent) setStorage(td Data) (err error) {
|
|
|
|
if t.data != nil {
|
|
|
|
t.data.Close()
|
2015-02-27 09:45:55 +08:00
|
|
|
}
|
2015-10-01 22:09:04 +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
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:36:59 +08:00
|
|
|
func (t *torrent) setMetadataSize(bytes int64, cl *Client) {
|
|
|
|
if t.haveInfo() {
|
|
|
|
// We already know the correct metadata size.
|
2014-06-26 22:57:07 +08:00
|
|
|
return
|
|
|
|
}
|
2015-03-24 13:46:34 +08:00
|
|
|
if bytes <= 0 || bytes > 10000000 { // 10MB, pulled from my ass.
|
2015-03-27 12:36:59 +08:00
|
|
|
log.Printf("received bad metadata size: %d", bytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if t.MetaData != nil && len(t.MetaData) == int(bytes) {
|
2015-02-06 11:54:59 +08:00
|
|
|
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))
|
2015-03-27 12:36:59 +08:00
|
|
|
for _, c := range t.Conns {
|
|
|
|
cl.requestPendingMetadata(t, c)
|
|
|
|
}
|
|
|
|
|
2014-06-26 22:57:07 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 14:35:29 +08:00
|
|
|
// The current working name for the torrent. Either the name in the info dict,
|
|
|
|
// or a display name given such as by the dn value in a magnet link, or "".
|
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
|
|
|
|
}
|
2015-11-22 15:44:08 +08:00
|
|
|
return t.displayName
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:22:12 +08:00
|
|
|
func (t *torrent) pieceState(index int) (ret PieceState) {
|
2015-10-16 19:10:03 +08:00
|
|
|
p := &t.Pieces[index]
|
2015-06-01 16:22:12 +08:00
|
|
|
ret.Priority = p.Priority
|
|
|
|
if t.pieceComplete(index) {
|
|
|
|
ret.Complete = true
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
2015-06-01 16:22:12 +08:00
|
|
|
if p.QueuedForHash || p.Hashing {
|
|
|
|
ret.Checking = true
|
|
|
|
}
|
2015-07-17 19:07:01 +08:00
|
|
|
if !ret.Complete && t.piecePartiallyDownloaded(index) {
|
2015-06-01 16:22:12 +08:00
|
|
|
ret.Partial = true
|
|
|
|
}
|
|
|
|
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-06-01 16:22:12 +08:00
|
|
|
func (t *torrent) pieceStateRuns() (ret []PieceStateRun) {
|
|
|
|
rle := missinggo.NewRunLengthEncoder(func(el interface{}, count uint64) {
|
|
|
|
ret = append(ret, PieceStateRun{
|
|
|
|
PieceState: el.(PieceState),
|
|
|
|
Length: int(count),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
for index := range t.Pieces {
|
|
|
|
rle.Append(t.pieceState(index), 1)
|
|
|
|
}
|
|
|
|
rle.Flush()
|
|
|
|
return
|
2015-01-26 17:52:59 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:22:12 +08:00
|
|
|
// Produces a small string representing a PieceStateRun.
|
|
|
|
func pieceStateRunStatusChars(psr PieceStateRun) (ret string) {
|
|
|
|
ret = fmt.Sprintf("%d", psr.Length)
|
|
|
|
ret += func() string {
|
|
|
|
switch psr.Priority {
|
|
|
|
case PiecePriorityNext:
|
|
|
|
return "N"
|
|
|
|
case PiecePriorityNormal:
|
|
|
|
return "."
|
|
|
|
case PiecePriorityReadahead:
|
|
|
|
return "R"
|
|
|
|
case PiecePriorityNow:
|
|
|
|
return "!"
|
|
|
|
default:
|
|
|
|
return ""
|
2015-01-26 17:52:59 +08:00
|
|
|
}
|
2015-06-01 16:22:12 +08:00
|
|
|
}()
|
|
|
|
if psr.Checking {
|
|
|
|
ret += "H"
|
|
|
|
}
|
|
|
|
if psr.Partial {
|
|
|
|
ret += "P"
|
2015-01-26 17:52:59 +08:00
|
|
|
}
|
2015-06-01 16:22:12 +08:00
|
|
|
if psr.Complete {
|
|
|
|
ret += "C"
|
2015-01-26 17:52:59 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-16 14:57:47 +08:00
|
|
|
func (t *torrent) writeStatus(w io.Writer, cl *Client) {
|
2014-07-09 22:26:58 +08:00
|
|
|
fmt.Fprintf(w, "Infohash: %x\n", t.InfoHash)
|
2015-03-25 12:50:31 +08:00
|
|
|
fmt.Fprintf(w, "Metadata length: %d\n", t.metadataSize())
|
2015-06-29 22:46:24 +08:00
|
|
|
if !t.haveInfo() {
|
|
|
|
fmt.Fprintf(w, "Metadata have: ")
|
|
|
|
for _, h := range t.metadataHave {
|
|
|
|
fmt.Fprintf(w, "%c", func() rune {
|
|
|
|
if h {
|
|
|
|
return 'H'
|
|
|
|
} else {
|
|
|
|
return '.'
|
|
|
|
}
|
|
|
|
}())
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w)
|
2015-03-25 12:50:31 +08:00
|
|
|
}
|
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() {
|
2015-09-25 20:05:32 +08:00
|
|
|
fmt.Fprintf(w, "Num Pieces: %d\n", t.numPieces())
|
|
|
|
fmt.Fprint(w, "Piece States:")
|
2015-06-01 16:22:12 +08:00
|
|
|
for _, psr := range t.pieceStateRuns() {
|
|
|
|
w.Write([]byte(" "))
|
|
|
|
w.Write([]byte(pieceStateRunStatusChars(psr)))
|
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
|
|
|
}
|
2015-04-14 21:59:41 +08:00
|
|
|
fmt.Fprintf(w, "Urgent:")
|
|
|
|
for req := range t.urgent {
|
2015-05-15 06:41:42 +08:00
|
|
|
fmt.Fprintf(w, " %v", req)
|
2015-04-14 21:59:41 +08:00
|
|
|
}
|
|
|
|
fmt.Fprintln(w)
|
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{
|
2015-06-16 14:57:47 +08:00
|
|
|
c: t.Conns,
|
|
|
|
t: t,
|
|
|
|
cl: cl,
|
2014-09-14 01:40:35 +08:00
|
|
|
})
|
2015-06-29 22:46:24 +08:00
|
|
|
for i, c := range t.Conns {
|
|
|
|
fmt.Fprintf(w, "%2d. ", i+1)
|
2015-03-12 17:06:23 +08:00
|
|
|
c.WriteStatus(w, t)
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-06-29 22:46:24 +08:00
|
|
|
return t != nil && 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
|
|
|
|
}
|
|
|
|
|
2015-03-20 07:52:01 +08:00
|
|
|
// Returns a run-time generated MetaInfo that includes the info bytes and
|
|
|
|
// announce-list as currently known to the client.
|
2014-12-02 06:37:40 +08:00
|
|
|
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-03-10 23:39:01 +08:00
|
|
|
for i := 0; i < t.numPieces(); i++ {
|
2015-03-20 07:52:01 +08:00
|
|
|
left += int64(t.pieceNumPendingBytes(i))
|
2014-05-22 22:35:24 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) piecePartiallyDownloaded(index int) bool {
|
2015-06-01 16:22:12 +08:00
|
|
|
pendingBytes := t.pieceNumPendingBytes(index)
|
|
|
|
return pendingBytes != 0 && pendingBytes != t.pieceLength(index)
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
|
|
|
|
2015-03-08 14:28:14 +08:00
|
|
|
func numChunksForPiece(chunkSize int, pieceSize int) int {
|
2014-05-23 19:01:05 +08:00
|
|
|
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-03-20 20:52:53 +08:00
|
|
|
return int(t.pieceLength(t.numPieces() - 1))
|
2014-05-23 19:01:05 +08:00
|
|
|
}
|
|
|
|
|
2015-02-09 21:12:29 +08:00
|
|
|
func (t *torrent) numPieces() int {
|
2015-03-12 17:06:23 +08:00
|
|
|
return t.Info.NumPieces()
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 12:42:47 +08:00
|
|
|
func (t *torrent) numPiecesCompleted() (num int) {
|
2015-03-10 23:41:21 +08:00
|
|
|
for i := range iter.N(t.Info.NumPieces()) {
|
|
|
|
if t.pieceComplete(i) {
|
2014-04-03 20:16:59 +08:00
|
|
|
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-03-01 11:32:54 +08:00
|
|
|
if c, ok := t.data.(io.Closer); ok {
|
|
|
|
c.Close()
|
2014-12-05 14:54:55 +08:00
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
for _, conn := range t.Conns {
|
|
|
|
conn.Close()
|
|
|
|
}
|
2015-09-06 10:33:22 +08:00
|
|
|
t.pieceStateChanges.Close()
|
2014-04-03 20:16:59 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-08 00:20:01 +08:00
|
|
|
// Return the request that would include the given offset into the torrent
|
|
|
|
// data. Returns !ok if there is no such request.
|
2014-04-16 19:13:44 +08:00
|
|
|
func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
|
2015-07-15 13:31:18 +08:00
|
|
|
return torrentOffsetRequest(t.Length(), t.Info.PieceLength, int64(t.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-06-02 22:03:43 +08:00
|
|
|
n, err := t.data.WriteAt(data, int64(piece)*t.Info.PieceLength+begin)
|
|
|
|
if err == nil && n != len(data) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (t *torrent) bitfield() (bf []bool) {
|
2015-10-16 19:10:03 +08:00
|
|
|
for i := range t.Pieces {
|
|
|
|
p := &t.Pieces[i]
|
2015-04-14 21:59:41 +08:00
|
|
|
// TODO: Check this logic.
|
2015-05-16 08:51:48 +08:00
|
|
|
bf = append(bf, p.EverHashed && p.numPendingChunks() == 0)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-16 08:51:48 +08:00
|
|
|
func (t *torrent) validOutgoingRequest(r request) bool {
|
|
|
|
if r.Index >= pp.Integer(t.Info.NumPieces()) {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-15 13:31:18 +08:00
|
|
|
if r.Begin%t.chunkSize != 0 {
|
2015-05-16 08:51:48 +08:00
|
|
|
return false
|
|
|
|
}
|
2015-07-15 13:31:18 +08:00
|
|
|
if r.Length > t.chunkSize {
|
2015-05-16 08:51:48 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
pieceLength := t.pieceLength(int(r.Index))
|
|
|
|
if r.Begin+r.Length > pieceLength {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-15 13:31:18 +08:00
|
|
|
return r.Length == t.chunkSize || r.Begin+r.Length == pieceLength
|
2015-05-16 08:51:48 +08:00
|
|
|
}
|
|
|
|
|
2014-08-22 01:42:38 +08:00
|
|
|
func (t *torrent) pieceChunks(piece int) (css []chunkSpec) {
|
2015-07-15 13:31:18 +08:00
|
|
|
css = make([]chunkSpec, 0, (t.pieceLength(piece)+t.chunkSize-1)/t.chunkSize)
|
2014-08-22 01:42:38 +08:00
|
|
|
var cs chunkSpec
|
2015-03-20 20:52:53 +08:00
|
|
|
for left := t.pieceLength(piece); left != 0; left -= cs.Length {
|
2014-08-22 01:42:38 +08:00
|
|
|
cs.Length = left
|
2015-07-15 13:31:18 +08:00
|
|
|
if cs.Length > t.chunkSize {
|
|
|
|
cs.Length = t.chunkSize
|
2014-08-22 01:42:38 +08:00
|
|
|
}
|
|
|
|
css = append(css, cs)
|
|
|
|
cs.Begin += cs.Length
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-16 08:51:48 +08:00
|
|
|
func (t *torrent) pendAllChunkSpecs(pieceIndex int) {
|
2015-10-16 19:10:03 +08:00
|
|
|
piece := &t.Pieces[pieceIndex]
|
2014-04-03 20:16:59 +08:00
|
|
|
if piece.PendingChunkSpecs == nil {
|
2015-05-16 08:51:48 +08:00
|
|
|
// Allocate to exact size.
|
2015-07-15 13:31:18 +08:00
|
|
|
piece.PendingChunkSpecs = make([]bool, (t.pieceLength(pieceIndex)+t.chunkSize-1)/t.chunkSize)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
2015-05-16 08:51:48 +08:00
|
|
|
// Pend all the chunks.
|
2014-08-22 01:42:38 +08:00
|
|
|
pcss := piece.PendingChunkSpecs
|
2015-05-16 08:51:48 +08:00
|
|
|
for i := range pcss {
|
|
|
|
pcss[i] = true
|
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
|
2015-03-18 15:37:26 +08:00
|
|
|
// Peer is known to support encryption.
|
|
|
|
SupportsEncryption bool
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-20 20:52:53 +08:00
|
|
|
func (t *torrent) pieceLength(piece int) (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
|
|
|
|
}
|
|
|
|
|
2016-01-04 19:34:24 +08:00
|
|
|
func (t *torrent) hashPiece(piece int) (ps pieceSum) {
|
2014-04-09 00:36:05 +08:00
|
|
|
hash := pieceHash.New()
|
2015-10-16 19:10:03 +08:00
|
|
|
p := &t.Pieces[piece]
|
2015-08-05 00:40:46 +08:00
|
|
|
p.pendingWritesMutex.Lock()
|
|
|
|
for p.pendingWrites != 0 {
|
|
|
|
p.noPendingWrites.Wait()
|
|
|
|
}
|
|
|
|
p.pendingWritesMutex.Unlock()
|
2015-10-03 22:22:46 +08:00
|
|
|
pl := t.Info.Piece(int(piece)).Length()
|
|
|
|
n, err := t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, pl)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.ErrUnexpectedEOF {
|
|
|
|
log.Printf("error hashing piece with %T: %s", t.data, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != pl {
|
2015-10-20 14:53:12 +08:00
|
|
|
panic(fmt.Sprintf("%T: %d != %d", t.data, n, pl))
|
2015-10-03 22:22:46 +08:00
|
|
|
}
|
2015-08-03 22:29:01 +08:00
|
|
|
missinggo.CopyExact(ps[:], hash.Sum(nil))
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
2015-02-27 09:45:55 +08:00
|
|
|
|
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
|
|
|
|
}
|
2015-03-10 23:41:21 +08:00
|
|
|
for i := range t.Pieces {
|
|
|
|
if !t.pieceComplete(i) {
|
2014-04-03 20:16:59 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (me *torrent) haveAnyPieces() bool {
|
2015-03-10 23:41:21 +08:00
|
|
|
for i := range me.Pieces {
|
|
|
|
if me.pieceComplete(i) {
|
2014-04-03 20:16:59 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-09-14 01:50:15 +08:00
|
|
|
func (t *torrent) havePiece(index int) bool {
|
2015-03-10 23:41:21 +08:00
|
|
|
return t.haveInfo() && t.pieceComplete(index)
|
2014-09-14 01:50:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *torrent) haveChunk(r request) bool {
|
2015-04-14 21:59:41 +08:00
|
|
|
if !t.haveInfo() {
|
2014-09-14 01:50:15 +08:00
|
|
|
return false
|
|
|
|
}
|
2015-07-17 19:07:01 +08:00
|
|
|
if t.pieceComplete(int(r.Index)) {
|
|
|
|
return true
|
|
|
|
}
|
2015-10-16 19:10:03 +08:00
|
|
|
p := &t.Pieces[r.Index]
|
2015-07-17 19:07:01 +08:00
|
|
|
if p.PendingChunkSpecs == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-15 13:31:18 +08:00
|
|
|
return !p.pendingChunk(r.chunkSpec, t.chunkSize)
|
2015-05-16 08:51:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 13:31:18 +08:00
|
|
|
func chunkIndex(cs chunkSpec, chunkSize pp.Integer) int {
|
2015-05-16 08:51:48 +08:00
|
|
|
return int(cs.Begin / chunkSize)
|
2014-09-14 01:50:15 +08:00
|
|
|
}
|
|
|
|
|
2015-05-16 08:51:48 +08:00
|
|
|
// TODO: This should probably be called wantPiece.
|
2014-07-24 11:42:31 +08:00
|
|
|
func (t *torrent) wantChunk(r request) bool {
|
|
|
|
if !t.wantPiece(int(r.Index)) {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-15 13:31:18 +08:00
|
|
|
if t.Pieces[r.Index].pendingChunk(r.chunkSpec, t.chunkSize) {
|
2015-04-14 21:59:41 +08:00
|
|
|
return true
|
|
|
|
}
|
2015-05-16 08:51:48 +08:00
|
|
|
_, ok := t.urgent[r]
|
2014-07-24 11:42:31 +08:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-04-14 21:59:41 +08:00
|
|
|
func (t *torrent) urgentChunkInPiece(piece int) bool {
|
2015-06-16 14:57:47 +08:00
|
|
|
p := pp.Integer(piece)
|
2015-04-14 21:59:41 +08:00
|
|
|
for req := range t.urgent {
|
2015-06-16 14:57:47 +08:00
|
|
|
if req.Index == p {
|
2015-04-14 21:59:41 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-16 08:51:48 +08:00
|
|
|
// TODO: This should be called wantPieceIndex.
|
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
|
|
|
|
}
|
2015-10-16 19:10:03 +08:00
|
|
|
p := &t.Pieces[index]
|
2015-04-14 21:59:41 +08:00
|
|
|
if p.QueuedForHash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p.Hashing {
|
|
|
|
return false
|
|
|
|
}
|
2015-06-01 16:22:12 +08:00
|
|
|
if p.Priority == PiecePriorityNone {
|
2015-04-14 21:59:41 +08:00
|
|
|
if !t.urgentChunkInPiece(index) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Put piece complete check last, since it's the slowest as it can involve
|
|
|
|
// calling out into external data stores.
|
|
|
|
return !t.pieceComplete(index)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
2014-09-14 02:06:17 +08:00
|
|
|
|
|
|
|
func (t *torrent) connHasWantedPieces(c *connection) bool {
|
2015-09-20 09:29:52 +08:00
|
|
|
return c.pieceRequestOrder != nil && !c.pieceRequestOrder.Empty()
|
2014-09-14 02:06:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-06-29 22:45:26 +08:00
|
|
|
|
|
|
|
func (t *torrent) worstBadConn(cl *Client) *connection {
|
|
|
|
wcs := t.worstConns(cl)
|
|
|
|
heap.Init(wcs)
|
2015-08-03 23:32:45 +08:00
|
|
|
for wcs.Len() != 0 {
|
2015-06-29 22:45:26 +08:00
|
|
|
c := heap.Pop(wcs).(*connection)
|
2015-08-03 23:32:45 +08:00
|
|
|
if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if wcs.Len() >= (socketsPerTorrent+1)/2 {
|
|
|
|
// Give connections 1 minute to prove themselves.
|
|
|
|
if time.Since(c.completedHandshake) > time.Minute {
|
|
|
|
return c
|
|
|
|
}
|
2015-06-29 22:45:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-06 10:33:22 +08:00
|
|
|
|
|
|
|
func (t *torrent) publishPieceChange(piece int) {
|
|
|
|
cur := t.pieceState(piece)
|
2015-10-16 19:10:03 +08:00
|
|
|
p := &t.Pieces[piece]
|
2015-09-06 10:33:22 +08:00
|
|
|
if cur != p.PublicPieceState {
|
|
|
|
t.pieceStateChanges.Publish(piece)
|
|
|
|
}
|
|
|
|
p.PublicPieceState = cur
|
|
|
|
}
|