lib/protocol: Don't send anything else before cluster config (#5741)

This commit is contained in:
Simon Frei
2019-05-27 12:15:34 +02:00
committed by Audrius Butkevicius
parent 129df0613b
commit 3775a64d5c
2 changed files with 58 additions and 2 deletions

View File

@@ -184,6 +184,7 @@ type rawConnection struct {
inbox chan message
outbox chan asyncMessage
clusterConfigBox chan *ClusterConfig
dispatcherLoopStopped chan struct{}
closed chan struct{}
closeOnce sync.Once
@@ -230,6 +231,7 @@ func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, receiv
awaiting: make(map[int32]chan asyncResult),
inbox: make(chan message),
outbox: make(chan asyncMessage),
clusterConfigBox: make(chan *ClusterConfig),
dispatcherLoopStopped: make(chan struct{}),
closed: make(chan struct{}),
compression: compress,
@@ -327,9 +329,11 @@ func (c *rawConnection) Request(folder string, name string, offset int64, size i
return res.val, res.err
}
// ClusterConfig send the cluster configuration message to the peer and returns any error
// ClusterConfig sends the cluster configuration message to the peer.
// It must be called just once (as per BEP), otherwise it will panic.
func (c *rawConnection) ClusterConfig(config ClusterConfig) {
c.send(&config, nil)
c.clusterConfigBox <- &config
close(c.clusterConfigBox)
}
func (c *rawConnection) Closed() bool {
@@ -657,6 +661,16 @@ func (c *rawConnection) send(msg message, done chan struct{}) bool {
}
func (c *rawConnection) writerLoop() {
select {
case cc := <-c.clusterConfigBox:
err := c.writeMessage(cc)
if err != nil {
c.internalClose(err)
return
}
case <-c.closed:
return
}
for {
select {
case hm := <-c.outbox: