-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathprotocol.go
318 lines (275 loc) · 7.11 KB
/
protocol.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2017 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package socket
import (
"encoding/binary"
"errors"
"io"
"math"
"strconv"
"sync"
"github.com/andeya/goutil"
"github.com/andeya/erpc/v7/utils"
)
type (
// Proto pack/unpack protocol scheme of socket message.
// NOTE: Implementation specifications for Message interface should be complied with.
Proto interface {
// Version returns the protocol's id and name.
Version() (byte, string)
// Pack writes the Message into the connection.
// NOTE: Make sure to write only once or there will be package contamination!
Pack(Message) error
// Unpack reads bytes from the connection to the Message.
// NOTE: Concurrent unsafe!
Unpack(Message) error
}
// IOWithReadBuffer implements buffered I/O with buffered reader.
IOWithReadBuffer interface {
io.ReadWriter
}
// ProtoFunc function used to create a custom Proto interface.
ProtoFunc func(IOWithReadBuffer) Proto
)
// default builder of socket communication protocol.
var defaultProtoFunc = RawProtoFunc
// DefaultProtoFunc gets the default builder of socket communication protocol
func DefaultProtoFunc() ProtoFunc {
return defaultProtoFunc
}
// SetDefaultProtoFunc sets the default builder of socket communication protocol
func SetDefaultProtoFunc(protoFunc ProtoFunc) {
defaultProtoFunc = protoFunc
}
// default protocol
/*
# raw protocol format(Big Endian):
{4 bytes message length}
{1 byte protocol version} # 6
{1 byte transfer pipe length}
{transfer pipe IDs}
# The following is handled data by transfer pipe
{1 bytes sequence length}
{sequence (HEX 36 string of int32)}
{1 byte message type} # e.g. CALL:1; REPLY:2; PUSH:3
{1 bytes service method length}
{service method}
{2 bytes status length}
{status(urlencoded)}
{2 bytes metadata length}
{metadata(urlencoded)}
{1 byte body codec id}
{body}
*/
// rawProto fast socket communication protocol.
type rawProto struct {
r io.Reader
w io.Writer
rMu sync.Mutex
name string
id byte
}
// RawProtoFunc is creation function of fast socket protocol.
// NOTE: it is the default protocol.
var RawProtoFunc = func(rw IOWithReadBuffer) Proto {
return &rawProto{
id: 6,
name: "raw",
r: rw,
w: rw,
}
}
// Version returns the protocol's id and name.
func (r *rawProto) Version() (byte, string) {
return r.id, r.name
}
// Pack writes the Message into the connection.
// NOTE: Make sure to write only once or there will be package contamination!
// nolint:ineffassign
func (r *rawProto) Pack(m Message) error {
bb := utils.AcquireByteBuffer()
defer utils.ReleaseByteBuffer(bb)
// fake size
err := binary.Write(bb, binary.BigEndian, uint32(0))
// transfer pipe
bb.WriteByte(byte(m.XferPipe().Len()))
bb.Write(m.XferPipe().IDs())
prefixLen := bb.Len()
// header
err = r.writeHeader(bb, m)
if err != nil {
return err
}
// body
err = r.writeBody(bb, m)
if err != nil {
return err
}
// do transfer pipe
payload, err := m.XferPipe().OnPack(bb.B[prefixLen:])
if err != nil {
return err
}
bb.B = append(bb.B[:prefixLen], payload...)
// set and check message size
err = m.SetSize(uint32(bb.Len()))
if err != nil {
return err
}
// reset real size
binary.BigEndian.PutUint32(bb.B, m.Size())
// real write
_, err = r.w.Write(bb.B)
if err != nil {
return err
}
return err
}
func (r *rawProto) writeHeader(bb *utils.ByteBuffer, m Message) error {
seqStr := strconv.FormatInt(int64(m.Seq()), 36)
bb.WriteByte(byte(len(seqStr)))
bb.Write(goutil.StringToBytes(seqStr))
bb.WriteByte(m.Mtype())
serviceMethod := goutil.StringToBytes(m.ServiceMethod())
serviceMethodLength := len(serviceMethod)
if serviceMethodLength > math.MaxUint8 {
return errors.New("raw proto: not support service method longer than 255")
}
bb.WriteByte(byte(serviceMethodLength))
bb.Write(serviceMethod)
statusBytes := m.Status(true).EncodeQuery()
binary.Write(bb, binary.BigEndian, uint16(len(statusBytes)))
bb.Write(statusBytes)
metaBytes := m.Meta().QueryString()
binary.Write(bb, binary.BigEndian, uint16(len(metaBytes)))
bb.Write(metaBytes)
return nil
}
func (r *rawProto) writeBody(bb *utils.ByteBuffer, m Message) error {
bb.WriteByte(m.BodyCodec())
bodyBytes, err := m.MarshalBody()
if err != nil {
return err
}
bb.Write(bodyBytes)
return nil
}
// Unpack reads bytes from the connection to the Message.
// NOTE: Concurrent unsafe!
func (r *rawProto) Unpack(m Message) error {
bb := utils.AcquireByteBuffer()
defer utils.ReleaseByteBuffer(bb)
// read message
err := r.readMessage(bb, m)
if err != nil {
return err
}
// do transfer pipe
data, err := m.XferPipe().OnUnpack(bb.B)
if err != nil {
return err
}
// header
data, err = r.readHeader(data, m)
if err != nil {
return err
}
// body
return r.readBody(data, m)
}
func (r *rawProto) readMessage(bb *utils.ByteBuffer, m Message) error {
r.rMu.Lock()
defer r.rMu.Unlock()
// size
bb.ChangeLen(4)
_, err := io.ReadFull(r.r, bb.B)
if err != nil {
return err
}
_lastSize := binary.BigEndian.Uint32(bb.B)
if err = m.SetSize(_lastSize); err != nil {
return err
}
lastSize := int(_lastSize)
lastSize, err = minus(lastSize, 4)
if err != nil {
return err
}
bb.ChangeLen(lastSize)
// transfer pipe
_, err = io.ReadFull(r.r, bb.B[:1])
if err != nil {
return err
}
var xferLen = bb.B[0]
if xferLen > 0 {
_, err = io.ReadFull(r.r, bb.B[:xferLen])
if err != nil {
return err
}
err = m.XferPipe().Append(bb.B[:xferLen]...)
if err != nil {
return err
}
}
lastSize, err = minus(lastSize, 1+int(xferLen))
if err != nil {
return err
}
// read last all
bb.ChangeLen(lastSize)
_, err = io.ReadFull(r.r, bb.B)
return err
}
func minus(a int, b int) (int, error) {
r := a - b
if r < 0 || b < 0 {
return a, errors.New("raw proto: bad package")
}
return r, nil
}
func (r *rawProto) readHeader(data []byte, m Message) ([]byte, error) {
// seq
seqLen := data[0]
data = data[1:]
seq, err := strconv.ParseInt(goutil.BytesToString(data[:seqLen]), 36, 32)
if err != nil {
return nil, err
}
m.SetSeq(int32(seq))
data = data[seqLen:]
// type
m.SetMtype(data[0])
data = data[1:]
// service method
serviceMethodLen := data[0]
data = data[1:]
m.SetServiceMethod(string(data[:serviceMethodLen]))
data = data[serviceMethodLen:]
// status
statusLen := binary.BigEndian.Uint16(data)
data = data[2:]
m.Status(true).DecodeQuery(data[:statusLen])
data = data[statusLen:]
// meta
metaLen := binary.BigEndian.Uint16(data)
data = data[2:]
m.Meta().ParseBytes(data[:metaLen])
data = data[metaLen:]
return data, nil
}
func (r *rawProto) readBody(data []byte, m Message) error {
m.SetBodyCodec(data[0])
return m.UnmarshalBody(data[1:])
}