fix(socket): 修复连接处理逻辑并优化数据解码流程

- 修复 `OnOpen` 中网络类型判断位置不正确的问题,提前过滤非 TCP 连接
- 移除 `OnTraffic` 中重复的网络类型判断
- 优化 `TomeeSocketCodec` 的解码逻辑,使用 `InboundBuffered` 和 `Next` 提高效率
- 调整 `ByteArray` 创建方法参数,避免可变参数带来的性能损耗
- 在 `ClientData` 中将 `IsCrossDomain` 改为 `sync.Once` 避免重复处理
- 使用 `AsyncWrite` 替代 `Write` 提升写入异步性
- 修复连接关闭流程,使用
This commit is contained in:
2025-11-01 14:31:19 +08:00
parent 008701d3de
commit ea4ca98e49
11 changed files with 60 additions and 92 deletions

View File

@@ -63,21 +63,11 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) {
return nil, errors.New("packet body exceeds max length")
}
// 检查整个包是否完整
buf, err := c.Peek(int(bodyLen))
if err != nil {
if errors.Is(err, io.ErrShortBuffer) {
return nil, ErrIncompletePacket
}
return nil, err
if c.InboundBuffered() < int(bodyLen) {
return nil, ErrIncompletePacket
}
// 提取包体
body := make([]byte, bodyLen)
copy(body, buf)
// 从缓冲区中丢弃已读取的数据
_, _ = c.Discard(int(bodyLen))
body, _ := c.Next(int(bodyLen))
return body, nil
}