forked from icedream/go-stagelinq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.go
471 lines (404 loc) · 10.8 KB
/
messages.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
package stagelinq
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"io"
)
// Token contains the identifying Token for a device in the StagelinQ network.
type Token [16]byte
type message interface {
readFrom(io.Reader) error
writeTo(io.Writer) error
// checkMatch MUST use the Peek method to read any bytes needed to exactly identify whether a message matches.
// It SHOULD not peek more bytes than are necessary to identify the message.
// The method MUST avoid Read to allow other message types to validate the message properly.
checkMatch(*bufio.Reader) (bool, error)
}
func readMessageID(r io.Reader) (id int32, err error) {
err = binary.Read(r, binary.BigEndian, &id)
return
}
func peekMessageID(r *bufio.Reader) (id int32, err error) {
b, err := r.Peek(4)
if err != nil {
return
}
return readMessageID(bytes.NewReader(b))
}
func writeMessageID(w io.Writer, id int32) (err error) {
err = binary.Write(w, binary.BigEndian, id)
return
}
type tokenPrefixedMessage struct {
Token Token
}
func (m *tokenPrefixedMessage) readFrom(r io.Reader) (err error) {
_, err = r.Read(m.Token[:])
return
}
func (m *tokenPrefixedMessage) writeTo(w io.Writer) (err error) {
_, err = w.Write(m.Token[:])
return
}
type serviceAnnouncementMessage struct {
tokenPrefixedMessage
Service string
Port uint16
}
func (m *serviceAnnouncementMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
id, err := peekMessageID(r)
if err != nil {
return
}
ok = id == 0x00000000
return
}
func (m *serviceAnnouncementMessage) readFrom(r io.Reader) (err error) {
messageID, err := readMessageID(r)
if err != nil {
return
} else if messageID != 0x00000000 {
err = ErrInvalidMessageReceived
return
}
if err = m.tokenPrefixedMessage.readFrom(r); err != nil {
return
}
if err = readNetworkString(r, &m.Service); err != nil {
return
}
if err = binary.Read(r, binary.BigEndian, &m.Port); err != nil {
return
}
return
}
func (m *serviceAnnouncementMessage) writeTo(w io.Writer) (err error) {
if err = writeMessageID(w, 0x00000000); err != nil {
return
}
if err = m.tokenPrefixedMessage.writeTo(w); err != nil {
return
}
if err = writeNetworkString(w, m.Service); err != nil {
return
}
err = binary.Write(w, binary.BigEndian, m.Port)
return
}
type referenceMessage struct {
tokenPrefixedMessage
Token2 Token
Reference int64
}
func (m *referenceMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
id, err := peekMessageID(r)
if err != nil {
return
}
ok = id == 0x00000001
return
}
func (m *referenceMessage) readFrom(r io.Reader) (err error) {
messageID, err := readMessageID(r)
if err != nil {
return
} else if messageID != 0x00000001 {
err = ErrInvalidMessageReceived
return
}
if err = m.tokenPrefixedMessage.readFrom(r); err != nil {
return
}
if _, err = r.Read(m.Token2[:]); err != nil {
return
}
err = binary.Read(r, binary.BigEndian, &m.Reference)
return
}
func (m *referenceMessage) writeTo(w io.Writer) (err error) {
if err = writeMessageID(w, 0x00000001); err != nil {
return
}
if err = m.tokenPrefixedMessage.writeTo(w); err != nil {
return
}
if _, err = w.Write(m.Token2[:]); err != nil {
return
}
err = binary.Write(w, binary.BigEndian, m.Reference)
return
}
type servicesRequestMessage struct {
tokenPrefixedMessage
}
func (m *servicesRequestMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
id, err := peekMessageID(r)
if err != nil {
return
}
ok = id == 0x00000002
return
}
func (m *servicesRequestMessage) readFrom(r io.Reader) (err error) {
messageID, err := readMessageID(r)
if err != nil {
return
} else if messageID != 0x00000002 {
err = ErrInvalidMessageReceived
return
}
err = m.tokenPrefixedMessage.readFrom(r)
return
}
func (m *servicesRequestMessage) writeTo(w io.Writer) (err error) {
if err = writeMessageID(w, 0x00000002); err != nil {
return
}
err = m.tokenPrefixedMessage.writeTo(w)
return
}
// TODO - StateSubscribeMessage.Interval: Check what Interval actually is, it seems to either be 00 00 00 00 (Resolume Arena) or 00 00 00 0a (SoundSwitch)
var smaaMagicBytes = []byte{0x73, 0x6d, 0x61, 0x61}
func checkSmaa(r *bufio.Reader, id int32) (ok bool, err error) {
// peek length bytes and smaa magic bytes
b, err := r.Peek(4 + 4 + 4)
if err != nil {
return
}
// check smaa magic bytes
if ok = bytes.Equal(b[4:8], smaaMagicBytes); !ok {
return
}
// check id
if int32(binary.BigEndian.Uint32(b[8:12])) != id {
ok = false
}
return
}
type stateSubscribeMessage struct {
//Length uint32
//Unknown []byte = {0x73,0x6d,0x61,0x61}
//Unknown2 []byte = {0x00,0x00,0x07,0xd2}
Name string
Interval uint32
}
func (m *stateSubscribeMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
return checkSmaa(r, 0x000007d2)
}
func (m *stateSubscribeMessage) readFrom(r io.Reader) (err error) {
var expectedLength uint32
if err = binary.Read(r, binary.BigEndian, &expectedLength); err != nil {
return
}
// read smaa magic bytes
magicBytes := make([]byte, 4)
if _, err = r.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, smaaMagicBytes) {
err = errors.New("invalid smaa magic bytes")
return
}
// TODO - figure this out
if _, err = r.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, []byte{0x00, 0x00, 0x07, 0xd2}) {
err = errors.New("invalid post-smaa magic bytes")
return
}
// read value name
if err = readNetworkString(r, &m.Name); err != nil {
return
}
// TODO - figure this out
err = binary.Read(r, binary.BigEndian, &m.Interval)
return
}
func (m *stateSubscribeMessage) writeTo(w io.Writer) (err error) {
// write smaa magic bytes
buf := new(bytes.Buffer)
if _, err = buf.Write(smaaMagicBytes); err != nil {
return
}
// TODO - figure this out
if _, err = buf.Write([]byte{0x00, 0x00, 0x07, 0xd2}); err != nil {
return
}
// write value name
if err = writeNetworkString(buf, m.Name); err != nil {
return
}
// TODO - figure this out
if err = binary.Write(buf, binary.BigEndian, m.Interval); err != nil {
return
}
// send message length over wire
if err = binary.Write(w, binary.BigEndian, uint32(buf.Len())); err != nil {
return
}
// send actual message over wire
_, err = w.Write(buf.Bytes())
return
}
type stateEmitMessage struct {
//Length uint32
//Unknown []byte = {0x73,0x6d,0x61,0x61}
//Unknown2 []byte = {0x00,0x00,0x00,0x00}
Name string
JSON string
}
func (m *stateEmitMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
return checkSmaa(r, 0x00000000)
}
func (m *stateEmitMessage) readFrom(r io.Reader) (err error) {
// read expected message length
var expectedLength uint32
if err = binary.Read(r, binary.BigEndian, &expectedLength); err != nil {
return
}
// set up buffer to write message into
msgBytes := make([]byte, int(expectedLength))
msgBytesOffset := 0
for msgBytesOffset < int(expectedLength) {
var n int
if n, err = r.Read(msgBytes[msgBytesOffset:]); err != nil {
return
}
msgBytesOffset += n
}
msgReader := bytes.NewReader(msgBytes)
// read smaa magic bytes
magicBytes := make([]byte, 4)
if _, err = msgReader.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, smaaMagicBytes) {
err = errors.New("invalid smaa magic bytes")
return
}
// TODO - figure this out
if _, err = msgReader.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, []byte{0x00, 0x00, 0x00, 0x00}) {
err = errors.New("invalid post-smaa magic bytes")
return
}
// read value name
if err = readNetworkString(msgReader, &m.Name); err != nil {
return
}
// read value JSON
if err = readNetworkString(msgReader, &m.JSON); err != nil {
return
}
return
}
func (m *stateEmitMessage) writeTo(w io.Writer) (err error) {
buf := new(bytes.Buffer)
// write smaa magic bytes to message buffer
if _, err = buf.Write(smaaMagicBytes); err != nil {
return
}
// TODO - figure this out
if _, err = buf.Write([]byte{0x00, 0x00, 0x00, 0x00}); err != nil {
return
}
// write value name to message buffer
if err = writeNetworkString(buf, m.Name); err != nil {
return
}
// write value JSON to message buffer
if err = writeNetworkString(buf, m.JSON); err != nil {
return
}
// send message length over wire
if err = binary.Write(w, binary.BigEndian, uint32(buf.Len())); err != nil {
return
}
// send actual message over wire
_, err = w.Write(buf.Bytes())
return
}
// discovererMessageAction is the action taken by a device as part of StagelinQ device discovery.
// Possible values are DiscovererHowdy or DiscovererExit.
type discovererMessageAction string
const (
// discovererHowdy is the value set on the Action field of a DiscoveryMessage when a StagelinQ-compatible device announces itself in the network.
discovererHowdy discovererMessageAction = "DISCOVERER_HOWDY_"
// discovererExit is the value set on the Action field of a DiscoveryMessage when a StagelinQ-compatible device leaves the network.
discovererExit discovererMessageAction = "DISCOVERER_EXIT_"
)
// discoveryMessage contains the data carried in the message payload for device trying to handshake the StagelinQ protocol to any other device in the network.
type discoveryMessage struct {
tokenPrefixedMessage
Source string
Action discovererMessageAction
SoftwareName string
SoftwareVersion string
Port uint16
}
var discoveryMagic = []byte("airD")
func (m *discoveryMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
var readMagic []byte
if readMagic, err = r.Peek(4); err != nil {
return
}
ok = bytes.Equal(readMagic, discoveryMagic)
return
}
func (m *discoveryMessage) readFrom(r io.Reader) (err error) {
readMagic := make([]byte, 4)
if _, err = r.Read(readMagic); err != nil {
return
} else if !bytes.Equal(readMagic, discoveryMagic) {
err = ErrInvalidMessageReceived
return
}
if err = m.tokenPrefixedMessage.readFrom(r); err != nil {
return
}
if err = readNetworkString(r, &m.Source); err != nil {
return
}
actionString := ""
if err = readNetworkString(r, &actionString); err != nil {
return
}
m.Action = discovererMessageAction(actionString)
if err = readNetworkString(r, &m.SoftwareName); err != nil {
return
}
if err = readNetworkString(r, &m.SoftwareVersion); err != nil {
return
}
err = binary.Read(r, binary.BigEndian, &m.Port)
return
}
func (m *discoveryMessage) writeTo(w io.Writer) (err error) {
if _, err = w.Write(discoveryMagic); err != nil {
return
}
if err = m.tokenPrefixedMessage.writeTo(w); err != nil {
return
}
if err = writeNetworkString(w, m.Source); err != nil {
return
}
if err = writeNetworkString(w, string(m.Action)); err != nil {
return
}
if err = writeNetworkString(w, m.SoftwareName); err != nil {
return
}
if err = writeNetworkString(w, m.SoftwareVersion); err != nil {
return
}
if err = binary.Write(w, binary.BigEndian, m.Port); err != nil {
return
}
return
}