2023-05-11 13:47:51 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2023-05-11 15:34:28 +08:00
|
|
|
"sync"
|
2023-05-11 13:47:51 +08:00
|
|
|
|
2023-05-11 19:01:41 +08:00
|
|
|
"github.com/Dreamacro/clash/common/net/deadline"
|
|
|
|
"github.com/Dreamacro/clash/common/net/packet"
|
2023-05-11 13:47:51 +08:00
|
|
|
)
|
|
|
|
|
2023-05-11 19:01:41 +08:00
|
|
|
type EnhancePacketConn = packet.EnhancePacketConn
|
2023-05-11 13:47:51 +08:00
|
|
|
|
2023-05-11 19:01:41 +08:00
|
|
|
var NewEnhancePacketConn = packet.NewEnhancePacketConn
|
|
|
|
var NewDeadlinePacketConn = deadline.NewPacketConn
|
2023-05-11 19:58:50 +08:00
|
|
|
var NewDeadlineEnhancePacketConn = deadline.NewEnhancePacketConn
|
2023-05-11 15:34:28 +08:00
|
|
|
|
|
|
|
type threadSafePacketConn struct {
|
2023-05-12 09:14:27 +08:00
|
|
|
EnhancePacketConn
|
2023-05-11 15:34:28 +08:00
|
|
|
access sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *threadSafePacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
2023-05-12 09:14:27 +08:00
|
|
|
return c.EnhancePacketConn.WriteTo(b, addr)
|
2023-05-11 15:34:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *threadSafePacketConn) Upstream() any {
|
2023-05-12 09:14:27 +08:00
|
|
|
return c.EnhancePacketConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *threadSafePacketConn) ReaderReplaceable() bool {
|
|
|
|
return true
|
2023-05-11 15:34:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewThreadSafePacketConn(pc net.PacketConn) net.PacketConn {
|
2023-05-12 09:14:27 +08:00
|
|
|
return &threadSafePacketConn{EnhancePacketConn: NewEnhancePacketConn(pc)}
|
2023-05-11 15:34:28 +08:00
|
|
|
}
|