Use read deadline to quit incoming loop
This commit is contained in:
parent
5ff41b76d4
commit
81aba11a4a
1 changed files with 8 additions and 3 deletions
11
main.go
11
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.drjosh.dev/josh/jrouter/aurp"
|
"gitea.drjosh.dev/josh/jrouter/aurp"
|
||||||
)
|
)
|
||||||
|
@ -115,11 +117,14 @@ func main() {
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
ln.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
pktbuf := make([]byte, 65536)
|
pktbuf := make([]byte, 65536)
|
||||||
pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf)
|
pktlen, raddr, readErr := ln.ReadFromUDP(pktbuf)
|
||||||
// net.PacketConn.ReadFrom: "Callers should always process
|
|
||||||
// the n > 0 bytes returned before considering the error err."
|
var operr *net.OpError
|
||||||
|
if errors.As(readErr, &operr) && operr.Timeout() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("Received packet of length %d from %v", pktlen, raddr)
|
log.Printf("Received packet of length %d from %v", pktlen, raddr)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue