readLoop 和 tx 优化 (#313)

* chores: use modern platform optimize

* refactor: readloop, tx

* chores: drop older linux compatibility

Go 1.24+ requires linux kernel 3.2
This commit is contained in:
Coia Prant
2025-12-09 15:04:29 +08:00
committed by GitHub
parent 24b381788c
commit 6504d5b9f9
7 changed files with 160 additions and 134 deletions
+26 -16
View File
@@ -31,24 +31,33 @@ import (
// defaultReadLoop is the standard procedure for reading from a connection
func (s *UDPSession) defaultReadLoop() {
buf := make([]byte, mtuLimit)
var src string
for {
if n, addr, err := s.conn.ReadFrom(buf); err == nil {
if s.isClosed() {
return
}
// make sure the packet is from the same source
if src == "" { // set source address
src = addr.String()
} else if addr.String() != src {
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
continue
}
s.packetInput(buf[:n])
} else {
n, addr, err := s.conn.ReadFrom(buf)
if err != nil {
s.notifyReadError(errors.WithStack(err))
return
}
if s.isClosed() {
return
}
// make sure the packet is from the same source
switch src {
case "":
// set source address if not set
src = addr.String()
case addr.String():
// source valid
default:
// source invalid
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
continue
}
s.packetInput(buf[:n])
}
}
@@ -56,11 +65,12 @@ func (s *UDPSession) defaultReadLoop() {
func (l *Listener) defaultMonitor() {
buf := make([]byte, mtuLimit)
for {
if n, from, err := l.conn.ReadFrom(buf); err == nil {
l.packetInput(buf[:n], from)
} else {
n, from, err := l.conn.ReadFrom(buf)
if err != nil {
l.notifyReadError(errors.WithStack(err))
return
}
l.packetInput(buf[:n], from)
}
}