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

This commit is contained in:
Mitchell Hashimoto 2013-12-19 17:32:43 -08:00
parent 54e96d100a
commit f06cd56e72
4 changed files with 15 additions and 7 deletions

View File

@ -17,7 +17,7 @@ type Client struct {
}
func NewClient(rwc io.ReadWriteCloser) (*Client, error) {
result, err := NewClientWithMux(NewMuxConn(rwc), 0)
result, err := NewClientWithMux(NewMuxConn(rwc, 0), 0)
if err != nil {
return nil, err
}

View File

@ -41,11 +41,12 @@ const (
)
// Create a new MuxConn around any io.ReadWriteCloser.
func NewMuxConn(rwc io.ReadWriteCloser) *MuxConn {
func NewMuxConn(rwc io.ReadWriteCloser, startId uint32) *MuxConn {
m := &MuxConn{
rwc: rwc,
streams: make(map[uint32]*Stream),
doneCh: make(chan struct{}),
curId: startId,
}
go m.cleaner()
@ -145,7 +146,7 @@ func (m *MuxConn) NextId() uint32 {
for {
result := m.curId
m.curId++
m.curId += 2
if _, ok := m.streams[result]; !ok {
return result
}

View File

@ -33,7 +33,7 @@ func testMux(t *testing.T) (client *MuxConn, server *MuxConn) {
t.Fatalf("err: %s", err)
}
server = NewMuxConn(conn)
server = NewMuxConn(conn, 1)
}()
// Client side
@ -41,7 +41,7 @@ func testMux(t *testing.T) (client *MuxConn, server *MuxConn) {
if err != nil {
t.Fatalf("err: %s", err)
}
client = NewMuxConn(conn)
client = NewMuxConn(conn, 0)
// Wait for the server
<-doneCh
@ -241,7 +241,14 @@ func TestMuxConnNextId(t *testing.T) {
a := client.NextId()
b := client.NextId()
if a != 0 || b != 1 {
if a != 0 || b != 2 {
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)
}
}

View File

@ -36,7 +36,7 @@ type Server struct {
// NewServer returns a new Packer RPC server.
func NewServer(conn io.ReadWriteCloser) *Server {
result := NewServerWithMux(NewMuxConn(conn), 0)
result := NewServerWithMux(NewMuxConn(conn, 1), 0)
result.closeMux = true
return result
}