From 4e313f02c74beb04b228275ea36b42209887c44e Mon Sep 17 00:00:00 2001 From: xinian Date: Sun, 22 Feb 2026 01:01:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86XOR=E8=A7=A3=E5=AF=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=92=8C=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=A7=BB=E8=87=B3player=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/socket/ServerEvent.go | 63 ++-------------------------------- logic/controller/fight_擂台.go | 4 +-- logic/service/player/pack.go | 60 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 62 deletions(-) diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index a7345fada..f947d1761 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -4,7 +4,6 @@ import ( "context" "encoding/binary" "errors" - "fmt" "io" "log" "os" @@ -19,7 +18,6 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" "github.com/panjf2000/gnet/v2" - "github.com/valyala/bytebufferpool" ) func (s *Server) Boot(serverid, port uint16) error { @@ -282,65 +280,10 @@ func (s *Server) onevent(c gnet.Conn, v []byte) { header.Data = v[17:] } - if header.CMD > 1001 { - if t.Player == nil { - fmt.Println(header.UserID, "账号未注册") - return - } - if t.Player.Info == nil { - fmt.Println(header.UserID, "未创建角色") - return - } - 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) - } - - t.LF.Producer().Write(header) + s.workerPool.Submit(func() { + t.LF.Producer().Write(header) + }) } } - -// 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 -} diff --git a/logic/controller/fight_擂台.go b/logic/controller/fight_擂台.go index d8979b890..9f90c8660 100644 --- a/logic/controller/fight_擂台.go +++ b/logic/controller/fight_擂台.go @@ -85,7 +85,7 @@ func (h Controller) ArenaFightOwner(data1 *fight.ARENA_FIGHT_OWENR, c *player.Pl c.Info.EVPool += addev c.SendPackCmd(8004, &info.S2C_GET_BOSS_MONSTER{ //发送EV - ItemList: []data.ItemInfo{data.ItemInfo{ + ItemList: []data.ItemInfo{{ ItemId: 9, ItemCnt: int64(addev), }}, @@ -101,7 +101,7 @@ func (h Controller) ArenaFightOwner(data1 *fight.ARENA_FIGHT_OWENR, c *player.Pl c.GetSpace().Owner.ARENA_Player.GetInfo().EVPool += addev c.GetSpace().Owner.ARENA_Player.SendPackCmd(8004, &info.S2C_GET_BOSS_MONSTER{ //发送EV - ItemList: []data.ItemInfo{data.ItemInfo{ + ItemList: []data.ItemInfo{{ ItemId: 9, ItemCnt: int64(addev), }}, diff --git a/logic/service/player/pack.go b/logic/service/player/pack.go index 5ccc5d76a..a2b997172 100644 --- a/logic/service/player/pack.go +++ b/logic/service/player/pack.go @@ -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 {