Skip to content

Commit

Permalink
Wither stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 19, 2024
1 parent 1862626 commit d4f9731
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions protocol/czar/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ func NewStream(idx uint8, mux *Mux) *Stream {
}
}

// NewWither returns a new Stream. Stream has been automatically closed, used as a placeholder.
func NewWither(idx uint8, mux *Mux) *Stream {
stm := NewStream(idx, mux)
stm.zo0.Do(func() {})
stm.zo1.Do(func() {})
stm.Close()
return stm
}

// Mux is used to wrap a reliable ordered connection and to multiplex it into multiple streams.
type Mux struct {
ach chan *Stream
Expand Down Expand Up @@ -223,6 +232,8 @@ func (m *Mux) Recv() {
case cmd == 0x02:
stm = m.usb[idx]
stm.Esolc()
old = NewWither(idx, m)
m.usb[idx] = old
case cmd >= 0x03:
// Packet format error, connection closed.
m.con.Close()
Expand Down Expand Up @@ -264,11 +275,7 @@ func NewMuxServer(conn net.Conn) *Mux {
mux := NewMux(conn)
mux.idp = NewSip()
for i := range 256 {
old := NewStream(uint8(i), mux)
old.zo0.Do(func() {})
old.zo1.Do(func() {})
old.Close()
mux.usb[i] = old
mux.usb[i] = NewWither(uint8(i), mux)
}
go mux.Recv()
return mux
Expand Down

0 comments on commit d4f9731

Please sign in to comment.