2013-12-15 03:43:31 -07:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/flate"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"sync"
|
2013-12-15 08:19:45 -07:00
|
|
|
"time"
|
2013-12-15 03:43:31 -07:00
|
|
|
|
|
|
|
"github.com/calmh/syncthing/buffers"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
messageTypeReserved = iota
|
|
|
|
messageTypeIndex
|
|
|
|
messageTypeRequest
|
|
|
|
messageTypeResponse
|
|
|
|
messageTypePing
|
|
|
|
messageTypePong
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileInfo struct {
|
|
|
|
Name string
|
|
|
|
Flags uint32
|
|
|
|
Modified int64
|
|
|
|
Blocks []BlockInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockInfo struct {
|
|
|
|
Length uint32
|
|
|
|
Hash []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type Model interface {
|
|
|
|
// An index was received from the peer node
|
|
|
|
Index(nodeID string, files []FileInfo)
|
|
|
|
// A request was made by the peer node
|
|
|
|
Request(nodeID, name string, offset uint64, size uint32, hash []byte) ([]byte, error)
|
|
|
|
// The peer node closed the connection
|
|
|
|
Close(nodeID string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Connection struct {
|
2013-12-15 08:19:45 -07:00
|
|
|
ID string
|
|
|
|
receiver Model
|
|
|
|
reader io.Reader
|
|
|
|
mreader *marshalReader
|
|
|
|
writer io.Writer
|
|
|
|
mwriter *marshalWriter
|
|
|
|
wLock sync.RWMutex
|
|
|
|
closed bool
|
|
|
|
closedLock sync.RWMutex
|
|
|
|
awaiting map[int]chan asyncResult
|
|
|
|
nextId int
|
|
|
|
lastReceive time.Time
|
|
|
|
peerLatency time.Duration
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
|
2013-12-15 07:58:27 -07:00
|
|
|
var ErrClosed = errors.New("Connection closed")
|
|
|
|
|
|
|
|
type asyncResult struct {
|
|
|
|
val []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2013-12-15 08:19:45 -07:00
|
|
|
const pingTimeout = 30 * time.Second
|
|
|
|
const pingIdleTime = 5 * time.Minute
|
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver Model) *Connection {
|
|
|
|
flrd := flate.NewReader(reader)
|
|
|
|
flwr, err := flate.NewWriter(writer, flate.BestSpeed)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := Connection{
|
|
|
|
receiver: receiver,
|
|
|
|
reader: flrd,
|
|
|
|
mreader: &marshalReader{flrd, 0, nil},
|
|
|
|
writer: flwr,
|
|
|
|
mwriter: &marshalWriter{flwr, 0, nil},
|
2013-12-15 07:58:27 -07:00
|
|
|
awaiting: make(map[int]chan asyncResult),
|
2013-12-15 03:43:31 -07:00
|
|
|
ID: nodeID,
|
|
|
|
}
|
|
|
|
|
|
|
|
go c.readerLoop()
|
2013-12-15 08:19:45 -07:00
|
|
|
go c.pingerLoop()
|
2013-12-15 03:43:31 -07:00
|
|
|
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index writes the list of file information to the connected peer node
|
|
|
|
func (c *Connection) Index(idx []FileInfo) {
|
|
|
|
c.wLock.Lock()
|
|
|
|
defer c.wLock.Unlock()
|
|
|
|
|
|
|
|
c.mwriter.writeHeader(header{0, c.nextId, messageTypeIndex})
|
|
|
|
c.nextId = (c.nextId + 1) & 0xfff
|
|
|
|
c.mwriter.writeIndex(idx)
|
|
|
|
c.flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request returns the bytes for the specified block after fetching them from the connected peer.
|
|
|
|
func (c *Connection) Request(name string, offset uint64, size uint32, hash []byte) ([]byte, error) {
|
|
|
|
c.wLock.Lock()
|
2013-12-15 07:58:27 -07:00
|
|
|
rc := make(chan asyncResult)
|
2013-12-15 03:43:31 -07:00
|
|
|
c.awaiting[c.nextId] = rc
|
|
|
|
c.mwriter.writeHeader(header{0, c.nextId, messageTypeRequest})
|
|
|
|
c.mwriter.writeRequest(request{name, offset, size, hash})
|
|
|
|
c.flush()
|
|
|
|
c.nextId = (c.nextId + 1) & 0xfff
|
|
|
|
c.wLock.Unlock()
|
|
|
|
|
2013-12-15 07:58:27 -07:00
|
|
|
res, ok := <-rc
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
2013-12-15 07:58:27 -07:00
|
|
|
return res.val, res.err
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Ping() bool {
|
|
|
|
c.wLock.Lock()
|
2013-12-15 07:58:27 -07:00
|
|
|
rc := make(chan asyncResult)
|
2013-12-15 03:43:31 -07:00
|
|
|
c.awaiting[c.nextId] = rc
|
|
|
|
c.mwriter.writeHeader(header{0, c.nextId, messageTypePing})
|
|
|
|
c.flush()
|
|
|
|
c.nextId = (c.nextId + 1) & 0xfff
|
|
|
|
c.wLock.Unlock()
|
|
|
|
|
|
|
|
_, ok := <-rc
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Stop() {
|
|
|
|
}
|
|
|
|
|
|
|
|
type flusher interface {
|
|
|
|
Flush() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) flush() {
|
|
|
|
if f, ok := c.writer.(flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) close() {
|
|
|
|
c.closedLock.Lock()
|
|
|
|
c.closed = true
|
|
|
|
c.closedLock.Unlock()
|
2013-12-15 07:58:27 -07:00
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
c.wLock.Lock()
|
|
|
|
for _, ch := range c.awaiting {
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
c.awaiting = nil
|
|
|
|
c.wLock.Unlock()
|
2013-12-15 07:58:27 -07:00
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
c.receiver.Close(c.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) isClosed() bool {
|
|
|
|
c.closedLock.RLock()
|
|
|
|
defer c.closedLock.RUnlock()
|
|
|
|
return c.closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) readerLoop() {
|
|
|
|
for !c.isClosed() {
|
|
|
|
hdr := c.mreader.readHeader()
|
|
|
|
if c.mreader.err != nil {
|
|
|
|
c.close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-12-15 08:19:45 -07:00
|
|
|
c.wLock.Lock()
|
|
|
|
c.lastReceive = time.Now()
|
|
|
|
c.wLock.Unlock()
|
|
|
|
|
2013-12-15 03:43:31 -07:00
|
|
|
switch hdr.msgType {
|
|
|
|
case messageTypeIndex:
|
|
|
|
files := c.mreader.readIndex()
|
|
|
|
if c.mreader.err != nil {
|
|
|
|
c.close()
|
|
|
|
} else {
|
|
|
|
c.receiver.Index(c.ID, files)
|
|
|
|
}
|
|
|
|
|
|
|
|
case messageTypeRequest:
|
|
|
|
c.processRequest(hdr.msgID)
|
2013-12-18 10:29:15 -07:00
|
|
|
if c.mreader.err != nil || c.mwriter.err != nil {
|
|
|
|
c.close()
|
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
|
|
|
case messageTypeResponse:
|
|
|
|
data := c.mreader.readResponse()
|
|
|
|
|
|
|
|
if c.mreader.err != nil {
|
|
|
|
c.close()
|
|
|
|
} else {
|
|
|
|
c.wLock.RLock()
|
|
|
|
rc, ok := c.awaiting[hdr.msgID]
|
|
|
|
c.wLock.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
2013-12-15 07:58:27 -07:00
|
|
|
rc <- asyncResult{data, c.mreader.err}
|
2013-12-15 03:43:31 -07:00
|
|
|
close(rc)
|
2013-12-15 07:58:27 -07:00
|
|
|
|
|
|
|
c.wLock.Lock()
|
|
|
|
delete(c.awaiting, hdr.msgID)
|
|
|
|
c.wLock.Unlock()
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case messageTypePing:
|
|
|
|
c.wLock.Lock()
|
|
|
|
c.mwriter.writeUint32(encodeHeader(header{0, hdr.msgID, messageTypePong}))
|
|
|
|
c.flush()
|
|
|
|
c.wLock.Unlock()
|
2013-12-18 10:29:15 -07:00
|
|
|
if c.mwriter.err != nil {
|
|
|
|
c.close()
|
|
|
|
}
|
2013-12-15 03:43:31 -07:00
|
|
|
|
|
|
|
case messageTypePong:
|
2013-12-15 07:58:27 -07:00
|
|
|
c.wLock.RLock()
|
|
|
|
rc, ok := c.awaiting[hdr.msgID]
|
|
|
|
c.wLock.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
rc <- asyncResult{}
|
2013-12-15 03:43:31 -07:00
|
|
|
close(rc)
|
2013-12-15 07:58:27 -07:00
|
|
|
|
|
|
|
c.wLock.Lock()
|
2013-12-15 03:43:31 -07:00
|
|
|
delete(c.awaiting, hdr.msgID)
|
2013-12-15 07:58:27 -07:00
|
|
|
c.wLock.Unlock()
|
2013-12-15 03:43:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) processRequest(msgID int) {
|
|
|
|
req := c.mreader.readRequest()
|
|
|
|
if c.mreader.err != nil {
|
|
|
|
c.close()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
data, _ := c.receiver.Request(c.ID, req.name, req.offset, req.size, req.hash)
|
|
|
|
c.wLock.Lock()
|
|
|
|
c.mwriter.writeUint32(encodeHeader(header{0, msgID, messageTypeResponse}))
|
|
|
|
c.mwriter.writeResponse(data)
|
|
|
|
buffers.Put(data)
|
|
|
|
c.flush()
|
|
|
|
c.wLock.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2013-12-15 08:19:45 -07:00
|
|
|
|
|
|
|
func (c *Connection) pingerLoop() {
|
|
|
|
var rc = make(chan time.Duration)
|
|
|
|
for !c.isClosed() {
|
|
|
|
c.wLock.RLock()
|
|
|
|
lr := c.lastReceive
|
|
|
|
c.wLock.RUnlock()
|
|
|
|
|
|
|
|
if time.Since(lr) > pingIdleTime {
|
|
|
|
go func() {
|
|
|
|
t0 := time.Now()
|
|
|
|
c.Ping()
|
|
|
|
rc <- time.Since(t0)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case lat := <-rc:
|
|
|
|
c.wLock.Lock()
|
|
|
|
c.peerLatency = (c.peerLatency + lat) / 2
|
|
|
|
c.wLock.Unlock()
|
|
|
|
case <-time.After(pingTimeout):
|
|
|
|
c.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|