github.com/metacubex/mihomo@v1.18.5/common/net/packet/thread.go (about) 1 package packet 2 3 import ( 4 "net" 5 "sync" 6 ) 7 8 type threadSafePacketConn struct { 9 EnhancePacketConn 10 access sync.Mutex 11 } 12 13 func (c *threadSafePacketConn) WriteTo(b []byte, addr net.Addr) (int, error) { 14 c.access.Lock() 15 defer c.access.Unlock() 16 return c.EnhancePacketConn.WriteTo(b, addr) 17 } 18 19 func (c *threadSafePacketConn) Upstream() any { 20 return c.EnhancePacketConn 21 } 22 23 func (c *threadSafePacketConn) ReaderReplaceable() bool { 24 return true 25 } 26 27 func NewThreadSafePacketConn(pc net.PacketConn) EnhancePacketConn { 28 tsPC := &threadSafePacketConn{EnhancePacketConn: NewEnhancePacketConn(pc)} 29 if singPC, isSingPC := pc.(SingPacketConn); isSingPC { 30 return &threadSafeSingPacketConn{ 31 threadSafePacketConn: tsPC, 32 singPacketConn: singPC, 33 } 34 } 35 return tsPC 36 }