mirror of
https://github.com/xtaci/kcp-go.git
synced 2026-10-19 16:14:38 +00:00
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:
+26
-16
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user