/
device_connection.go
177 lines (143 loc) · 3.93 KB
/
device_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
package gocast
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gogo/protobuf/proto"
"github.com/stampzilla/gocast/api"
"github.com/stampzilla/gocast/events"
"github.com/stampzilla/gocast/responses"
)
func (d *Device) reader(ctx context.Context) {
for {
select {
case <-ctx.Done():
if ctx.Err() != nil {
d.logger.Errorf("closing reader: %s", ctx.Err())
}
return
case p := <-d.wrapper.packets:
if p.err != nil {
d.logger.Errorf("Error reading from chromecast error: %s Packet: %#v", p.err, p)
return
}
packet := p.payload
message := &api.CastMessage{}
err := proto.Unmarshal(packet, message)
if err != nil {
d.logger.Errorf("Failed to unmarshal CastMessage: %s", err)
continue
}
headers := &responses.Headers{}
err = json.Unmarshal([]byte(*message.PayloadUtf8), headers)
if err != nil {
d.logger.Errorf("Failed to unmarshal message: %s", err)
continue
}
catched := false
for _, subscription := range d.getSubscriptionsAsSlice() {
if subscription.Receive(message, headers) {
catched = true
}
}
if !catched {
d.logger.Debug("LOST MESSAGE:")
d.logger.Debug(spew.Sdump(message))
}
}
}
}
func (d *Device) Connect(ctx context.Context) error {
d.heartbeatHandler.OnFailure = func() { // make sure we reconnect if we loose heartbeat
d.logger.Errorf("heartbeat timeout, trying to reconnect")
d.Disconnect()
for { // try to connect until no error
err := d.connect(ctx)
if err == nil {
break
}
d.logger.Error("error reconnect: ", err)
time.Sleep(2 * time.Second)
}
}
return d.connect(ctx)
}
func (d *Device) connect(pCtx context.Context) error {
ctx, cancel := context.WithCancel(pCtx)
d.stop = cancel
d.logger.Info("connecting")
if d.getConn() != nil {
err := d.conn.Close()
if err != nil {
d.logger.Error("trying to connect with existing connection. error closing: ", err)
}
}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", d.Ip(), d.Port()), &tls.Config{
InsecureSkipVerify: true,
})
if err != nil {
return fmt.Errorf("failed to connect to Chromecast. Error:%s", err)
}
d.Lock()
d.conn = conn
d.Unlock()
d.wrapper = NewPacketStream(d.conn, d.logger)
go d.wrapper.readPackets(ctx)
go d.reader(ctx)
d.Subscribe("urn:x-cast:com.google.cast.tp.connection", "receiver-0", d.connectionHandler)
d.Subscribe("urn:x-cast:com.google.cast.tp.heartbeat", "receiver-0", d.heartbeatHandler)
d.Subscribe("urn:x-cast:com.google.cast.receiver", "receiver-0", d.ReceiverHandler)
d.Dispatch(events.Connected{})
return nil
}
func (d *Device) Disconnect() {
d.logger.Debug("disconnecting: ", d.Name())
for _, subscription := range d.getSubscriptionsAsSlice() {
d.logger.Debugf("disconnect subscription: %s ", subscription.Urn)
subscription.Handler.Disconnect()
}
d.Lock()
d.subscriptions = make(map[string]*Subscription)
d.Unlock()
if d.stop != nil { // make sure any old goroutines are stopped
d.stop()
}
if c := d.getConn(); d != nil {
c.Close()
d.Lock()
d.conn = nil
d.Unlock()
}
d.Dispatch(events.Disconnected{})
}
func (d *Device) Send(urn, sourceId, destinationId string, payload responses.Payload) error {
payloadJson, err := json.Marshal(payload)
if err != nil {
return err
}
payloadString := string(payloadJson)
message := &api.CastMessage{
ProtocolVersion: api.CastMessage_CASTV2_1_0.Enum(),
SourceId: &sourceId,
DestinationId: &destinationId,
Namespace: &urn,
PayloadType: api.CastMessage_STRING.Enum(),
PayloadUtf8: &payloadString,
}
proto.SetDefaults(message)
data, err := proto.Marshal(message)
if err != nil {
return err
}
if *message.Namespace != "urn:x-cast:com.google.cast.tp.heartbeat" {
d.logger.Debugf("Writing: %s", spew.Sdump(message))
}
if d.conn == nil {
return fmt.Errorf("we are disconnected, cannot send!")
}
_, err = d.wrapper.Write(data)
return err
}