This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelemetry.go
98 lines (79 loc) · 2.14 KB
/
telemetry.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
package cmtelemetry
import (
"net"
)
// Telemetry is the basic
type Telemetry struct {
dataAccessor TelemetryAccessor
udp *net.UDPConn
buffer []byte
quit chan struct{}
rec chan TelemetryAccessor
}
// TelemetryAccessor is an interface that is responsible for accessing the current
// telemetry values.
type TelemetryAccessor interface {
GetFieldValue(field TelemetryField) (float32, error)
}
// GatherDefaultTelemetry will open the default udp port and begin processing
// telemetry from Codemasters' games.
func GatherDefaultTelemetry(frames chan TelemetryAccessor) (*Telemetry, error) {
return GatherMode3Telemetry(":20777", frames)
}
// GatherMode0Telemetry will open a udp port up at the specified location and begin
// processing telemetry from Codemasters' games.
func GatherMode0Telemetry(address string, frames chan TelemetryAccessor) (t *Telemetry, err error) {
acc, buff := createMode0Accessor()
return gatherTelemetry(
address,
frames,
acc,
buff)
}
// GatherMode3Telemetry will open a udp port up at the specified location and begin
// processing telemetry from Codemasters' games.
func GatherMode3Telemetry(address string, frames chan TelemetryAccessor) (t *Telemetry, err error) {
acc, buff := createMode3Accessor()
return gatherTelemetry(
address,
frames,
acc,
buff)
}
func gatherTelemetry(address string, frames chan TelemetryAccessor, accessor TelemetryAccessor, buffer []byte) (t *Telemetry, err error) {
addr, err := net.ResolveUDPAddr("udp", address)
if err != nil {
return
}
udp, err := net.ListenUDP("udp", addr)
if err != nil {
return
}
t = &Telemetry{
dataAccessor: accessor,
udp: udp,
buffer: buffer,
quit: make(chan struct{}),
rec: frames,
}
go t.telemetryRoutine()
return
}
// Close will stop the connection.
func (t *Telemetry) Close() {
close(t.rec)
close(t.quit)
t.udp.Close()
}
func (t *Telemetry) telemetryRoutine() {
for {
select {
case <-t.quit:
return
default:
t.udp.ReadFromUDP(t.buffer)
t.rec <- t.dataAccessor
break
}
}
}