-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.go
More file actions
290 lines (241 loc) · 6.54 KB
/
protocol.go
File metadata and controls
290 lines (241 loc) · 6.54 KB
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
package flow
import (
"encoding/binary"
)
// frame format:
// | protocol version (uint32) |
// | frame type (uint32) |
// | len(payload) (uint32) |
// | payload ([]byte) |
//
// The version field is reserved for later frame evolution. Currently
// this field is set, but not used anywhere.
const (
protocolVersion = 1
frameHeaderLen = 12 // version + frame type + payload length
)
type frameType uint
const (
// clique
frameTypeJoin = frameType(uint('J')<<24 | uint('O')<<16 | uint('I')<<8 | uint('N'))
frameTypeLeave = frameType(uint('L')<<24 | uint('E')<<16 | uint('A')<<8 | uint('V'))
frameTypeInfo = frameType(uint('I')<<24 | uint('N')<<16 | uint('F')<<8 | uint('O'))
frameTypePing = frameType(uint('P')<<24 | uint('I')<<16 | uint('N')<<8 | uint('G'))
// messages
frameTypeMsg = frameType(uint('M')<<24 | uint('S')<<16 | uint('G')<<8 | uint(' '))
frameTypeFwd = frameType(uint('F')<<24 | uint('W')<<16 | uint('D')<<8 | uint(' '))
frameTypeAck = frameType(uint('A')<<24 | uint('C')<<16 | uint('K')<<8 | uint(' '))
)
func (t frameType) String() string {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(t))
res := buf[:]
if res[3] == ' ' {
res = buf[:3]
}
return string(res)
}
type frame []byte
func newFrame(typ frameType, payloadSize int, buf frame) frame {
buf = alloc(frameHeaderLen+payloadSize, buf)
binary.BigEndian.PutUint32(buf[0:4], protocolVersion)
binary.BigEndian.PutUint32(buf[4:8], uint32(typ))
binary.BigEndian.PutUint32(buf[8:12], uint32(payloadSize))
return buf
}
func unmarshalFrame(p []byte) (frame, error) {
if len(p) < frameHeaderLen || len(p) < frameHeaderLen+frame(p).payloadSize() {
return nil, errMalformedFrame
}
return frame(p), nil
}
func (f frame) typ() frameType {
return frameType(binary.BigEndian.Uint32(f[4:8]))
}
func (f frame) payloadSize() int {
return int(binary.BigEndian.Uint32(f[8:]))
}
func (f frame) payload() []byte {
return f[frameHeaderLen : frameHeaderLen+f.payloadSize()]
}
// join
type join struct {
sender Key
}
func marshalJoin(join join) frame {
f := newFrame(frameTypeJoin, keySize, nil)
marshalKey(join.sender, f.payload())
return f
}
func unmarshalJoin(f frame) (join join, err error) {
if join.sender, err = unmarshalKey(f.payload()); err != nil {
return join, errMalformedJoin
}
return join, nil
}
// leave
type leave struct {
node Key
}
func marshalLeave(leave leave) frame {
f := newFrame(frameTypeLeave, keySize, nil)
marshalKey(leave.node, f.payload())
return f
}
func unmarshalLeave(f frame) (leave leave, err error) {
if leave.node, err = unmarshalKey(f.payload()); err != nil {
return leave, errMalformedLeave
}
return leave, nil
}
// info
type info struct {
id uint64
neighbors keys
}
func marshalInfo(info info) frame {
f := newFrame(frameTypeInfo, 8+len(info.neighbors), nil)
p := f.payload()
binary.BigEndian.PutUint64(p, info.id)
copy(p[8:], info.neighbors)
return f
}
func unmarshalInfo(f frame) (info info, err error) {
p := f.payload()
if len(p) < 8 {
return info, errMalformedInfo
}
info.id = binary.BigEndian.Uint64(p)
if info.neighbors, err = unmarshalKeys(p[8:]); err != nil {
return info, errMalformedInfo
}
return info, nil
}
// ping
type ping struct {
id uint64 // used for replies
sender Key
}
func marshalPing(ping ping, buf frame) frame {
f := newFrame(frameTypePing, 8+keySize, buf)
p := f.payload()
binary.BigEndian.PutUint64(p, ping.id)
marshalKey(ping.sender, p[8:])
return f
}
func unmarshalPing(f frame) (ping ping, err error) {
p := f.payload()
if len(p) < 8+keySize {
return ping, errMalformedPing
}
ping.id = binary.BigEndian.Uint64(p)
if ping.sender, err = unmarshalKey(p[8:]); err != nil {
return ping, errMalformedPing
}
return ping, nil
}
// msg
type msg struct {
id uint64
reply []byte // node stream of sender
stream []byte
pkey []byte
data []byte
}
func msgSize(msg msg) int {
return 20 + len(msg.reply) + len(msg.stream) + len(msg.pkey) + len(msg.data)
}
func marshalMsgPayload(msg msg, p []byte) {
replyLen := uint32(len(msg.reply))
streamLen := uint32(len(msg.stream))
pkeyLen := uint32(len(msg.pkey))
binary.BigEndian.PutUint64(p, msg.id)
binary.BigEndian.PutUint32(p[8:], replyLen)
copy(p[12:], msg.reply)
binary.BigEndian.PutUint32(p[12+replyLen:], streamLen)
copy(p[16+replyLen:], msg.stream)
binary.BigEndian.PutUint32(p[16+replyLen+streamLen:], pkeyLen)
copy(p[20+replyLen+streamLen:], msg.pkey)
copy(p[20+replyLen+streamLen+pkeyLen:], msg.data)
}
func unmarshalMsgPayload(p []byte) (msg msg, err error) {
const minSize = 20
if len(p) < minSize {
return msg, errMalformedMsg
}
replyLen := binary.BigEndian.Uint32(p[8:])
if len(p) < minSize+int(replyLen) {
return msg, errMalformedMsg
}
streamLen := binary.BigEndian.Uint32(p[12+replyLen:])
if len(p) < minSize+int(replyLen+streamLen) {
return msg, errMalformedMsg
}
pkeyLen := binary.BigEndian.Uint32(p[16+replyLen+streamLen:])
if len(p) < minSize+int(replyLen+streamLen+pkeyLen) {
return msg, errMalformedMsg
}
msg.id = binary.BigEndian.Uint64(p)
msg.reply = p[12 : 12+replyLen]
msg.stream = p[16+replyLen : 16+replyLen+streamLen]
msg.pkey = p[20+replyLen+streamLen : 20+replyLen+streamLen+pkeyLen]
msg.data = p[20+replyLen+streamLen+pkeyLen:]
return msg, nil
}
func marshalMsg(msg msg) frame {
f := newFrame(frameTypeMsg, msgSize(msg), nil)
marshalMsgPayload(msg, f.payload())
return f
}
func unmarshalMsg(f frame) (msg msg, err error) {
return unmarshalMsgPayload(f.payload())
}
// fwd
type fwd struct {
id uint64
ack Key
msg msg // needs to be last
}
func marshalFwd(fwd fwd) frame {
f := newFrame(frameTypeFwd, 8+keySize+msgSize(fwd.msg), nil)
p := f.payload()
binary.BigEndian.PutUint64(p, fwd.id)
marshalKey(fwd.ack, p[8:])
marshalMsgPayload(fwd.msg, p[8+keySize:])
return f
}
func unmarshalFwd(f frame) (fwd fwd, err error) {
p := f.payload()
if len(p) < 8+keySize {
return fwd, errMalformedFwd
}
fwd.id = binary.BigEndian.Uint64(p)
if fwd.ack, err = unmarshalKey(p[8 : 8+keySize]); err != nil {
return fwd, errMalformedFwd
}
if fwd.msg, err = unmarshalMsgPayload(p[8+keySize:]); err != nil {
return fwd, errMalformedFwd
}
return fwd, nil
}
// ack
type ack struct {
id uint64
data []byte
}
func marshalAck(ack ack) frame {
f := newFrame(frameTypeAck, 8+len(ack.data), nil)
p := f.payload()
binary.BigEndian.PutUint64(p, ack.id)
copy(p[8:], ack.data)
return f
}
func unmarshalAck(f frame) (ack ack, err error) {
p := f.payload()
if len(p) < 8 {
return ack, errMalformedAck
}
ack.id = binary.BigEndian.Uint64(p)
ack.data = p[8:]
return ack, nil
}