Skip to content

Commit

Permalink
2024-08-18 14:39:53
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 18, 2024
1 parent 7cb75bd commit 3f696e9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion protocol/czar/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
// Close the specified stream.
//
// +-----+-----+-----+-----+
// | Sid | 2 | Rsv |
// | Sid | 2 | 0/1 | Rsv |
// +-----+-----+-----+-----+

// Server implemented the czar protocol.
Expand Down
28 changes: 20 additions & 8 deletions protocol/czar/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ type Stream struct {
rbf []byte
rch chan []byte
rer *Err
son sync.Once
wer *Err
zo0 sync.Once
zo1 sync.Once
zo2 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.zo0.Do(func() {
s.mux.Write(0, []byte{s.idx, 0x02, 0x00, 0x00})
s.idp <- s.idx
})
return nil
}
Expand All @@ -34,8 +35,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.zo1.Do(func() {
s.mux.Write(0, []byte{s.idx, 0x02, 0x01, 0x00})
})
return nil
}
Expand Down Expand Up @@ -109,8 +110,10 @@ func NewStream(idx uint8, mux *Mux) *Stream {
rbf: make([]byte, 0),
rch: make(chan []byte, 32),
rer: NewErr(),
son: sync.Once{},
wer: NewErr(),
zo0: sync.Once{},
zo1: sync.Once{},
zo2: sync.Once{},
}
}

Expand Down Expand Up @@ -201,7 +204,14 @@ func (m *Mux) Spawn() {
}
case cmd == 0x02:
stm = m.usb[idx]
stm.Esolc()
switch buf[2] {
case 0x00:
stm.Esolc()
case 0x01:
}
stm.zo2.Do(func() {
stm.idp <- stm.idx
})
case cmd >= 0x03:
// Packet format error, connection closed.
m.con.Close()
Expand Down Expand Up @@ -243,7 +253,9 @@ func NewMuxServer(conn net.Conn) *Mux {
mux := NewMux(conn)
for i := range 256 {
old := NewStream(uint8(i), mux)
old.son.Do(func() {})
old.zo0.Do(func() {})
old.zo1.Do(func() {})
old.zo2.Do(func() {})
old.Close()
mux.usb[i] = old
}
Expand Down

0 comments on commit 3f696e9

Please sign in to comment.