github.com/metacubex/mihomo@v1.18.5/common/net/bind.go (about)

     1  package net
     2  
     3  import "net"
     4  
     5  type bindPacketConn struct {
     6  	EnhancePacketConn
     7  	rAddr net.Addr
     8  }
     9  
    10  func (c *bindPacketConn) Read(b []byte) (n int, err error) {
    11  	n, _, err = c.EnhancePacketConn.ReadFrom(b)
    12  	return n, err
    13  }
    14  
    15  func (c *bindPacketConn) WaitRead() (data []byte, put func(), err error) {
    16  	data, put, _, err = c.EnhancePacketConn.WaitReadFrom()
    17  	return
    18  }
    19  
    20  func (c *bindPacketConn) Write(b []byte) (n int, err error) {
    21  	return c.EnhancePacketConn.WriteTo(b, c.rAddr)
    22  }
    23  
    24  func (c *bindPacketConn) RemoteAddr() net.Addr {
    25  	return c.rAddr
    26  }
    27  
    28  func (c *bindPacketConn) LocalAddr() net.Addr {
    29  	if c.EnhancePacketConn.LocalAddr() == nil {
    30  		return &net.UDPAddr{IP: net.IPv4zero, Port: 0}
    31  	} else {
    32  		return c.EnhancePacketConn.LocalAddr()
    33  	}
    34  }
    35  
    36  func (c *bindPacketConn) Upstream() any {
    37  	return c.EnhancePacketConn
    38  }
    39  
    40  func NewBindPacketConn(pc net.PacketConn, rAddr net.Addr) net.Conn {
    41  	return &bindPacketConn{
    42  		EnhancePacketConn: NewEnhancePacketConn(pc),
    43  		rAddr:             rAddr,
    44  	}
    45  }