This commit is contained in:
昔念
2026-04-26 15:43:11 +08:00
parent 9171e53e9c
commit 949a93b2d5

View File

@@ -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 "<nil>"
}
return reflect.TypeOf(data).String()
}