Skip to content

Commit

Permalink
2024-08-18 10:34:13
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 18, 2024
1 parent 38f7299 commit 6722a87
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions protocol/czar/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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{{}, {}, {}},
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6722a87

Please sign in to comment.