-
Notifications
You must be signed in to change notification settings - Fork 6
/
beat_info_connection.go
94 lines (79 loc) · 2.28 KB
/
beat_info_connection.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
package stagelinq
import (
"net"
)
// BeatInfo represents a received BeatInfo message.
type BeatInfo struct {
Clock uint64
Players []PlayerInfo
Timelines []float64
}
// BeatInfoConnection provides functionality to communicate with the BeatInfo data source.
type BeatInfoConnection struct {
conn *messageConnection
errC chan error
beatInfoC chan *BeatInfo
}
var beatInfoConnectionMessageSet = newDeviceConnMessageSet([]message{&beatEmitMessage{}})
func NewBeatInfoConnection(conn net.Conn, token Token) (bic *BeatInfoConnection, err error) {
msgConn := newMessageConnection(conn, beatInfoConnectionMessageSet)
errC := make(chan error, 1)
beatInfoC := make(chan *BeatInfo, 1)
beatInfoConn := BeatInfoConnection{
conn: msgConn,
errC: errC,
beatInfoC: beatInfoC,
}
// perform in-protocol service request
msgConn.WriteMessage(&serviceAnnouncementMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: token,
},
Service: "BeatInfo",
Port: uint16(getPort(conn.LocalAddr())),
})
go func() {
var err error
defer func() {
if err != nil {
beatInfoConn.errC <- err
close(beatInfoConn.errC)
}
close(beatInfoConn.beatInfoC)
}()
for {
var msg message
msg, err = msgConn.ReadMessage()
if err != nil {
return
}
switch v := msg.(type) {
case *beatEmitMessage:
beatInfo := &BeatInfo{
Clock: v.Clock,
Players: v.Players,
Timelines: v.Timelines,
}
beatInfoC <- beatInfo
}
}
}()
bic = &beatInfoConn
return
}
// StartStream tells the StagelinQ device to start publishing the device BeatInfo data stream.
func (bic *BeatInfoConnection) StartStream() error {
return bic.conn.WriteMessage(&beatInfoStartStreamMessage{})
}
// StopStream tells the StagelinQ device to stop publishing the device BeatInfo data stream.
func (bic *BeatInfoConnection) StopStream() error {
return bic.conn.WriteMessage(&beatInfoStopStreamMessage{})
}
// BeatInfoC returns the channel via which the BeatInfo data stream will be published for this connection.
func (bic *BeatInfoConnection) BeatInfoC() <-chan *BeatInfo {
return bic.beatInfoC
}
// ErrorC returns the channel via which connection errors will be returned for this connection.
func (bic *BeatInfoConnection) ErrorC() <-chan error {
return bic.errC
}