From 729c910911746b89a34522ac30763325fbe791f8 Mon Sep 17 00:00:00 2001 From: mohanson Date: Thu, 15 Aug 2024 09:06:18 +0800 Subject: [PATCH] 2024-08-15 09:06:18 --- protocol/czar/engine.go | 12 ++++++------ protocol/czar/mux.go | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/protocol/czar/engine.go b/protocol/czar/engine.go index 3b2bc6d..29209f0 100644 --- a/protocol/czar/engine.go +++ b/protocol/czar/engine.go @@ -25,9 +25,9 @@ import ( // // To open a stream: // -// +-----+-----+-----+-----+ -// | Sid | 0 | Rsv | -// +-----+-----+-----+-----+ +// +-----+-----+ +// | Sid | 0 | +// +-----+-----+ // // Both server and client can push data to each other. // @@ -37,9 +37,9 @@ import ( // // Close the specified stream. // -// +-----+-----+-----+-----+ -// | Sid | 2 | Rsv | -// +-----+-----+-----+-----+ +// +-----+-----+ +// | Sid | 2 | +// +-----+-----+ // Server implemented the czar protocol. type Server struct { diff --git a/protocol/czar/mux.go b/protocol/czar/mux.go index ace9e6b..fbb7a67 100644 --- a/protocol/czar/mux.go +++ b/protocol/czar/mux.go @@ -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 @@ -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 @@ -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 @@ -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: