-
Notifications
You must be signed in to change notification settings - Fork 6
/
messages.go
849 lines (723 loc) · 19.5 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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
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 stateEmitResponseMessage struct {
// Length uint32
// Unknown []byte = {0x73,0x6d,0x61,0x61}
// Unknown2 []byte = {0x00,0x00,0x07,0xd1}
Name string
Interval uint32
}
func (m *stateEmitResponseMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
return checkSmaa(r, 0x000007d1)
}
func (m *stateEmitResponseMessage) 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, 0xd1}) {
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 *stateEmitResponseMessage) 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, 0xd1}); 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
}
// BeatInfo
var beatInfoStartStreamMagicBytes = []byte{0x0, 0x0, 0x0, 0x0}
type beatInfoStartStreamMessage struct {
}
func (m *beatInfoStartStreamMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
// peek length bytes and magic bytes
b, err := r.Peek(4 + 4)
if err != nil {
return
}
// check magic bytes
if ok = bytes.Equal(b[4:8], beatInfoStartStreamMagicBytes); !ok {
return
}
return
}
func (m *beatInfoStartStreamMessage) 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 beatInfoStartStream magic bytes
magicBytes := make([]byte, 4)
if _, err = msgReader.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, beatInfoStartStreamMagicBytes) {
return errors.New("invalid magic bytes")
}
return
}
func (m *beatInfoStartStreamMessage) writeTo(w io.Writer) (err error) {
buf := new(bytes.Buffer)
payload_len := len(beatInfoStartStreamMagicBytes)
if err = binary.Write(buf, binary.BigEndian, uint32(payload_len)); err != nil {
return
}
// write BeatInfo "start stream" magic bytes
if _, err = buf.Write(beatInfoStartStreamMagicBytes); err != nil {
return
}
// send actual message over wire
_, err = w.Write(buf.Bytes())
return
}
var beatInfoStopStreamMagicBytes = []byte{0x0, 0x0, 0x0, 0x1}
type beatInfoStopStreamMessage struct {
}
func (m *beatInfoStopStreamMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
// peek length bytes and magic bytes
b, err := r.Peek(4 + 4)
if err != nil {
return
}
// check magic bytes
if ok = bytes.Equal(b[4:8], beatInfoStopStreamMagicBytes); !ok {
return
}
return
}
func (m *beatInfoStopStreamMessage) 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 beatInfoStopStream magic bytes
magicBytes := make([]byte, 4)
if _, err = msgReader.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, beatInfoStopStreamMagicBytes) {
return errors.New("invalid magic bytes")
}
return
}
func (m *beatInfoStopStreamMessage) writeTo(w io.Writer) (err error) {
buf := new(bytes.Buffer)
payload_len := len(beatInfoStopStreamMagicBytes)
if err = binary.Write(buf, binary.BigEndian, uint32(payload_len)); err != nil {
return
}
// write BeatInfo "stop stream" magic bytes
if _, err = buf.Write(beatInfoStopStreamMagicBytes); err != nil {
return
}
// send actual message over wire
_, err = w.Write(buf.Bytes())
return
}
var beatEmitMagicBytes = []byte{0x0, 0x0, 0x0, 0x2}
type PlayerInfo struct {
Beat float64
TotalBeats float64
Bpm float64
}
type beatEmitMessage struct {
//Length uint32
//Magic []byte = {0x00,0x00,0x00,0x02}
Clock uint64
Players []PlayerInfo
Timelines []float64
}
func (m *beatEmitMessage) checkMatch(r *bufio.Reader) (ok bool, err error) {
// peek length bytes and magic bytes
b, err := r.Peek(4 + 4)
if err != nil {
return
}
// check magic bytes
if ok = bytes.Equal(b[4:8], beatEmitMagicBytes); !ok {
return
}
return
}
func (m *beatEmitMessage) 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 beatEmit magic bytes
magicBytes := make([]byte, 4)
if _, err = msgReader.Read(magicBytes); err != nil {
return
}
if !bytes.Equal(magicBytes, beatEmitMagicBytes) {
err = errors.New("invalid magic bytes")
return
}
// read clock value
if err = binary.Read(msgReader, binary.BigEndian, &m.Clock); err != nil {
return
}
// read expected player records
var expectedRecords uint32
if err = binary.Read(msgReader, binary.BigEndian, &expectedRecords); err != nil {
return
}
// bounds check
// each playerInfo record is 24 bytes
if msgReader.Len() < int(expectedRecords)*24 {
err = errors.New("unknown packet format")
}
// loop through players records
for i := 0; i < int(expectedRecords); i++ {
var p PlayerInfo
if err = binary.Read(msgReader, binary.BigEndian, &p.Beat); err != nil {
return
}
if err = binary.Read(msgReader, binary.BigEndian, &p.TotalBeats); err != nil {
return
}
if err = binary.Read(msgReader, binary.BigEndian, &p.Bpm); err != nil {
return
}
m.Players = append(m.Players, p)
}
// bounds check
// the rest of our payload should contain exactly enough bytes for the timeline records
if msgReader.Len() == int(expectedRecords)*8 {
err = errors.New("unknown packet format")
}
// loop through timelines
for i := 0; i < int(expectedRecords); i++ {
var t float64
if err = binary.Read(msgReader, binary.BigEndian, &t); err != nil {
return
}
m.Timelines = append(m.Timelines, t)
}
return
}
func (m *beatEmitMessage) writeTo(w io.Writer) (err error) {
// sanity check number of records
numRecords := len(m.Players)
if numRecords != len(m.Timelines) {
err = errors.New("number of player records must match number of timeline records")
return
}
buf := new(bytes.Buffer)
// write magic bytes to message buffer
if _, err = buf.Write(beatEmitMagicBytes); err != nil {
return
}
// write clock
if err = binary.Write(buf, binary.BigEndian, m.Clock); err != nil {
return
}
// write number of records
if err = binary.Write(buf, binary.BigEndian, uint32(numRecords)); err != nil {
return
}
// write beat info records
for _, bi := range m.Players {
if err = binary.Write(buf, binary.BigEndian, bi.Beat); err != nil {
return
}
if err = binary.Write(buf, binary.BigEndian, bi.TotalBeats); err != nil {
return
}
if err = binary.Write(buf, binary.BigEndian, bi.Bpm); err != nil {
return
}
}
// write timeline records
for _, tl := range m.Timelines {
if err = binary.Write(buf, binary.BigEndian, tl); 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
}