From 949a93b2d5876707d56bf5be63b66f6746cdd7cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <12574910+72wo@users.noreply.github.com> Date: Sun, 26 Apr 2026 15:43:11 +0800 Subject: [PATCH] 1 --- logic/service/common/pack.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/logic/service/common/pack.go b/logic/service/common/pack.go index a98b79f5..e45630e8 100644 --- a/logic/service/common/pack.go +++ b/logic/service/common/pack.go @@ -1,22 +1,24 @@ package common import ( + "blazing/cool" "bytes" + "context" "encoding/binary" - "fmt" + "reflect" "github.com/lunixbochs/struc" ) // TomeeHeader 定义协议包头。 type TomeeHeader struct { - Len uint32 `json:"len"` // 包总长度(包头 + 数据体)。 + Len uint32 `json:"len"` // 包总长度(包头 + 数据体)。 Version byte `json:"version" struc:"[1]byte"` // 协议版本。 CMD uint32 `json:"cmdId" struc:"uint32"` // 命令 ID。 - UserID uint32 `json:"userId"` // 玩家 ID。 - Result uint32 `json:"result"` // 结果码。 - Data []byte `json:"data" struc:"skip"` // 数据体,序列化时跳过。 - Res []byte `struc:"skip"` // 预留返回数据,序列化时跳过。 + UserID uint32 `json:"userId"` // 玩家 ID。 + Result uint32 `json:"result"` // 结果码。 + Data []byte `json:"data" struc:"skip"` // 数据体,序列化时跳过。 + Res []byte `struc:"skip"` // 预留返回数据,序列化时跳过。 } // NewTomeeHeader 创建用于下行封包的默认 TomeeHeader。 @@ -39,7 +41,14 @@ func (h *TomeeHeader) Pack(data any) []byte { var data1 bytes.Buffer err := struc.Pack(&data1, data) if err != nil { - fmt.Println(err) + cool.Logger.Error(context.Background(), + "struc pack failed", + "cmd", h.CMD, + "userID", h.UserID, + "result", h.Result, + "payloadType", packetPayloadType(data), + "err", err, + ) } if len(data1.Bytes()) == 0 { @@ -89,3 +98,10 @@ func (h *TomeeHeader) packHeaderWithData(data []byte) []byte { return buf } + +func packetPayloadType(data any) string { + if data == nil { + return "" + } + return reflect.TypeOf(data).String() +}