Fix ^C not closing packet loop

This commit is contained in:
Josh Deprez 2024-03-30 14:30:58 +11:00
parent 64f491fcba
commit e3a6aecd51
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw

16
main.go
View file

@ -59,11 +59,15 @@ func main() {
if err != nil {
log.Fatalf("Couldn't listen on udp4:387: %v", err)
}
defer ln.Close()
log.Printf("Listening on %v", ln.LocalAddr())
log.Println("Press ^C or send SIGINT to stop the router gracefully")
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
go func() {
// net.UDPConn is not context-aware? Hmmm.
<-ctx.Done()
ln.Close()
}()
for _, peerStr := range cfg.Peers {
if !hasPortRE.MatchString(peerStr) {
@ -113,7 +117,7 @@ func main() {
if readErr != nil {
log.Printf("Failed to read packet: %v", readErr)
continue
return
}
// Existing peer?
@ -137,7 +141,13 @@ func main() {
}
// Pass the packet to the goroutine in charge of this peer.
pr.recv <- pkt
select {
case pr.recv <- pkt:
// That's it for us.
case <-ctx.Done():
return
}
}
}