forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binpacket.go
208 lines (173 loc) · 4.41 KB
/
binpacket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package tarantool
import (
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/tinylib/msgp/msgp"
)
type BinaryPacket struct {
body []byte
header [32]byte
pool *BinaryPacketPool
packet Packet
}
type UnmarshalBinaryBodyFunc func(*Packet, []byte) error
// WriteTo implements the io.WriterTo interface
func (pp *BinaryPacket) WriteTo(w io.Writer) (n int64, err error) {
h32 := pp.header[:32]
h32[0], h32[1], h32[2], h32[3], h32[4] = 0xce, 0, 0, 0, 0
h := h32[5:5]
body := pp.body
var ne uint32 = 2
if pp.packet.SchemaID != 0 {
ne++
}
h = msgp.AppendMapHeader(h, ne)
h = msgp.AppendUint(h, KeyCode)
h = msgp.AppendUint(h, pp.packet.Cmd)
h = msgp.AppendUint(h, KeySync)
h = msgp.AppendUint64(h, pp.packet.requestID)
if pp.packet.SchemaID != 0 {
h = msgp.AppendUint(h, KeySchemaID)
h = msgp.AppendUint64(h, pp.packet.SchemaID)
}
l := len(h) + len(body)
h = h32[:5+len(h)]
binary.BigEndian.PutUint32(h[1:], uint32(l))
m, err := w.Write(h)
n += int64(m)
if err != nil {
return
}
m, err = w.Write(body)
n += int64(m)
pp.body = pp.body[:0]
return
}
func (pp *BinaryPacket) Reset() {
pp.packet.Cmd = OKCommand
pp.packet.SchemaID = 0
pp.packet.requestID = 0
pp.packet.Result = nil
pp.packet.opts = PacketOpts{}
pp.body = pp.body[:0]
}
func (pp *BinaryPacket) Release() {
if pp.pool != nil && cap(pp.body) <= DefaultMaxPoolPacketSize {
pp.pool.Put(pp)
}
}
// ReadFrom implements the io.ReaderFrom interface
func (pp *BinaryPacket) ReadFrom(r io.Reader) (n int64, err error) {
var h = pp.header[:8]
var bodyLength uint
var headerLength uint
var rr, crr int
if rr, err = io.ReadFull(r, h[:1]); err != nil {
return int64(rr), err
}
c := h[0]
switch {
case c <= 0x7f:
headerLength = 1
case c == 0xcc:
headerLength = 2
case c == 0xcd:
headerLength = 3
case c == 0xce:
headerLength = 5
default:
return int64(rr), fmt.Errorf("wrong packet header: %#v", c)
}
if headerLength > 1 {
crr, err = io.ReadFull(r, h[1:headerLength])
if rr = rr + crr; err != nil {
return int64(rr), err
}
}
if bodyLength, _, err = msgp.ReadUintBytes(h[:headerLength]); err != nil {
return int64(rr), err
}
if bodyLength == 0 {
return int64(rr), errors.New("Packet should not be 0 length")
}
if uint(cap(pp.body)) < bodyLength {
pp.body = make([]byte, bodyLength+bodyLength/2)
}
pp.body = pp.body[:bodyLength]
crr, err = io.ReadFull(r, pp.body)
return int64(rr) + int64(crr), err
}
func (pp *BinaryPacket) Unmarshal() error {
if err := pp.packet.UnmarshalBinary(pp.body); err != nil {
return fmt.Errorf("Error decoding packet type %d: %s", pp.packet.Cmd, err)
}
return nil
}
func (pp *BinaryPacket) UnmarshalCustomBody(um UnmarshalBinaryBodyFunc) (err error) {
buf := pp.body
if buf, err = pp.packet.UnmarshalBinaryHeader(buf); err != nil {
return fmt.Errorf("Error decoding packet type %d: %s", pp.packet.Cmd, err)
}
if err = um(&pp.packet, buf); err != nil {
return fmt.Errorf("Error decoding packet type %d: %s", pp.packet.Cmd, err)
}
return nil
}
func (pp *BinaryPacket) Bytes() []byte {
return pp.body
}
func (pp *BinaryPacket) Result() *Result {
return pp.packet.Result
}
func (pp *BinaryPacket) readPacket(r io.Reader) (err error) {
if _, err = pp.ReadFrom(r); err != nil {
return
}
return pp.packet.UnmarshalBinary(pp.body)
}
// ReadRawPacket reads the whole packet body and only unpacks request ID for routing purposes
func (pp *BinaryPacket) readRawPacket(r io.Reader) (requestID uint64, err error) {
var l uint32
requestID = 0
if _, err = pp.ReadFrom(r); err != nil {
return
}
buf := pp.body
if l, buf, err = msgp.ReadMapHeaderBytes(buf); err != nil {
return
}
for ; l > 0; l-- {
var cd uint
if cd, buf, err = msgp.ReadUintBytes(buf); err != nil {
return
}
if cd == KeySync {
requestID, _, err = msgp.ReadUint64Bytes(buf)
return
}
if buf, err = msgp.Skip(buf); err != nil {
return
}
}
return
}
func (pp *BinaryPacket) packMsg(q Query, packdata *packData) (err error) {
if iq, ok := q.(internalQuery); ok {
if pp.body, err = iq.packMsg(packdata, pp.body[:0]); err != nil {
pp.packet.Cmd = ErrorFlag
return err
}
} else if mp, ok := q.(msgp.Marshaler); ok {
if pp.body, err = mp.MarshalMsg(pp.body[:0]); err != nil {
pp.packet.Cmd = ErrorFlag
return err
}
} else {
pp.packet.Cmd = ErrorFlag
return errors.New("query struct doesn't implement any known marshalling interface")
}
pp.packet.Cmd = q.GetCommandID()
return nil
}