2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"encoding"
|
2014-06-26 15:29:12 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-04-03 20:16:59 +08:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
2014-07-16 15:06:18 +08:00
|
|
|
type peerSource byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
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 {
|
2014-07-16 15:06:18 +08:00
|
|
|
Socket net.Conn
|
|
|
|
Discovery peerSource
|
|
|
|
closed bool
|
|
|
|
mu sync.Mutex // Only for closing.
|
|
|
|
post chan peer_protocol.Message
|
|
|
|
write chan []byte
|
2014-04-03 20:16:59 +08:00
|
|
|
|
|
|
|
// Stuff controlled by the local peer.
|
|
|
|
Interested bool
|
|
|
|
Choked bool
|
2014-04-16 19:13:44 +08:00
|
|
|
Requests map[request]struct{}
|
2014-04-03 20:16:59 +08:00
|
|
|
|
|
|
|
// Stuff controlled by the remote peer.
|
2014-06-26 22:57:07 +08:00
|
|
|
PeerId [20]byte
|
|
|
|
PeerInterested bool
|
|
|
|
PeerChoked bool
|
|
|
|
PeerRequests map[request]struct{}
|
|
|
|
PeerExtensions [8]byte
|
|
|
|
PeerPieces []bool
|
|
|
|
PeerMaxRequests int // Maximum pending requests the peer allows.
|
|
|
|
PeerExtensionIDs map[string]int64
|
2014-06-29 16:57:49 +08:00
|
|
|
PeerClientName string
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 15:29:12 +08:00
|
|
|
func (cn *connection) completedString() string {
|
|
|
|
if cn.PeerPieces == nil {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
f := float32(cn.piecesPeerHasCount()) / float32(cn.totalPiecesCount())
|
|
|
|
return fmt.Sprintf("%d%%", int(f*100))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) totalPiecesCount() int {
|
|
|
|
return len(cn.PeerPieces)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) piecesPeerHasCount() (count int) {
|
|
|
|
for _, has := range cn.PeerPieces {
|
|
|
|
if has {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *connection) WriteStatus(w io.Writer) {
|
|
|
|
fmt.Fprintf(w, "%q: %s-%s: %s completed: ", cn.PeerId, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr(), cn.completedString())
|
|
|
|
c := func(b byte) {
|
|
|
|
fmt.Fprintf(w, "%c", b)
|
|
|
|
}
|
|
|
|
// https://trac.transmissionbt.com/wiki/PeerStatusText
|
2014-07-09 22:16:09 +08:00
|
|
|
if cn.PeerInterested && !cn.Choked {
|
|
|
|
c('O')
|
|
|
|
}
|
2014-06-26 15:29:12 +08:00
|
|
|
if len(cn.Requests) != 0 {
|
|
|
|
c('D')
|
2014-07-09 22:16:09 +08:00
|
|
|
}
|
|
|
|
if cn.PeerChoked && cn.Interested {
|
2014-06-26 15:29:12 +08:00
|
|
|
c('d')
|
|
|
|
}
|
2014-07-09 22:16:09 +08:00
|
|
|
if !cn.Choked && cn.PeerInterested {
|
|
|
|
c('U')
|
|
|
|
} else {
|
|
|
|
c('u')
|
|
|
|
}
|
2014-06-26 15:29:12 +08:00
|
|
|
if !cn.PeerChoked && !cn.Interested {
|
|
|
|
c('K')
|
|
|
|
}
|
|
|
|
if !cn.Choked && !cn.PeerInterested {
|
|
|
|
c('?')
|
|
|
|
}
|
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-06-26 15:29:12 +08:00
|
|
|
fmt.Fprintln(w)
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (c *connection) Close() {
|
2014-04-03 20:16:59 +08:00
|
|
|
c.mu.Lock()
|
|
|
|
if c.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Socket.Close()
|
|
|
|
close(c.post)
|
|
|
|
c.closed = true
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (c *connection) getClosed() bool {
|
2014-04-03 20:16:59 +08:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.closed
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (c *connection) PeerHasPiece(index peer_protocol.Integer) bool {
|
2014-04-03 20:16:59 +08:00
|
|
|
if c.PeerPieces == nil {
|
|
|
|
return false
|
|
|
|
}
|
2014-06-30 22:06:58 +08:00
|
|
|
if int(index) >= len(c.PeerPieces) {
|
|
|
|
return false
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
return c.PeerPieces[index]
|
|
|
|
}
|
|
|
|
|
2014-05-28 23:27:48 +08:00
|
|
|
func (c *connection) Post(msg peer_protocol.Message) {
|
2014-04-03 20:16:59 +08:00
|
|
|
c.post <- msg
|
|
|
|
}
|
|
|
|
|
2014-05-23 19:01:05 +08:00
|
|
|
func (c *connection) RequestPending(r request) bool {
|
|
|
|
_, ok := c.Requests[r]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:16:59 +08:00
|
|
|
// Returns true if more requests can be sent.
|
2014-04-16 19:13:44 +08:00
|
|
|
func (c *connection) Request(chunk request) bool {
|
2014-05-21 15:47:42 +08:00
|
|
|
if len(c.Requests) >= c.PeerMaxRequests {
|
2014-04-03 20:16:59 +08:00
|
|
|
return false
|
|
|
|
}
|
2014-05-21 15:42:06 +08:00
|
|
|
if !c.PeerHasPiece(chunk.Index) {
|
2014-04-03 20:16:59 +08:00
|
|
|
return true
|
|
|
|
}
|
2014-06-28 17:38:31 +08:00
|
|
|
if c.RequestPending(chunk) {
|
|
|
|
return true
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
c.SetInterested(true)
|
|
|
|
if c.PeerChoked {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.Requests == nil {
|
2014-05-21 15:47:42 +08:00
|
|
|
c.Requests = make(map[request]struct{}, c.PeerMaxRequests)
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
c.Requests[chunk] = struct{}{}
|
2014-05-23 19:01:05 +08:00
|
|
|
c.Post(peer_protocol.Message{
|
|
|
|
Type: peer_protocol.Request,
|
|
|
|
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.
|
|
|
|
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)
|
|
|
|
c.Post(peer_protocol.Message{
|
|
|
|
Type: peer_protocol.Cancel,
|
|
|
|
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.
|
2014-04-16 19:13:44 +08:00
|
|
|
func (c *connection) PeerCancel(r request) bool {
|
2014-04-16 15:33:33 +08:00
|
|
|
if c.PeerRequests == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if _, ok := c.PeerRequests[r]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delete(c.PeerRequests, r)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-05-21 15:49:28 +08:00
|
|
|
func (c *connection) Choke() {
|
|
|
|
if c.Choked {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Post(peer_protocol.Message{
|
|
|
|
Type: peer_protocol.Choke,
|
|
|
|
})
|
|
|
|
c.Choked = true
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (c *connection) Unchoke() {
|
2014-04-03 20:16:59 +08:00
|
|
|
if !c.Choked {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Post(peer_protocol.Message{
|
|
|
|
Type: peer_protocol.Unchoke,
|
|
|
|
})
|
|
|
|
c.Choked = false
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (c *connection) SetInterested(interested bool) {
|
2014-04-03 20:16:59 +08:00
|
|
|
if c.Interested == interested {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Post(peer_protocol.Message{
|
|
|
|
Type: func() peer_protocol.MessageType {
|
|
|
|
if interested {
|
|
|
|
return peer_protocol.Interested
|
|
|
|
} else {
|
|
|
|
return peer_protocol.NotInterested
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
})
|
|
|
|
c.Interested = interested
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Four consecutive zero bytes that comprise a keep alive on the wire.
|
|
|
|
keepAliveBytes [4]byte
|
|
|
|
)
|
|
|
|
|
2014-05-28 23:27:48 +08:00
|
|
|
// Writes buffers to the socket from the write channel.
|
2014-04-09 00:36:05 +08:00
|
|
|
func (conn *connection) writer() {
|
2014-05-28 23:27:48 +08:00
|
|
|
for b := range conn.write {
|
2014-04-03 20:16:59 +08:00
|
|
|
_, err := conn.Socket.Write(b)
|
2014-06-28 17:38:31 +08:00
|
|
|
// log.Printf("wrote %q to %s", b, conn.Socket.RemoteAddr())
|
2014-04-03 20:16:59 +08:00
|
|
|
if err != nil {
|
2014-05-28 23:27:48 +08:00
|
|
|
if !conn.getClosed() {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 23:27:48 +08:00
|
|
|
func (conn *connection) writeOptimizer(keepAliveDelay time.Duration) {
|
|
|
|
defer close(conn.write) // 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.
|
|
|
|
timer := time.NewTimer(keepAliveDelay)
|
|
|
|
defer timer.Stop()
|
|
|
|
lastWrite := time.Now()
|
2014-04-03 20:16:59 +08:00
|
|
|
for {
|
2014-05-28 23:27:48 +08:00
|
|
|
write := conn.write // 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
|
|
|
|
}
|
|
|
|
pending.PushBack(peer_protocol.Message{Keepalive: true})
|
2014-04-03 20:16:59 +08:00
|
|
|
case msg, ok := <-conn.post:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2014-05-28 23:27:48 +08:00
|
|
|
if msg.Type == peer_protocol.Cancel {
|
|
|
|
for e := pending.Back(); e != nil; e = e.Prev() {
|
|
|
|
elemMsg := e.Value.(peer_protocol.Message)
|
|
|
|
if elemMsg.Type == peer_protocol.Request && msg.Index == elemMsg.Index && msg.Begin == elemMsg.Begin && msg.Length == elemMsg.Length {
|
|
|
|
pending.Remove(e)
|
2014-06-26 15:30:16 +08:00
|
|
|
log.Printf("optimized cancel! %v", msg)
|
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)
|
|
|
|
}
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|