-
Notifications
You must be signed in to change notification settings - Fork 3
/
message.go
118 lines (102 loc) · 3.01 KB
/
message.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
package riago
import (
"errors"
"github.com/golang/protobuf/proto"
)
const (
MsgRpbErrorResp = 0
MsgRpbPingReq = 1
MsgRpbPingResp = 2
MsgRpbGetClientIdReq = 3
MsgRpbGetClientIdResp = 4
MsgRpbSetClientIdReq = 5
MsgRpbSetClientIdResp = 6
MsgRpbGetServerInfoReq = 7
MsgRpbGetServerInfoResp = 8
MsgRpbGetReq = 9
MsgRpbGetResp = 10
MsgRpbPutReq = 11
MsgRpbPutResp = 12
MsgRpbDelReq = 13
MsgRpbDelResp = 14
MsgRpbListBucketsReq = 15
MsgRpbListBucketsResp = 16
MsgRpbListKeysReq = 17
MsgRpbListKeysResp = 18
MsgRpbGetBucketReq = 19
MsgRpbGetBucketResp = 20
MsgRpbSetBucketReq = 21
MsgRpbSetBucketResp = 22
MsgRpbMapRedReq = 23
MsgRpbMapRedResp = 24
MsgRpbIndexReq = 25
MsgRpbIndexResp = 26
MsgRpbSearchQueryReq = 27
MsgRbpSearchQueryResp = 28
MsgRpbResetBucketReq = 29
MsgRpbResetBucketResp = 30
MsgRpbCSBucketReq = 40
MsgRpbCSBucketResp = 41
MsgRpbCounterUpdateReq = 50
MsgRpbCounterUpdateResp = 51
MsgRpbCounterGetReq = 52
MsgRpbCounterGetResp = 53
MsgRpbYokozunaIndexGetReq = 54
MsgRpbYokozunaIndexGetResp = 55
MsgRpbYokozunaIndexPutReq = 56
MsgRpbYokozunaIndexDeleteReq = 57
MsgRpbYokozunaSchemaGetReq = 58
MsgRpbYokozunaSchemaGetResp = 59
MsgRpbYokozunaSchemaPutReq = 60
MsgDtFetchReq = 80
MsgDtFetchResp = 81
MsgDtUpdateReq = 82
MsgDtUpdateResp = 83
)
var (
ErrInvalidResponseBody = errors.New("invalid response body")
ErrInvalidResponseCode = errors.New("invalid response code")
ErrInvalidRequestCode = errors.New("invalid request code")
)
// Encodes a request code and proto structure into a message byte buffer
func encode(code uint8, req proto.Message) (buf []byte, err error) {
var reqbuf []byte
var size int32
if req != nil {
if reqbuf, err = proto.Marshal(req); err != nil {
return
}
}
size = int32(len(reqbuf) + 1)
buf = []byte{byte(size >> 24), byte(size >> 16), byte(size >> 8), byte(size), code}
buf = append(buf, reqbuf...)
return
}
// Decodes a message byte buffer into a proto response, error code or nil
// Resulting object depends on response type.
func decode(buf []byte, resp proto.Message) (err error) {
var code uint8
var respbuf []byte
if len(buf) < 1 {
err = ErrInvalidResponseCode
return
}
code = buf[0]
if len(buf) > 1 {
respbuf = buf[1:]
} else {
respbuf = make([]byte, 0)
}
switch code {
case MsgRpbErrorResp:
errResp := &RpbErrorResp{}
if err = proto.Unmarshal(respbuf, errResp); err == nil {
err = errors.New(string(errResp.Errmsg))
}
case MsgRpbPingResp, MsgRpbSetClientIdResp, MsgRpbSetBucketResp, MsgRpbDelResp:
resp = nil
default:
err = proto.Unmarshal(respbuf, resp)
}
return
}