2014-04-03 20:16:59 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"encoding"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Maintains the state of a connection with a peer.
|
2014-04-09 00:36:05 +08:00
|
|
|
type connection struct {
|
2014-04-03 20:16:59 +08:00
|
|
|
Socket net.Conn
|
|
|
|
closed bool
|
|
|
|
mu sync.Mutex // Only for closing.
|
|
|
|
post chan encoding.BinaryMarshaler
|
|
|
|
write chan []byte
|
|
|
|
|
|
|
|
// 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-05-21 15:47:42 +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.
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return c.PeerPieces[index]
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (c *connection) Post(msg encoding.BinaryMarshaler) {
|
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
|
|
|
|
}
|
|
|
|
c.SetInterested(true)
|
|
|
|
if c.PeerChoked {
|
|
|
|
return false
|
|
|
|
}
|
2014-05-23 19:01:05 +08:00
|
|
|
if c.RequestPending(chunk) {
|
|
|
|
return true
|
2014-04-03 20:16:59 +08:00
|
|
|
}
|
|
|
|
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-04-09 00:36:05 +08:00
|
|
|
func (conn *connection) writer() {
|
2014-04-03 20:16:59 +08:00
|
|
|
timer := time.NewTimer(0)
|
|
|
|
defer timer.Stop()
|
|
|
|
for {
|
|
|
|
if !timer.Reset(time.Minute) {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
var b []byte
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
b = keepAliveBytes[:]
|
|
|
|
case b = <-conn.write:
|
|
|
|
if b == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := conn.Socket.Write(b)
|
|
|
|
if conn.getClosed() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (conn *connection) writeOptimizer() {
|
2014-04-03 20:16:59 +08:00
|
|
|
pending := list.New()
|
|
|
|
var nextWrite []byte
|
|
|
|
defer close(conn.write)
|
|
|
|
for {
|
|
|
|
write := conn.write
|
|
|
|
if pending.Len() == 0 {
|
|
|
|
write = nil
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
nextWrite, err = pending.Front().Value.(encoding.BinaryMarshaler).MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case msg, ok := <-conn.post:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pending.PushBack(msg)
|
|
|
|
case write <- nextWrite:
|
|
|
|
pending.Remove(pending.Front())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|