From f514a4fde17a22b2e2b9920ce97f0f03de0aa2e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=94=E5=BF=B5?= <12574910+72wo@users.noreply.github.com> Date: Tue, 10 Feb 2026 12:44:34 +0800 Subject: [PATCH] 1 --- common/socket/ServerEvent.go | 64 +++++++++++++++++++++++++++++-- logic/service/player/pack.go | 74 ++++-------------------------------- 2 files changed, 68 insertions(+), 70 deletions(-) diff --git a/common/socket/ServerEvent.go b/common/socket/ServerEvent.go index aac33ca38..f4868e011 100644 --- a/common/socket/ServerEvent.go +++ b/common/socket/ServerEvent.go @@ -12,6 +12,7 @@ import ( "time" "blazing/cool" + "blazing/logic/service/common" "blazing/logic/service/player" "blazing/modules/config/service" @@ -74,8 +75,8 @@ func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) { //logging.Infof("conn[%v] disconnected", c.RemoteAddr().String()) v, _ := c.Context().(*player.ClientData) - v.LF.Close() + v.LF.Close() if v.Player != nil { v.Player.Save() //保存玩家数据 @@ -248,15 +249,70 @@ func handle(c gnet.Conn) { //return } -func (s *Server) onevent(c gnet.Conn, data []byte) { +func (s *Server) onevent(c gnet.Conn, v []byte) { if t, ok := c.Context().(*player.ClientData); ok { + var header common.TomeeHeader + // 解析Len(0-3字节) + header.Len = binary.BigEndian.Uint32(v[0:4]) + // 解析Version(第4字节) + //header.Version = v[4] + // 解析CMD(5-8字节) + header.CMD = binary.BigEndian.Uint32(v[5:9]) + // 解析UserID(9-12字节) + header.UserID = binary.BigEndian.Uint32(v[9:13]) + // 解析Result(13-16字节) + //header.Result = binary.BigEndian.Uint32(v[13:17]) + // 解析数据部分(17字节之后) + if len(v) > 17 { + header.Data = v[17:] - if t.LF.Running() { - t.LF.Producer().Write(data) + } else { + header.Data = []byte{} // 数据部分为空时显式初始化 } + 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.OnEvent(data) // }) } } +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 +} diff --git a/logic/service/player/pack.go b/logic/service/player/pack.go index 633732245..53f04bc68 100644 --- a/logic/service/player/pack.go +++ b/logic/service/player/pack.go @@ -4,7 +4,6 @@ import ( "blazing/common/socket/errorcode" "blazing/cool" "blazing/logic/service/common" - "encoding/binary" "sync" "context" @@ -137,7 +136,7 @@ type ClientData struct { ERROR_CONNUT int Wsmsg *WsCodec Conn gnet.Conn - LF *lockfree.Lockfree[[]byte] + LF *lockfree.Lockfree[common.TomeeHeader] //SaveL sync.Once //保存锁 //SaveDone chan struct{} @@ -152,7 +151,7 @@ func NewClientData(c gnet.Conn) *ClientData { Conn: c, Wsmsg: &WsCodec{}, } - cd.LF = lockfree.NewLockfree[[]byte]( + cd.LF = lockfree.NewLockfree[common.TomeeHeader]( 8, cd, lockfree.NewConditionBlockStrategy(), @@ -161,6 +160,10 @@ func NewClientData(c gnet.Conn) *ClientData { if err := cd.LF.Start(); err != nil { panic(err) } + // // 启动Lockfree + // if err := cd.LF.Start(); err != nil { + // panic(err) + // } return cd } @@ -173,28 +176,7 @@ func NewClientData(c gnet.Conn) *ClientData { // 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{} @@ -217,7 +199,7 @@ func XORDecrypt(encryptedData []byte, keyStr string) []byte { return decrypted } -func (h *ClientData) OnEvent(v []byte) { +func (h *ClientData) OnEvent(t common.TomeeHeader) { defer func() { if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值 // 1. 打印错误信息 @@ -236,47 +218,7 @@ func (h *ClientData) OnEvent(v []byte) { } }() - t, ok := h.Conn.Context().(*ClientData) - if !ok { - return - } - var header common.TomeeHeader - // 解析Len(0-3字节) - header.Len = binary.BigEndian.Uint32(v[0:4]) - // 解析Version(第4字节) - //header.Version = v[4] - // 解析CMD(5-8字节) - header.CMD = binary.BigEndian.Uint32(v[5:9]) - // 解析UserID(9-12字节) - header.UserID = binary.BigEndian.Uint32(v[9:13]) - // 解析Result(13-16字节) - //header.Result = binary.BigEndian.Uint32(v[13:17]) - // 解析数据部分(17字节之后) - if len(v) > 17 { - header.Data = v[17:] - - } else { - header.Data = []byte{} // 数据部分为空时显式初始化 - } - 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) - } - - h.Recv(header) + h.Recv(t) } func (p *ClientData) SendPack(b []byte) error {