-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsender.go
370 lines (320 loc) · 9.37 KB
/
sender.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
package sacn
import (
"errors"
"fmt"
"log"
"net"
"sync"
"time"
"github.com/google/uuid"
"gitlab.com/patopest/go-sacn/packet"
)
// A sACN Sender. Use [NewSender] to create a receiver.
type Sender struct {
conn *net.UDPConn
universes map[uint16]*senderUniverse
discovery *senderUniverse
wg sync.WaitGroup
// common options for packets
cid [16]byte
sourceName string
keepAlive time.Duration
}
// Optional arguments for [NewSender] to be applied to all packets being sent by the sender.
// These can be overridden on a per packet basis if set in the [packet.SACNPacket] being sent.
type SenderOptions struct {
CID [16]byte // the CID (Component Identifier): a RFC4122 compliant UUID.
SourceName string // A source name (must not be longer than 64 characters)
// KeepAlive time.Duration
}
// Stores all the information required per universe a sender is handling
type senderUniverse struct {
number uint16
dataCh chan packet.SACNPacket
enabled bool
sequence uint8
multicast bool
destinations []net.UDPAddr
}
var universeNotFoundError = errors.New("Universe is not initialised, please use StartUniverse() first")
// NewSender creates a new [Sender]. Optionally pass a bind string of the host's ip address it should bind to (eg: "192.168.1.100").
// This is mandatory if multicast is being used on any universe.
func NewSender(address string, options *SenderOptions) (*Sender, error) {
// Generate RFC 4122 compliant UUID. From ANSI E1.31-2019 Section 5.6
cid, err := uuid.NewV7()
if err != nil {
return nil, err
}
if options.CID[0] == 0 {
bytes, _ := cid.MarshalBinary()
copy(options.CID[:], bytes[:16])
}
if options.SourceName == "" {
options.SourceName = "gitlab.com/patopest/go-sacn"
}
if len(options.SourceName) > 64 {
return nil, errors.New("Source name is too long. Maximum is 64 bytes")
}
// if options.KeepAlive == 0 {
// options.KeepAlive = 1 * time.Second
// }
server, err := net.ResolveUDPAddr("udp", address+":0")
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp", server)
if err != nil {
return nil, err
}
s := &Sender{
conn: conn,
universes: make(map[uint16]*senderUniverse),
cid: options.CID,
sourceName: options.SourceName,
// keepAlive: options.KeepAlive,
}
go s.sendDiscoveryLoop()
return s, nil
}
// Stops the sender and all initialised universes
func (s *Sender) Close() {
for _, uni := range s.universes {
if uni.enabled {
close(uni.dataCh)
}
}
close(s.discovery.dataCh)
s.wg.Wait()
defer s.conn.Close()
}
// StartUniverse initialises a new universe to be sent by the sender.
// It returns a channel into which [packet.SACNPacket] can be written to for sending out on the network.
// Optionally you can use [Sender.Send] to also send packets for a universe.
func (s *Sender) StartUniverse(universe uint16) (chan<- packet.SACNPacket, error) {
if s.IsEnabled(universe) == true {
return nil, errors.New("Universe is already enabled")
}
if universe < 1 || universe >= 64000 { // From ANSI E1.31-2019 Section 6.2.7
return nil, errors.New("Universe value is incorrect, should be between 1 and 63999")
}
ch := make(chan packet.SACNPacket, 3)
uni := &senderUniverse{
number: universe,
enabled: true,
dataCh: ch,
sequence: 0,
multicast: false,
destinations: make([]net.UDPAddr, 0),
}
s.universes[universe] = uni
go s.sendLoop(universe)
return ch, nil
}
// StopUniverse stops sending packet for a universe.
// This closes the channel returned to by [Sender.StartUniverse].
// On closing, 3 [packet.DataPacket] will be sent out with the StreamTerminated bit set as specified in section 6.7.1 of ANSI E1.31—2018.
func (s *Sender) StopUniverse(universe uint16) error {
uni, exists := s.universes[universe]
if exists {
close(uni.dataCh)
return nil
}
return universeNotFoundError
}
// Send a packet on a universe.
// This is an alternative way to writing packets directly on the channel returned by [Sender.StartUniverse]
func (s *Sender) Send(universe uint16, p packet.SACNPacket) error {
uni, exists := s.universes[universe]
if exists {
uni.dataCh <- p
return nil
}
return universeNotFoundError
}
func (s *Sender) sendLoop(universe uint16) {
uni := s.universes[universe]
s.wg.Add(1)
ch := uni.dataCh
// Receive new packets to send out
for p := range ch {
uni.sequence += 1
sequence := uni.sequence
packetType := p.GetType()
switch packetType {
case packet.PacketTypeData:
d, _ := p.(*packet.DataPacket)
if d.CID[0] == 0 {
d.CID = s.cid
}
d.Universe = universe
d.Sequence = sequence // increment sequence number
if d.GetSourceName() == "" {
d.SetSourceName(s.sourceName)
}
case packet.PacketTypeSync:
d, _ := p.(*packet.SyncPacket)
if d.CID[0] == 0 {
d.CID = s.cid
}
d.SyncAddress = universe
d.Sequence = sequence
case packet.PacketTypeDiscovery: // technically should never have this type of packet here
d, _ := p.(*packet.DiscoveryPacket)
if d.CID[0] == 0 {
d.CID = s.cid
}
if d.GetSourceName() == "" {
d.SetSourceName(s.sourceName)
}
default:
continue
}
s.sendPacket(uni, p)
}
uni.enabled = false
// Send packet with stream terminated bit 3 times
p := packet.NewDataPacket()
p.SetStreamTerminated(true)
for i := 0; i < 3; i++ {
s.sendPacket(uni, p)
}
// Destroy universe
s.wg.Done()
delete(s.universes, universe)
}
func (s *Sender) sendDiscoveryLoop() {
s.discovery = &senderUniverse{
number: DISCOVERY_UNIVERSE,
enabled: true,
multicast: true,
dataCh: make(chan packet.SACNPacket, 0), // still create a data channel to close on sender Close()
}
s.wg.Add(1)
timer := time.NewTicker(UNIVERSE_DISCOVERY_INTERVAL * time.Second)
defer timer.Stop()
defer s.wg.Done()
for {
select {
case <-s.discovery.dataCh: // channel was closed
return
case <-timer.C:
num := len(s.universes)
pages := num / 512
universes := s.GetUniverses()
for page := 0; page < pages+1; page += 1 {
p := packet.NewDiscoveryPacket()
p.Page = uint8(page)
p.Last = uint8(pages)
p.CID = s.cid
p.SetSourceName(s.sourceName)
start := page * 512
end := (page + 1) * 512
if end > len(universes) {
end = len(universes)
}
p.SetUniverses(universes[start:end])
s.sendPacket(s.discovery, p)
}
}
}
}
func (s *Sender) sendPacket(universe *senderUniverse, p packet.SACNPacket) {
bytes, err := p.MarshalBinary()
if err != nil {
log.Println("Error", err)
return
}
// send multicast if enabled
if universe.multicast {
_, err := s.conn.WriteToUDP(bytes, universeToAddress(universe.number))
if err != nil {
log.Printf("Error sending multicat packet: %v\n", err)
}
}
// send unicast
for _, dest := range universe.destinations {
_, err := s.conn.WriteToUDP(bytes, &dest)
if err != nil {
log.Printf("Error sending unicast packet: %v\n", err)
}
}
}
// GetUniverses returns the list of all currently enabled universes for the sender.
func (s *Sender) GetUniverses() []uint16 {
unis := make([]uint16, 0)
for n, uni := range s.universes {
if uni.enabled {
unis = append(unis, n)
}
}
return unis
}
// IsEnabled returns true if the universe is currently enabled.
func (s *Sender) IsEnabled(universe uint16) bool {
uni, exists := s.universes[universe]
if exists && uni.enabled {
return true
}
return false
}
// IsMulticast returns wether or not multicast is turned on for the given universe.
func (s *Sender) IsMulticast(universe uint16) (bool, error) {
uni, exists := s.universes[universe]
if exists {
return uni.multicast, nil
}
return false, universeNotFoundError
}
// SetMulticast is for setting whether or not a universe should be send out via multicast.
func (s *Sender) SetMulticast(universe uint16, multicast bool) error {
uni, exists := s.universes[universe]
if exists {
uni.multicast = multicast
return nil
}
return universeNotFoundError
}
// GetDestinations returns the list of unicast destinations the universe is configured to send it's packets to.
func (s *Sender) GetDestinations(universe uint16) ([]string, error) {
dests := make([]string, 0)
uni, exists := s.universes[universe]
if exists {
for _, dest := range uni.destinations {
dests = append(dests, dest.IP.String())
}
return dests, nil
}
return nil, universeNotFoundError
}
// AddDestination adds a unicast destination that a universe should sent it's packets to.
// destination should be in the form of a string (eg: "192.168.1.100").
func (s *Sender) AddDestination(universe uint16, destination string) error {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", destination, SACN_PORT))
if err != nil {
return err
}
uni, exists := s.universes[universe]
if exists {
uni.destinations = append(uni.destinations, *addr)
return nil
}
return universeNotFoundError
}
// SetDestinations sets the list of unicast destinations that a univese should sent it's packets to.
// This overwrites the current list created by previous calls to this function or [Sender.AddDestination].
func (s *Sender) SetDestinations(universe uint16, destinations []string) error {
dests := make([]net.UDPAddr, 0)
for _, dest := range destinations {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", dest, SACN_PORT))
if err != nil {
return err
}
dests = append(dests, *addr)
}
uni, exists := s.universes[universe]
if exists {
uni.destinations = dests
return nil
}
return universeNotFoundError
}