/
defs.go
executable file
·104 lines (78 loc) · 2.21 KB
/
defs.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
package sub
import (
"net"
"time"
)
var (
uNet = "udp4"
// Maximum of 9 packets per message, so 16kb round is enough
defaultBufferSize = 16384
// FEC expands message by 150%, we don't split message chunks over more than one packet
maxMessageSize = 3072
// default channel buffer sizes for Base
baseChanBufs = 128
// latency maximum
latencyMax = time.Millisecond * 250
)
// BaseInterface is the core functions required for a Base
type BaseInterface interface {
SetupListener()
}
// BaseCfg is the configuration for a Base
type BaseCfg struct {
Handler func(message Message)
Listener string
Password []byte
BufferSize int
}
// Base is the common structure between a worker and a node
type Base struct {
cfg BaseCfg
listener *net.UDPConn
packets chan Packet
incoming chan Bundle
returning chan Bundle
trash chan Bundle
doneRet chan bool
message chan Message
quit chan bool
}
// A Node is a server with some number of subscribers
type Node struct {
Base
subscribers []*net.UDPAddr
}
// A Worker is a node that subscribes to a Node's messages
type Worker struct {
Base
node *net.UDPAddr
}
// Packet is the structure of individual encoded packets of the message. These are made from a 9/3 Reed Solomon code and 9 are sent in distinct packets and only 3 are required to guarantee retransmit-free delivery.
type Packet struct {
sender string // address packet was received from
bytes []byte // raw FEC encoded bytes of packet
}
// A Bundle is a collection of the received packets received from the same sender with up to 9 pieces.
type Bundle struct {
uuid int32
sender string
received time.Time
packets [][]byte
}
// Message is the data reconstructed from a complete Bundle, containing data in messagepack format
type Message struct {
uuid int32
sender string
timestamp time.Time
bytes []byte
}
// Subscription is the message sent by a worker node to request updates from the node
type Subscription struct {
address string
pubKey []byte
}
// Confirmation is the reply message for a subscription request
type Confirmation struct {
subscriber string // confirming address of subscriber
pubKey []byte // public key of server for message verification
}