299 lines
7.7 KiB
Go
299 lines
7.7 KiB
Go
package player
|
||
|
||
import (
|
||
"blazing/common/socket/errorcode"
|
||
"blazing/cool"
|
||
"blazing/logic/service/common"
|
||
"encoding/binary"
|
||
"encoding/hex"
|
||
"sync"
|
||
|
||
"context"
|
||
|
||
"bytes"
|
||
"fmt"
|
||
"reflect"
|
||
|
||
"github.com/gobwas/ws"
|
||
"github.com/gobwas/ws/wsutil"
|
||
"github.com/gogf/gf/v2/os/glog"
|
||
"github.com/lunixbochs/struc"
|
||
"github.com/panjf2000/gnet/v2"
|
||
"github.com/valyala/bytebufferpool"
|
||
)
|
||
|
||
// 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
|
||
}
|
||
}
|
||
}
|
||
|
||
// XORDecryptU 优化后的异或解密:减少内存分配,支持复用缓冲区
|
||
// XORDecryptU 基于bytebufferpool优化的异或解密函数
|
||
// 保留原有接口,无侵入式优化,高频调用下大幅减少内存分配和GC
|
||
func XORDecryptU(encryptedData []byte, key uint32) []byte {
|
||
if len(encryptedData) == 0 {
|
||
return []byte{}
|
||
}
|
||
|
||
// 1. 栈上分配密钥字节数组(无GC压力,保留原优化)
|
||
var keyBytes [4]byte
|
||
binary.BigEndian.PutUint32(keyBytes[:], key)
|
||
keyLen := len(keyBytes)
|
||
|
||
// 2. 从bytebufferpool获取池化缓冲区(替代make分配)
|
||
buf := bytebufferpool.Get()
|
||
defer bytebufferpool.Put(buf) // 函数结束自动归还缓冲区到池
|
||
|
||
// 3. 调整缓冲区长度,匹配待解密数据(避免扩容)
|
||
buf.B = buf.B[:0] // 清空原有数据,保留底层数组
|
||
if cap(buf.B) < len(encryptedData) {
|
||
// 若缓冲区容量不足,直接扩容(bytebufferpool会自动管理)
|
||
buf.B = make([]byte, len(encryptedData))
|
||
} else {
|
||
// 容量足够,直接调整长度
|
||
buf.B = buf.B[:len(encryptedData)]
|
||
}
|
||
|
||
// 4. 核心异或解密逻辑(直接操作buf.B,无额外内存分配)
|
||
decrypted := buf.B
|
||
for i, b := range encryptedData {
|
||
decrypted[i] = b ^ keyBytes[i%keyLen]
|
||
}
|
||
|
||
// 5. 拷贝结果(关键:避免返回池化缓冲区,防止被后续调用覆盖)
|
||
result := make([]byte, len(decrypted))
|
||
copy(result, decrypted)
|
||
|
||
return result
|
||
}
|
||
|
||
// 遍历结构体方法并执行RECV_cmd
|
||
func (h *ClientData) OnEvent(data common.TomeeHeader) {
|
||
// defer func() {
|
||
// if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
||
// // 1. 打印错误信息
|
||
// if h.Player != nil {
|
||
// if h.Player.Info != nil {
|
||
// cool.Logger.Error(context.TODO(), "panic 错误:", cool.Config.ServerInfo.OnlineID, h.Player.Info.UserID, err)
|
||
// } else {
|
||
// cool.Logger.Error(context.TODO(), "panic 错误:", cool.Config.ServerInfo.OnlineID, err)
|
||
|
||
// }
|
||
|
||
// } else {
|
||
// cool.Logger.Error(context.TODO(), "panic 错误:", err)
|
||
// }
|
||
|
||
// }
|
||
// }()
|
||
if data.CMD > 1001 {
|
||
|
||
if h.Player == nil {
|
||
fmt.Println(data.UserID, "账号未注册")
|
||
return
|
||
}
|
||
if h.Player.Info == nil {
|
||
fmt.Println(data.UserID, "未创建角色")
|
||
return
|
||
}
|
||
if data.Data != nil {
|
||
data.Data = XORDecryptU(data.Data, h.Player.Hash)
|
||
}
|
||
|
||
}
|
||
if cool.Config.ServerInfo.IsDebug != 0 {
|
||
fmt.Println("接收数据", data.UserID, data.CMD)
|
||
}
|
||
if data.UserID == 0 {
|
||
return
|
||
}
|
||
cmdlister, ok := cool.CmdCache[data.CMD]
|
||
if !ok {
|
||
|
||
// glog.Debug(context.Background(), data.UserID, data.CMD, "cmd未注册")
|
||
return //TODO 待实现cmd未注册
|
||
}
|
||
|
||
params := []reflect.Value{}
|
||
|
||
//funct := cmdlister.Type().NumIn()
|
||
|
||
// 如果需要可设置的变量(用于修改值),创建指针并解引用
|
||
ptrValue := reflect.New(cmdlister.Req)
|
||
|
||
tt1 := ptrValue.Elem().Addr().Interface()
|
||
|
||
// fmt.Println(tt1)
|
||
err := struc.Unpack(bytes.NewBuffer(data.Data), tt1)
|
||
playerconn, cok := h.Conn.Context().(*ClientData)
|
||
if !cok { //如果链接断开,就返回
|
||
return
|
||
}
|
||
if err != nil {
|
||
|
||
cool.Logger.Error(context.Background(), data.UserID, data.CMD, "解包失败,", err, hex.EncodeToString(data.Data))
|
||
//fmt.Println(data.UserID, data.CMD, "解包失败,", hex.EncodeToString(data.Data))
|
||
data.Result = uint32(errorcode.ErrorCodes.ErrSystemProcessingError)
|
||
playerconn.SendPack(data.Pack(nil))
|
||
return
|
||
}
|
||
|
||
ptrValue1 := ptrValue.Elem().Addr()
|
||
// 设置 Name 字段
|
||
nameField := ptrValue.Elem().Field(0) //首个为header
|
||
nameField.Set(reflect.ValueOf(data))
|
||
|
||
if data.CMD > 1001 { //if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
|
||
//t := GetPlayer(c, data.UserID)
|
||
|
||
// fmt.Println(data.CMD, "接收 变量的地址 ", &t.Info, t.Info.UserID)
|
||
|
||
params = append(params, ptrValue1, reflect.ValueOf(h.Conn.Context().(*ClientData).Player))
|
||
} else {
|
||
|
||
params = append(params, ptrValue1, reflect.ValueOf(h.Conn))
|
||
}
|
||
|
||
ret := cmdlister.Func.Call(params)
|
||
|
||
if len(ret) <= 0 { //如果判断没有参数,那就说明这个包没有返回参数
|
||
return
|
||
}
|
||
|
||
aa, ok := ret[1].Interface().(errorcode.ErrorCode) //判断错误
|
||
data.Result = uint32(aa)
|
||
if aa == -1 {
|
||
return
|
||
|
||
}
|
||
|
||
if ok && aa != 0 { //这里实现回复错误包
|
||
|
||
glog.Info(context.Background(), data.UserID, data.CMD, aa.Code())
|
||
playerconn.SendPack(data.Pack(nil))
|
||
|
||
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
|
||
playerconn.SendPack(t1)
|
||
|
||
}
|
||
|
||
type ClientData struct {
|
||
IsCrossDomain sync.Once //是否跨域过
|
||
Player *Player //客户实体
|
||
//Mu sync.RWMutex
|
||
ERROR_CONNUT int
|
||
Wsmsg *WsCodec
|
||
Conn gnet.Conn
|
||
//LF *lockfree.Lockfree[common.TomeeHeader]
|
||
//SaveL sync.Once //保存锁
|
||
|
||
//SaveDone chan struct{}
|
||
}
|
||
|
||
func NewClientData(c gnet.Conn) *ClientData {
|
||
// 创建事件处理器
|
||
// 创建消费端串行处理的Lockfree
|
||
|
||
cd := &ClientData{
|
||
|
||
Conn: c,
|
||
Wsmsg: &WsCodec{},
|
||
}
|
||
// cd.LF = lockfree.NewLockfree(
|
||
// 8,
|
||
// cd,
|
||
// lockfree.NewSleepBlockStrategy(time.Millisecond),
|
||
// )
|
||
// // 启动Lockfree
|
||
// if err := cd.LF.Start(); err != nil {
|
||
// panic(err)
|
||
// }
|
||
// // // 启动Lockfree
|
||
// // if err := cd.LF.Start(); err != nil {
|
||
// // panic(err)
|
||
// // }
|
||
return cd
|
||
|
||
}
|
||
|
||
// XORDecrypt 异或解密函数(密钥改为uint32版本)
|
||
// 核心逻辑:将uint32密钥拆分为4字节数组(大端序,适配AS3二进制处理习惯),循环与加密数据异或
|
||
// 参数:
|
||
//
|
||
// encryptedData - 待解密的字节数组
|
||
// key - 32位无符号整数密钥(替代原字符串密钥)
|
||
//
|
||
// 返回值:解密后的字节数组
|
||
|
||
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 (p *ClientData) SendPack(b []byte) error {
|
||
cli, ok := p.Conn.Context().(*ClientData)
|
||
if !ok {
|
||
return fmt.Errorf("链接错误,取消发包")
|
||
|
||
}
|
||
if cli.Wsmsg == nil {
|
||
return fmt.Errorf("ws空")
|
||
}
|
||
|
||
if cli.Wsmsg.Upgraded {
|
||
// This is the echo server
|
||
wsutil.WriteServerMessage(p.Conn, ws.OpBinary, b)
|
||
|
||
} else {
|
||
|
||
p.Conn.AsyncWrite(b, nil)
|
||
|
||
}
|
||
|
||
return nil
|
||
}
|