diff --git a/protocol/czar/mux.go b/protocol/czar/mux.go index bbd1c29..6bb7f3a 100644 --- a/protocol/czar/mux.go +++ b/protocol/czar/mux.go @@ -15,17 +15,16 @@ type Stream struct { rbf []byte rch chan []byte rer *Err - son sync.Once wer *Err + zon [3]sync.Once } // Close implements io.Closer. func (s *Stream) Close() error { s.rer.Put(io.ErrClosedPipe) s.wer.Put(io.ErrClosedPipe) - s.son.Do(func() { + s.zon[0].Do(func() { s.mux.Write(0, []byte{s.idx, 0x02, 0x00, 0x00}) - s.idp <- s.idx }) return nil } @@ -34,8 +33,8 @@ func (s *Stream) Close() error { func (s *Stream) Esolc() error { s.rer.Put(io.EOF) s.wer.Put(io.ErrClosedPipe) - s.son.Do(func() { - s.idp <- s.idx + s.zon[1].Do(func() { + s.mux.Write(0, []byte{s.idx, 0x02, 0x01, 0x00}) }) return nil } @@ -109,8 +108,8 @@ func NewStream(idx uint8, mux *Mux) *Stream { rbf: make([]byte, 0), rch: make(chan []byte, 32), rer: NewErr(), - son: sync.Once{}, wer: NewErr(), + zon: [3]sync.Once{{}, {}, {}}, } } @@ -200,7 +199,12 @@ func (m *Mux) Spawn() { } case cmd == 0x02: stm = m.usb[idx] - stm.Esolc() + if buf[2] == 0x00 { + stm.Esolc() + } + stm.zon[2].Do(func() { + stm.idp <- stm.idx + }) case cmd >= 0x03: // Packet format error, connection closed. m.con.Close() @@ -242,7 +246,9 @@ func NewMuxServer(conn net.Conn) *Mux { mux := NewMux(conn) for i := range 256 { old := NewStream(uint8(i), mux) - old.son.Do(func() {}) + for i := range 3 { + old.zon[i].Do(func() {}) + } old.Close() mux.usb[i] = old }