1
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

This commit is contained in:
昔念
2026-02-10 12:44:34 +08:00
parent d17cd28c94
commit f514a4fde1
2 changed files with 68 additions and 70 deletions

View File

@@ -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
// 解析Len0-3字节
header.Len = binary.BigEndian.Uint32(v[0:4])
// 解析Version第4字节
//header.Version = v[4]
// 解析CMD5-8字节
header.CMD = binary.BigEndian.Uint32(v[5:9])
// 解析UserID9-12字节
header.UserID = binary.BigEndian.Uint32(v[9:13])
// 解析Result13-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
}

View File

@@ -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 { // 恢复 panicerr 为 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
// 解析Len0-3字节
header.Len = binary.BigEndian.Uint32(v[0:4])
// 解析Version第4字节
//header.Version = v[4]
// 解析CMD5-8字节
header.CMD = binary.BigEndian.Uint32(v[5:9])
// 解析UserID9-12字节
header.UserID = binary.BigEndian.Uint32(v[9:13])
// 解析Result13-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 {