2025-09-14 01:35:16 +08:00
|
|
|
|
package player
|
2025-06-20 17:00:56 +08:00
|
|
|
|
|
2025-06-23 10:15:22 +08:00
|
|
|
|
import (
|
2025-10-27 09:36:49 +00:00
|
|
|
|
"blazing/common/socket/errorcode"
|
|
|
|
|
|
"blazing/cool"
|
2025-11-19 16:11:02 +08:00
|
|
|
|
"blazing/logic/service/common"
|
2026-02-22 01:01:37 +08:00
|
|
|
|
"encoding/binary"
|
2026-02-23 21:42:36 +08:00
|
|
|
|
"encoding/hex"
|
2025-10-30 16:54:20 +00:00
|
|
|
|
"sync"
|
2026-04-05 21:59:22 +08:00
|
|
|
|
"sync/atomic"
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
"context"
|
2025-07-15 11:24:49 +08:00
|
|
|
|
|
2025-07-14 11:07:13 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"reflect"
|
2025-06-24 22:09:05 +08:00
|
|
|
|
|
2026-03-04 14:00:55 +08:00
|
|
|
|
"github.com/bruceshao/lockfree"
|
2025-11-01 00:40:19 +08:00
|
|
|
|
"github.com/gobwas/ws"
|
|
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2025-10-27 09:36:49 +00:00
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2025-07-14 11:07:13 +08:00
|
|
|
|
"github.com/lunixbochs/struc"
|
2025-10-30 16:54:20 +00:00
|
|
|
|
"github.com/panjf2000/gnet/v2"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-11-19 16:11:02 +08:00
|
|
|
|
// 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
|
2025-07-14 11:07:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-15 21:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
// XORDecryptU 原地执行异或解密,避免额外分配和拷贝。
|
2026-02-22 01:01:37 +08:00
|
|
|
|
func XORDecryptU(encryptedData []byte, key uint32) []byte {
|
|
|
|
|
|
if len(encryptedData) == 0 {
|
|
|
|
|
|
return []byte{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var keyBytes [4]byte
|
|
|
|
|
|
binary.BigEndian.PutUint32(keyBytes[:], key)
|
|
|
|
|
|
keyLen := len(keyBytes)
|
|
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
for i, b := range encryptedData {
|
|
|
|
|
|
encryptedData[i] = b ^ keyBytes[i%keyLen]
|
|
|
|
|
|
}
|
2026-02-22 01:01:37 +08:00
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return encryptedData
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var packetDataPoolSizes = [...]int{64, 128, 256, 512, 1024, 2048, 4096}
|
|
|
|
|
|
|
|
|
|
|
|
var packetDataPools = func() []sync.Pool {
|
|
|
|
|
|
pools := make([]sync.Pool, len(packetDataPoolSizes))
|
|
|
|
|
|
for i, size := range packetDataPoolSizes {
|
|
|
|
|
|
size := size
|
|
|
|
|
|
pools[i].New = func() any {
|
|
|
|
|
|
return make([]byte, size)
|
|
|
|
|
|
}
|
2026-02-22 01:01:37 +08:00
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return pools
|
|
|
|
|
|
}()
|
2026-02-22 01:01:37 +08:00
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
func getPacketData(size int) []byte {
|
|
|
|
|
|
if size <= 0 {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
for i, poolSize := range packetDataPoolSizes {
|
|
|
|
|
|
if size <= poolSize {
|
|
|
|
|
|
return packetDataPools[i].Get().([]byte)[:size]
|
|
|
|
|
|
}
|
2026-02-22 01:01:37 +08:00
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
return make([]byte, size)
|
|
|
|
|
|
}
|
2026-02-22 01:01:37 +08:00
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
func putPacketData(buf []byte) {
|
|
|
|
|
|
if buf == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
for i, poolSize := range packetDataPoolSizes {
|
|
|
|
|
|
if cap(buf) == poolSize {
|
|
|
|
|
|
packetDataPools[i].Put(buf[:poolSize])
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h *ClientData) PushEvent(v []byte, submit func(task func()) error) {
|
2026-04-05 21:59:22 +08:00
|
|
|
|
if h == nil || h.IsClosed() {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
var header common.TomeeHeader
|
|
|
|
|
|
header.Len = binary.BigEndian.Uint32(v[0:4])
|
|
|
|
|
|
header.CMD = binary.BigEndian.Uint32(v[5:9])
|
|
|
|
|
|
header.UserID = binary.BigEndian.Uint32(v[9:13])
|
|
|
|
|
|
if dataLen := len(v) - 17; dataLen > 0 {
|
|
|
|
|
|
header.Data = getPacketData(dataLen)
|
|
|
|
|
|
copy(header.Data, v[17:])
|
|
|
|
|
|
}
|
2026-02-22 01:01:37 +08:00
|
|
|
|
|
2026-04-05 21:59:22 +08:00
|
|
|
|
err := submit(func() {
|
|
|
|
|
|
if h.IsClosed() || h.LF == nil || !h.LF.Running() {
|
|
|
|
|
|
putPacketData(header.Data)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := h.LF.Producer().Write(header); err != nil {
|
|
|
|
|
|
putPacketData(header.Data)
|
|
|
|
|
|
}
|
2026-03-31 08:19:53 +08:00
|
|
|
|
})
|
2026-04-05 21:59:22 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
putPacketData(header.Data)
|
|
|
|
|
|
}
|
2026-02-22 01:01:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 14:00:55 +08:00
|
|
|
|
// 重写
|
|
|
|
|
|
// 遍历结构体方法并执行RECV_cmd
|
|
|
|
|
|
func (h *ClientData) OnEvent(data common.TomeeHeader) {
|
2026-03-03 23:42:23 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
// 1. 打印错误信息
|
2026-03-04 14:00:55 +08:00
|
|
|
|
if h.Player != nil {
|
|
|
|
|
|
if h.Player.Info != nil {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "panic 错误:", cool.Config.ServerInfo.OnlineID, h.Player.Info.UserID, err)
|
2026-03-03 23:42:23 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "panic 错误:", cool.Config.ServerInfo.OnlineID, err)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cool.Logger.Error(context.TODO(), "panic 错误:", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2026-02-22 01:01:37 +08:00
|
|
|
|
if data.CMD > 1001 {
|
|
|
|
|
|
|
|
|
|
|
|
if h.Player == nil {
|
|
|
|
|
|
fmt.Println(data.UserID, "账号未注册")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if h.Player.Info == nil {
|
|
|
|
|
|
fmt.Println(data.UserID, "未创建角色")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-02-22 22:33:19 +08:00
|
|
|
|
if data.Data != nil {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
defer putPacketData(data.Data)
|
|
|
|
|
|
data.Res = XORDecryptU(data.Data, h.Player.Hash)
|
2026-02-22 01:01:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
} else if data.Data != nil {
|
|
|
|
|
|
defer putPacketData(data.Data)
|
|
|
|
|
|
data.Res = data.Data
|
2026-02-22 01:01:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if cool.Config.ServerInfo.IsDebug != 0 {
|
|
|
|
|
|
fmt.Println("接收数据", data.UserID, data.CMD)
|
|
|
|
|
|
}
|
2026-02-23 00:54:47 +08:00
|
|
|
|
if data.UserID == 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-01-23 14:59:15 +00:00
|
|
|
|
cmdlister, ok := cool.CmdCache[data.CMD]
|
2025-10-27 09:36:49 +00:00
|
|
|
|
if !ok {
|
|
|
|
|
|
|
2026-02-02 11:00:37 +08:00
|
|
|
|
// glog.Debug(context.Background(), data.UserID, data.CMD, "cmd未注册")
|
2025-10-27 09:36:49 +00:00
|
|
|
|
return //TODO 待实现cmd未注册
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params := []reflect.Value{}
|
|
|
|
|
|
|
|
|
|
|
|
//funct := cmdlister.Type().NumIn()
|
|
|
|
|
|
|
|
|
|
|
|
// 如果需要可设置的变量(用于修改值),创建指针并解引用
|
2025-11-30 04:57:48 +00:00
|
|
|
|
ptrValue := reflect.New(cmdlister.Req)
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
// fmt.Println(tt1)
|
2026-03-04 14:00:55 +08:00
|
|
|
|
if data.Res != nil {
|
|
|
|
|
|
tt1 := ptrValue.Elem().Addr().Interface()
|
|
|
|
|
|
err := struc.Unpack(bytes.NewBuffer(data.Res), tt1)
|
2026-02-07 23:53:07 +08:00
|
|
|
|
|
2026-03-04 14:00:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
|
|
|
|
cool.Logger.Error(context.Background(), data.UserID, data.CMD, "解包失败,", err, hex.EncodeToString(data.Res))
|
|
|
|
|
|
//fmt.Println(data.UserID, data.CMD, "解包失败,", hex.EncodeToString(data.Data))
|
|
|
|
|
|
data.Result = uint32(errorcode.ErrorCodes.ErrSystemProcessingError)
|
|
|
|
|
|
h.SendPack(data.Pack(nil))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-27 09:36:49 +00:00
|
|
|
|
}
|
2026-02-08 03:49:11 +08:00
|
|
|
|
|
2025-10-27 09:36:49 +00:00
|
|
|
|
ptrValue1 := ptrValue.Elem().Addr()
|
|
|
|
|
|
// 设置 Name 字段
|
|
|
|
|
|
nameField := ptrValue.Elem().Field(0) //首个为header
|
2025-11-30 04:57:48 +00:00
|
|
|
|
nameField.Set(reflect.ValueOf(data))
|
2025-10-29 02:05:34 +08:00
|
|
|
|
|
2025-10-31 00:53:22 +08:00
|
|
|
|
if data.CMD > 1001 { //if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
|
2025-10-30 01:37:53 +00:00
|
|
|
|
//t := GetPlayer(c, data.UserID)
|
2025-10-29 02:05:34 +08:00
|
|
|
|
|
2025-10-27 09:36:49 +00:00
|
|
|
|
// fmt.Println(data.CMD, "接收 变量的地址 ", &t.Info, t.Info.UserID)
|
|
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
params = append(params, ptrValue1, reflect.ValueOf(h.Player))
|
2025-10-27 09:36:49 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
|
2025-10-30 01:37:53 +00:00
|
|
|
|
params = append(params, ptrValue1, reflect.ValueOf(h.Conn))
|
2025-10-27 09:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-30 04:57:48 +00:00
|
|
|
|
ret := cmdlister.Func.Call(params)
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
if len(ret) <= 0 { //如果判断没有参数,那就说明这个包没有返回参数
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
aa, ok := ret[1].Interface().(errorcode.ErrorCode) //判断错误
|
|
|
|
|
|
data.Result = uint32(aa)
|
|
|
|
|
|
if aa == -1 {
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-30 01:37:53 +00:00
|
|
|
|
|
2025-10-27 09:36:49 +00:00
|
|
|
|
if ok && aa != 0 { //这里实现回复错误包
|
|
|
|
|
|
|
2025-12-09 00:09:51 +08:00
|
|
|
|
glog.Info(context.Background(), data.UserID, data.CMD, aa.Code())
|
2026-03-04 14:00:55 +08:00
|
|
|
|
h.SendPack(data.Pack(nil))
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-20 15:19:13 +08:00
|
|
|
|
t1 := data.Pack(ret[0].Interface())
|
2026-03-04 01:30:40 +08:00
|
|
|
|
|
2026-03-04 14:00:55 +08:00
|
|
|
|
h.SendPack(t1)
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
type ClientData struct {
|
2025-11-01 14:31:19 +08:00
|
|
|
|
IsCrossDomain sync.Once //是否跨域过
|
|
|
|
|
|
Player *Player //客户实体
|
2026-03-31 08:19:53 +08:00
|
|
|
|
ERROR_CONNUT int
|
|
|
|
|
|
Wsmsg *WsCodec
|
|
|
|
|
|
Conn gnet.Conn
|
|
|
|
|
|
LF *lockfree.Lockfree[common.TomeeHeader]
|
2026-04-05 21:59:22 +08:00
|
|
|
|
closed int32
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientData) IsClosed() bool {
|
|
|
|
|
|
return atomic.LoadInt32(&p.closed) == 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (p *ClientData) Close() {
|
|
|
|
|
|
if !atomic.CompareAndSwapInt32(&p.closed, 0, 1) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if p.LF != nil && p.LF.Running() {
|
|
|
|
|
|
_ = p.LF.Close()
|
|
|
|
|
|
}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-16 22:30:12 +08:00
|
|
|
|
func (p *ClientData) GetPlayer(userid uint32) *Player { //TODO 这里待优化,可能存在内存泄漏问题
|
2026-03-19 14:50:11 +08:00
|
|
|
|
if p.Player == nil {
|
|
|
|
|
|
p.Player = NewPlayer(p.Conn)
|
|
|
|
|
|
}
|
2026-03-16 22:30:12 +08:00
|
|
|
|
_, ok := Mainplayer.LoadOrStore(userid, p)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
p.Player = NewPlayer(p.Conn)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return p.Player
|
|
|
|
|
|
// return nil
|
|
|
|
|
|
}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
func NewClientData(c gnet.Conn) *ClientData {
|
2025-11-05 22:17:03 +00:00
|
|
|
|
cd := &ClientData{
|
2026-03-04 14:00:55 +08:00
|
|
|
|
Conn: c,
|
|
|
|
|
|
Wsmsg: &WsCodec{},
|
2026-03-03 23:40:21 +08:00
|
|
|
|
}
|
2026-03-04 14:00:55 +08:00
|
|
|
|
cd.LF = lockfree.NewLockfree(
|
|
|
|
|
|
8,
|
|
|
|
|
|
cd,
|
2026-03-04 14:45:52 +08:00
|
|
|
|
lockfree.NewConditionBlockStrategy(),
|
2026-03-04 14:00:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
if err := cd.LF.Start(); err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
2025-11-05 22:17:03 +00:00
|
|
|
|
return cd
|
2025-10-30 16:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-08 02:11:46 +08:00
|
|
|
|
// XORDecrypt 异或解密函数(密钥改为uint32版本)
|
|
|
|
|
|
// 核心逻辑:将uint32密钥拆分为4字节数组(大端序,适配AS3二进制处理习惯),循环与加密数据异或
|
|
|
|
|
|
// 参数:
|
|
|
|
|
|
//
|
|
|
|
|
|
// encryptedData - 待解密的字节数组
|
|
|
|
|
|
// key - 32位无符号整数密钥(替代原字符串密钥)
|
|
|
|
|
|
//
|
|
|
|
|
|
// 返回值:解密后的字节数组
|
|
|
|
|
|
|
2025-11-03 19:14:34 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2026-02-10 10:49:01 +08:00
|
|
|
|
|
2025-11-01 00:40:19 +08:00
|
|
|
|
func (p *ClientData) SendPack(b []byte) error {
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if p.Wsmsg == nil {
|
2026-02-07 14:59:22 +08:00
|
|
|
|
return fmt.Errorf("ws空")
|
|
|
|
|
|
}
|
2025-11-01 00:40:19 +08:00
|
|
|
|
|
2026-03-31 08:19:53 +08:00
|
|
|
|
if p.Wsmsg.Upgraded {
|
2025-11-01 00:40:19 +08:00
|
|
|
|
// This is the echo server
|
2026-02-07 18:21:52 +08:00
|
|
|
|
wsutil.WriteServerMessage(p.Conn, ws.OpBinary, b)
|
|
|
|
|
|
|
2025-11-01 00:40:19 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
|
2026-02-10 00:30:23 +08:00
|
|
|
|
p.Conn.AsyncWrite(b, nil)
|
2025-11-01 00:40:19 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|