2014-04-03 12:16:59 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-11-21 06:04:07 +00:00
|
|
|
"bufio"
|
2015-06-29 09:37:52 +00:00
|
|
|
"bytes"
|
2014-04-03 12:16:59 +00:00
|
|
|
"container/list"
|
|
|
|
"encoding"
|
2014-07-16 07:09:30 +00:00
|
|
|
"errors"
|
2014-08-23 17:04:07 +00:00
|
|
|
"expvar"
|
2014-06-26 07:29:12 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-04-03 12:16:59 +00:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2016-02-01 10:08:52 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
2016-02-01 10:11:41 +00:00
|
|
|
"github.com/anacrolix/missinggo/itertools"
|
|
|
|
"github.com/anacrolix/missinggo/prioritybitmap"
|
|
|
|
|
2015-04-27 04:55:01 +00:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2015-03-20 05:37:44 +00:00
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
2014-04-03 12:16:59 +00:00
|
|
|
)
|
|
|
|
|
2014-08-23 17:04:07 +00:00
|
|
|
var optimizedCancels = expvar.NewInt("optimizedCancels")
|
|
|
|
|
2014-07-16 07:06:18 +00:00
|
|
|
type peerSource byte
|
|
|
|
|
|
|
|
const (
|
2015-09-28 05:30:13 +00:00
|
|
|
peerSourceTracker = '\x00' // It's the default.
|
2014-07-16 07:06:18 +00:00
|
|
|
peerSourceIncoming = 'I'
|
|
|
|
peerSourceDHT = 'H'
|
|
|
|
peerSourcePEX = 'X'
|
|
|
|
)
|
|
|
|
|
2014-04-03 12:16:59 +00:00
|
|
|
// Maintains the state of a connection with a peer.
|
2014-04-08 16:36:05 +00:00
|
|
|
type connection struct {
|
2016-01-24 04:21:17 +00:00
|
|
|
t *torrent
|
2015-03-12 19:21:13 +00:00
|
|
|
conn net.Conn
|
|
|
|
rw io.ReadWriter // The real slim shady
|
2015-03-18 07:28:13 +00:00
|
|
|
encrypted bool
|
2014-12-03 00:22:38 +00:00
|
|
|
Discovery peerSource
|
|
|
|
uTP bool
|
2016-02-01 10:08:52 +00:00
|
|
|
closed missinggo.Event
|
2014-12-03 00:22:38 +00:00
|
|
|
post chan pp.Message
|
|
|
|
writeCh chan []byte
|
|
|
|
|
2014-09-11 10:30:13 +00:00
|
|
|
UnwantedChunksReceived int
|
|
|
|
UsefulChunksReceived int
|
2015-05-14 22:39:53 +00:00
|
|
|
chunksSent int
|
2014-08-27 23:32:49 +00:00
|
|
|
|
|
|
|
lastMessageReceived time.Time
|
|
|
|
completedHandshake time.Time
|
|
|
|
lastUsefulChunkReceived time.Time
|
2015-06-16 06:57:47 +00:00
|
|
|
lastChunkSent time.Time
|
2014-08-27 23:32:49 +00:00
|
|
|
|
2014-04-03 12:16:59 +00:00
|
|
|
// Stuff controlled by the local peer.
|
2014-12-05 06:58:43 +00:00
|
|
|
Interested bool
|
|
|
|
Choked bool
|
|
|
|
Requests map[request]struct{}
|
|
|
|
requestsLowWater int
|
2015-03-27 04:36:59 +00:00
|
|
|
// Indexed by metadata piece, set to true if posted and pending a
|
|
|
|
// response.
|
|
|
|
metadataRequests []bool
|
2016-01-04 11:37:49 +00:00
|
|
|
sentHaves []bool
|
2014-04-03 12:16:59 +00:00
|
|
|
|
|
|
|
// Stuff controlled by the remote peer.
|
2014-08-21 08:12:49 +00:00
|
|
|
PeerID [20]byte
|
|
|
|
PeerInterested bool
|
|
|
|
PeerChoked bool
|
|
|
|
PeerRequests map[request]struct{}
|
|
|
|
PeerExtensionBytes peerExtensionBytes
|
2014-07-16 07:09:30 +00:00
|
|
|
// Whether the peer has the given piece. nil if they've not sent any
|
|
|
|
// related messages yet.
|
2015-03-12 09:06:23 +00:00
|
|
|
PeerPieces []bool
|
|
|
|
peerHasAll bool
|
2015-08-03 15:12:09 +00:00
|
|
|
// Pieces we've accepted chunks for from the peer.
|
|
|
|
peerTouchedPieces map[int]struct{}
|
2015-03-12 09:06:23 +00:00
|
|
|
|
2014-06-26 14:57:07 +00:00
|
|
|
PeerMaxRequests int // Maximum pending requests the peer allows.
|
2015-03-27 04:36:59 +00:00
|
|
|
PeerExtensionIDs map[string]byte
|
2014-06-29 08:57:49 +00:00
|
|
|
PeerClientName string
|
2016-02-01 10:11:41 +00:00
|
|
|
|
|
|
|
pieceInclination []int
|
|
|
|
pieceRequestOrder prioritybitmap.PriorityBitmap
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 07:28:13 +00:00
|
|
|
func newConnection() (c *connection) {
|
2014-08-21 08:12:49 +00:00
|
|
|
c = &connection{
|
2015-03-18 07:28:13 +00:00
|
|
|
Choked: true,
|
|
|
|
PeerChoked: true,
|
|
|
|
PeerMaxRequests: 250,
|
2014-08-21 08:12:49 +00:00
|
|
|
|
|
|
|
writeCh: make(chan []byte),
|
|
|
|
post: make(chan pp.Message),
|
|
|
|
}
|
|
|
|
return
|
2014-07-17 16:37:33 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 19:21:13 +00:00
|
|
|
func (cn *connection) remoteAddr() net.Addr {
|
|
|
|
return cn.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) localAddr() net.Addr {
|
|
|
|
return cn.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
2014-12-03 07:07:50 +00:00
|
|
|
func (cn *connection) supportsExtension(ext string) bool {
|
|
|
|
_, ok := cn.PeerExtensionIDs[ext]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:06:23 +00:00
|
|
|
func (cn *connection) completedString(t *torrent) string {
|
|
|
|
if cn.PeerPieces == nil && !cn.peerHasAll {
|
2014-06-26 07:29:12 +00:00
|
|
|
return "?"
|
|
|
|
}
|
2015-03-12 09:06:23 +00:00
|
|
|
return fmt.Sprintf("%d/%d", func() int {
|
|
|
|
if cn.peerHasAll {
|
|
|
|
if t.haveInfo() {
|
|
|
|
return t.numPieces()
|
|
|
|
}
|
|
|
|
return -1
|
2014-06-26 07:29:12 +00:00
|
|
|
}
|
2015-03-12 09:06:23 +00:00
|
|
|
ret := 0
|
|
|
|
for _, b := range cn.PeerPieces {
|
|
|
|
if b {
|
|
|
|
ret++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}(), func() int {
|
|
|
|
if cn.peerHasAll || cn.PeerPieces == nil {
|
|
|
|
if t.haveInfo() {
|
|
|
|
return t.numPieces()
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return len(cn.PeerPieces)
|
|
|
|
}())
|
2014-06-26 07:29:12 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 07:09:30 +00: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 {
|
2015-03-12 09:06:23 +00:00
|
|
|
if cn.peerHasAll {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-16 07:09:30 +00:00
|
|
|
if cn.PeerPieces == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(cn.PeerPieces) == num {
|
|
|
|
} else if len(cn.PeerPieces) < num {
|
|
|
|
cn.PeerPieces = append(cn.PeerPieces, make([]bool, num-len(cn.PeerPieces))...)
|
2014-08-21 15:26:41 +00:00
|
|
|
} else if len(cn.PeerPieces) <= (num+7)/8*8 {
|
2014-07-16 07:09:30 +00:00
|
|
|
for _, have := range cn.PeerPieces[num:] {
|
|
|
|
if have {
|
|
|
|
return errors.New("peer has invalid piece")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cn.PeerPieces = cn.PeerPieces[:num]
|
|
|
|
} else {
|
2014-08-21 15:26:41 +00:00
|
|
|
return fmt.Errorf("peer bitfield is excessively long: expected %d, have %d", num, len(cn.PeerPieces))
|
2014-07-16 07:09:30 +00:00
|
|
|
}
|
|
|
|
if len(cn.PeerPieces) != num {
|
|
|
|
panic("wat")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-11 10:30:13 +00:00
|
|
|
func eventAgeString(t time.Time) string {
|
|
|
|
if t.IsZero() {
|
|
|
|
return "never"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%.2fs ago", time.Now().Sub(t).Seconds())
|
|
|
|
}
|
|
|
|
|
2015-08-01 18:06:22 +00:00
|
|
|
func (cn *connection) connectionFlags() (ret string) {
|
2014-06-26 07:29:12 +00:00
|
|
|
c := func(b byte) {
|
2015-03-18 07:38:29 +00:00
|
|
|
ret += string([]byte{b})
|
2014-12-09 06:26:06 +00:00
|
|
|
}
|
2015-03-18 07:38:29 +00:00
|
|
|
if cn.encrypted {
|
|
|
|
c('E')
|
2014-06-26 07:29:12 +00:00
|
|
|
}
|
2014-07-16 07:06:18 +00:00
|
|
|
if cn.Discovery != 0 {
|
|
|
|
c(byte(cn.Discovery))
|
2014-06-30 14:04:28 +00:00
|
|
|
}
|
2014-11-16 19:29:31 +00:00
|
|
|
if cn.uTP {
|
|
|
|
c('T')
|
|
|
|
}
|
2015-08-01 18:06:22 +00: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 07:38:29 +00:00
|
|
|
c('-')
|
|
|
|
if cn.PeerInterested {
|
|
|
|
c('i')
|
|
|
|
}
|
|
|
|
if cn.PeerChoked {
|
|
|
|
c('c')
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-29 09:37:52 +00:00
|
|
|
func (cn *connection) String() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
cn.WriteStatus(&buf, nil)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2015-03-18 07:38:29 +00:00
|
|
|
func (cn *connection) WriteStatus(w io.Writer, t *torrent) {
|
|
|
|
// \t isn't preserved in <pre> blocks?
|
2015-06-22 09:46:26 +00:00
|
|
|
fmt.Fprintf(w, "%+q: %s-%s\n", cn.PeerID, cn.localAddr(), cn.remoteAddr())
|
2015-03-18 07:38:29 +00: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-14 22:39:53 +00:00
|
|
|
fmt.Fprintf(w,
|
2015-08-03 15:12:09 +00:00
|
|
|
" %s completed, %d pieces touched, good chunks: %d/%d-%d reqq: %d-%d, flags: %s\n",
|
2015-03-18 07:38:29 +00:00
|
|
|
cn.completedString(t),
|
2015-08-03 15:12:09 +00:00
|
|
|
len(cn.peerTouchedPieces),
|
2015-03-18 07:38:29 +00:00
|
|
|
cn.UsefulChunksReceived,
|
|
|
|
cn.UnwantedChunksReceived+cn.UsefulChunksReceived,
|
2015-05-14 22:39:53 +00:00
|
|
|
cn.chunksSent,
|
2015-03-18 07:38:29 +00:00
|
|
|
len(cn.Requests),
|
|
|
|
len(cn.PeerRequests),
|
2015-05-14 22:39:53 +00:00
|
|
|
cn.statusFlags(),
|
|
|
|
)
|
2014-06-26 07:29:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (c *connection) Close() {
|
2016-02-01 10:08:52 +00:00
|
|
|
c.closed.Set()
|
2016-02-01 10:11:41 +00:00
|
|
|
c.discardPieceInclination()
|
2016-02-01 13:45:26 +00:00
|
|
|
c.pieceRequestOrder.Clear()
|
2014-11-19 03:53:57 +00:00
|
|
|
// TODO: This call blocks sometimes, why?
|
2015-03-12 19:21:13 +00:00
|
|
|
go c.conn.Close()
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 09:06:23 +00:00
|
|
|
func (c *connection) PeerHasPiece(piece int) bool {
|
|
|
|
if c.peerHasAll {
|
|
|
|
return true
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
2015-03-12 09:06:23 +00:00
|
|
|
if piece >= len(c.PeerPieces) {
|
2014-06-30 14:06:58 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-03-12 09:06:23 +00:00
|
|
|
return c.PeerPieces[piece]
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 08:12:49 +00:00
|
|
|
func (c *connection) Post(msg pp.Message) {
|
|
|
|
select {
|
|
|
|
case c.post <- msg:
|
2016-02-01 10:08:52 +00:00
|
|
|
case <-c.closed.C():
|
2014-08-21 08:12:49 +00:00
|
|
|
}
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 11:01:05 +00:00
|
|
|
func (c *connection) RequestPending(r request) bool {
|
|
|
|
_, ok := c.Requests[r]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2015-03-27 04:36:59 +00:00
|
|
|
func (c *connection) requestMetadataPiece(index int) {
|
|
|
|
eID := c.PeerExtensionIDs["ut_metadata"]
|
|
|
|
if eID == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if index < len(c.metadataRequests) && c.metadataRequests[index] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Post(pp.Message{
|
|
|
|
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
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
for index >= len(c.metadataRequests) {
|
|
|
|
c.metadataRequests = append(c.metadataRequests, false)
|
|
|
|
}
|
|
|
|
c.metadataRequests[index] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) requestedMetadataPiece(index int) bool {
|
|
|
|
return index < len(c.metadataRequests) && c.metadataRequests[index]
|
|
|
|
}
|
|
|
|
|
2014-04-03 12:16:59 +00:00
|
|
|
// Returns true if more requests can be sent.
|
2014-04-16 11:13:44 +00:00
|
|
|
func (c *connection) Request(chunk request) bool {
|
2014-05-21 07:47:42 +00:00
|
|
|
if len(c.Requests) >= c.PeerMaxRequests {
|
2014-04-03 12:16:59 +00:00
|
|
|
return false
|
|
|
|
}
|
2015-03-12 09:06:23 +00:00
|
|
|
if !c.PeerHasPiece(int(chunk.Index)) {
|
2014-04-03 12:16:59 +00:00
|
|
|
return true
|
|
|
|
}
|
2014-06-28 09:38:31 +00:00
|
|
|
if c.RequestPending(chunk) {
|
|
|
|
return true
|
|
|
|
}
|
2014-04-03 12:16:59 +00:00
|
|
|
c.SetInterested(true)
|
|
|
|
if c.PeerChoked {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Requests == nil {
|
2014-05-21 07:47:42 +00:00
|
|
|
c.Requests = make(map[request]struct{}, c.PeerMaxRequests)
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
c.Requests[chunk] = struct{}{}
|
2014-12-05 06:58:43 +00:00
|
|
|
c.requestsLowWater = len(c.Requests) / 2
|
2014-08-21 08:12:49 +00:00
|
|
|
c.Post(pp.Message{
|
|
|
|
Type: pp.Request,
|
2014-05-23 11:01:05 +00:00
|
|
|
Index: chunk.Index,
|
|
|
|
Begin: chunk.Begin,
|
|
|
|
Length: chunk.Length,
|
|
|
|
})
|
2014-04-03 12:16:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-21 07:49:28 +00:00
|
|
|
// Returns true if an unsatisfied request was canceled.
|
|
|
|
func (c *connection) Cancel(r request) bool {
|
|
|
|
if c.Requests == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := c.Requests[r]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(c.Requests, r)
|
2014-08-21 08:12:49 +00:00
|
|
|
c.Post(pp.Message{
|
|
|
|
Type: pp.Cancel,
|
2014-05-21 07:49:28 +00:00
|
|
|
Index: r.Index,
|
|
|
|
Begin: r.Begin,
|
|
|
|
Length: r.Length,
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-04-16 07:33:33 +00:00
|
|
|
// Returns true if an unsatisfied request was canceled.
|
2014-04-16 11:13:44 +00:00
|
|
|
func (c *connection) PeerCancel(r request) bool {
|
2014-04-16 07:33:33 +00:00
|
|
|
if c.PeerRequests == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := c.PeerRequests[r]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(c.PeerRequests, r)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-21 07:49:28 +00:00
|
|
|
func (c *connection) Choke() {
|
|
|
|
if c.Choked {
|
|
|
|
return
|
|
|
|
}
|
2014-08-21 08:12:49 +00:00
|
|
|
c.Post(pp.Message{
|
|
|
|
Type: pp.Choke,
|
2014-05-21 07:49:28 +00:00
|
|
|
})
|
2015-06-02 14:03:43 +00:00
|
|
|
c.PeerRequests = nil
|
2014-05-21 07:49:28 +00:00
|
|
|
c.Choked = true
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (c *connection) Unchoke() {
|
2014-04-03 12:16:59 +00:00
|
|
|
if !c.Choked {
|
|
|
|
return
|
|
|
|
}
|
2014-08-21 08:12:49 +00:00
|
|
|
c.Post(pp.Message{
|
|
|
|
Type: pp.Unchoke,
|
2014-04-03 12:16:59 +00:00
|
|
|
})
|
|
|
|
c.Choked = false
|
|
|
|
}
|
|
|
|
|
2014-04-08 16:36:05 +00:00
|
|
|
func (c *connection) SetInterested(interested bool) {
|
2014-04-03 12:16:59 +00:00
|
|
|
if c.Interested == interested {
|
|
|
|
return
|
|
|
|
}
|
2014-08-21 08:12:49 +00:00
|
|
|
c.Post(pp.Message{
|
|
|
|
Type: func() pp.MessageType {
|
2014-04-03 12:16:59 +00:00
|
|
|
if interested {
|
2014-08-21 08:12:49 +00:00
|
|
|
return pp.Interested
|
2014-04-03 12:16:59 +00:00
|
|
|
} else {
|
2014-08-21 08:12:49 +00:00
|
|
|
return pp.NotInterested
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
c.Interested = interested
|
|
|
|
}
|
|
|
|
|
2015-08-02 10:39:31 +00:00
|
|
|
var (
|
|
|
|
// Track connection writer buffer writes and flushes, to determine its
|
|
|
|
// efficiency.
|
|
|
|
connectionWriterFlush = expvar.NewInt("connectionWriterFlush")
|
|
|
|
connectionWriterWrite = expvar.NewInt("connectionWriterWrite")
|
|
|
|
)
|
2015-08-02 04:30:33 +00:00
|
|
|
|
2014-05-28 15:27:48 +00:00
|
|
|
// Writes buffers to the socket from the write channel.
|
2014-04-08 16:36:05 +00:00
|
|
|
func (conn *connection) writer() {
|
2016-02-01 13:45:26 +00:00
|
|
|
defer func() {
|
|
|
|
conn.t.cl.mu.Lock()
|
|
|
|
defer conn.t.cl.mu.Unlock()
|
|
|
|
conn.Close()
|
|
|
|
}()
|
2014-11-21 06:04:07 +00:00
|
|
|
// Reduce write syscalls.
|
2015-08-02 10:39:31 +00:00
|
|
|
buf := bufio.NewWriter(conn.rw)
|
2014-08-21 08:12:49 +00:00
|
|
|
for {
|
2015-08-02 10:39:31 +00:00
|
|
|
if buf.Buffered() == 0 {
|
|
|
|
// There's nothing to write, so block until we get something.
|
2014-11-21 06:04:07 +00:00
|
|
|
select {
|
2015-08-02 10:39:31 +00:00
|
|
|
case b, ok := <-conn.writeCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionWriterWrite.Add(1)
|
|
|
|
_, err := buf.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-02-01 10:08:52 +00:00
|
|
|
case <-conn.closed.C():
|
2014-08-21 08:12:49 +00:00
|
|
|
return
|
2014-05-28 15:27:48 +00:00
|
|
|
}
|
2015-08-02 10:39:31 +00:00
|
|
|
} else {
|
|
|
|
// We already have something to write, so flush if there's nothing
|
|
|
|
// more to write.
|
|
|
|
select {
|
|
|
|
case b, ok := <-conn.writeCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connectionWriterWrite.Add(1)
|
|
|
|
_, err := buf.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-02-01 10:08:52 +00:00
|
|
|
case <-conn.closed.C():
|
2014-11-21 06:04:07 +00:00
|
|
|
return
|
2015-08-02 10:39:31 +00:00
|
|
|
default:
|
|
|
|
connectionWriterFlush.Add(1)
|
|
|
|
err := buf.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-11-21 06:04:07 +00:00
|
|
|
}
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 15:27:48 +00:00
|
|
|
func (conn *connection) writeOptimizer(keepAliveDelay time.Duration) {
|
2014-07-17 16:37:33 +00:00
|
|
|
defer close(conn.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 15:27:48 +00:00
|
|
|
timer := time.NewTimer(keepAliveDelay)
|
|
|
|
defer timer.Stop()
|
|
|
|
lastWrite := time.Now()
|
2014-04-03 12:16:59 +00:00
|
|
|
for {
|
2014-07-17 16:37:33 +00:00
|
|
|
write := conn.writeCh // Set to nil if there's nothing to write.
|
2014-04-03 12:16:59 +00:00
|
|
|
if pending.Len() == 0 {
|
|
|
|
write = nil
|
2014-05-28 15:27:48 +00:00
|
|
|
} else if nextWrite == nil {
|
2014-04-03 12:16:59 +00:00
|
|
|
var err error
|
|
|
|
nextWrite, err = pending.Front().Value.(encoding.BinaryMarshaler).MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 15:27:48 +00:00
|
|
|
event:
|
2014-04-03 12:16:59 +00:00
|
|
|
select {
|
2014-05-28 15:27:48 +00: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 08:12:49 +00:00
|
|
|
pending.PushBack(pp.Message{Keepalive: true})
|
2014-04-03 12:16:59 +00:00
|
|
|
case msg, ok := <-conn.post:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2014-08-21 08:12:49 +00:00
|
|
|
if msg.Type == pp.Cancel {
|
2014-05-28 15:27:48 +00:00
|
|
|
for e := pending.Back(); e != nil; e = e.Prev() {
|
2014-08-21 08:12:49 +00: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 15:27:48 +00:00
|
|
|
pending.Remove(e)
|
2014-08-23 17:04:07 +00:00
|
|
|
optimizedCancels.Add(1)
|
2014-05-28 15:27:48 +00:00
|
|
|
break event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-03 12:16:59 +00:00
|
|
|
pending.PushBack(msg)
|
|
|
|
case write <- nextWrite:
|
|
|
|
pending.Remove(pending.Front())
|
2014-05-28 15:27:48 +00:00
|
|
|
nextWrite = nil
|
|
|
|
lastWrite = time.Now()
|
|
|
|
if pending.Len() == 0 {
|
|
|
|
timer.Reset(keepAliveDelay)
|
|
|
|
}
|
2016-02-01 10:08:52 +00:00
|
|
|
case <-conn.closed.C():
|
2014-08-21 08:12:49 +00:00
|
|
|
return
|
2014-04-03 12:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-04 11:37:49 +00: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 11:56:36 +00:00
|
|
|
if cn.sentHaves != nil {
|
2016-01-04 11:37:49 +00: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 04:21:17 +00:00
|
|
|
|
|
|
|
func (c *connection) updateRequests() {
|
|
|
|
if !c.t.haveInfo() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.Interested {
|
|
|
|
if c.PeerChoked {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(c.Requests) > c.requestsLowWater {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.fillRequests()
|
|
|
|
if len(c.Requests) == 0 && !c.PeerChoked {
|
|
|
|
// 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.
|
|
|
|
c.SetInterested(!c.t.haveAllPieces())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) fillRequests() {
|
2016-02-01 10:11:41 +00:00
|
|
|
itertools.ForIterable(&c.pieceRequestOrder, func(_piece interface{}) (more bool) {
|
|
|
|
return c.requestPiecePendingChunks(_piece.(int))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) requestPiecePendingChunks(piece int) (again bool) {
|
|
|
|
return c.t.connRequestPiecePendingChunks(c, piece)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) stopRequestingPiece(piece int) {
|
|
|
|
c.pieceRequestOrder.Remove(piece)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) updatePiecePriority(piece int) {
|
|
|
|
if !c.PeerHasPiece(piece) {
|
2016-01-24 04:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
2016-02-01 10:11:41 +00:00
|
|
|
tpp := c.t.piecePriority(piece)
|
|
|
|
if tpp == PiecePriorityNone {
|
|
|
|
c.stopRequestingPiece(piece)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prio := c.getPieceInclination()[piece]
|
|
|
|
switch tpp {
|
|
|
|
case PiecePriorityNormal:
|
|
|
|
case PiecePriorityReadahead:
|
|
|
|
prio -= c.t.numPieces()
|
|
|
|
case PiecePriorityNext, PiecePriorityNow:
|
|
|
|
prio -= 2 * c.t.numPieces()
|
|
|
|
default:
|
|
|
|
panic(tpp)
|
|
|
|
}
|
|
|
|
c.pieceRequestOrder.Set(piece, prio)
|
|
|
|
c.updateRequests()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) getPieceInclination() []int {
|
|
|
|
if c.pieceInclination == nil {
|
|
|
|
c.pieceInclination = c.t.getConnPieceInclination()
|
|
|
|
}
|
|
|
|
return c.pieceInclination
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *connection) discardPieceInclination() {
|
|
|
|
if c.pieceInclination == nil {
|
|
|
|
return
|
2016-01-24 04:21:17 +00:00
|
|
|
}
|
2016-02-01 10:11:41 +00:00
|
|
|
c.t.putPieceInclination(c.pieceInclination)
|
|
|
|
c.pieceInclination = nil
|
2016-01-24 04:21:17 +00:00
|
|
|
}
|