-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasy_package.go
359 lines (295 loc) · 8.26 KB
/
easy_package.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package easycall
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"github.com/vmihailenco/msgpack"
)
const (
STX = 0x2
ETX = 0x3
HEAD_MAX_LEN = 128 * 2014
BODY_MAX_LEN = 2 * 1024 * 1024
FORMAT_JSON = 1
FORMAT_MSGPACK = 0
)
//EasyHead for EasyPackage
type EasyHead struct {
Service string `json:"service"` //service name
Method string `json:"method"` //service method
RouteKey string `json:"routeKey"` //for hash loadbalnce
Token string `json:"token"` // user login token
Uid uint64 `json:"uid"` //user login Uid
RequestIp string `json:"requestIp"` //set caller's internet ip address
TraceId string `json:"traceId"` //traceId for trace request call chain
Seq uint64 `json:"seq"` //seq for async call
Ret int `json:"ret"` //ret code,when process failed,set error code into it
Msg string `json:"msg"` //msg,when process failed,set errmsg into it
}
func NewEasyHead() *EasyHead {
return &EasyHead{}
}
func (head *EasyHead) GetService() string {
return head.Service
}
func (head *EasyHead) GetMethod() string {
return head.Method
}
func (head *EasyHead) GetRouteKey() string {
return head.RouteKey
}
func (head *EasyHead) GetToken() string {
return head.Token
}
func (head *EasyHead) GetUid() uint64 {
return head.Uid
}
func (head *EasyHead) GetRequestIp() string {
return head.RequestIp
}
func (head *EasyHead) GetTraceId() string {
return head.TraceId
}
func (head *EasyHead) GetSeq() uint64 {
return head.Seq
}
func (head *EasyHead) GetRet() int {
return head.Ret
}
func (head *EasyHead) GetMsg() string {
return head.Msg
}
func (head *EasyHead) SetService(service string) *EasyHead {
head.Service = service
return head
}
func (head *EasyHead) SetMethod(method string) *EasyHead {
head.Method = method
return head
}
func (head *EasyHead) SetRouteKey(routeKey string) *EasyHead {
head.RouteKey = routeKey
return head
}
func (head *EasyHead) SetToken(token string) *EasyHead {
head.Token = token
return head
}
func (head *EasyHead) SetUid(uid uint64) *EasyHead {
head.Uid = uid
return head
}
func (head *EasyHead) SetRequestIp(requestIp string) *EasyHead {
head.RequestIp = requestIp
return head
}
func (head *EasyHead) SetTraceId(traceId string) *EasyHead {
head.TraceId = traceId
return head
}
func (head *EasyHead) SetSeq(seq uint64) *EasyHead {
head.Seq = seq
return head
}
func (head *EasyHead) SetRet(ret int) *EasyHead {
head.Ret = ret
return head
}
func (head *EasyHead) SetMsg(msg string) *EasyHead {
head.Msg = msg
return head
}
//EasyPackage for Easycall
type EasyPackage struct {
format byte // pkg format 0 for msgpack,1 for json
head *EasyHead //pkg head
bodyData []byte //pkg body byte array
pkgData []byte //whole pkg byte array
body interface{} //pkg body decoded
}
func NewPackageWithBodyData(format byte, head *EasyHead, bodyData []byte) *EasyPackage {
return &EasyPackage{format, head, bodyData, nil, nil}
}
func NewPackageWithBody(format byte, head *EasyHead, body interface{}) *EasyPackage {
return &EasyPackage{format, head, nil, nil, body}
}
func DecodeWithBodyData(pkgData []byte) (*EasyPackage, error) {
var err error = nil
format := pkgData[1]
headLen := binary.BigEndian.Uint32(pkgData[2:6])
bodyLen := binary.BigEndian.Uint32(pkgData[6:10])
headData := pkgData[10 : 10+headLen]
bodyData := pkgData[10+headLen : 10+headLen+bodyLen]
if format == FORMAT_JSON {
head := &EasyHead{}
err = json.NewDecoder(bytes.NewReader(headData)).Decode(head)
if err != nil {
return nil, err
}
return &EasyPackage{format, head, bodyData, pkgData, nil}, nil
} else if format == FORMAT_MSGPACK {
head := &EasyHead{}
err = msgpack.NewDecoder(bytes.NewReader(headData)).UseJSONTag(true).Decode(head)
if err != nil {
return nil, err
}
return &EasyPackage{format, head, bodyData, pkgData, nil}, nil
} else {
return nil, errors.New("invalid pkg format")
}
}
func DecodeWithBody(pkgData []byte) (*EasyPackage, error) {
var err error = nil
format := pkgData[1]
headLen := binary.BigEndian.Uint32(pkgData[2:6])
bodyLen := binary.BigEndian.Uint32(pkgData[6:10])
headData := pkgData[10 : 10+headLen]
bodyData := pkgData[10+headLen : 10+headLen+bodyLen]
if format == FORMAT_JSON {
head := &EasyHead{}
err = json.NewDecoder(bytes.NewReader(headData)).Decode(head)
if err != nil {
return nil, err
}
body := make(map[string]interface{}, 0)
err = json.NewDecoder(bytes.NewReader(bodyData)).Decode(&body)
if err != nil {
return nil, err
}
return &EasyPackage{format, head, nil, pkgData, (interface{})(body)}, nil
return NewPackageWithBody(format, head, (interface{})(body)), nil
} else if format == FORMAT_MSGPACK {
head := &EasyHead{}
err = msgpack.NewDecoder(bytes.NewReader(headData)).UseJSONTag(true).Decode(head)
if err != nil {
return nil, err
}
body := make(map[string]interface{}, 0)
err = msgpack.NewDecoder(bytes.NewReader(bodyData)).UseJSONTag(true).Decode(body)
if err != nil {
return nil, err
}
return &EasyPackage{format, head, nil, pkgData, (interface{})(body)}, nil
} else {
return nil, errors.New("invalid pkg format")
}
}
func (pkg *EasyPackage) EncodeWithBody() ([]byte, error) {
var headLen int
var bodyLen int
var err error
var buf bytes.Buffer
prefix := make([]byte, 10)
buf.Write(prefix)
if pkg.format == FORMAT_MSGPACK {
err = msgpack.NewEncoder(&buf).UseJSONTag(true).Encode(pkg.head)
if err != nil {
return nil, err
}
headLen = buf.Len() - 10
err = msgpack.NewEncoder(&buf).UseJSONTag(true).Encode(pkg.body)
if err != nil {
return nil, err
}
bodyLen = buf.Len() - headLen - 10
} else if pkg.format == FORMAT_JSON {
err = json.NewEncoder(&buf).Encode(pkg.head)
if err != nil {
return nil, err
}
headLen = buf.Len() - 10
err = json.NewEncoder(&buf).Encode(pkg.body)
if err != nil {
return nil, err
}
bodyLen = buf.Len() - headLen - 10
} else {
return nil, errors.New("invalid pkg format")
}
buf.WriteByte(ETX)
pkgData := buf.Bytes()
pkgData[0] = STX
pkgData[1] = pkg.format
binary.BigEndian.PutUint32(pkgData[2:6], uint32(headLen))
binary.BigEndian.PutUint32(pkgData[6:10], uint32(bodyLen))
return pkgData, nil
}
func (pkg *EasyPackage) EncodeWithBodyData() ([]byte, error) {
var headLen int
var bodyLen int
var err error
var buf bytes.Buffer
prefix := make([]byte, 10)
buf.Write(prefix)
if pkg.format == FORMAT_MSGPACK {
err = msgpack.NewEncoder(&buf).UseJSONTag(true).Encode(pkg.head)
if err != nil {
return nil, err
}
headLen = buf.Len() - 10
buf.Write(pkg.bodyData)
bodyLen = buf.Len() - headLen - 10
} else if pkg.format == FORMAT_JSON {
err = json.NewEncoder(&buf).Encode(pkg.head)
if err != nil {
return nil, err
}
headLen = buf.Len() - 10
buf.Write(pkg.bodyData)
bodyLen = buf.Len() - headLen - 10
} else {
return nil, errors.New("invalid pkg format")
}
buf.WriteByte(ETX)
pkgData := buf.Bytes()
pkgData[0] = STX
pkgData[1] = pkg.format
binary.BigEndian.PutUint32(pkgData[2:6], uint32(headLen))
binary.BigEndian.PutUint32(pkgData[6:10], uint32(bodyLen))
return pkgData, nil
}
func (pkg *EasyPackage) GetFormat() byte {
return pkg.format
}
func (pkg *EasyPackage) SetFormat(format byte) *EasyPackage {
pkg.format = format
return pkg
}
func (pkg *EasyPackage) SetHead(head *EasyHead) *EasyPackage {
pkg.head = head
return pkg
}
func (pkg *EasyPackage) SetBody(body interface{}) *EasyPackage {
pkg.body = body
return pkg
}
func (pkg *EasyPackage) GetHead() *EasyHead {
return pkg.head
}
func (pkg *EasyPackage) GetBodyData() []byte {
return pkg.bodyData
}
func (pkg *EasyPackage) SetBodyData(bodyData []byte) *EasyPackage {
pkg.bodyData = bodyData
return pkg
}
func (pkg *EasyPackage) GetPkgData() []byte {
return pkg.pkgData
}
func (pkg *EasyPackage) SetPkgData(pkgData []byte) *EasyPackage {
pkg.pkgData = pkgData
return pkg
}
func (pkg *EasyPackage) GetBody() interface{} {
return pkg.body
}
func (pkg *EasyPackage) DecodeBody(body interface{}) error {
if pkg.format == FORMAT_MSGPACK {
return msgpack.NewDecoder(bytes.NewReader(pkg.bodyData)).UseJSONTag(true).Decode(body)
} else if pkg.format == FORMAT_JSON {
return json.NewDecoder(bytes.NewReader(pkg.bodyData)).Decode(body)
} else {
return errors.New("invalid package format")
}
}