Skip to content

Commit 34e03fe

Browse files
committed
Always respond with fixed-size cmd in the header + add schema id
1 parent 95b7565 commit 34e03fe

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

binpacket.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"math"
89

910
"github.com/tinylib/msgp/msgp"
1011
)
@@ -21,28 +22,23 @@ type UnmarshalBinaryBodyFunc func(*Packet, []byte) error
2122
// WriteTo implements the io.WriterTo interface
2223
func (pp *BinaryPacket) WriteTo(w io.Writer) (n int64, err error) {
2324
h32 := pp.header[:32]
24-
h32[0], h32[1], h32[2], h32[3], h32[4] = 0xce, 0, 0, 0, 0
25-
26-
h := h32[5:5]
2725
body := pp.body
2826

29-
var ne uint32 = 2
30-
if pp.packet.SchemaID != 0 {
31-
ne++
32-
}
33-
h = msgp.AppendMapHeader(h, ne)
27+
h := msgp.AppendUint(h32[:0], math.MaxUint32)
28+
mappos := len(h)
29+
h = msgp.AppendMapHeader(h, 3)
3430
h = msgp.AppendUint(h, KeyCode)
35-
h = msgp.AppendUint(h, pp.packet.Cmd)
31+
h = msgp.AppendUint(h, math.MaxUint32)
32+
syncpos := len(h)
3633
h = msgp.AppendUint(h, KeySync)
3734
h = msgp.AppendUint64(h, pp.packet.requestID)
38-
if pp.packet.SchemaID != 0 {
39-
h = msgp.AppendUint(h, KeySchemaID)
40-
h = msgp.AppendUint64(h, pp.packet.SchemaID)
41-
}
35+
h = msgp.AppendUint(h, KeySchemaID)
36+
h = msgp.AppendUint64(h, pp.packet.SchemaID)
37+
38+
binary.BigEndian.PutUint32(h[syncpos-4:], uint32(pp.packet.Cmd))
4239

43-
l := len(h) + len(body)
44-
h = h32[:5+len(h)]
45-
binary.BigEndian.PutUint32(h[1:], uint32(l))
40+
l := len(h) + len(body) - mappos
41+
binary.BigEndian.PutUint32(h32[mappos-4:], uint32(l))
4642

4743
m, err := w.Write(h)
4844
n += int64(m)

0 commit comments

Comments
 (0)