fix: transport.UDPConn 新增了 WithUDPLinuxTimestamping(false) 开关
This commit is contained in:
@@ -10,23 +10,22 @@ import (
|
||||
"omnisocketgo/cmd/internal/protocol"
|
||||
)
|
||||
|
||||
// UDPConn 是对 UDP 连接的轻量封装。
|
||||
// server 侧共享一个 net.UDPConn,通过 SendTo 指定目标地址;
|
||||
// peer 侧使用已连接的 net.UDPConn,直接 Send 即可。
|
||||
// UDPConn wraps a UDP socket for protocol message send/receive.
|
||||
type UDPConn struct {
|
||||
conn *net.UDPConn
|
||||
peerAddr *net.UDPAddr
|
||||
raw syscall.RawConn
|
||||
|
||||
logger latencylog.Logger
|
||||
txTimestampDebugLogger TXTimestampDebugLogger
|
||||
txPacketSeq uint32
|
||||
pendingTX map[uint32]udpTXPendingRecord
|
||||
nodeRole string
|
||||
nodeID string
|
||||
writeMu sync.Mutex
|
||||
closeOnce sync.Once
|
||||
closeErr error
|
||||
linuxTimestampingEnabled bool
|
||||
logger latencylog.Logger
|
||||
txTimestampDebugLogger TXTimestampDebugLogger
|
||||
txPacketSeq uint32
|
||||
pendingTX map[uint32]udpTXPendingRecord
|
||||
nodeRole string
|
||||
nodeID string
|
||||
writeMu sync.Mutex
|
||||
closeOnce sync.Once
|
||||
closeErr error
|
||||
}
|
||||
|
||||
type udpTXPendingRecord struct {
|
||||
@@ -37,10 +36,10 @@ type udpTXPendingRecord struct {
|
||||
observedTimestamps map[string]int64
|
||||
}
|
||||
|
||||
// UDPOption 用于为 UDPConn 注入可选行为。
|
||||
// UDPOption configures an optional behavior on UDPConn.
|
||||
type UDPOption func(*UDPConn)
|
||||
|
||||
// WithUDPLogger 为 UDP 连接注入业务消息日志上下文。
|
||||
// WithUDPLogger attaches latency logging context to a UDP connection.
|
||||
func WithUDPLogger(logger latencylog.Logger, nodeRole, nodeID string) UDPOption {
|
||||
return func(conn *UDPConn) {
|
||||
conn.logger = logger
|
||||
@@ -49,20 +48,28 @@ func WithUDPLogger(logger latencylog.Logger, nodeRole, nodeID string) UDPOption
|
||||
}
|
||||
}
|
||||
|
||||
// WithUDPTXTimestampDebugLogger 为 UDP 连接注入 TX errqueue 调试日志器。
|
||||
// WithUDPTXTimestampDebugLogger attaches a TX errqueue debug logger.
|
||||
func WithUDPTXTimestampDebugLogger(logger TXTimestampDebugLogger) UDPOption {
|
||||
return func(conn *UDPConn) {
|
||||
conn.txTimestampDebugLogger = logger
|
||||
}
|
||||
}
|
||||
|
||||
// NewUDPConn 创建 UDP transport 连接封装。
|
||||
// WithUDPLinuxTimestamping controls whether Linux UDP timestamping is enabled.
|
||||
func WithUDPLinuxTimestamping(enabled bool) UDPOption {
|
||||
return func(conn *UDPConn) {
|
||||
conn.linuxTimestampingEnabled = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// NewUDPConn creates a UDP transport wrapper.
|
||||
func NewUDPConn(conn *net.UDPConn, peerAddr *net.UDPAddr, opts ...UDPOption) (*UDPConn, error) {
|
||||
udpConn := &UDPConn{
|
||||
conn: conn,
|
||||
peerAddr: peerAddr,
|
||||
logger: latencylog.NoopLogger{},
|
||||
pendingTX: make(map[uint32]udpTXPendingRecord),
|
||||
conn: conn,
|
||||
peerAddr: peerAddr,
|
||||
linuxTimestampingEnabled: true,
|
||||
logger: latencylog.NoopLogger{},
|
||||
pendingTX: make(map[uint32]udpTXPendingRecord),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -73,14 +80,16 @@ func NewUDPConn(conn *net.UDPConn, peerAddr *net.UDPAddr, opts ...UDPOption) (*U
|
||||
udpConn.logger = latencylog.NoopLogger{}
|
||||
}
|
||||
|
||||
if err := udpConn.initUDPLinuxTimestamping(); err != nil {
|
||||
return nil, err
|
||||
if udpConn.linuxTimestampingEnabled {
|
||||
if err := udpConn.initUDPLinuxTimestamping(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return udpConn, nil
|
||||
}
|
||||
|
||||
// Send 将一条协议消息编码为 UDP 数据报并发送。
|
||||
// Send encodes and sends one protocol message over UDP.
|
||||
func (c *UDPConn) Send(msg protocol.Message) error {
|
||||
c.writeMu.Lock()
|
||||
defer c.writeMu.Unlock()
|
||||
@@ -94,7 +103,7 @@ func (c *UDPConn) Send(msg protocol.Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendTo 将一条协议消息编码为 UDP 数据报并发送到指定地址。
|
||||
// SendTo encodes and sends one protocol message to a specific UDP address.
|
||||
func (c *UDPConn) SendTo(msg protocol.Message, addr *net.UDPAddr) error {
|
||||
c.writeMu.Lock()
|
||||
defer c.writeMu.Unlock()
|
||||
@@ -108,7 +117,7 @@ func (c *UDPConn) SendTo(msg protocol.Message, addr *net.UDPAddr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Receive 从 UDP 连接读取一条完整协议消息。
|
||||
// Receive reads one full protocol message from UDP.
|
||||
func (c *UDPConn) Receive() (protocol.Message, *net.UDPAddr, error) {
|
||||
msg, addr, err := c.receiveMessageLinux()
|
||||
if err != nil {
|
||||
@@ -118,7 +127,7 @@ func (c *UDPConn) Receive() (protocol.Message, *net.UDPAddr, error) {
|
||||
return msg, addr, nil
|
||||
}
|
||||
|
||||
// ReceiveLoop 持续从 UDP 连接读取消息并交给 handler 处理。
|
||||
// ReceiveLoop continuously receives messages and passes them to handler.
|
||||
func (c *UDPConn) ReceiveLoop(handler func(protocol.Message, *net.UDPAddr) error) error {
|
||||
for {
|
||||
msg, addr, err := c.Receive()
|
||||
@@ -132,7 +141,7 @@ func (c *UDPConn) ReceiveLoop(handler func(protocol.Message, *net.UDPAddr) error
|
||||
}
|
||||
}
|
||||
|
||||
// Close 关闭底层 UDP 连接,保证重复调用安全。
|
||||
// Close closes the underlying UDP socket.
|
||||
func (c *UDPConn) Close() error {
|
||||
c.closeOnce.Do(func() {
|
||||
c.closeErr = c.conn.Close()
|
||||
|
||||
Reference in New Issue
Block a user