Skip to content

Commit

Permalink
2024-08-15 09:06:18
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 15, 2024
1 parent 254d604 commit 729c910
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
12 changes: 6 additions & 6 deletions protocol/czar/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
//
// To open a stream:
//
// +-----+-----+-----+-----+
// | Sid | 0 | Rsv |
// +-----+-----+-----+-----+
// +-----+-----+
// | Sid | 0 |
// +-----+-----+
//
// Both server and client can push data to each other.
//
Expand All @@ -37,9 +37,9 @@ import (
//
// Close the specified stream.
//
// +-----+-----+-----+-----+
// | Sid | 2 | Rsv |
// +-----+-----+-----+-----+
// +-----+-----+
// | Sid | 2 |
// +-----+-----+

// Server implemented the czar protocol.
type Server struct {
Expand Down
21 changes: 12 additions & 9 deletions protocol/czar/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *Stream) Close() error {
s.ron.Do(func() { close(s.rdn) })
s.won.Do(func() { close(s.wdn) })
s.son.Do(func() {
s.mux.Write(0, []byte{s.idx, 0x02, 0x00, 0x00})
s.mux.Write(0, []byte{s.idx, 0x02})
s.sip.Put(s.idx)
})
return nil
Expand Down Expand Up @@ -140,12 +140,12 @@ func (m *Mux) Open() (*Stream, error) {
if err != nil {
return nil, err
}
cnt, err := m.Write(0, []byte{idx, 0x00, 0x00, 0x00})
cnt, err := m.Write(0, []byte{idx, 0x00})
if err != nil {
m.sip.Put(idx)
return nil, err
}
doa.Doa(cnt == 4)
doa.Doa(cnt == 2)
stm := NewStream(idx, m)
stm.sip = m.sip
m.usb[idx] = stm
Expand All @@ -154,9 +154,11 @@ func (m *Mux) Open() (*Stream, error) {

// Spawn continues to receive data until a fatal error is encountered.
func (m *Mux) Spawn() {
var (
buf = make([]byte, 2)
)
for {
buf := make([]byte, 2048)
_, err := io.ReadFull(m.con, buf[:4])
_, err := io.ReadFull(m.con, buf[:2])
if err != nil {
m.rer = err
break
Expand All @@ -179,20 +181,21 @@ func (m *Mux) Spawn() {
m.usb[idx] = stm
m.ach <- stm
case cmd == 0x01:
bsz := binary.BigEndian.Uint16(buf[2:4])
io.ReadFull(m.con, buf[:2])
bsz := binary.BigEndian.Uint16(buf[:2])
if bsz > 2044 {
// Packet format error, connection closed.
m.con.Close()
break
}
end := bsz + 4
_, err := io.ReadFull(m.con, buf[4:end])
msg := make([]byte, bsz)
_, err := io.ReadFull(m.con, msg)
if err != nil {
break
}
stm := m.usb[idx]
select {
case stm.rch <- buf[4:end]:
case stm.rch <- msg:
case <-stm.rdn:
}
case cmd == 0x02:
Expand Down

0 comments on commit 729c910

Please sign in to comment.