Skip to content

Commit

Permalink
Avoid too many UDP error messages (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Jan 17, 2024
1 parent 4ebd0a9 commit 8c963dc
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions cmd/jag/commands/proxy_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,36 @@ func createIdentityPayload(identity *uartIdentity, localIP string, localPort int
}

func broadcastIdentity(identityPayload []byte) error {
// Create a UDP address for broadcasting (use broadcast IP)
addr, err := net.ResolveUDPAddr("udp", udpIdentifyAddress+":"+strconv.Itoa(udpIdentifyPort))
if err != nil {
return err
}

// Create a UDP connection
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
return err
}

// Create a goroutine to send the payload every 200ms.
go func() {
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
for {
// Create a UDP address for broadcasting (use broadcast IP)
addr, err := net.ResolveUDPAddr("udp", udpIdentifyAddress+":"+strconv.Itoa(udpIdentifyPort))
if err != nil {
// Sleep for a few seconds and try again.
time.Sleep(5 * time.Second)
continue
}

for range ticker.C {
_, err := conn.Write(identityPayload)
// Create a UDP connection
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
println("Error broadcasting payload:", err.Error())
// Sleep for a few seconds and try again.
time.Sleep(5 * time.Second)
continue
}

ticker := time.NewTicker(200 * time.Millisecond)

for range ticker.C {
_, err := conn.Write(identityPayload)
if err != nil {
println("Error broadcasting payload:", err.Error())
ticker.Stop()
conn.Close()
// Try to reconnect.
break
}
}
}
}()
Expand Down

0 comments on commit 8c963dc

Please sign in to comment.