forked from icedream/go-stagelinq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_connection.go
178 lines (151 loc) · 4.05 KB
/
main_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
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
package stagelinq
import (
"net"
"sync"
"time"
)
// Service contains information about a data service a device provides.
type Service struct {
Name string
Port uint16
}
// MainConnection represents a connection to the main TCP port of a StagelinQ device.
type MainConnection struct {
lock sync.Mutex
token Token
msgConn *messageConnection
offeredServices []*Service
servicesC chan *Service
atLeastOneServiceReceived bool
errorC chan error
reference int64
}
var mainConnectionMessageSet = newDeviceConnMessageSet([]message{
&serviceAnnouncementMessage{},
&referenceMessage{},
&servicesRequestMessage{},
})
// newMainConnection wraps an existing network connection to communicate StagelinQ main connection messages with it.
func newMainConnection(conn net.Conn, token Token, targetToken Token, offeredServices []*Service) (retval *MainConnection, err error) {
msgConn := newMessageConnection(conn, mainConnectionMessageSet)
mainConn := &MainConnection{
token: token,
msgConn: msgConn,
errorC: make(chan error, 1),
offeredServices: offeredServices,
}
go func() {
for {
<-time.After(250 * time.Millisecond)
// TODO - we're always returning zero as timestamp here just like SoundSwitch does, we still need to implement the behavior Resolume Arena
mainConn.lock.Lock()
ref := mainConn.reference
mainConn.lock.Unlock()
if err = mainConn.msgConn.WriteMessage(&referenceMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: mainConn.token,
},
Token2: targetToken,
Reference: ref,
}); err != nil {
return
}
}
}()
go func() {
var err error
defer func() {
if err != nil {
mainConn.errorC <- err
close(mainConn.errorC)
}
if mainConn.servicesC != nil {
close(mainConn.servicesC)
mainConn.servicesC = nil
}
}()
for {
var msg message
msg, err = mainConn.msgConn.ReadMessage()
if err != nil {
return
}
func() {
mainConn.lock.Lock()
defer mainConn.lock.Unlock()
switch v := msg.(type) {
case *serviceAnnouncementMessage:
if mainConn.servicesC == nil {
err = ErrInvalidMessageReceived
break
}
mainConn.servicesC <- &Service{
Name: v.Service,
Port: v.Port,
}
case *referenceMessage:
if mainConn.servicesC != nil {
close(mainConn.servicesC)
mainConn.servicesC = nil
}
// TODO - not sure what else to actually do with this information yet
// mainConn.reference = v.Reference
case *servicesRequestMessage:
for _, service := range mainConn.offeredServices {
if err = mainConn.announceService(service.Name, service.Port); err != nil {
return
}
}
}
}()
}
}()
retval = mainConn
return
}
// Close terminates the connection.
func (conn *MainConnection) Close() (err error) {
return conn.msgConn.conn.Close()
}
// RequestServices asks the device to return other TCP ports it is listening on and which services it provides on them.
func (conn *MainConnection) RequestServices() (retval []*Service, err error) {
if err = conn.requestServices(); err != nil {
return
}
conn.lock.Lock()
serviceC := make(chan *Service)
conn.servicesC = serviceC
conn.atLeastOneServiceReceived = false
services := []*Service{}
conn.lock.Unlock()
for service := range serviceC {
services = append(services, service)
}
select {
case err = <-conn.errorC:
default:
}
// the message reading loop already will set conn.servicesC back to nil
retval = services
return
}
func (conn *MainConnection) requestServices() (err error) {
if err = conn.msgConn.WriteMessage(&servicesRequestMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: conn.token,
},
}); err != nil {
return
}
return
}
// announceService tells the device about a service we provide.
func (conn *MainConnection) announceService(name string, port uint16) error {
return conn.msgConn.WriteMessage(&serviceAnnouncementMessage{
tokenPrefixedMessage: tokenPrefixedMessage{
Token: conn.token,
},
Service: name,
Port: port,
})
}