refactor: 将XOR解密逻辑和事件处理移至player服务
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/cool"
|
||||
"blazing/logic/service/common"
|
||||
"encoding/binary"
|
||||
"sync"
|
||||
|
||||
"context"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/lunixbochs/struc"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
"github.com/valyala/bytebufferpool"
|
||||
)
|
||||
|
||||
// getUnderlyingValue 递归解析reflect.Value,解包指针、interface{}到底层具体类型
|
||||
@@ -45,6 +47,46 @@ func getUnderlyingValue(val reflect.Value) (reflect.Value, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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() {
|
||||
@@ -64,6 +106,24 @@ func (h *ClientData) OnEvent(data common.TomeeHeader) {
|
||||
|
||||
}
|
||||
}()
|
||||
if data.CMD > 1001 {
|
||||
|
||||
if h.Player == nil {
|
||||
fmt.Println(data.UserID, "账号未注册")
|
||||
return
|
||||
}
|
||||
if h.Player.Info == nil {
|
||||
fmt.Println(data.UserID, "未创建角色")
|
||||
return
|
||||
}
|
||||
if len(data.Data) > 0 {
|
||||
data.Data = XORDecryptU(data.Data, h.Player.Hash)
|
||||
}
|
||||
|
||||
}
|
||||
if cool.Config.ServerInfo.IsDebug != 0 {
|
||||
fmt.Println("接收数据", data.UserID, data.CMD)
|
||||
}
|
||||
cmdlister, ok := cool.CmdCache[data.CMD]
|
||||
if !ok {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user