fix(socket): 修复连接处理逻辑并优化数据解码流程
- 修复 `OnOpen` 中网络类型判断位置不正确的问题,提前过滤非 TCP 连接 - 移除 `OnTraffic` 中重复的网络类型判断 - 优化 `TomeeSocketCodec` 的解码逻辑,使用 `InboundBuffered` 和 `Next` 提高效率 - 调整 `ByteArray` 创建方法参数,避免可变参数带来的性能损耗 - 在 `ClientData` 中将 `IsCrossDomain` 改为 `sync.Once` 避免重复处理 - 使用 `AsyncWrite` 替代 `Write` 提升写入异步性 - 修复连接关闭流程,使用
This commit is contained in:
@@ -87,7 +87,7 @@ func (h *TomeeHeader) Pack(data any) []byte { //组包
|
||||
}
|
||||
h.Len = uint32(len(datar) + 17)
|
||||
|
||||
by := bytearray.CreateByteArray()
|
||||
by := bytearray.CreateByteArray(nil)
|
||||
by.WriteUInt32(h.Len)
|
||||
by.WriteByte(h.Version)
|
||||
by.WriteUInt32(uint32(h.CMD))
|
||||
@@ -147,7 +147,8 @@ func (h *ClientData) Recv(data TomeeHeader) {
|
||||
// fmt.Println(tt1)
|
||||
err := struc.Unpack(bytes.NewBuffer(data.Data), tt1)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
cool.Loger.Error(context.Background(), data.UserID, data.CMD, "解包失败")
|
||||
return
|
||||
}
|
||||
//fmt.Println(tt1)
|
||||
ptrValue1 := ptrValue.Elem().Addr()
|
||||
@@ -197,25 +198,24 @@ func (h *ClientData) Recv(data TomeeHeader) {
|
||||
}
|
||||
|
||||
type ClientData struct {
|
||||
IsCrossDomain bool //是否跨域过
|
||||
Player *Player //客户实体
|
||||
Mu sync.RWMutex
|
||||
ERROR_CONNUT int
|
||||
Wsmsg *WsCodec
|
||||
Conn gnet.Conn
|
||||
SaveL sync.Once //保存锁
|
||||
IsCrossDomain sync.Once //是否跨域过
|
||||
Player *Player //客户实体
|
||||
//Mu sync.RWMutex
|
||||
ERROR_CONNUT int
|
||||
Wsmsg *WsCodec
|
||||
Conn gnet.Conn
|
||||
SaveL sync.Once //保存锁
|
||||
|
||||
CloseChan chan struct{}
|
||||
}
|
||||
|
||||
func NewClientData(c gnet.Conn) *ClientData {
|
||||
// 创建事件处理器
|
||||
|
||||
cd := ClientData{
|
||||
IsCrossDomain: false,
|
||||
Player: nil,
|
||||
Conn: c,
|
||||
Wsmsg: &WsCodec{},
|
||||
|
||||
Player: nil,
|
||||
Conn: c,
|
||||
Wsmsg: &WsCodec{},
|
||||
}
|
||||
return &cd
|
||||
|
||||
@@ -249,13 +249,10 @@ func (h *ClientData) OnEvent(v []byte) {
|
||||
|
||||
}
|
||||
func (p *ClientData) SendPack(b []byte) error {
|
||||
// if _, ok := p.MainConn.Context().(*ClientData); !ok {
|
||||
// return fmt.Errorf("链接错误,取消发包")
|
||||
if _, ok := p.Conn.Context().(*ClientData); !ok {
|
||||
return fmt.Errorf("链接错误,取消发包")
|
||||
|
||||
// }
|
||||
|
||||
p.Conn.Context().(*ClientData).Mu.Lock()
|
||||
defer p.Conn.Context().(*ClientData).Mu.Unlock()
|
||||
}
|
||||
|
||||
if p.Conn.Context().(*ClientData).Wsmsg.Upgraded {
|
||||
// This is the echo server
|
||||
@@ -266,7 +263,7 @@ func (p *ClientData) SendPack(b []byte) error {
|
||||
}
|
||||
} else {
|
||||
|
||||
_, err := p.Conn.Write(b)
|
||||
err := p.Conn.AsyncWrite(b, nil)
|
||||
if err != nil {
|
||||
glog.Debug(context.Background(), err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user