Files
bl/common/socket/codec/SocketCodec_Tomee.go
昔念 ea4ca98e49 fix(socket): 修复连接处理逻辑并优化数据解码流程
- 修复 `OnOpen` 中网络类型判断位置不正确的问题,提前过滤非 TCP 连接
- 移除 `OnTraffic` 中重复的网络类型判断
- 优化 `TomeeSocketCodec` 的解码逻辑,使用 `InboundBuffered` 和 `Next` 提高效率
- 调整 `ByteArray` 创建方法参数,避免可变参数带来的性能损耗
- 在 `ClientData` 中将 `IsCrossDomain` 改为 `sync.Once` 避免重复处理
- 使用 `AsyncWrite` 替代 `Write` 提升写入异步性
- 修复连接关闭流程,使用
2025-11-01 14:31:19 +08:00

74 lines
1.5 KiB
Go

package codec
import (
"encoding/binary"
"errors"
"io"
"github.com/panjf2000/gnet/v2"
)
var ErrIncompletePacket = errors.New("incomplete packet")
const maxBodyLen = 10 * 1024 * 1024 // 业务最大包体长度,按需调整
// TomeeSocketCodec 协议格式:
//
// * 0 4
// * +-----------+
// * | body len |
// * +-----------+
// * | |
// * + +
// * | body bytes|
// * + +
// * | ... ... |
// * +-----------+
type TomeeSocketCodec struct{}
var _ SocketCodec = (*TomeeSocketCodec)(nil)
func NewTomeeSocketCodec() *TomeeSocketCodec {
return &TomeeSocketCodec{}
}
func (codec TomeeSocketCodec) Encode(buf []byte) ([]byte, error) {
bodyLen := len(buf)
data := make([]byte, 4+bodyLen)
// 写入4字节的包长度
binary.BigEndian.PutUint32(data[:4], uint32(bodyLen))
// 写入包体
copy(data[4:], buf)
return data, nil
}
func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) {
// handle(c)
// 先读取4字节的包长度
lenBuf, err := c.Peek(4)
if err != nil {
if errors.Is(err, io.ErrShortBuffer) {
return nil, ErrIncompletePacket
}
return nil, err
}
bodyLen := binary.BigEndian.Uint32(lenBuf)
if bodyLen > maxBodyLen {
return nil, errors.New("packet body exceeds max length")
}
if c.InboundBuffered() < int(bodyLen) {
return nil, ErrIncompletePacket
}
// 提取包体
body, _ := c.Next(int(bodyLen))
return body, nil
}