forked from samuel/go-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
367 lines (317 loc) · 6.34 KB
/
structs.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
360
361
362
363
364
365
366
367
package zk
import (
"encoding/binary"
"errors"
"reflect"
)
type id struct {
Scheme string
Id int32
}
type acl struct {
Perms int32
Id id
}
type Stat struct {
Czxid int64
Mzxid int64
Ctime int64
Mtime int64
Version int32
Cversion int32
Aversion int32
EphemeralOwner int64
DataLength int32
NumChildren int32
Pzxid int64
}
type requestHeader struct {
Xid int32
Opcode int32
}
type responseHeader struct {
Xid int32
Zxid int64
Err int32
}
type multiHeader struct {
Type int32
Done bool
Err int32
}
type auth struct {
Type int32
Scheme string
Auth []byte
}
// Generic request structs
type emptyRequest struct {
requestHeader
}
type pathRequest struct {
requestHeader
Path string
}
type pathVersionRequest struct {
requestHeader
Path string
Version int32
}
type pathWatchRequest struct {
requestHeader
Path string
Watch bool
}
type emptyResponse struct {
responseHeader
}
type pathResponse struct {
responseHeader
Path string
}
type statResponse struct {
responseHeader
Stat Stat
}
//
type checkVersionRequest pathVersionRequest
type closeRequest emptyRequest
type closeResponse emptyResponse
type connectRequest struct {
ProtocolVersion int32
LastZxidSeen int64
TimeOut int32
SessionId int64
Passwd []byte
}
type connectResponse struct {
ProtocolVersion int32
TimeOut int32
SessionId int64
Passwd []byte
}
type createRequest struct {
requestHeader
Path string
Data []byte
Acl []acl
Flags int32
}
type createResponse pathResponse
type deleteRequest pathVersionRequest
type deleteResponse emptyResponse
type errorResponse struct {
responseHeader
Err int32
}
type existsRequest pathWatchRequest
type existsResponse statResponse
type getAclRequest pathRequest
type getAclResponse struct {
responseHeader
Acl []acl
Stat Stat
}
type getChildrenRequest pathRequest
type getChildrenResponse struct {
responseHeader
Children []string
}
type getChildren2Request pathWatchRequest
type getChildren2Response struct {
responseHeader
Children []string
Stat Stat
}
type getDataRequest pathWatchRequest
type getDataResponse struct {
responseHeader
Data []byte
Stat Stat
}
type getMaxChildrenRequest pathRequest
type getMaxChildrenResponse struct {
responseHeader
Max int32
}
type getSaslRequest struct {
requestHeader
Token []byte
}
type pingRequest emptyRequest
type pingResponse emptyResponse
type setAclRequest struct {
requestHeader
Path string
Acl []acl
Version int32
}
type setAclResponse statResponse
type setDataRequest struct {
requestHeader
Path string
Data []byte
Version int32
}
type setDataResponse statResponse
type setMaxChildren struct {
requestHeader
Path string
Max int32
}
type setSaslRequest struct {
requestHeader
Token string
}
type setSaslResponse struct {
responseHeader
Token string
}
type setWatchesRequest struct {
requestHeader
RealtiveZxid int64
DataWatches []string
ExistWatches []string
ChildWatches []string
}
type setWatchesResponse struct {
responseHeader
}
type syncRequest pathRequest
type syncResponse pathResponse
type watcherEvent struct {
responseHeader
Type int32
State int32
Path string
}
func decodePacket(buf []byte, st interface{}) (int, error) {
v := reflect.ValueOf(st)
if v.Kind() != reflect.Ptr || v.IsNil() {
return 0, errors.New("Invalid ptr")
}
return decodePacketValue(buf, v.Elem())
}
func decodePacketValue(buf []byte, v reflect.Value) (int, error) {
n := 0
switch v.Kind() {
default:
return n, errors.New("Unhandled field type")
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
n2, err := decodePacketValue(buf[n:], field)
n += n2
if err != nil {
return n, err
}
}
case reflect.Bool:
v.SetBool(buf[n] != 0)
n += 1
case reflect.Int32:
v.SetInt(int64(binary.BigEndian.Uint32(buf[n : n+4])))
n += 4
case reflect.Int64:
v.SetInt(int64(binary.BigEndian.Uint64(buf[n : n+8])))
n += 8
case reflect.String:
ln := int(binary.BigEndian.Uint32(buf[n : n+4]))
v.SetString(string(buf[n+4 : n+4+ln]))
n += 4 + ln
case reflect.Slice:
switch v.Type().Elem().Kind() {
default:
count := int(binary.BigEndian.Uint32(buf[n : n+4]))
n += 4
values := reflect.MakeSlice(v.Type(), count, count)
v.Set(values)
for i := 0; i < count; i++ {
n2, err := decodePacketValue(buf[n:], values.Index(i))
n += n2
if err != nil {
return n, err
}
}
case reflect.Uint8:
ln := int(binary.BigEndian.Uint32(buf[n : n+4]))
if ln < 0 {
n += 4
v.SetBytes(nil)
} else {
bytes := make([]byte, ln)
copy(bytes, buf[n+4:n+4+ln])
v.SetBytes(bytes)
n += 4 + ln
}
}
}
return n, nil
}
func encodePacket(buf []byte, st interface{}) (int, error) {
v := reflect.ValueOf(st)
if v.Kind() != reflect.Ptr || v.IsNil() {
return 0, errors.New("Invalid ptr")
}
return encodePacketValue(buf, v.Elem())
}
func encodePacketValue(buf []byte, v reflect.Value) (int, error) {
n := 0
switch v.Kind() {
default:
return n, errors.New("Unhandled field type")
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
n2, err := encodePacketValue(buf[n:], field)
n += n2
if err != nil {
return n, err
}
}
case reflect.Bool:
if v.Bool() {
buf[n] = 1
} else {
buf[n] = 0
}
n += 1
case reflect.Int32:
binary.BigEndian.PutUint32(buf[n:n+4], uint32(v.Int()))
n += 4
case reflect.Int64:
binary.BigEndian.PutUint64(buf[n:n+8], uint64(v.Int()))
n += 8
case reflect.String:
str := v.String()
binary.BigEndian.PutUint32(buf[n:n+4], uint32(len(str)))
copy(buf[n+4:n+4+len(str)], []byte(str))
n += 4 + len(str)
case reflect.Slice:
switch v.Type().Elem().Kind() {
default:
count := int(binary.BigEndian.Uint32(buf[n : n+4]))
n += 4
values := reflect.MakeSlice(v.Type(), count, count)
v.Set(values)
for i := 0; i < count; i++ {
n2, err := decodePacketValue(buf[n:], values.Index(i))
n += n2
if err != nil {
return n, err
}
}
case reflect.Uint8:
if v.IsNil() {
binary.BigEndian.PutUint32(buf[n:n+4], uint32(0xffffffff))
n += 4
} else {
bytes := v.Bytes()
binary.BigEndian.PutUint32(buf[n:n+4], uint32(len(bytes)))
copy(buf[n+4:n+4+len(bytes)], bytes)
n += 4 + len(bytes)
}
}
}
return n, nil
}