This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
proto.go
606 lines (557 loc) · 14.6 KB
/
proto.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Copyright (c) 2013 Project Iris. All rights reserved.
//
// The current language binding is an official support library of the Iris
// cloud messaging framework, and as such, the same licensing terms apply.
// For details please see http://iris.karalabe.com/downloads#License
// Contains the wire protocol for communicating with the Iris relay endpoint.
// The specification version implemented is v1.0-draft2, available at:
// http://iris.karalabe.com/specs/relay-protocol-v1.0-draft2.pdf
package iris
import (
"fmt"
"io"
"sync/atomic"
"time"
)
// Packet opcodes
const (
opInit byte = 0x00 // Out: connection initiation | In: connection acceptance
opDeny = 0x01 // Out: <never sent> | In: connection refusal
opClose = 0x02 // Out: connection tear-down initiation | In: connection tear-down notification
opBroadcast = 0x03 // Out: application broadcast initiation | In: application broadcast delivery
opRequest = 0x04 // Out: application request initiation | In: application request delivery
opReply = 0x05 // Out: application reply initiation | In: application reply delivery
opSubscribe = 0x06 // Out: topic subscription | In: <never received>
opUnsubscribe = 0x07 // Out: topic subscription removal | In: <never received>
opPublish = 0x08 // Out: topic event publish | In: topic event delivery
opTunInit = 0x09 // Out: tunnel construction request | In: tunnel initiation
opTunConfirm = 0x0a // Out: tunnel confirmation | In: tunnel construction result
opTunAllow = 0x0b // Out: tunnel transfer allowance | In: <same as out>
opTunTransfer = 0x0c // Out: tunnel data exchange | In: <same as out>
opTunClose = 0x0d // Out: tunnel termination request | In: tunnel termination notification
)
// Protocol constants
var (
protoVersion = "v1.0-draft2"
clientMagic = "iris-client-magic"
relayMagic = "iris-relay-magic"
)
// Serializes a single byte into the relay connection.
func (c *Connection) sendByte(data byte) error {
return c.sockBuf.WriteByte(data)
}
// Serializes a boolean into the relay connection.
func (c *Connection) sendBool(data bool) error {
if data {
return c.sendByte(1)
}
return c.sendByte(0)
}
// Serializes a variable int using base 128 encoding into the relay connection.
func (c *Connection) sendVarint(data uint64) error {
for data > 127 {
// Internal byte, set the continuation flag and send
if err := c.sendByte(byte(128 + data%128)); err != nil {
return err
}
data /= 128
}
// Final byte, send and return
return c.sendByte(byte(data))
}
// Serializes a length-tagged binary array into the relay connection.
func (c *Connection) sendBinary(data []byte) error {
if err := c.sendVarint(uint64(len(data))); err != nil {
return err
}
if _, err := c.sockBuf.Write([]byte(data)); err != nil {
return err
}
return nil
}
// Serializes a length-tagged string into the relay connection.
func (c *Connection) sendString(data string) error {
return c.sendBinary([]byte(data))
}
// Serializes a packet through a closure into the relay connection.
func (c *Connection) sendPacket(closure func() error) error {
// Increment the pending write count
atomic.AddInt32(&c.sockWait, 1)
// Acquire the socket lock
c.sockLock.Lock()
defer c.sockLock.Unlock()
// Send the packet itself
if err := closure(); err != nil {
// Decrement the pending count and error out
atomic.AddInt32(&c.sockWait, -1)
return err
}
// Flush the stream if no more messages are pending
if atomic.AddInt32(&c.sockWait, -1) == 0 {
return c.sockBuf.Flush()
}
return nil
}
// Sends a connection initiation.
func (c *Connection) sendInit(cluster string) error {
return c.sendPacket(func() error {
if err := c.sendByte(opInit); err != nil {
return err
}
if err := c.sendString(clientMagic); err != nil {
return err
}
if err := c.sendString(protoVersion); err != nil {
return err
}
return c.sendString(cluster)
})
}
// Sends a connection tear-down initiation.
func (c *Connection) sendClose() error {
return c.sendPacket(func() error {
return c.sendByte(opClose)
})
}
// Sends an application broadcast initiation.
func (c *Connection) sendBroadcast(cluster string, message []byte) error {
return c.sendPacket(func() error {
if err := c.sendByte(opBroadcast); err != nil {
return err
}
if err := c.sendString(cluster); err != nil {
return err
}
return c.sendBinary(message)
})
}
// Sends an application request initiation.
func (c *Connection) sendRequest(id uint64, cluster string, request []byte, timeout int) error {
return c.sendPacket(func() error {
if err := c.sendByte(opRequest); err != nil {
return err
}
if err := c.sendVarint(id); err != nil {
return err
}
if err := c.sendString(cluster); err != nil {
return err
}
if err := c.sendBinary(request); err != nil {
return err
}
return c.sendVarint(uint64(timeout))
})
}
// Sends an application reply initiation.
func (c *Connection) sendReply(id uint64, reply []byte, fault string) error {
return c.sendPacket(func() error {
if err := c.sendByte(opReply); err != nil {
return err
}
if err := c.sendVarint(id); err != nil {
return err
}
success := (len(fault) == 0)
if err := c.sendBool(success); err != nil {
return err
}
if success {
return c.sendBinary(reply)
} else {
return c.sendString(fault)
}
})
}
// Sends a topic subscription.
func (c *Connection) sendSubscribe(topic string) error {
return c.sendPacket(func() error {
if err := c.sendByte(opSubscribe); err != nil {
return err
}
return c.sendString(topic)
})
}
// Sends a topic subscription removal.
func (c *Connection) sendUnsubscribe(topic string) error {
return c.sendPacket(func() error {
if err := c.sendByte(opUnsubscribe); err != nil {
return err
}
return c.sendString(topic)
})
}
// Sends a topic event publish.
func (c *Connection) sendPublish(topic string, event []byte) error {
return c.sendPacket(func() error {
if err := c.sendByte(opPublish); err != nil {
return err
}
if err := c.sendString(topic); err != nil {
return err
}
return c.sendBinary(event)
})
}
// Sends a tunnel construction request.
func (c *Connection) sendTunnelInit(id uint64, cluster string, timeout int) error {
return c.sendPacket(func() error {
if err := c.sendByte(opTunInit); err != nil {
return err
}
if err := c.sendVarint(id); err != nil {
return err
}
if err := c.sendString(cluster); err != nil {
return err
}
return c.sendVarint(uint64(timeout))
})
}
// Sends a tunnel confirmation.
func (c *Connection) sendTunnelConfirm(buildId, tunId uint64) error {
return c.sendPacket(func() error {
if err := c.sendByte(opTunConfirm); err != nil {
return err
}
if err := c.sendVarint(buildId); err != nil {
return err
}
return c.sendVarint(tunId)
})
}
// Sends a tunnel transfer allowance.
func (c *Connection) sendTunnelAllowance(id uint64, space int) error {
return c.sendPacket(func() error {
if err := c.sendByte(opTunAllow); err != nil {
return err
}
if err := c.sendVarint(id); err != nil {
return err
}
return c.sendVarint(uint64(space))
})
}
// Sends a tunnel data exchange.
func (c *Connection) sendTunnelTransfer(id uint64, sizeOrCont int, payload []byte) error {
return c.sendPacket(func() error {
if err := c.sendByte(opTunTransfer); err != nil {
return err
}
if err := c.sendVarint(id); err != nil {
return err
}
if err := c.sendVarint(uint64(sizeOrCont)); err != nil {
return err
}
return c.sendBinary(payload)
})
}
// Sends a tunnel termination request.
func (c *Connection) sendTunnelClose(id uint64) error {
return c.sendPacket(func() error {
if err := c.sendByte(opTunClose); err != nil {
return err
}
return c.sendVarint(id)
})
}
// Retrieves a single byte from the relay connection.
func (c *Connection) recvByte() (byte, error) {
return c.sockBuf.ReadByte()
}
// Retrieves a boolean from the relay connection.
func (c *Connection) recvBool() (bool, error) {
b, err := c.recvByte()
if err != nil {
return false, err
}
switch b {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, fmt.Errorf("protocol violation: invalid boolean value: %v", b)
}
}
// Retrieves a variable int in base 128 encoding from the relay connection.
func (c *Connection) recvVarint() (uint64, error) {
var num uint64
for i := uint(0); ; i++ {
chunk, err := c.recvByte()
if err != nil {
return 0, err
}
num += uint64(chunk&127) << (7 * i)
if chunk <= 127 {
break
}
}
return num, nil
}
// Retrieves a length-tagged binary array from the relay connection.
func (c *Connection) recvBinary() ([]byte, error) {
// Fetch the length of the binary blob
size, err := c.recvVarint()
if err != nil {
return nil, err
}
// Fetch the blob itself
data := make([]byte, size)
if _, err := io.ReadFull(c.sockBuf, data); err != nil {
return nil, err
}
return data, nil
}
// Retrieves a length-tagged string from the relay connection.
func (c *Connection) recvString() (string, error) {
if data, err := c.recvBinary(); err != nil {
return "", err
} else {
return string(data), nil
}
}
// Retrieves a connection initiation response (either accept or deny).
func (c *Connection) procInit() (string, error) {
// Retrieve the response opcode
op, err := c.recvByte()
if err != nil {
return "", err
}
// Verify the opcode validity and relay magic string
switch {
case op == opInit || op == opDeny:
if magic, err := c.recvString(); err != nil {
return "", err
} else if magic != relayMagic {
return "", fmt.Errorf("protocol violation: invalid relay magic: %s", magic)
}
default:
return "", fmt.Errorf("protocol violation: invalid init response opcode: %v", op)
}
// Depending on success or failure, proceed and return
switch op {
case opInit:
// Read the highest supported protocol version
if version, err := c.recvString(); err != nil {
return "", err
} else {
return version, nil
}
case opDeny:
// Read the reason for connection denial
if reason, err := c.recvString(); err != nil {
return "", err
} else {
return "", fmt.Errorf("connection denied: %s", reason)
}
default:
panic("unreachable code")
}
}
// Retrieves a connection tear-down notification.
func (c *Connection) procClose() (string, error) {
return c.recvString()
}
// Retrieves an application broadcast delivery.
func (c *Connection) procBroadcast() error {
message, err := c.recvBinary()
if err != nil {
return err
}
c.handleBroadcast(message)
return nil
}
// Retrieves an application request delivery.
func (c *Connection) procRequest() error {
id, err := c.recvVarint()
if err != nil {
return err
}
request, err := c.recvBinary()
if err != nil {
return err
}
timeout, err := c.recvVarint()
if err != nil {
return err
}
c.handleRequest(id, request, time.Duration(timeout)*time.Millisecond)
return nil
}
// Retrieves an application reply delivery.
func (c *Connection) procReply() error {
id, err := c.recvVarint()
if err != nil {
return err
}
timeout, err := c.recvBool()
if err != nil {
return err
}
if timeout {
c.handleReply(id, nil, "")
return nil
}
// The request didn't time out, get the result
success, err := c.recvBool()
if err != nil {
return err
}
if success {
reply, err := c.recvBinary()
if err != nil {
return err
}
c.handleReply(id, reply, "")
} else {
fault, err := c.recvString()
if err != nil {
return err
}
c.handleReply(id, nil, fault)
}
return nil
}
// Retrieves a topic event delivery.
func (c *Connection) procPublish() error {
topic, err := c.recvString()
if err != nil {
return err
}
event, err := c.recvBinary()
if err != nil {
return err
}
go c.handlePublish(topic, event)
return nil
}
// Retrieves a tunnel initiation message.
func (c *Connection) procTunnelInit() error {
id, err := c.recvVarint()
if err != nil {
return err
}
chunkLimit, err := c.recvVarint()
if err != nil {
return err
}
c.handleTunnelInit(id, int(chunkLimit))
return nil
}
// Retrieves a tunnel construction result.
func (c *Connection) procTunnelResult() error {
id, err := c.recvVarint()
if err != nil {
return err
}
timeout, err := c.recvBool()
if err != nil {
return err
}
if timeout {
c.handleTunnelResult(id, 0)
return nil
}
// The tunnel didn't time out, proceed
chunkLimit, err := c.recvVarint()
if err != nil {
return err
}
c.handleTunnelResult(id, int(chunkLimit))
return nil
}
// Retrieves a tunnel transfer allowance message.
func (c *Connection) procTunnelAllowance() error {
id, err := c.recvVarint()
if err != nil {
return err
}
space, err := c.recvVarint()
if err != nil {
return err
}
c.handleTunnelAllowance(id, int(space))
return nil
}
// Retrieves a tunnel data exchange message.
func (c *Connection) procTunnelTransfer() error {
id, err := c.recvVarint()
if err != nil {
return err
}
size, err := c.recvVarint()
if err != nil {
return err
}
payload, err := c.recvBinary()
if err != nil {
return err
}
c.handleTunnelTransfer(id, int(size), payload)
return nil
}
// Retrieves a tunnel closure notification.
func (c *Connection) procTunnelClose() error {
id, err := c.recvVarint()
if err != nil {
return err
}
reason, err := c.recvString()
if err != nil {
return err
}
go c.handleTunnelClose(id, reason)
return nil
}
// Retrieves messages from the client connection and keeps processing them until
// either the relay closes (graceful close) or the connection drops.
func (c *Connection) process() {
var op byte
var err error
for closed := false; !closed && err == nil; {
// Retrieve the next opcode and call the specific handler for the rest
if op, err = c.recvByte(); err == nil {
switch op {
case opBroadcast:
err = c.procBroadcast()
case opRequest:
err = c.procRequest()
case opReply:
err = c.procReply()
case opPublish:
err = c.procPublish()
case opTunInit:
err = c.procTunnelInit()
case opTunConfirm:
err = c.procTunnelResult()
case opTunAllow:
err = c.procTunnelAllowance()
case opTunTransfer:
err = c.procTunnelTransfer()
case opTunClose:
err = c.procTunnelClose()
case opClose:
// Retrieve any reason for remote closure
if reason, cerr := c.procClose(); cerr != nil {
err = cerr
} else if len(reason) > 0 {
err = fmt.Errorf("connection dropped: %s", reason)
} else {
closed = true
}
default:
err = fmt.Errorf("protocol violation: unknown opcode: %v", op)
}
}
}
// Close the socket and signal termination to all blocked threads
c.sock.Close()
close(c.term)
// Notify the application of the connection closure
c.handleClose(err)
// Wait for termination sync
errc := <-c.quit
errc <- err
}