5
5
"errors"
6
6
"fmt"
7
7
"io"
8
+ "math"
8
9
9
10
"github.com/tinylib/msgp/msgp"
10
11
)
@@ -21,28 +22,23 @@ type UnmarshalBinaryBodyFunc func(*Packet, []byte) error
21
22
// WriteTo implements the io.WriterTo interface
22
23
func (pp * BinaryPacket ) WriteTo (w io.Writer ) (n int64 , err error ) {
23
24
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 ]
27
25
body := pp .body
28
26
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 )
34
30
h = msgp .AppendUint (h , KeyCode )
35
- h = msgp .AppendUint (h , pp .packet .Cmd )
31
+ h = msgp .AppendUint (h , math .MaxUint32 )
32
+ syncpos := len (h )
36
33
h = msgp .AppendUint (h , KeySync )
37
34
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 ))
42
39
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 ))
46
42
47
43
m , err := w .Write (h )
48
44
n += int64 (m )
0 commit comments