chore: decrease direct udp read memory used for no-windows platform
This commit is contained in:
68
common/net/packet.go
Normal file
68
common/net/packet.go
Normal file
@ -0,0 +1,68 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
type EnhancePacketConn interface {
|
||||
net.PacketConn
|
||||
WaitReadFrom() (data []byte, put func(), addr net.Addr, err error)
|
||||
Upstream() any
|
||||
}
|
||||
|
||||
func NewEnhancePacketConn(pc net.PacketConn) EnhancePacketConn {
|
||||
if udpConn, isUDPConn := pc.(*net.UDPConn); isUDPConn {
|
||||
return &enhanceUDPConn{UDPConn: udpConn}
|
||||
}
|
||||
return &enhancePacketConn{PacketConn: pc}
|
||||
}
|
||||
|
||||
type enhancePacketConn struct {
|
||||
net.PacketConn
|
||||
}
|
||||
|
||||
func (c *enhancePacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||
return waitReadFrom(c.PacketConn)
|
||||
}
|
||||
|
||||
func (c *enhancePacketConn) Upstream() any {
|
||||
return c.PacketConn
|
||||
}
|
||||
|
||||
func (c *enhancePacketConn) WriterReplaceable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *enhancePacketConn) ReaderReplaceable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *enhanceUDPConn) Upstream() any {
|
||||
return c.UDPConn
|
||||
}
|
||||
|
||||
func (c *enhanceUDPConn) WriterReplaceable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *enhanceUDPConn) ReaderReplaceable() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func waitReadFrom(pc net.PacketConn) (data []byte, put func(), addr net.Addr, err error) {
|
||||
readBuf := pool.Get(pool.UDPBufferSize)
|
||||
put = func() {
|
||||
_ = pool.Put(readBuf)
|
||||
}
|
||||
var readN int
|
||||
readN, addr, err = pc.ReadFrom(readBuf)
|
||||
if readN > 0 {
|
||||
data = readBuf[:readN]
|
||||
} else {
|
||||
put()
|
||||
put = nil
|
||||
}
|
||||
return
|
||||
}
|
64
common/net/packet_posix.go
Normal file
64
common/net/packet_posix.go
Normal file
@ -0,0 +1,64 @@
|
||||
//go:build !windows
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
type enhanceUDPConn struct {
|
||||
*net.UDPConn
|
||||
rawConn syscall.RawConn
|
||||
}
|
||||
|
||||
func (c *enhanceUDPConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||
if c.rawConn == nil {
|
||||
c.rawConn, _ = c.UDPConn.SyscallConn()
|
||||
}
|
||||
var readErr error
|
||||
err = c.rawConn.Read(func(fd uintptr) (done bool) {
|
||||
readBuf := pool.Get(pool.UDPBufferSize)
|
||||
put = func() {
|
||||
_ = pool.Put(readBuf)
|
||||
}
|
||||
var readFrom syscall.Sockaddr
|
||||
var readN int
|
||||
readN, _, _, readFrom, readErr = syscall.Recvmsg(int(fd), readBuf, nil, 0)
|
||||
if readN > 0 {
|
||||
data = readBuf[:readN]
|
||||
} else {
|
||||
put()
|
||||
put = nil
|
||||
}
|
||||
if readErr == syscall.EAGAIN {
|
||||
return false
|
||||
}
|
||||
if readFrom != nil {
|
||||
switch from := readFrom.(type) {
|
||||
case *syscall.SockaddrInet4:
|
||||
ip := from.Addr // copy from.Addr; ip escapes, so this line allocates 4 bytes
|
||||
addr = &net.UDPAddr{IP: ip[:], Port: from.Port}
|
||||
case *syscall.SockaddrInet6:
|
||||
ip := from.Addr // copy from.Addr; ip escapes, so this line allocates 16 bytes
|
||||
addr = &net.UDPAddr{IP: ip[:], Port: from.Port, Zone: strconv.FormatInt(int64(from.ZoneId), 10)}
|
||||
}
|
||||
}
|
||||
if readN == 0 {
|
||||
readErr = io.EOF
|
||||
}
|
||||
return true
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if readErr != nil {
|
||||
err = readErr
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
15
common/net/packet_windows.go
Normal file
15
common/net/packet_windows.go
Normal file
@ -0,0 +1,15 @@
|
||||
//go:build windows
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type enhanceUDPConn struct {
|
||||
*net.UDPConn
|
||||
}
|
||||
|
||||
func (c *enhanceUDPConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
||||
return waitReadFrom(c.UDPConn)
|
||||
}
|
Reference in New Issue
Block a user