-
Notifications
You must be signed in to change notification settings - Fork 1
/
codec.code.go
254 lines (245 loc) · 5.96 KB
/
codec.code.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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"github.com/hslam/code"
)
type request struct {
Seq uint64
Upgrade []byte
ServiceMethod string
Args []byte
}
//Marshal marshals the Request into buf and returns the bytes.
func (req *request) Marshal(buf []byte) ([]byte, error) {
var size uint64
size += 10
size += 10 + uint64(len(req.Upgrade))
size += 10 + uint64(len(req.ServiceMethod))
size += 10 + uint64(len(req.Args))
if uint64(cap(buf)) >= size {
buf = buf[:size]
} else {
buf = make([]byte, size)
}
var offset uint64
var n uint64
//n = code.EncodeVarint(buf[offset:], req.Seq)
{
var t = req.Seq
var size = code.SizeofVarint(t)
for i := uint64(0); i < size-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+size-1] = byte(t)
n = size
}
offset += n
//n = code.EncodeBytes(buf[offset:], req.Upgrade)
if len(req.Upgrade) > 127 {
var length = uint64(len(req.Upgrade))
var lengthSize = code.SizeofVarint(length)
var s = lengthSize + length
t := length
for i := uint64(0); i < lengthSize-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+lengthSize-1] = byte(t)
copy(buf[offset+lengthSize:], req.Upgrade)
n = s
} else if len(req.Upgrade) > 0 {
var length = uint64(len(req.Upgrade))
buf[offset] = byte(length)
copy(buf[offset+1:], req.Upgrade)
n = 1 + length
} else {
buf[offset] = 0
n = 1
}
offset += n
//n = code.EncodeString(buf[offset:], req.ServiceMethod)
if len(req.ServiceMethod) > 127 {
var length = uint64(len(req.ServiceMethod))
var lengthSize = code.SizeofVarint(length)
var s = lengthSize + length
t := length
for i := uint64(0); i < lengthSize-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+lengthSize-1] = byte(t)
copy(buf[offset+lengthSize:], req.ServiceMethod)
n = s
} else if len(req.ServiceMethod) > 0 {
var length = uint64(len(req.ServiceMethod))
buf[offset] = byte(length)
copy(buf[offset+1:], req.ServiceMethod)
n = 1 + length
} else {
buf[offset] = 0
n = 1
}
offset += n
//n = code.EncodeBytes(buf[offset:], req.Args)
if len(req.Args) > 127 {
var length = uint64(len(req.Args))
var lengthSize = code.SizeofVarint(length)
var s = lengthSize + length
t := length
for i := uint64(0); i < lengthSize-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+lengthSize-1] = byte(t)
copy(buf[offset+lengthSize:], req.Args)
n = s
} else if len(req.Args) > 0 {
var length = uint64(len(req.Args))
buf[offset] = byte(length)
copy(buf[offset+1:], req.Args)
n = 1 + length
} else {
buf[offset] = 0
n = 1
}
offset += n
return buf[:offset], nil
}
//Unmarshal unmarshals the Request from buf and returns the number of bytes read (> 0).
func (req *request) Unmarshal(data []byte) (uint64, error) {
var offset uint64
var n uint64
n = code.DecodeVarint(data[offset:], &req.Seq)
offset += n
if data[offset] > 127 {
n = code.DecodeBytes(data[offset:], &req.Upgrade)
} else if data[offset] > 0 {
var s = 1 + uint64(data[offset])
req.Upgrade = data[offset+1 : offset+s]
n = s
} else {
n = 1
}
offset += n
if data[offset] > 0 {
n = code.DecodeString(data[offset:], &req.ServiceMethod)
} else {
n = 1
}
offset += n
if data[offset] > 127 {
n = code.DecodeBytes(data[offset:], &req.Args)
} else if data[offset] > 0 {
var s = 1 + uint64(data[offset])
req.Args = data[offset+1 : offset+s]
n = s
} else {
n = 1
}
offset += n
return offset, nil
}
type response struct {
Seq uint64
Error string
Reply []byte
}
//Marshal marshals the Response into buf and returns the bytes.
func (res *response) Marshal(buf []byte) ([]byte, error) {
var size uint64
size += 10
size += 10 + uint64(len(res.Error))
size += 10 + uint64(len(res.Reply))
if uint64(cap(buf)) >= size {
buf = buf[:size]
} else {
buf = make([]byte, size)
}
var offset uint64
var n uint64
//n = code.EncodeVarint(buf[offset:], res.Seq)
{
var t = res.Seq
var size = code.SizeofVarint(t)
for i := uint64(0); i < size-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+size-1] = byte(t)
n = size
}
offset += n
//n = code.EncodeString(buf[offset:], res.Error)
if len(res.Error) > 127 {
var length = uint64(len(res.Error))
var lengthSize = code.SizeofVarint(length)
var s = lengthSize + length
t := length
for i := uint64(0); i < lengthSize-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+lengthSize-1] = byte(t)
copy(buf[offset+lengthSize:], res.Error)
n = s
} else if len(res.Error) > 0 {
var length = uint64(len(res.Error))
buf[offset] = byte(length)
copy(buf[offset+1:], res.Error)
n = 1 + length
} else {
buf[offset] = 0
n = 1
}
offset += n
//n = code.EncodeBytes(buf[offset:], res.Reply)
if len(res.Reply) > 127 {
var length = uint64(len(res.Reply))
var lengthSize = code.SizeofVarint(length)
var s = lengthSize + length
t := length
for i := uint64(0); i < lengthSize-1; i++ {
buf[offset+i] = byte(t) | 0x80
t >>= 7
}
buf[offset+lengthSize-1] = byte(t)
copy(buf[offset+lengthSize:], res.Reply)
n = s
} else if len(res.Reply) > 0 {
var length = uint64(len(res.Reply))
buf[offset] = byte(length)
copy(buf[offset+1:], res.Reply)
n = 1 + length
} else {
buf[offset] = 0
n = 1
}
offset += n
return buf[:offset], nil
}
//Unmarshal unmarshals the Response from buf and returns the number of bytes read (> 0).
func (res *response) Unmarshal(data []byte) (uint64, error) {
var offset uint64
var n uint64
n = code.DecodeVarint(data[offset:], &res.Seq)
offset += n
if data[offset] > 0 {
n = code.DecodeString(data[offset:], &res.Error)
} else {
n = 1
}
offset += n
if data[offset] > 127 {
n = code.DecodeBytes(data[offset:], &res.Reply)
} else if data[offset] > 0 {
var s = 1 + uint64(data[offset])
res.Reply = data[offset+1 : offset+s]
n = s
} else {
n = 1
}
offset += n
return offset, nil
}