2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-11-21 14:04:07 +08:00
|
|
|
"bufio"
|
2015-06-29 17:37:52 +08:00
|
|
|
"bytes"
|
2014-04-03 20:16:59 +08:00
|
|
|
"container/list"
|
|
|
|
"encoding"
|
2014-07-16 15:09:30 +08:00
|
|
|
"errors"
|
2014-08-24 01:04:07 +08:00
|
|
|
"expvar"
|
2014-06-26 15:29:12 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-04-03 20:16:59 +08:00
|
|
|
"net"
|
2016-02-09 21:45:47 +08:00
|
|
|
"strconv"
|
2014-04-03 20:16:59 +08:00
|
|
|
"time"
|
|
|
|
|
2016-02-01 18:08:52 +08:00
|
|
|
"github.com/anacrolix/missinggo"
|
2016-03-22 09:07:03 +08:00
|
|
|
"github.com/anacrolix/missinggo/bitmap"
|
2016-02-01 18:11:41 +08:00
|
|
|
"github.com/anacrolix/missinggo/prioritybitmap"
|
2016-03-22 09:07:03 +08:00
|
|
|
"github.com/bradfitz/iter"
|
2016-02-01 18:11:41 +08:00
|
|
|
|
2015-04-27 12:55:01 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-03-20 13:37:44 +08:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2014-04-03 20:16:59 +08:00
|
|
|
)
|
|
|
|
|
2014-08-24 01:04:07 +08:00
|
|
|
var optimizedCancels = expvar.NewInt("optimizedCancels")
|
|
|
|
|
2014-07-16 15:06:18 +08:00
|
|
|
type peerSource byte
|
|
|
|
|
|
|
|
const (
|
2015-09-28 13:30:13 +08:00
|
|
|
peerSourceTracker = '\x00' // It's the default.
|
2014-07-16 15:06:18 +08:00
|
|
|
peerSourceIncoming = 'I'
|
|
|
|
peerSourceDHT = 'H'
|
|
|
|
peerSourcePEX = 'X'
|
|
|
|
)
|
|
|
|
|
2014-04-03 20:16:59 +08:00
|
|
|
// Maintains the state of a connection with a peer.
|
2014-04-09 00:36:05 +08:00
|
|
|
type connection struct {
|
2016-04-03 16:40:43 +08:00
|
|
|
t *Torrent
|
2015-03-13 03:21:13 +08:00
|
|
|
conn net.Conn
|
|
|
|
rw io.ReadWriter // The real slim shady
|
2015-03-18 15:28:13 +08:00
|
|
|
encrypted bool
|
2014-12-03 08:22:38 +08:00
|
|
|
Discovery peerSource
|
|
|
|
uTP bool
|
2016-02-01 18:08:52 +08:00
|
|
|
closed missinggo.Event
|
2014-12-03 08:22:38 +08:00
|
|
|
post chan pp.Message
|
|
|
|
writeCh chan []byte
|
|
|
|
|
2014-09-11 18:30:13 +08:00
|
|
|
UnwantedChunksReceived int
|
|
|
|
UsefulChunksReceived int
|
2015-05-15 06:39:53 +08:00
|
|
|
chunksSent int
|
2014-08-28 07:32:49 +08:00
|
|
|
|
|
|
|
lastMessageReceived time.Time
|
|
|
|
completedHandshake time.Time
|
|
|
|
lastUsefulChunkReceived time.Time
|
2015-06-16 14:57:47 +08:00
|
|
|
lastChunkSent time.Time
|
2014-08-28 07:32:49 +08:00
|
|
|
|
2014-04-03 20:16:59 +08:00
|
|
|
// Stuff controlled by the local peer.
|
2014-12-05 14:58:43 +08:00
|
|
|
Interested bool
|
|
|
|
Choked bool
|
|
|
|
Requests map[request]struct{}
|
|
|
|
requestsLowWater int
|
2015-03-27 12:36:59 +08:00
|
|
|
// Indexed by metadata piece, set to true if posted and pending a
|
|
|
|
// response.
|
|
|
|
metadataRequests []bool
|
2016-01-04 19:37:49 +08:00
|
|
|
sentHaves []bool
|
2014-04-03 20:16:59 +08:00
|
|
|
|
|
|
|
// Stuff controlled by the remote peer.
|
2014-08-21 16:12:49 +08:00
|
|
|
PeerID [20]byte
|
|
|
|
PeerInterested bool
|
|
|
|
PeerChoked bool
|
|
|
|
PeerRequests map[request]struct{}
|
|
|
|
PeerExtensionBytes peerExtensionBytes
|
2016-03-22 09:07:03 +08:00
|
|
|
// The pieces the peer has claimed to have.
|
|
|
|
peerPieces bitmap.Bitmap
|
|
|
|
// The peer has everything. This can occur due to a special message, when
|
|
|
|
// we may not even know the number of pieces in the torrent yet.
|
2015-03-12 17:06:23 +08:00
|
|
|
peerHasAll bool
|
2016-03-22 09:07:03 +08:00
|
|
|
// The highest possible number of pieces the torrent could have based on
|
|
|
|
// communication with the peer. Generally only useful until we have the
|
|
|
|
// torrent info.
|
|
|
|
peerMinPieces int
|
2015-08-03 23:12:09 +08:00
|
|
|
// Pieces we've accepted chunks for from the peer.
|
|
|
|
peerTouchedPieces map[int]struct{}
|
2015-03-12 17:06:23 +08:00
|
|
|
|
2014-06-26 22:57:07 +08:00
|
|
|
PeerMaxRequests int // Maximum pending requests the peer allows.
|
2015-03-27 12:36:59 +08:00
|
|
|
PeerExtensionIDs map[string]byte
|
2014-06-29 16:57:49 +08:00
|
|
|
PeerClientName string
|
2016-02-01 18:11:41 +08:00
|
|
|
|
|
|
|
pieceInclination []int
|
|
|
|
pieceRequestOrder prioritybitmap.PriorityBitmap
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-03-18 15:28:13 +08:00
|
|
|
func newConnection() (c *connection) {
|
2014-08-21 16:12:49 +08:00
|
|
|
c = &connection{
|
2015-03-18 15:28:13 +08:00
|
|
|
Choked: true,
|
|
|
|
PeerChoked: true,
|
|
|
|
PeerMaxRequests: 250,
|
2014-08-21 16:12:49 +08:00
|
|
|
|
|
|
|
writeCh: make(chan []byte),
|
|
|
|
post: make(chan pp.Message),
|
|
|
|
}
|
|
|
|
return
|
2014-07-18 00:37:33 +08:00
|
|
|
}
|
|
|
|
|
2015-03-13 03:21:13 +08:00
|
|
|
func (cn *connection) remoteAddr() net.Addr {
|
|
|
|
return cn.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) localAddr() net.Addr {
|
|
|
|
return cn.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
2014-12-03 15:07:50 +08:00
|
|
|
func (cn *connection) supportsExtension(ext string) bool {
|
|
|
|
_, ok := cn.PeerExtensionIDs[ext]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-03-22 09:07:03 +08:00
|
|
|
// The best guess at number of pieces in the torrent for this peer.
|
|
|
|
func (cn *connection) bestPeerNumPieces() int {
|
|
|
|
if cn.t.haveInfo() {
|
|
|
|
return cn.t.numPieces()
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
2016-03-22 09:07:03 +08:00
|
|
|
return cn.peerMinPieces
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) completedString() string {
|
|
|
|
return fmt.Sprintf("%d/%d", cn.peerPieces.Len(), cn.bestPeerNumPieces())
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-16 15:09:30 +08:00
|
|
|
// Correct the PeerPieces slice length. Return false if the existing slice is
|
|
|
|
// invalid, such as by receiving badly sized BITFIELD, or invalid HAVE
|
|
|
|
// messages.
|
|
|
|
func (cn *connection) setNumPieces(num int) error {
|
2016-03-22 09:07:03 +08:00
|
|
|
cn.peerPieces.RemoveRange(num, -1)
|
|
|
|
cn.peerPiecesChanged()
|
2014-07-16 15:09:30 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-11 18:30:13 +08:00
|
|
|
func eventAgeString(t time.Time) string {
|
|
|
|
if t.IsZero() {
|
|
|
|
return "never"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.2fs ago", time.Now().Sub(t).Seconds())
|
|
|
|
}
|
|
|
|
|
2015-08-02 02:06:22 +08:00
|
|
|
func (cn *connection) connectionFlags() (ret string) {
|
2014-06-26 15:29:12 +08:00
|
|
|
c := func(b byte) {
|
2015-03-18 15:38:29 +08:00
|
|
|
ret += string([]byte{b})
|
2014-12-09 14:26:06 +08:00
|
|
|
}
|
2015-03-18 15:38:29 +08:00
|
|
|
if cn.encrypted {
|
|
|
|
c('E')
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
2014-07-16 15:06:18 +08:00
|
|
|
if cn.Discovery != 0 {
|
|
|
|
c(byte(cn.Discovery))
|
2014-06-30 22:04:28 +08:00
|
|
|
}
|
2014-11-17 03:29:31 +08:00
|
|
|
if cn.uTP {
|
|
|
|
c('T')
|
|
|
|
}
|
2015-08-02 02:06:22 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inspired by https://trac.transmissionbt.com/wiki/PeerStatusText
|
|
|
|
func (cn *connection) statusFlags() (ret string) {
|
|
|
|
c := func(b byte) {
|
|
|
|
ret += string([]byte{b})
|
|
|
|
}
|
|
|
|
if cn.Interested {
|
|
|
|
c('i')
|
|
|
|
}
|
|
|
|
if cn.Choked {
|
|
|
|
c('c')
|
|
|
|
}
|
|
|
|
c('-')
|
|
|
|
ret += cn.connectionFlags()
|
2015-03-18 15:38:29 +08:00
|
|
|
c('-')
|
|
|
|
if cn.PeerInterested {
|
|
|
|
c('i')
|
|
|
|
}
|
|
|
|
if cn.PeerChoked {
|
|
|
|
c('c')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-29 17:37:52 +08:00
|
|
|
func (cn *connection) String() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
cn.WriteStatus(&buf, nil)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-04-03 16:40:43 +08:00
|
|
|
func (cn *connection) WriteStatus(w io.Writer, t *Torrent) {
|
2015-03-18 15:38:29 +08:00
|
|
|
// \t isn't preserved in <pre> blocks?
|
2015-06-22 17:46:26 +08:00
|
|
|
fmt.Fprintf(w, "%+q: %s-%s\n", cn.PeerID, cn.localAddr(), cn.remoteAddr())
|
2015-03-18 15:38:29 +08:00
|
|
|
fmt.Fprintf(w, " last msg: %s, connected: %s, last useful chunk: %s\n",
|
|
|
|
eventAgeString(cn.lastMessageReceived),
|
|
|
|
eventAgeString(cn.completedHandshake),
|
|
|
|
eventAgeString(cn.lastUsefulChunkReceived))
|
2015-05-15 06:39:53 +08:00
|
|
|
fmt.Fprintf(w,
|
2015-08-03 23:12:09 +08:00
|
|
|
" %s completed, %d pieces touched, good chunks: %d/%d-%d reqq: %d-%d, flags: %s\n",
|
2016-03-22 09:07:03 +08:00
|
|
|
cn.completedString(),
|
2015-08-03 23:12:09 +08:00
|
|
|
len(cn.peerTouchedPieces),
|
2015-03-18 15:38:29 +08:00
|
|
|
cn.UsefulChunksReceived,
|
|
|
|
cn.UnwantedChunksReceived+cn.UsefulChunksReceived,
|
2015-05-15 06:39:53 +08:00
|
|
|
cn.chunksSent,
|
2015-03-18 15:38:29 +08:00
|
|
|
len(cn.Requests),
|
|
|
|
len(cn.PeerRequests),
|
2015-05-15 06:39:53 +08:00
|
|
|
cn.statusFlags(),
|
|
|
|
)
|
2014-06-26 15:29:12 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) Close() {
|
|
|
|
cn.closed.Set()
|
|
|
|
cn.discardPieceInclination()
|
|
|
|
cn.pieceRequestOrder.Clear()
|
2014-11-19 11:53:57 +08:00
|
|
|
// TODO: This call blocks sometimes, why?
|
2016-04-19 12:11:11 +08:00
|
|
|
go cn.conn.Close()
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) PeerHasPiece(piece int) bool {
|
|
|
|
return cn.peerHasAll || cn.peerPieces.Contains(piece)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) Post(msg pp.Message) {
|
2014-08-21 16:12:49 +08:00
|
|
|
select {
|
2016-04-19 12:11:11 +08:00
|
|
|
case cn.post <- msg:
|
2016-02-09 21:45:47 +08:00
|
|
|
postedMessageTypes.Add(strconv.FormatInt(int64(msg.Type), 10), 1)
|
2016-04-19 12:11:11 +08:00
|
|
|
case <-cn.closed.C():
|
2014-08-21 16:12:49 +08:00
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) RequestPending(r request) bool {
|
|
|
|
_, ok := cn.Requests[r]
|
2014-05-23 19:01:05 +08:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) requestMetadataPiece(index int) {
|
|
|
|
eID := cn.PeerExtensionIDs["ut_metadata"]
|
2015-03-27 12:36:59 +08:00
|
|
|
if eID == 0 {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if index < len(cn.metadataRequests) && cn.metadataRequests[index] {
|
2015-03-27 12:36:59 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Post(pp.Message{
|
2015-03-27 12:36:59 +08:00
|
|
|
Type: pp.Extended,
|
|
|
|
ExtendedID: eID,
|
|
|
|
ExtendedPayload: func() []byte {
|
|
|
|
b, err := bencode.Marshal(map[string]int{
|
|
|
|
"msg_type": pp.RequestMetadataExtensionMsgType,
|
|
|
|
"piece": index,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}(),
|
|
|
|
})
|
2016-04-19 12:11:11 +08:00
|
|
|
for index >= len(cn.metadataRequests) {
|
|
|
|
cn.metadataRequests = append(cn.metadataRequests, false)
|
2015-03-27 12:36:59 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.metadataRequests[index] = true
|
2015-03-27 12:36:59 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) requestedMetadataPiece(index int) bool {
|
|
|
|
return index < len(cn.metadataRequests) && cn.metadataRequests[index]
|
2015-03-27 12:36:59 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 13:09:12 +08:00
|
|
|
// The actual value to use as the maximum outbound requests.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) nominalMaxRequests() (ret int) {
|
|
|
|
ret = cn.PeerMaxRequests
|
2016-02-08 13:09:12 +08:00
|
|
|
if ret > 64 {
|
|
|
|
ret = 64
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:16:59 +08:00
|
|
|
// Returns true if more requests can be sent.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) Request(chunk request) bool {
|
|
|
|
if len(cn.Requests) >= cn.nominalMaxRequests() {
|
2014-04-03 20:16:59 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if !cn.PeerHasPiece(int(chunk.Index)) {
|
2014-04-03 20:16:59 +08:00
|
|
|
return true
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if cn.RequestPending(chunk) {
|
2014-06-28 17:38:31 +08:00
|
|
|
return true
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.SetInterested(true)
|
|
|
|
if cn.PeerChoked {
|
2014-04-03 20:16:59 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if cn.Requests == nil {
|
|
|
|
cn.Requests = make(map[request]struct{}, cn.PeerMaxRequests)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Requests[chunk] = struct{}{}
|
|
|
|
cn.requestsLowWater = len(cn.Requests) / 2
|
|
|
|
cn.Post(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: pp.Request,
|
2014-05-23 19:01:05 +08:00
|
|
|
Index: chunk.Index,
|
|
|
|
Begin: chunk.Begin,
|
|
|
|
Length: chunk.Length,
|
|
|
|
})
|
2014-04-03 20:16:59 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-21 15:49:28 +08:00
|
|
|
// Returns true if an unsatisfied request was canceled.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) Cancel(r request) bool {
|
|
|
|
if cn.Requests == nil {
|
2014-05-21 15:49:28 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if _, ok := cn.Requests[r]; !ok {
|
2014-05-21 15:49:28 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
delete(cn.Requests, r)
|
|
|
|
cn.Post(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: pp.Cancel,
|
2014-05-21 15:49:28 +08:00
|
|
|
Index: r.Index,
|
|
|
|
Begin: r.Begin,
|
|
|
|
Length: r.Length,
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-04-16 15:33:33 +08:00
|
|
|
// Returns true if an unsatisfied request was canceled.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) PeerCancel(r request) bool {
|
|
|
|
if cn.PeerRequests == nil {
|
2014-04-16 15:33:33 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if _, ok := cn.PeerRequests[r]; !ok {
|
2014-04-16 15:33:33 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
delete(cn.PeerRequests, r)
|
2014-04-16 15:33:33 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) Choke() {
|
|
|
|
if cn.Choked {
|
2014-05-21 15:49:28 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Post(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: pp.Choke,
|
2014-05-21 15:49:28 +08:00
|
|
|
})
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.PeerRequests = nil
|
|
|
|
cn.Choked = true
|
2014-05-21 15:49:28 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) Unchoke() {
|
|
|
|
if !cn.Choked {
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Post(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: pp.Unchoke,
|
2014-04-03 20:16:59 +08:00
|
|
|
})
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Choked = false
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) SetInterested(interested bool) {
|
|
|
|
if cn.Interested == interested {
|
2014-04-03 20:16:59 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Post(pp.Message{
|
2014-08-21 16:12:49 +08:00
|
|
|
Type: func() pp.MessageType {
|
2014-04-03 20:16:59 +08:00
|
|
|
if interested {
|
2014-08-21 16:12:49 +08:00
|
|
|
return pp.Interested
|
2014-04-03 20:16:59 +08:00
|
|
|
} else {
|
2014-08-21 16:12:49 +08:00
|
|
|
return pp.NotInterested
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.Interested = interested
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2015-08-02 18:39:31 +08:00
|
|
|
var (
|
|
|
|
// Track connection writer buffer writes and flushes, to determine its
|
|
|
|
// efficiency.
|
|
|
|
connectionWriterFlush = expvar.NewInt("connectionWriterFlush")
|
|
|
|
connectionWriterWrite = expvar.NewInt("connectionWriterWrite")
|
|
|
|
)
|
2015-08-02 12:30:33 +08:00
|
|
|
|
2014-05-28 23:27:48 +08:00
|
|
|
// Writes buffers to the socket from the write channel.
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) writer() {
|
2016-02-01 21:45:26 +08:00
|
|
|
defer func() {
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.t.cl.mu.Lock()
|
|
|
|
defer cn.t.cl.mu.Unlock()
|
|
|
|
cn.Close()
|
2016-02-01 21:45:26 +08:00
|
|
|
}()
|
2014-11-21 14:04:07 +08:00
|
|
|
// Reduce write syscalls.
|
2016-04-19 12:11:11 +08:00
|
|
|
buf := bufio.NewWriter(cn.rw)
|
2014-08-21 16:12:49 +08:00
|
|
|
for {
|
2015-08-02 18:39:31 +08:00
|
|
|
if buf.Buffered() == 0 {
|
|
|
|
// There's nothing to write, so block until we get something.
|
2014-11-21 14:04:07 +08:00
|
|
|
select {
|
2016-04-19 12:11:11 +08:00
|
|
|
case b, ok := <-cn.writeCh:
|
2015-08-02 18:39:31 +08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionWriterWrite.Add(1)
|
|
|
|
_, err := buf.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
case <-cn.closed.C():
|
2014-08-21 16:12:49 +08:00
|
|
|
return
|
2014-05-28 23:27:48 +08:00
|
|
|
}
|
2015-08-02 18:39:31 +08:00
|
|
|
} else {
|
|
|
|
// We already have something to write, so flush if there's nothing
|
|
|
|
// more to write.
|
|
|
|
select {
|
2016-04-19 12:11:11 +08:00
|
|
|
case b, ok := <-cn.writeCh:
|
2015-08-02 18:39:31 +08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionWriterWrite.Add(1)
|
|
|
|
_, err := buf.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
case <-cn.closed.C():
|
2014-11-21 14:04:07 +08:00
|
|
|
return
|
2015-08-02 18:39:31 +08:00
|
|
|
default:
|
|
|
|
connectionWriterFlush.Add(1)
|
|
|
|
err := buf.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-21 14:04:07 +08:00
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) writeOptimizer(keepAliveDelay time.Duration) {
|
|
|
|
defer close(cn.writeCh) // Responsible for notifying downstream routines.
|
|
|
|
pending := list.New() // Message queue.
|
|
|
|
var nextWrite []byte // Set to nil if we need to need to marshal the next message.
|
2014-05-28 23:27:48 +08:00
|
|
|
timer := time.NewTimer(keepAliveDelay)
|
|
|
|
defer timer.Stop()
|
|
|
|
lastWrite := time.Now()
|
2014-04-03 20:16:59 +08:00
|
|
|
for {
|
2016-04-19 12:11:11 +08:00
|
|
|
write := cn.writeCh // Set to nil if there's nothing to write.
|
2014-04-03 20:16:59 +08:00
|
|
|
if pending.Len() == 0 {
|
|
|
|
write = nil
|
2014-05-28 23:27:48 +08:00
|
|
|
} else if nextWrite == nil {
|
2014-04-03 20:16:59 +08:00
|
|
|
var err error
|
|
|
|
nextWrite, err = pending.Front().Value.(encoding.BinaryMarshaler).MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 23:27:48 +08:00
|
|
|
event:
|
2014-04-03 20:16:59 +08:00
|
|
|
select {
|
2014-05-28 23:27:48 +08:00
|
|
|
case <-timer.C:
|
|
|
|
if pending.Len() != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
keepAliveTime := lastWrite.Add(keepAliveDelay)
|
|
|
|
if time.Now().Before(keepAliveTime) {
|
|
|
|
timer.Reset(keepAliveTime.Sub(time.Now()))
|
|
|
|
break
|
|
|
|
}
|
2014-08-21 16:12:49 +08:00
|
|
|
pending.PushBack(pp.Message{Keepalive: true})
|
2016-02-09 21:45:47 +08:00
|
|
|
postedKeepalives.Add(1)
|
2016-04-19 12:11:11 +08:00
|
|
|
case msg, ok := <-cn.post:
|
2014-04-03 20:16:59 +08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2014-08-21 16:12:49 +08:00
|
|
|
if msg.Type == pp.Cancel {
|
2014-05-28 23:27:48 +08:00
|
|
|
for e := pending.Back(); e != nil; e = e.Prev() {
|
2014-08-21 16:12:49 +08:00
|
|
|
elemMsg := e.Value.(pp.Message)
|
|
|
|
if elemMsg.Type == pp.Request && msg.Index == elemMsg.Index && msg.Begin == elemMsg.Begin && msg.Length == elemMsg.Length {
|
2014-05-28 23:27:48 +08:00
|
|
|
pending.Remove(e)
|
2014-08-24 01:04:07 +08:00
|
|
|
optimizedCancels.Add(1)
|
2014-05-28 23:27:48 +08:00
|
|
|
break event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
pending.PushBack(msg)
|
|
|
|
case write <- nextWrite:
|
|
|
|
pending.Remove(pending.Front())
|
2014-05-28 23:27:48 +08:00
|
|
|
nextWrite = nil
|
|
|
|
lastWrite = time.Now()
|
|
|
|
if pending.Len() == 0 {
|
|
|
|
timer.Reset(keepAliveDelay)
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
case <-cn.closed.C():
|
2014-08-21 16:12:49 +08:00
|
|
|
return
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 19:37:49 +08:00
|
|
|
|
|
|
|
func (cn *connection) Have(piece int) {
|
|
|
|
for piece >= len(cn.sentHaves) {
|
|
|
|
cn.sentHaves = append(cn.sentHaves, false)
|
|
|
|
}
|
|
|
|
if cn.sentHaves[piece] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cn.Post(pp.Message{
|
|
|
|
Type: pp.Have,
|
|
|
|
Index: pp.Integer(piece),
|
|
|
|
})
|
|
|
|
cn.sentHaves[piece] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) Bitfield(haves []bool) {
|
2016-01-04 19:56:36 +08:00
|
|
|
if cn.sentHaves != nil {
|
2016-01-04 19:37:49 +08:00
|
|
|
panic("bitfield must be first have-related message sent")
|
|
|
|
}
|
|
|
|
cn.Post(pp.Message{
|
|
|
|
Type: pp.Bitfield,
|
|
|
|
Bitfield: haves,
|
|
|
|
})
|
|
|
|
cn.sentHaves = haves
|
|
|
|
}
|
2016-01-24 12:21:17 +08:00
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) updateRequests() {
|
|
|
|
if !cn.t.haveInfo() {
|
2016-01-24 12:21:17 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if cn.Interested {
|
|
|
|
if cn.PeerChoked {
|
2016-01-24 12:21:17 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if len(cn.Requests) > cn.requestsLowWater {
|
2016-01-24 12:21:17 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.fillRequests()
|
|
|
|
if len(cn.Requests) == 0 && !cn.PeerChoked {
|
2016-01-24 12:21:17 +08:00
|
|
|
// So we're not choked, but we don't want anything right now. We may
|
|
|
|
// have completed readahead, and the readahead window has not rolled
|
|
|
|
// over to the next piece. Better to stay interested in case we're
|
|
|
|
// going to want data in the near future.
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.SetInterested(!cn.t.haveAllPieces())
|
2016-01-24 12:21:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) fillRequests() {
|
|
|
|
cn.pieceRequestOrder.IterTyped(func(piece int) (more bool) {
|
|
|
|
if cn.t.cl.config.Debug && cn.t.havePiece(piece) {
|
2016-02-15 14:47:04 +08:00
|
|
|
panic(piece)
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return cn.requestPiecePendingChunks(piece)
|
2016-02-01 18:11:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) requestPiecePendingChunks(piece int) (again bool) {
|
|
|
|
return cn.t.connRequestPiecePendingChunks(cn, piece)
|
2016-02-01 18:11:41 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) stopRequestingPiece(piece int) {
|
|
|
|
cn.pieceRequestOrder.Remove(piece)
|
2016-02-01 18:11:41 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) updatePiecePriority(piece int) {
|
|
|
|
tpp := cn.t.piecePriority(piece)
|
|
|
|
if !cn.PeerHasPiece(piece) {
|
2016-02-09 21:47:53 +08:00
|
|
|
tpp = PiecePriorityNone
|
2016-01-24 12:21:17 +08:00
|
|
|
}
|
2016-02-01 18:11:41 +08:00
|
|
|
if tpp == PiecePriorityNone {
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.stopRequestingPiece(piece)
|
2016-02-01 18:11:41 +08:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
prio := cn.getPieceInclination()[piece]
|
2016-02-01 18:11:41 +08:00
|
|
|
switch tpp {
|
|
|
|
case PiecePriorityNormal:
|
|
|
|
case PiecePriorityReadahead:
|
2016-04-19 12:11:11 +08:00
|
|
|
prio -= cn.t.numPieces()
|
2016-02-01 18:11:41 +08:00
|
|
|
case PiecePriorityNext, PiecePriorityNow:
|
2016-04-19 12:11:11 +08:00
|
|
|
prio -= 2 * cn.t.numPieces()
|
2016-02-01 18:11:41 +08:00
|
|
|
default:
|
|
|
|
panic(tpp)
|
|
|
|
}
|
2016-03-19 14:40:18 +08:00
|
|
|
prio += piece
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.pieceRequestOrder.Set(piece, prio)
|
|
|
|
cn.updateRequests()
|
2016-02-01 18:11:41 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) getPieceInclination() []int {
|
|
|
|
if cn.pieceInclination == nil {
|
|
|
|
cn.pieceInclination = cn.t.getConnPieceInclination()
|
2016-02-01 18:11:41 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
return cn.pieceInclination
|
2016-02-01 18:11:41 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) discardPieceInclination() {
|
|
|
|
if cn.pieceInclination == nil {
|
2016-02-01 18:11:41 +08:00
|
|
|
return
|
2016-01-24 12:21:17 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.t.putPieceInclination(cn.pieceInclination)
|
|
|
|
cn.pieceInclination = nil
|
2016-01-24 12:21:17 +08:00
|
|
|
}
|
2016-03-21 11:17:15 +08:00
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) peerHasPieceChanged(piece int) {
|
|
|
|
cn.updatePiecePriority(piece)
|
2016-03-22 09:07:03 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) peerPiecesChanged() {
|
|
|
|
if cn.t.haveInfo() {
|
|
|
|
for i := range iter.N(cn.t.numPieces()) {
|
|
|
|
cn.peerHasPieceChanged(i)
|
2016-03-21 11:17:15 +08:00
|
|
|
}
|
2016-03-22 09:07:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) raisePeerMinPieces(newMin int) {
|
|
|
|
if newMin > cn.peerMinPieces {
|
|
|
|
cn.peerMinPieces = newMin
|
2016-03-22 09:07:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) peerSentHave(piece int) error {
|
|
|
|
if cn.t.haveInfo() && piece >= cn.t.numPieces() {
|
2016-03-22 09:07:03 +08:00
|
|
|
return errors.New("invalid piece")
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
if cn.PeerHasPiece(piece) {
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.raisePeerMinPieces(piece + 1)
|
|
|
|
cn.peerPieces.Set(piece, true)
|
|
|
|
cn.peerHasPieceChanged(piece)
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) peerSentBitfield(bf []bool) error {
|
|
|
|
cn.peerHasAll = false
|
2016-03-22 09:07:03 +08:00
|
|
|
if len(bf)%8 != 0 {
|
|
|
|
panic("expected bitfield length divisible by 8")
|
|
|
|
}
|
|
|
|
// We know that the last byte means that at most the last 7 bits are
|
|
|
|
// wasted.
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.raisePeerMinPieces(len(bf) - 7)
|
2016-04-30 02:08:02 +08:00
|
|
|
if cn.t.haveInfo() && len(bf) > cn.t.numPieces() {
|
2016-03-22 09:07:03 +08:00
|
|
|
// Ignore known excess pieces.
|
2016-04-19 12:11:11 +08:00
|
|
|
bf = bf[:cn.t.numPieces()]
|
2016-03-22 09:07:03 +08:00
|
|
|
}
|
|
|
|
for i, have := range bf {
|
|
|
|
if have {
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.raisePeerMinPieces(i + 1)
|
2016-03-21 11:17:15 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.peerPieces.Set(i, have)
|
2016-03-21 11:17:15 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
cn.peerPiecesChanged()
|
2016-03-22 09:07:03 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) peerSentHaveAll() error {
|
|
|
|
cn.peerHasAll = true
|
|
|
|
cn.peerPieces.Clear()
|
|
|
|
cn.peerPiecesChanged()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (cn *connection) peerSentHaveNone() error {
|
|
|
|
cn.peerPieces.Clear()
|
|
|
|
cn.peerHasAll = false
|
|
|
|
cn.peerPiecesChanged()
|
2016-03-21 11:17:15 +08:00
|
|
|
return nil
|
|
|
|
}
|