2015-01-13 12:31:14 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
lz4 "github.com/bkaradzic/go-lz4"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-09-22 18:34:24 +00:00
|
|
|
// BlockSize is the standard ata block size (128 KiB)
|
2015-08-18 06:38:06 +00:00
|
|
|
BlockSize = 128 << 10
|
|
|
|
|
2015-09-22 18:34:24 +00:00
|
|
|
// MaxMessageLen is the largest message size allowed on the wire. (64 MiB)
|
2015-08-18 06:38:06 +00:00
|
|
|
MaxMessageLen = 64 << 20
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
messageTypeClusterConfig = 0
|
|
|
|
messageTypeIndex = 1
|
|
|
|
messageTypeRequest = 2
|
|
|
|
messageTypeResponse = 3
|
|
|
|
messageTypePing = 4
|
|
|
|
messageTypeIndexUpdate = 6
|
|
|
|
messageTypeClose = 7
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
stateInitial = iota
|
2015-06-26 13:38:56 +00:00
|
|
|
stateReady
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2015-02-13 23:27:01 +00:00
|
|
|
// FileInfo flags
|
2014-09-22 19:42:11 +00:00
|
|
|
const (
|
2014-11-07 23:06:04 +00:00
|
|
|
FlagDeleted uint32 = 1 << 12
|
|
|
|
FlagInvalid = 1 << 13
|
|
|
|
FlagDirectory = 1 << 14
|
|
|
|
FlagNoPermBits = 1 << 15
|
|
|
|
FlagSymlink = 1 << 16
|
|
|
|
FlagSymlinkMissingTarget = 1 << 17
|
|
|
|
|
2015-02-12 21:59:33 +00:00
|
|
|
FlagsAll = (1 << 18) - 1
|
2015-01-23 22:25:34 +00:00
|
|
|
|
2014-11-07 23:06:04 +00:00
|
|
|
SymlinkTypeMask = FlagDirectory | FlagSymlinkMissingTarget
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2015-02-13 23:27:01 +00:00
|
|
|
// IndexMessage message flags (for IndexUpdate)
|
|
|
|
const (
|
|
|
|
FlagIndexTemporary uint32 = 1 << iota
|
|
|
|
)
|
|
|
|
|
|
|
|
// Request message flags
|
|
|
|
const (
|
|
|
|
FlagRequestTemporary uint32 = 1 << iota
|
|
|
|
)
|
|
|
|
|
2015-09-27 11:11:34 +00:00
|
|
|
// ClusterConfigMessage.Folders flags
|
|
|
|
const (
|
|
|
|
FlagFolderReadOnly uint32 = 1 << 0
|
|
|
|
FlagFolderIgnorePerms = 1 << 1
|
|
|
|
FlagFolderIgnoreDelete = 1 << 2
|
|
|
|
)
|
|
|
|
|
2015-02-13 23:27:01 +00:00
|
|
|
// ClusterConfigMessage.Folders.Devices flags
|
2014-09-22 19:42:11 +00:00
|
|
|
const (
|
|
|
|
FlagShareTrusted uint32 = 1 << 0
|
|
|
|
FlagShareReadOnly = 1 << 1
|
|
|
|
FlagIntroducer = 1 << 2
|
|
|
|
FlagShareBits = 0x000000ff
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-21 06:51:42 +00:00
|
|
|
ErrClosed = errors.New("connection closed")
|
|
|
|
ErrTimeout = errors.New("read timeout")
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
// Specific variants of empty messages...
|
|
|
|
type pingMessage struct{ EmptyMessage }
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
type Model interface {
|
2014-09-28 11:00:38 +00:00
|
|
|
// An index was received from the peer device
|
2015-02-04 22:15:17 +00:00
|
|
|
Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option)
|
2014-09-28 11:00:38 +00:00
|
|
|
// An index update was received from the peer device
|
2015-02-04 22:15:17 +00:00
|
|
|
IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option)
|
2014-09-28 11:00:38 +00:00
|
|
|
// A request was made by the peer device
|
2015-07-29 20:23:43 +00:00
|
|
|
Request(deviceID DeviceID, folder string, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error
|
2014-09-22 19:42:11 +00:00
|
|
|
// A cluster configuration message was received
|
2014-09-28 11:00:38 +00:00
|
|
|
ClusterConfig(deviceID DeviceID, config ClusterConfigMessage)
|
|
|
|
// The peer device closed the connection
|
|
|
|
Close(deviceID DeviceID, err error)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Connection interface {
|
2015-07-10 06:34:54 +00:00
|
|
|
Start()
|
2014-09-28 11:00:38 +00:00
|
|
|
ID() DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
Name() string
|
2015-02-04 22:15:17 +00:00
|
|
|
Index(folder string, files []FileInfo, flags uint32, options []Option) error
|
|
|
|
IndexUpdate(folder string, files []FileInfo, flags uint32, options []Option) error
|
2015-01-24 21:56:12 +00:00
|
|
|
Request(folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error)
|
2014-09-22 19:42:11 +00:00
|
|
|
ClusterConfig(config ClusterConfigMessage)
|
|
|
|
Statistics() Statistics
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawConnection struct {
|
2014-09-28 11:00:38 +00:00
|
|
|
id DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
name string
|
|
|
|
receiver Model
|
|
|
|
|
|
|
|
cr *countingReader
|
|
|
|
cw *countingWriter
|
|
|
|
|
|
|
|
awaiting [4096]chan asyncResult
|
|
|
|
awaitingMut sync.Mutex
|
|
|
|
|
|
|
|
idxMut sync.Mutex // ensures serialization of Index calls
|
|
|
|
|
2015-07-29 20:23:43 +00:00
|
|
|
nextID chan int
|
|
|
|
outbox chan hdrMsg
|
|
|
|
closed chan struct{}
|
|
|
|
once sync.Once
|
|
|
|
pool sync.Pool
|
2015-02-23 08:30:47 +00:00
|
|
|
compression Compression
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
rdbuf0 []byte // used & reused by readMessage
|
|
|
|
rdbuf1 []byte // used & reused by readMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
type asyncResult struct {
|
|
|
|
val []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type hdrMsg struct {
|
2015-07-29 20:23:43 +00:00
|
|
|
hdr header
|
|
|
|
msg encodable
|
|
|
|
done chan struct{}
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type encodable interface {
|
2014-10-21 06:40:05 +00:00
|
|
|
AppendXDR([]byte) ([]byte, error)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 13:21:58 +00:00
|
|
|
type isEofer interface {
|
|
|
|
IsEOF() bool
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:51:42 +00:00
|
|
|
const (
|
2015-09-22 18:34:24 +00:00
|
|
|
// PingSendInterval is how often we make sure to send a message, by
|
|
|
|
// triggering pings if necessary.
|
2015-09-21 06:51:42 +00:00
|
|
|
PingSendInterval = 90 * time.Second
|
2015-09-22 18:34:24 +00:00
|
|
|
// ReceiveTimeout is the longest we'll wait for a message from the other
|
|
|
|
// side before closing the connection.
|
2015-09-21 06:51:42 +00:00
|
|
|
ReceiveTimeout = 300 * time.Second
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2015-02-23 08:30:47 +00:00
|
|
|
func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, receiver Model, name string, compress Compression) Connection {
|
2014-09-22 19:42:11 +00:00
|
|
|
cr := &countingReader{Reader: reader}
|
|
|
|
cw := &countingWriter{Writer: writer}
|
|
|
|
|
|
|
|
c := rawConnection{
|
2015-07-29 20:23:43 +00:00
|
|
|
id: deviceID,
|
|
|
|
name: name,
|
|
|
|
receiver: nativeModel{receiver},
|
|
|
|
cr: cr,
|
|
|
|
cw: cw,
|
|
|
|
outbox: make(chan hdrMsg),
|
|
|
|
nextID: make(chan int),
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
pool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return make([]byte, BlockSize)
|
|
|
|
},
|
|
|
|
},
|
2015-02-23 08:30:47 +00:00
|
|
|
compression: compress,
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:34:54 +00:00
|
|
|
return wireFormatConnection{&c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start creates the goroutines for sending and receiving of messages. It must
|
|
|
|
// be called exactly once after creating a connection.
|
|
|
|
func (c *rawConnection) Start() {
|
2014-09-22 19:42:11 +00:00
|
|
|
go c.readerLoop()
|
|
|
|
go c.writerLoop()
|
2015-09-21 06:51:42 +00:00
|
|
|
go c.pingSender()
|
|
|
|
go c.pingReceiver()
|
2014-09-22 19:42:11 +00:00
|
|
|
go c.idGenerator()
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (c *rawConnection) ID() DeviceID {
|
2014-09-22 19:42:11 +00:00
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) Name() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// Index writes the list of file information to the connected peer device
|
2015-02-04 22:15:17 +00:00
|
|
|
func (c *rawConnection) Index(folder string, idx []FileInfo, flags uint32, options []Option) error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.idxMut.Lock()
|
2015-01-08 09:28:39 +00:00
|
|
|
c.send(-1, messageTypeIndex, IndexMessage{
|
2015-02-04 22:15:17 +00:00
|
|
|
Folder: folder,
|
|
|
|
Files: idx,
|
|
|
|
Flags: flags,
|
|
|
|
Options: options,
|
2015-07-29 20:23:43 +00:00
|
|
|
}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
c.idxMut.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// IndexUpdate writes the list of file information to the connected peer device as an update
|
2015-02-04 22:15:17 +00:00
|
|
|
func (c *rawConnection) IndexUpdate(folder string, idx []FileInfo, flags uint32, options []Option) error {
|
2014-09-22 19:42:11 +00:00
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
c.idxMut.Lock()
|
2015-01-08 09:28:39 +00:00
|
|
|
c.send(-1, messageTypeIndexUpdate, IndexMessage{
|
2015-02-04 22:15:17 +00:00
|
|
|
Folder: folder,
|
|
|
|
Files: idx,
|
|
|
|
Flags: flags,
|
|
|
|
Options: options,
|
2015-07-29 20:23:43 +00:00
|
|
|
}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
c.idxMut.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request returns the bytes for the specified block after fetching them from the connected peer.
|
2015-01-24 21:56:12 +00:00
|
|
|
func (c *rawConnection) Request(folder string, name string, offset int64, size int, hash []byte, flags uint32, options []Option) ([]byte, error) {
|
2014-09-22 19:42:11 +00:00
|
|
|
var id int
|
|
|
|
select {
|
|
|
|
case id = <-c.nextID:
|
|
|
|
case <-c.closed:
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
c.awaitingMut.Lock()
|
|
|
|
if ch := c.awaiting[id]; ch != nil {
|
|
|
|
panic("id taken")
|
|
|
|
}
|
|
|
|
rc := make(chan asyncResult, 1)
|
|
|
|
c.awaiting[id] = rc
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
|
2015-01-08 09:28:39 +00:00
|
|
|
ok := c.send(id, messageTypeRequest, RequestMessage{
|
2015-01-24 21:56:12 +00:00
|
|
|
Folder: folder,
|
|
|
|
Name: name,
|
|
|
|
Offset: offset,
|
|
|
|
Size: int32(size),
|
|
|
|
Hash: hash,
|
|
|
|
Flags: flags,
|
|
|
|
Options: options,
|
2015-07-29 20:23:43 +00:00
|
|
|
}, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
res, ok := <-rc
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
return res.val, res.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClusterConfig send the cluster configuration message to the peer and returns any error
|
|
|
|
func (c *rawConnection) ClusterConfig(config ClusterConfigMessage) {
|
2015-07-29 20:23:43 +00:00
|
|
|
c.send(-1, messageTypeClusterConfig, config, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) ping() bool {
|
|
|
|
var id int
|
|
|
|
select {
|
|
|
|
case id = <-c.nextID:
|
|
|
|
case <-c.closed:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:51:42 +00:00
|
|
|
return c.send(id, messageTypePing, nil, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) readerLoop() (err error) {
|
|
|
|
defer func() {
|
|
|
|
c.close(err)
|
|
|
|
}()
|
|
|
|
|
2015-06-26 13:38:56 +00:00
|
|
|
state := stateInitial
|
2014-09-22 19:42:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.closed:
|
|
|
|
return ErrClosed
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr, msg, err := c.readMessage()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
switch msg := msg.(type) {
|
2015-06-26 13:38:56 +00:00
|
|
|
case ClusterConfigMessage:
|
|
|
|
if state != stateInitial {
|
|
|
|
return fmt.Errorf("protocol error: cluster config message in state %d", state)
|
|
|
|
}
|
|
|
|
go c.receiver.ClusterConfig(c.id, msg)
|
|
|
|
state = stateReady
|
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
case IndexMessage:
|
|
|
|
switch hdr.msgType {
|
|
|
|
case messageTypeIndex:
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: index message in state %d", state)
|
2015-01-11 12:24:56 +00:00
|
|
|
}
|
|
|
|
c.handleIndex(msg)
|
2015-06-26 13:38:56 +00:00
|
|
|
state = stateReady
|
2015-01-11 12:24:56 +00:00
|
|
|
|
|
|
|
case messageTypeIndexUpdate:
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: index update message in state %d", state)
|
2015-01-11 12:24:56 +00:00
|
|
|
}
|
|
|
|
c.handleIndexUpdate(msg)
|
2015-06-26 13:38:56 +00:00
|
|
|
state = stateReady
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
case RequestMessage:
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: request message in state %d", state)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
// Requests are handled asynchronously
|
2015-01-11 12:24:56 +00:00
|
|
|
go c.handleRequest(hdr.msgID, msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
case ResponseMessage:
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: response message in state %d", state)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2015-01-11 12:24:56 +00:00
|
|
|
c.handleResponse(hdr.msgID, msg)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
case pingMessage:
|
2015-06-26 13:38:56 +00:00
|
|
|
if state != stateReady {
|
|
|
|
return fmt.Errorf("protocol error: ping message in state %d", state)
|
|
|
|
}
|
2015-09-21 06:51:42 +00:00
|
|
|
// Nothing
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
case CloseMessage:
|
|
|
|
return errors.New(msg.Reason)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("protocol error: %s: unknown message type %#x", c.id, hdr.msgType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) readMessage() (hdr header, msg encodable, err error) {
|
|
|
|
if cap(c.rdbuf0) < 8 {
|
|
|
|
c.rdbuf0 = make([]byte, 8)
|
|
|
|
} else {
|
|
|
|
c.rdbuf0 = c.rdbuf0[:8]
|
|
|
|
}
|
|
|
|
_, err = io.ReadFull(c.cr, c.rdbuf0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr = decodeHeader(binary.BigEndian.Uint32(c.rdbuf0[0:4]))
|
|
|
|
msglen := int(binary.BigEndian.Uint32(c.rdbuf0[4:8]))
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("read header %v (msglen=%d)", hdr, msglen)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-08-18 06:38:06 +00:00
|
|
|
if msglen > MaxMessageLen {
|
|
|
|
err = fmt.Errorf("message length %d exceeds maximum %d", msglen, MaxMessageLen)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:11:10 +00:00
|
|
|
if hdr.version != 0 {
|
|
|
|
err = fmt.Errorf("unknown protocol version 0x%x", hdr.version)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
if cap(c.rdbuf0) < msglen {
|
|
|
|
c.rdbuf0 = make([]byte, msglen)
|
|
|
|
} else {
|
|
|
|
c.rdbuf0 = c.rdbuf0[:msglen]
|
|
|
|
}
|
|
|
|
_, err = io.ReadFull(c.cr, c.rdbuf0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("read %d bytes", len(c.rdbuf0))
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
msgBuf := c.rdbuf0
|
2015-08-18 06:42:39 +00:00
|
|
|
if hdr.compression && msglen > 0 {
|
2014-09-22 19:42:11 +00:00
|
|
|
c.rdbuf1 = c.rdbuf1[:cap(c.rdbuf1)]
|
|
|
|
c.rdbuf1, err = lz4.Decode(c.rdbuf1, c.rdbuf0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msgBuf = c.rdbuf1
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("decompressed to %d bytes", len(msgBuf))
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 09:27:18 +00:00
|
|
|
if shouldDebug() {
|
|
|
|
if len(msgBuf) > 1024 {
|
|
|
|
l.Debugf("message data:\n%s", hex.Dump(msgBuf[:1024]))
|
|
|
|
} else {
|
|
|
|
l.Debugf("message data:\n%s", hex.Dump(msgBuf))
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 13:21:58 +00:00
|
|
|
// We check each returned error for the XDRError.IsEOF() method.
|
|
|
|
// IsEOF()==true here means that the message contained fewer fields than
|
|
|
|
// expected. It does not signify an EOF on the socket, because we've
|
|
|
|
// successfully read a size value and that many bytes already. New fields
|
|
|
|
// we expected but the other peer didn't send should be interpreted as
|
|
|
|
// zero/nil, and if that's not valid we'll verify it somewhere else.
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
switch hdr.msgType {
|
|
|
|
case messageTypeIndex, messageTypeIndexUpdate:
|
|
|
|
var idx IndexMessage
|
|
|
|
err = idx.UnmarshalXDR(msgBuf)
|
2015-01-08 13:21:58 +00:00
|
|
|
if xdrErr, ok := err.(isEofer); ok && xdrErr.IsEOF() {
|
|
|
|
err = nil
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
msg = idx
|
|
|
|
|
|
|
|
case messageTypeRequest:
|
|
|
|
var req RequestMessage
|
|
|
|
err = req.UnmarshalXDR(msgBuf)
|
2015-01-08 13:21:58 +00:00
|
|
|
if xdrErr, ok := err.(isEofer); ok && xdrErr.IsEOF() {
|
|
|
|
err = nil
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
msg = req
|
|
|
|
|
|
|
|
case messageTypeResponse:
|
|
|
|
var resp ResponseMessage
|
|
|
|
err = resp.UnmarshalXDR(msgBuf)
|
2015-01-08 13:21:58 +00:00
|
|
|
if xdrErr, ok := err.(isEofer); ok && xdrErr.IsEOF() {
|
|
|
|
err = nil
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
msg = resp
|
|
|
|
|
2015-01-11 12:24:56 +00:00
|
|
|
case messageTypePing:
|
|
|
|
msg = pingMessage{}
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
case messageTypeClusterConfig:
|
|
|
|
var cc ClusterConfigMessage
|
|
|
|
err = cc.UnmarshalXDR(msgBuf)
|
2015-01-08 13:21:58 +00:00
|
|
|
if xdrErr, ok := err.(isEofer); ok && xdrErr.IsEOF() {
|
|
|
|
err = nil
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
msg = cc
|
|
|
|
|
|
|
|
case messageTypeClose:
|
|
|
|
var cm CloseMessage
|
|
|
|
err = cm.UnmarshalXDR(msgBuf)
|
2015-01-08 13:21:58 +00:00
|
|
|
if xdrErr, ok := err.(isEofer); ok && xdrErr.IsEOF() {
|
|
|
|
err = nil
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
msg = cm
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("protocol error: %s: unknown message type %#x", c.id, hdr.msgType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) handleIndex(im IndexMessage) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("Index(%v, %v, %d file, flags %x, opts: %s)", c.id, im.Folder, len(im.Files), im.Flags, im.Options)
|
2015-02-04 22:15:17 +00:00
|
|
|
c.receiver.Index(c.id, im.Folder, filterIndexMessageFiles(im.Files), im.Flags, im.Options)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) handleIndexUpdate(im IndexMessage) {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("queueing IndexUpdate(%v, %v, %d files, flags %x, opts: %s)", c.id, im.Folder, len(im.Files), im.Flags, im.Options)
|
2015-02-04 22:15:17 +00:00
|
|
|
c.receiver.IndexUpdate(c.id, im.Folder, filterIndexMessageFiles(im.Files), im.Flags, im.Options)
|
2015-01-07 14:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func filterIndexMessageFiles(fs []FileInfo) []FileInfo {
|
|
|
|
var out []FileInfo
|
|
|
|
for i, f := range fs {
|
2015-01-13 11:28:35 +00:00
|
|
|
switch f.Name {
|
|
|
|
case "", ".", "..", "/": // A few obviously invalid filenames
|
|
|
|
l.Infof("Dropping invalid filename %q from incoming index", f.Name)
|
2015-01-07 14:44:36 +00:00
|
|
|
if out == nil {
|
|
|
|
// Most incoming updates won't contain anything invalid, so we
|
|
|
|
// delay the allocation and copy to output slice until we
|
|
|
|
// really need to do it, then copy all the so var valid files
|
|
|
|
// to it.
|
|
|
|
out = make([]FileInfo, i, len(fs)-1)
|
|
|
|
copy(out, fs)
|
|
|
|
}
|
2015-01-13 11:28:35 +00:00
|
|
|
default:
|
|
|
|
if out != nil {
|
|
|
|
out = append(out, f)
|
|
|
|
}
|
2015-01-07 14:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
return fs
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) handleRequest(msgID int, req RequestMessage) {
|
2015-07-29 20:23:43 +00:00
|
|
|
size := int(req.Size)
|
|
|
|
usePool := size <= BlockSize
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
var done chan struct{}
|
|
|
|
|
|
|
|
if usePool {
|
|
|
|
buf = c.pool.Get().([]byte)[:size]
|
|
|
|
done = make(chan struct{})
|
|
|
|
} else {
|
|
|
|
buf = make([]byte, size)
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-07-29 20:23:43 +00:00
|
|
|
err := c.receiver.Request(c.id, req.Folder, req.Name, int64(req.Offset), req.Hash, req.Flags, req.Options, buf)
|
|
|
|
if err != nil {
|
|
|
|
c.send(msgID, messageTypeResponse, ResponseMessage{
|
|
|
|
Data: nil,
|
|
|
|
Code: errorToCode(err),
|
|
|
|
}, done)
|
|
|
|
} else {
|
|
|
|
c.send(msgID, messageTypeResponse, ResponseMessage{
|
|
|
|
Data: buf,
|
|
|
|
Code: errorToCode(err),
|
|
|
|
}, done)
|
|
|
|
}
|
|
|
|
|
|
|
|
if usePool {
|
|
|
|
<-done
|
|
|
|
c.pool.Put(buf)
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) handleResponse(msgID int, resp ResponseMessage) {
|
|
|
|
c.awaitingMut.Lock()
|
|
|
|
if rc := c.awaiting[msgID]; rc != nil {
|
|
|
|
c.awaiting[msgID] = nil
|
2015-02-08 11:04:01 +00:00
|
|
|
rc <- asyncResult{resp.Data, codeToError(resp.Code)}
|
2014-09-22 19:42:11 +00:00
|
|
|
close(rc)
|
|
|
|
}
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) handlePong(msgID int) {
|
|
|
|
c.awaitingMut.Lock()
|
|
|
|
if rc := c.awaiting[msgID]; rc != nil {
|
|
|
|
c.awaiting[msgID] = nil
|
|
|
|
rc <- asyncResult{}
|
|
|
|
close(rc)
|
|
|
|
}
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-07-29 20:23:43 +00:00
|
|
|
func (c *rawConnection) send(msgID int, msgType int, msg encodable, done chan struct{}) bool {
|
2014-09-22 19:42:11 +00:00
|
|
|
if msgID < 0 {
|
|
|
|
select {
|
|
|
|
case id := <-c.nextID:
|
|
|
|
msgID = id
|
|
|
|
case <-c.closed:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr := header{
|
|
|
|
version: 0,
|
|
|
|
msgID: msgID,
|
|
|
|
msgType: msgType,
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-07-29 20:23:43 +00:00
|
|
|
case c.outbox <- hdrMsg{hdr, msg, done}:
|
2014-09-22 19:42:11 +00:00
|
|
|
return true
|
|
|
|
case <-c.closed:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) writerLoop() {
|
|
|
|
var msgBuf = make([]byte, 8) // buffer for wire format message, kept and reused
|
|
|
|
var uncBuf []byte // buffer for uncompressed message, kept and reused
|
|
|
|
for {
|
|
|
|
var tempBuf []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
select {
|
|
|
|
case hm := <-c.outbox:
|
|
|
|
if hm.msg != nil {
|
|
|
|
// Uncompressed message in uncBuf
|
2014-10-21 06:40:05 +00:00
|
|
|
uncBuf, err = hm.msg.AppendXDR(uncBuf[:0])
|
2015-07-29 20:23:43 +00:00
|
|
|
if hm.done != nil {
|
|
|
|
close(hm.done)
|
|
|
|
}
|
2014-10-21 06:40:05 +00:00
|
|
|
if err != nil {
|
|
|
|
c.close(err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-02-23 08:30:47 +00:00
|
|
|
compress := false
|
|
|
|
switch c.compression {
|
|
|
|
case CompressAlways:
|
|
|
|
compress = true
|
|
|
|
case CompressMetadata:
|
|
|
|
compress = hm.hdr.msgType != messageTypeResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
if compress && len(uncBuf) >= compressionThreshold {
|
2014-09-22 19:42:11 +00:00
|
|
|
// Use compression for large messages
|
|
|
|
hm.hdr.compression = true
|
|
|
|
|
|
|
|
// Make sure we have enough space for the compressed message plus header in msgBug
|
|
|
|
msgBuf = msgBuf[:cap(msgBuf)]
|
|
|
|
if maxLen := lz4.CompressBound(len(uncBuf)) + 8; maxLen > len(msgBuf) {
|
|
|
|
msgBuf = make([]byte, maxLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compressed is written to msgBuf, we keep tb for the length only
|
|
|
|
tempBuf, err = lz4.Encode(msgBuf[8:], uncBuf)
|
|
|
|
binary.BigEndian.PutUint32(msgBuf[4:8], uint32(len(tempBuf)))
|
|
|
|
msgBuf = msgBuf[0 : len(tempBuf)+8]
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("write compressed message; %v (len=%d)", hm.hdr, len(tempBuf))
|
2014-09-22 19:42:11 +00:00
|
|
|
} else {
|
|
|
|
// No point in compressing very short messages
|
|
|
|
hm.hdr.compression = false
|
|
|
|
|
|
|
|
msgBuf = msgBuf[:cap(msgBuf)]
|
|
|
|
if l := len(uncBuf) + 8; l > len(msgBuf) {
|
|
|
|
msgBuf = make([]byte, l)
|
|
|
|
}
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(msgBuf[4:8], uint32(len(uncBuf)))
|
|
|
|
msgBuf = msgBuf[0 : len(uncBuf)+8]
|
|
|
|
copy(msgBuf[8:], uncBuf)
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("write uncompressed message; %v (len=%d)", hm.hdr, len(uncBuf))
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("write empty message; %v", hm.hdr)
|
2014-09-22 19:42:11 +00:00
|
|
|
binary.BigEndian.PutUint32(msgBuf[4:8], 0)
|
|
|
|
msgBuf = msgBuf[:8]
|
|
|
|
}
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(msgBuf[0:4], encodeHeader(hm.hdr))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
var n int
|
|
|
|
n, err = c.cw.Write(msgBuf)
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugf("wrote %d bytes on the wire", n)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.close(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) close(err error) {
|
|
|
|
c.once.Do(func() {
|
|
|
|
close(c.closed)
|
|
|
|
|
|
|
|
c.awaitingMut.Lock()
|
|
|
|
for i, ch := range c.awaiting {
|
|
|
|
if ch != nil {
|
|
|
|
close(ch)
|
|
|
|
c.awaiting[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.awaitingMut.Unlock()
|
|
|
|
|
|
|
|
go c.receiver.Close(c.id, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) idGenerator() {
|
|
|
|
nextID := 0
|
|
|
|
for {
|
|
|
|
nextID = (nextID + 1) & 0xfff
|
|
|
|
select {
|
|
|
|
case c.nextID <- nextID:
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 06:51:42 +00:00
|
|
|
// The pingSender makes sure that we've sent a message within the last
|
|
|
|
// PingSendInterval. If we already have something sent in the last
|
|
|
|
// PingSendInterval/2, we do nothing. Otherwise we send a ping message. This
|
|
|
|
// results in an effecting ping interval of somewhere between
|
|
|
|
// PingSendInterval/2 and PingSendInterval.
|
|
|
|
func (c *rawConnection) pingSender() {
|
|
|
|
ticker := time.Tick(PingSendInterval / 2)
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
2015-09-21 06:51:42 +00:00
|
|
|
d := time.Since(c.cw.Last())
|
|
|
|
if d < PingSendInterval/2 {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "ping skipped after wr", d)
|
2014-09-22 19:42:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-09-21 06:51:42 +00:00
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "ping -> after", d)
|
2015-09-21 06:51:42 +00:00
|
|
|
c.ping()
|
|
|
|
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The pingReciever checks that we've received a message (any message will do,
|
|
|
|
// but we expect pings in the absence of other messages) within the last
|
|
|
|
// ReceiveTimeout. If not, we close the connection with an ErrTimeout.
|
|
|
|
func (c *rawConnection) pingReceiver() {
|
|
|
|
ticker := time.Tick(ReceiveTimeout / 2)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
|
|
|
d := time.Since(c.cr.Last())
|
|
|
|
if d > ReceiveTimeout {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "ping timeout", d)
|
2015-09-21 06:51:42 +00:00
|
|
|
c.close(ErrTimeout)
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln(c.id, "last read within", d)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
case <-c.closed:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Statistics struct {
|
|
|
|
At time.Time
|
2015-01-18 00:26:52 +00:00
|
|
|
InBytesTotal int64
|
|
|
|
OutBytesTotal int64
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rawConnection) Statistics() Statistics {
|
|
|
|
return Statistics{
|
|
|
|
At: time.Now(),
|
|
|
|
InBytesTotal: c.cr.Tot(),
|
|
|
|
OutBytesTotal: c.cw.Tot(),
|
|
|
|
}
|
|
|
|
}
|