From b05c1a5bb9b30edb7691482befc2288f7fb7ce7b Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Fri, 10 Jul 2015 16:34:54 +1000 Subject: [PATCH] Connection now needs explicit Start() --- protocol.go | 9 +++++++-- protocol_test.go | 10 ++++++++++ wireformat.go | 4 ++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/protocol.go b/protocol.go index 50115c09c..d0e23055d 100644 --- a/protocol.go +++ b/protocol.go @@ -89,6 +89,7 @@ type Model interface { } type Connection interface { + Start() ID() DeviceID Name() string Index(folder string, files []FileInfo, flags uint32, options []Option) error @@ -161,12 +162,16 @@ func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, receiv compression: compress, } + 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() { go c.readerLoop() go c.writerLoop() go c.pingerLoop() go c.idGenerator() - - return wireFormatConnection{&c} } func (c *rawConnection) ID() DeviceID { diff --git a/protocol_test.go b/protocol_test.go index bb4fe7d95..051672411 100644 --- a/protocol_test.go +++ b/protocol_test.go @@ -68,7 +68,9 @@ func TestPing(t *testing.T) { br, bw := io.Pipe() c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressAlways).(wireFormatConnection).next.(*rawConnection) + c0.Start() c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(wireFormatConnection).next.(*rawConnection) + c1.Start() c0.ClusterConfig(ClusterConfigMessage{}) c1.ClusterConfig(ClusterConfigMessage{}) @@ -94,7 +96,9 @@ func TestPingErr(t *testing.T) { ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e} c0 := NewConnection(c0ID, ar, ebw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection) + c0.Start() c1 := NewConnection(c1ID, br, eaw, m1, "name", CompressAlways) + c1.Start() c0.ClusterConfig(ClusterConfigMessage{}) c1.ClusterConfig(ClusterConfigMessage{}) @@ -174,7 +178,9 @@ func TestVersionErr(t *testing.T) { br, bw := io.Pipe() c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection) + c0.Start() c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways) + c1.Start() c0.ClusterConfig(ClusterConfigMessage{}) c1.ClusterConfig(ClusterConfigMessage{}) @@ -199,7 +205,9 @@ func TestTypeErr(t *testing.T) { br, bw := io.Pipe() c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection) + c0.Start() c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways) + c1.Start() c0.ClusterConfig(ClusterConfigMessage{}) c1.ClusterConfig(ClusterConfigMessage{}) @@ -224,7 +232,9 @@ func TestClose(t *testing.T) { br, bw := io.Pipe() c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection) + c0.Start() c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways) + c1.Start() c0.ClusterConfig(ClusterConfigMessage{}) c1.ClusterConfig(ClusterConfigMessage{}) diff --git a/wireformat.go b/wireformat.go index 9411955ba..66b02ed6f 100644 --- a/wireformat.go +++ b/wireformat.go @@ -12,6 +12,10 @@ type wireFormatConnection struct { next Connection } +func (c wireFormatConnection) Start() { + c.next.Start() +} + func (c wireFormatConnection) ID() DeviceID { return c.next.ID() }