Skip to content

Commit f5e2a2e

Browse files
committed
better handle connection
1 parent c2adeba commit f5e2a2e

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

telemetry_udp.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,9 @@ type connectionResult struct {
4444
}
4545

4646
func (telemetry *AccTelemetry) connect() error {
47-
connectMessage, err := telemetry.createConnectMessage()
48-
if err != nil {
49-
return fmt.Errorf("failed to craete connect message: %w", err)
50-
}
51-
52-
// send connection request
53-
_, sendErr := telemetry.udpConnection.Write(connectMessage)
54-
if sendErr != nil {
55-
return fmt.Errorf("failed to send connection message: %w", sendErr)
56-
}
57-
58-
// give lot of time for connection
59-
telemetry.udpConnection.SetReadDeadline(time.Now().Add(15 * time.Second))
60-
inBuffer := make([]byte, 128)
61-
_, _, err = telemetry.udpConnection.ReadFromUDP(inBuffer)
62-
if err != nil {
63-
telemetry.Close()
64-
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
65-
return fmt.Errorf("UDP read timeout, ACC probably not running: %w", netErr)
66-
} else {
67-
return fmt.Errorf("UDP read failed: %w", err)
68-
}
69-
} else {
70-
fmt.Println("UDP Connected")
71-
telemetry.realtimeCarUpdate = &RealtimeCarUpdate{
72-
BestSessionLap: &LapInfo{Splits: [8]int32{}},
73-
LastLap: &LapInfo{Splits: [8]int32{}},
74-
CurrentLap: &LapInfo{Splits: [8]int32{}},
75-
}
76-
}
77-
78-
connectionResult, err := readConnectionResult(bytes.NewBuffer(inBuffer))
79-
if err != nil {
80-
return fmt.Errorf("failed to read connection response: %w", err)
47+
if handshakeErr := telemetry.handshake(); handshakeErr != nil {
48+
return fmt.Errorf("failed to connect to ACC: %w", handshakeErr)
8149
}
82-
fmt.Printf("Connected to ACC, listen for messages: '%+v'", connectionResult)
8350

8451
go func() {
8552
payload := make([]byte, 128)
@@ -98,6 +65,7 @@ func (telemetry *AccTelemetry) connect() error {
9865
} else {
9966
fmt.Printf("UDP read failed: %s", err)
10067
}
68+
telemetry.Close()
10169
} else {
10270
if err := telemetry.readMessage(payload); err != nil {
10371
fmt.Printf("failed to read the message: %s\n", err)
@@ -110,6 +78,47 @@ func (telemetry *AccTelemetry) connect() error {
11078
return nil
11179
}
11280

81+
func (t *AccTelemetry) handshake() error {
82+
connectMessage, err := t.createConnectMessage()
83+
if err != nil {
84+
return fmt.Errorf("failed to craete connect message: %w", err)
85+
}
86+
87+
// send connection request
88+
_, sendErr := t.udpConnection.Write(connectMessage)
89+
if sendErr != nil {
90+
return fmt.Errorf("failed to send connection message: %w", sendErr)
91+
}
92+
93+
// give lot of time for connection
94+
t.udpConnection.SetReadDeadline(time.Now().Add(15 * time.Second))
95+
inBuffer := make([]byte, 128)
96+
_, _, err = t.udpConnection.ReadFromUDP(inBuffer)
97+
if err != nil {
98+
t.Close()
99+
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
100+
return fmt.Errorf("UDP read timeout, ACC probably not running: %w", netErr)
101+
} else {
102+
return fmt.Errorf("UDP read failed: %w", err)
103+
}
104+
}
105+
106+
connectionResult, err := readConnectionResult(bytes.NewBuffer(inBuffer))
107+
if err != nil {
108+
return fmt.Errorf("failed to read connection response: %w", err)
109+
}
110+
111+
fmt.Printf("Connected to ACC, listen for messages: '%+v'", connectionResult)
112+
113+
t.realtimeCarUpdate = &RealtimeCarUpdate{
114+
BestSessionLap: &LapInfo{Splits: [8]int32{}},
115+
LastLap: &LapInfo{Splits: [8]int32{}},
116+
CurrentLap: &LapInfo{Splits: [8]int32{}},
117+
}
118+
119+
return nil
120+
}
121+
113122
func (telemetry *AccTelemetry) createConnectMessage() ([]byte, error) {
114123
outBuffer := bytes.NewBuffer([]byte{})
115124
var writeErr error

0 commit comments

Comments
 (0)