This commit is contained in:
Josh Deprez 2024-03-30 14:49:18 +11:00
parent 6a55dfa581
commit 785f6962e3
Signed by: josh
SSH key fingerprint: SHA256:zZji7w1Ilh2RuUpbQcqkLPrqmRwpiCSycbF2EfKm6Kw

25
main.go
View file

@ -8,6 +8,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"regexp" "regexp"
"sync"
"gitea.drjosh.dev/josh/jrouter/aurp" "gitea.drjosh.dev/josh/jrouter/aurp"
) )
@ -65,11 +66,20 @@ func main() {
cctx, cancel := context.WithCancel(context.Background()) cctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
ctx, _ := signal.NotifyContext(cctx, os.Interrupt) ctx, _ := signal.NotifyContext(cctx, os.Interrupt)
go func() {
// net.UDPConn is not context-aware? Hmmm. // Wait until all peer handlers have finished before closing the port
<-ctx.Done() var handlersWG sync.WaitGroup
defer func() {
handlersWG.Wait()
ln.Close() ln.Close()
}() }()
goHandler := func(p *peer) {
handlersWG.Add(1)
go func() {
defer handlersWG.Done()
p.handle(ctx)
}()
}
for _, peerStr := range cfg.Peers { for _, peerStr := range cfg.Peers {
if !hasPortRE.MatchString(peerStr) { if !hasPortRE.MatchString(peerStr) {
@ -95,8 +105,7 @@ func main() {
raddr: raddr, raddr: raddr,
recv: make(chan aurp.Packet, 1024), recv: make(chan aurp.Packet, 1024),
} }
go peer.handle(ctx) goHandler(peer)
peers[udpAddrFromNet(raddr)] = peer peers[udpAddrFromNet(raddr)] = peer
} }
@ -111,10 +120,6 @@ func main() {
dh, pkt, parseErr := aurp.ParsePacket(pktbuf[:pktlen]) dh, pkt, parseErr := aurp.ParsePacket(pktbuf[:pktlen])
if parseErr != nil { if parseErr != nil {
if readErr != nil {
log.Printf("Failed to read packet: %v", readErr)
return
}
log.Printf("Failed to parse packet: %v", parseErr) log.Printf("Failed to parse packet: %v", parseErr)
} }
if readErr != nil { if readErr != nil {
@ -141,7 +146,7 @@ func main() {
recv: make(chan aurp.Packet, 1024), recv: make(chan aurp.Packet, 1024),
} }
peers[ra] = pr peers[ra] = pr
go pr.handle(ctx) goHandler(pr)
} }
// Pass the packet to the goroutine in charge of this peer. // Pass the packet to the goroutine in charge of this peer.