From 7de149d94671f57838b9b58dc58ff5d43488bd55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <1@72wo.cn> Date: Sun, 19 Oct 2025 01:43:40 +0800 Subject: [PATCH] =?UTF-8?q?```=20refactor(socket):=20=E4=BC=98=E5=8C=96TCP?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=8C=85=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 handleTcp 中条件判断的空格格式问题 - 在解码失败时增加详细 Debug 日志输出 - 完善不完整数据包时手动唤醒连接的处理流程 refactor(pet): 重构宠物经验系统与升级逻辑 - 将经验计算函数移至 model 层统一管理 - 优化 AddPetExp 方法逻辑,避免直接修改原字段 - 升级过程中正确扣减经验池并防止溢出 - 抽离 Update 方法用于处理宠物进化和经验更新 refactor(model): 调整 PlayerInfo 结构体引用方式及相关初始化逻辑 - 修改 PlayerEX 中 Data 字段为值类型而非指针 - 更新 NewPlayerInfo 返回值为值类型 - 修正 defaults.Set 调用传参以适配结构体值类型 refactor(service): 统一 UserService 数据传递方式 - 修复 Person 方法返回值为指针类型 - 修复 Save 方法中赋值操作使用解引用方式 ``` --- common/socket/ServerEvent.go | 17 +++++++---------- common/socket/codec/SocketCodec_Tomee.go | 3 +-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index 4496ee289..c63ba3972 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -2,6 +2,7 @@ package socket import ( "context" + "encoding/hex" "fmt" "log" "os" @@ -140,7 +141,7 @@ func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) { for _, msg := range messages { //client := conn.RemoteAddr().String() _ = s.workerPool.Submit(func() { - s.parser(c, msg.Payload) + s.handler.Handle(c, msg.Payload) }) } @@ -158,13 +159,14 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) { if err == codec.ErrIncompletePacket && conn.InboundBuffered() > 0 { t, _ := conn.Peek(conn.InboundBuffered()) - cool.Loger.Debug(context.Background(), "断包", err.Error(), conn.InboundBuffered(), t) + cool.Loger.Debug(context.Background(), "断包", err.Error(), conn.InboundBuffered(), hex.EncodeToString(t)) if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data logging.Errorf("failed to wake up the connection, %v", err) return gnet.Close } } else { - cool.Loger.Debug(context.Background(), "数据错误", err.Error(), conn.InboundBuffered()) + t, _ := conn.Peek(conn.InboundBuffered()) + cool.Loger.Debug(context.Background(), "数据错误", err.Error(), conn.InboundBuffered(), hex.EncodeToString(t)) action = gnet.Close return } @@ -174,7 +176,8 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) { if data != nil { //client := conn.RemoteAddr().String() _ = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复 - s.parser(conn, data) + //todo 这里待实现注入player实体 + s.handler.Handle(conn, data) }) } @@ -182,12 +185,6 @@ func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) { } -func (s *Server) parser(c gnet.Conn, line []byte) { - - //todo 这里待实现注入player实体 - s.handler.Handle(c, line) -} - // CROSS_DOMAIN 定义跨域策略文件内容 const CROSS_DOMAIN = "\x00" diff --git a/common/socket/codec/SocketCodec_Tomee.go b/common/socket/codec/SocketCodec_Tomee.go index 85ba680c6..5dbf9c419 100644 --- a/common/socket/codec/SocketCodec_Tomee.go +++ b/common/socket/codec/SocketCodec_Tomee.go @@ -60,7 +60,6 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) { if bodyLen > maxBodyLen { return nil, errors.New("packet body exceeds max length") } - totalLen := 4 + int(bodyLen) // 检查整个包是否完整 buf, err := c.Peek(int(bodyLen)) @@ -76,7 +75,7 @@ func (codec TomeeSocketCodec) Decode(c gnet.Conn) ([]byte, error) { copy(body, buf) // 从缓冲区中丢弃已读取的数据 - _, _ = c.Discard(totalLen) + _, _ = c.Discard(4 + int(bodyLen)) return body, nil }