Files
bl/logic/service/player/pack.go

277 lines
6.9 KiB
Go
Raw Normal View History

package player
2025-06-20 17:00:56 +08:00
import (
2025-10-27 09:36:49 +00:00
"blazing/common/socket/errorcode"
"blazing/cool"
"blazing/logic/service/common"
2025-11-15 22:17:43 +00:00
"encoding/binary"
"sync"
2025-10-27 09:36:49 +00:00
"context"
"bytes"
"fmt"
"reflect"
2025-06-24 22:09:05 +08:00
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
2025-10-27 09:36:49 +00:00
"github.com/gogf/gf/v2/os/glog"
"github.com/lunixbochs/struc"
"github.com/panjf2000/gnet/v2"
)
// getUnderlyingValue 递归解析reflect.Value解包指针、interface{}到底层具体类型
func getUnderlyingValue(val reflect.Value) (reflect.Value, error) {
for {
switch val.Kind() {
// 解包指针:获取指针指向的值
case reflect.Ptr:
if val.IsNil() {
return reflect.Value{}, nil
}
val = val.Elem()
// 解包interface{}:获取接口包裹的动态值
case reflect.Interface:
if val.IsNil() {
return reflect.Value{}, nil
}
val = val.Elem()
// 非指针/接口类型,终止递归
default:
return val, nil
}
}
}
2025-10-27 09:36:49 +00:00
// 遍历结构体方法并执行RECV_cmd
func (h *ClientData) Recv(data common.TomeeHeader) {
defer func() {
if err := recover(); err != nil { // 恢复 panicerr 为 panic 错误值
// 1. 打印错误信息
2025-10-27 09:36:49 +00:00
cool.Logger.Error(context.TODO(), "panic 错误:", err)
}
}()
2026-01-23 14:59:15 +00:00
cmdlister, ok := cool.CmdCache[data.CMD]
2025-10-27 09:36:49 +00:00
if !ok {
2026-02-02 11:00:37 +08:00
// glog.Debug(context.Background(), data.UserID, data.CMD, "cmd未注册")
2025-10-27 09:36:49 +00:00
return //TODO 待实现cmd未注册
}
params := []reflect.Value{}
//funct := cmdlister.Type().NumIn()
// 如果需要可设置的变量(用于修改值),创建指针并解引用
ptrValue := reflect.New(cmdlister.Req)
2025-10-27 09:36:49 +00:00
tt1 := ptrValue.Elem().Addr().Interface()
2026-02-08 03:48:56 +08:00
2025-10-27 09:36:49 +00:00
// fmt.Println(tt1)
err := struc.Unpack(bytes.NewBuffer(data.Data), tt1)
2026-02-07 23:53:07 +08:00
playerconn, cok := h.Conn.Context().(*ClientData)
if !cok { //如果链接断开,就返回
return
}
2025-10-27 09:36:49 +00:00
if err != nil {
2026-02-07 23:53:07 +08:00
2026-02-08 12:02:09 +08:00
cool.Logger.Error(context.Background(), data.UserID, data.CMD, "解包失败,", err, data.Data)
2026-02-08 03:18:00 +08:00
//fmt.Println(data.UserID, data.CMD, "解包失败,", hex.EncodeToString(data.Data))
return
2025-10-27 09:36:49 +00:00
}
2026-02-08 03:49:11 +08:00
2025-10-27 09:36:49 +00:00
ptrValue1 := ptrValue.Elem().Addr()
// 设置 Name 字段
nameField := ptrValue.Elem().Field(0) //首个为header
nameField.Set(reflect.ValueOf(data))
2025-10-29 02:05:34 +08:00
if data.CMD > 1001 { //if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
//t := GetPlayer(c, data.UserID)
2025-10-29 02:05:34 +08:00
2025-10-27 09:36:49 +00:00
// fmt.Println(data.CMD, "接收 变量的地址 ", &t.Info, t.Info.UserID)
params = append(params, ptrValue1, reflect.ValueOf(h.Conn.Context().(*ClientData).Player))
2025-10-27 09:36:49 +00:00
} else {
params = append(params, ptrValue1, reflect.ValueOf(h.Conn))
2025-10-27 09:36:49 +00:00
}
ret := cmdlister.Func.Call(params)
2025-10-27 09:36:49 +00:00
if len(ret) <= 0 { //如果判断没有参数,那就说明这个包没有返回参数
return
}
aa, ok := ret[1].Interface().(errorcode.ErrorCode) //判断错误
data.Result = uint32(aa)
if aa == -1 {
return
}
2025-10-27 09:36:49 +00:00
if ok && aa != 0 { //这里实现回复错误包
glog.Info(context.Background(), data.UserID, data.CMD, aa.Code())
2026-02-07 23:53:07 +08:00
playerconn.SendPack(data.Pack(nil))
2025-10-27 09:36:49 +00:00
return
}
t1 := data.Pack(ret[0].Interface())
//cool.Loger.Debug(context.Background(), "发送数据_回包", data.UserID, data.CMD, ret[0].Interface(), hex.EncodeToString(t1))
//data.Version = 49
2026-02-07 23:53:07 +08:00
playerconn.SendPack(t1)
2025-10-27 09:36:49 +00:00
}
type ClientData struct {
IsCrossDomain sync.Once //是否跨域过
Player *Player //客户实体
//Mu sync.RWMutex
ERROR_CONNUT int
Wsmsg *WsCodec
Conn gnet.Conn
//SaveL sync.Once //保存锁
2026-02-08 04:58:58 +08:00
//SaveDone chan struct{}
}
func NewClientData(c gnet.Conn) *ClientData {
// 创建事件处理器
// 创建消费端串行处理的Lockfree
cd := &ClientData{
Conn: c,
Wsmsg: &WsCodec{},
}
2026-02-08 04:58:58 +08:00
return cd
}
2026-02-08 02:11:46 +08:00
// XORDecrypt 异或解密函数密钥改为uint32版本
// 核心逻辑将uint32密钥拆分为4字节数组大端序适配AS3二进制处理习惯循环与加密数据异或
// 参数:
//
// encryptedData - 待解密的字节数组
// key - 32位无符号整数密钥替代原字符串密钥
//
// 返回值:解密后的字节数组
func XORDecryptU(encryptedData []byte, key uint32) []byte {
// 边界条件:待解密数据为空,直接返回空
if len(encryptedData) == 0 {
return []byte{}
}
// 1. 将uint32密钥转换为4字节数组关键步骤
// 字节序选择BigEndian大端是AS3/Java等语言的默认二进制处理方式若需小端可改为binary.LittleEndian
keyBytes := make([]byte, 4) // uint32固定占4个字节
binary.BigEndian.PutUint32(keyBytes, key)
keyLen := len(keyBytes) // 固定为4无需额外判断长度
// 2. 执行异或解密(逻辑与原版本一致,仅密钥来源不同)
decrypted := make([]byte, len(encryptedData))
for i, b := range encryptedData {
// 循环复用4字节密钥索引取模i%4
keyIndex := i % keyLen
decrypted[i] = b ^ keyBytes[keyIndex]
}
return decrypted
}
func XORDecrypt(encryptedData []byte, keyStr string) []byte {
if len(encryptedData) == 0 || keyStr == "" {
return []byte{}
}
// 1. 将密钥字符串转换为UTF-8字节数组对应AS3的writeUTFBytes(_arg_2)
keyBytes := []byte(keyStr) // Go中string转[]byte默认是UTF-8编码与AS3的writeUTFBytes一致
keyLen := len(keyBytes)
if keyLen == 0 {
return encryptedData // 空密钥不加密,直接返回
}
// 2. 执行异或操作(与加密逻辑一致,异或两次还原数据)
decrypted := make([]byte, len(encryptedData))
for i, b := range encryptedData {
// 循环复用密钥字节(索引取模)
keyIndex := i % keyLen
decrypted[i] = b ^ keyBytes[keyIndex]
}
return decrypted
}
func (h *ClientData) OnEvent(v []byte) {
2026-02-04 19:43:05 +08:00
t, ok := h.Conn.Context().(*ClientData)
if !ok {
return
}
var header common.TomeeHeader
2025-11-15 22:17:43 +00:00
// 解析Len0-3字节
header.Len = binary.BigEndian.Uint32(v[0:4])
// 解析Version第4字节
2026-02-08 02:11:46 +08:00
//header.Version = v[4]
2025-11-15 22:17:43 +00:00
// 解析CMD5-8字节
header.CMD = binary.BigEndian.Uint32(v[5:9])
// 解析UserID9-12字节
header.UserID = binary.BigEndian.Uint32(v[9:13])
// 解析Result13-16字节
2026-02-08 02:11:46 +08:00
//header.Result = binary.BigEndian.Uint32(v[13:17])
2025-11-15 22:17:43 +00:00
// 解析数据部分17字节之后
if len(v) > 17 {
2026-02-08 02:11:46 +08:00
header.Data = v[17:]
2025-11-15 22:17:43 +00:00
} else {
header.Data = []byte{} // 数据部分为空时显式初始化
}
if header.CMD > 1001 {
2026-02-04 19:43:05 +08:00
if t.Player == nil {
2026-01-22 16:01:52 +00:00
fmt.Println(header.UserID, "账号未注册")
return
}
2026-02-04 19:43:05 +08:00
if t.Player.Info == nil {
2026-01-22 16:01:52 +00:00
fmt.Println(header.UserID, "未创建角色")
return
}
2026-02-08 03:30:15 +08:00
if len(header.Data) > 0 {
header.Data = XORDecryptU(header.Data, t.Player.Hash)
}
}
if cool.Config.ServerInfo.IsDebug != 0 {
fmt.Println("接收数据", header.UserID, header.CMD)
}
h.Recv(header)
}
func (p *ClientData) SendPack(b []byte) error {
2026-02-07 14:59:22 +08:00
cli, ok := p.Conn.Context().(*ClientData)
if !ok {
return fmt.Errorf("链接错误,取消发包")
}
2026-02-07 14:59:22 +08:00
if cli.Wsmsg == nil {
return fmt.Errorf("ws空")
}
2026-02-07 14:59:22 +08:00
if cli.Wsmsg.Upgraded {
// This is the echo server
2026-02-07 18:21:52 +08:00
wsutil.WriteServerMessage(p.Conn, ws.OpBinary, b)
} else {
2026-02-07 18:21:52 +08:00
p.Conn.Write(b)
}
return nil
}