Skip to content

Commit 652f689

Browse files
committed
Refactor byte slice creation for stream commands in mux and tests
1 parent 4ed2b96 commit 652f689

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

protocol/czar/mux.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ func (s *Stream) Close() error {
2828
s.wer.Put(io.ErrClosedPipe)
2929
s.zo0.Do(func() {
3030
s.mux.pri.Pri(0, func() error {
31-
s.mux.con.Write([]byte{0x02, s.idx, 0x00, 0x00})
31+
mph := make([]byte, 4)
32+
mph[0] = 0x02
33+
mph[1] = s.idx
34+
mph[2] = 0x00
35+
s.mux.con.Write(mph)
3236
return nil
3337
})
3438
})
@@ -41,7 +45,11 @@ func (s *Stream) Esolc() error {
4145
s.wer.Put(io.ErrClosedPipe)
4246
s.zo0.Do(func() {
4347
s.mux.pri.Pri(0, func() error {
44-
s.mux.con.Write([]byte{0x02, s.idx, 0x01, 0x00})
48+
mph := make([]byte, 4)
49+
mph[0] = 0x02
50+
mph[1] = s.idx
51+
mph[2] = 0x01
52+
s.mux.con.Write(mph)
4553
return nil
4654
})
4755
})
@@ -177,7 +185,10 @@ func (m *Mux) Open() (*Stream, error) {
177185
return nil, err
178186
}
179187
err = m.pri.Pri(0, func() error {
180-
return doa.Err(m.con.Write([]byte{0x00, idx, 0x00, 0x00}))
188+
mph := make([]byte, 4)
189+
mph[0] = 0x00
190+
mph[1] = idx
191+
return doa.Err(m.con.Write(mph))
181192
})
182193
if err != nil {
183194
m.idp.Put(idx)
@@ -200,7 +211,10 @@ func (m *Mux) Recv() {
200211
old *Stream
201212
prb = time.AfterFunc(Conf.IdleProbeDuration, func() {
202213
if m.pri.Pri(0, func() error {
203-
return doa.Err(m.con.Write([]byte{0x03, 0x00, 0x00, 0x00}))
214+
mph := make([]byte, 4)
215+
mph[0] = 0x03
216+
mph[1] = 0x00
217+
return doa.Err(m.con.Write(mph))
204218
}) != nil {
205219
m.Close()
206220
}
@@ -262,7 +276,10 @@ func (m *Mux) Recv() {
262276
switch idx {
263277
case 0x00:
264278
m.pri.Pri(0, func() error {
265-
return doa.Err(m.con.Write([]byte{0x03, 0x01, 0x00, 0x00}))
279+
mph := make([]byte, 4)
280+
mph[0] = 0x03
281+
mph[1] = 0x01
282+
return doa.Err(m.con.Write(mph))
266283
})
267284
case 0x01:
268285
}

protocol/czar/mux_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@ func TestProtocolCzarMuxServerReopen(t *testing.T) {
145145
cli := doa.Try(net.Dial("tcp", DazeTesterListenOn))
146146
defer cli.Close()
147147

148-
cli.Write([]byte{0x00, 0x00, 0x00, 0x00})
149-
cli.Write([]byte{0x00, 0x00, 0x00, 0x00})
148+
mph := make([]byte, 4)
149+
mph[0] = 0x00 // Cmd open stream
150+
mph[1] = 0x00 // Sid
151+
cli.Write(mph)
152+
cli.Write(mph)
150153
buf := make([]byte, 1)
151154
doa.Doa(doa.Err(io.ReadFull(cli, buf[:1])) != nil)
152155
}

0 commit comments

Comments
 (0)