mihomo/common/net/bind.go

46 lines
968 B
Go
Raw Normal View History

2022-12-19 21:34:07 +08:00
package net
import "net"
type bindPacketConn struct {
EnhancePacketConn
2022-12-19 21:34:07 +08:00
rAddr net.Addr
}
func (c *bindPacketConn) Read(b []byte) (n int, err error) {
n, _, err = c.EnhancePacketConn.ReadFrom(b)
2022-12-19 21:34:07 +08:00
return n, err
}
func (c *bindPacketConn) WaitRead() (data []byte, put func(), err error) {
data, put, _, err = c.EnhancePacketConn.WaitReadFrom()
return
2022-12-19 21:34:07 +08:00
}
func (c *bindPacketConn) Write(b []byte) (n int, err error) {
return c.EnhancePacketConn.WriteTo(b, c.rAddr)
2022-12-19 21:34:07 +08:00
}
func (c *bindPacketConn) RemoteAddr() net.Addr {
return c.rAddr
}
func (c *bindPacketConn) LocalAddr() net.Addr {
if c.EnhancePacketConn.LocalAddr() == nil {
2022-12-19 21:34:07 +08:00
return &net.UDPAddr{IP: net.IPv4zero, Port: 0}
} else {
return c.EnhancePacketConn.LocalAddr()
2022-12-19 21:34:07 +08:00
}
}
func (c *bindPacketConn) Upstream() any {
return c.EnhancePacketConn
}
func NewBindPacketConn(pc EnhancePacketConn, rAddr net.Addr) net.Conn {
2022-12-19 21:34:07 +08:00
return &bindPacketConn{
EnhancePacketConn: pc,
rAddr: rAddr,
2022-12-19 21:34:07 +08:00
}
}