Commit f06cd56e authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: make servers use odd seqnums, clients even [GH-727]

parent 54e96d10
...@@ -17,7 +17,7 @@ type Client struct { ...@@ -17,7 +17,7 @@ type Client struct {
} }
func NewClient(rwc io.ReadWriteCloser) (*Client, error) { func NewClient(rwc io.ReadWriteCloser) (*Client, error) {
result, err := NewClientWithMux(NewMuxConn(rwc), 0) result, err := NewClientWithMux(NewMuxConn(rwc, 0), 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -41,11 +41,12 @@ const ( ...@@ -41,11 +41,12 @@ const (
) )
// Create a new MuxConn around any io.ReadWriteCloser. // Create a new MuxConn around any io.ReadWriteCloser.
func NewMuxConn(rwc io.ReadWriteCloser) *MuxConn { func NewMuxConn(rwc io.ReadWriteCloser, startId uint32) *MuxConn {
m := &MuxConn{ m := &MuxConn{
rwc: rwc, rwc: rwc,
streams: make(map[uint32]*Stream), streams: make(map[uint32]*Stream),
doneCh: make(chan struct{}), doneCh: make(chan struct{}),
curId: startId,
} }
go m.cleaner() go m.cleaner()
...@@ -145,7 +146,7 @@ func (m *MuxConn) NextId() uint32 { ...@@ -145,7 +146,7 @@ func (m *MuxConn) NextId() uint32 {
for { for {
result := m.curId result := m.curId
m.curId++ m.curId += 2
if _, ok := m.streams[result]; !ok { if _, ok := m.streams[result]; !ok {
return result return result
} }
......
...@@ -33,7 +33,7 @@ func testMux(t *testing.T) (client *MuxConn, server *MuxConn) { ...@@ -33,7 +33,7 @@ func testMux(t *testing.T) (client *MuxConn, server *MuxConn) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
server = NewMuxConn(conn) server = NewMuxConn(conn, 1)
}() }()
// Client side // Client side
...@@ -41,7 +41,7 @@ func testMux(t *testing.T) (client *MuxConn, server *MuxConn) { ...@@ -41,7 +41,7 @@ func testMux(t *testing.T) (client *MuxConn, server *MuxConn) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
client = NewMuxConn(conn) client = NewMuxConn(conn, 0)
// Wait for the server // Wait for the server
<-doneCh <-doneCh
...@@ -241,7 +241,14 @@ func TestMuxConnNextId(t *testing.T) { ...@@ -241,7 +241,14 @@ func TestMuxConnNextId(t *testing.T) {
a := client.NextId() a := client.NextId()
b := client.NextId() b := client.NextId()
if a != 0 || b != 1 { if a != 0 || b != 2 {
t.Fatalf("IDs should increment") t.Fatalf("IDs should increment")
} }
a = server.NextId()
b = server.NextId()
if a != 1 || b != 3 {
t.Fatalf("IDs should increment: %d %d", a, b)
}
} }
...@@ -36,7 +36,7 @@ type Server struct { ...@@ -36,7 +36,7 @@ type Server struct {
// NewServer returns a new Packer RPC server. // NewServer returns a new Packer RPC server.
func NewServer(conn io.ReadWriteCloser) *Server { func NewServer(conn io.ReadWriteCloser) *Server {
result := NewServerWithMux(NewMuxConn(conn), 0) result := NewServerWithMux(NewMuxConn(conn, 1), 0)
result.closeMux = true result.closeMux = true
return result return result
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment