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"
|
2025-07-25 01:29:03 +00:00
|
|
|
|
"blazing/common/utils/bytearray"
|
2025-10-27 09:36:49 +00:00
|
|
|
|
"blazing/cool"
|
2025-11-15 22:17:43 +00:00
|
|
|
|
"encoding/binary"
|
2025-10-30 16:54:20 +00:00
|
|
|
|
"sync"
|
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
|
|
|
|
|
2025-11-05 22:07:46 +00: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-11-01 00:40:19 +08:00
|
|
|
|
"github.com/panjf2000/gnet/v2/pkg/logging"
|
2025-06-23 10:15:22 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-06-20 17:00:56 +08:00
|
|
|
|
// TomeeHeader 结构体字段定义
|
|
|
|
|
|
type TomeeHeader struct {
|
2025-07-15 11:24:49 +08:00
|
|
|
|
Len uint32 `json:"len"`
|
2025-10-28 02:28:15 +08:00
|
|
|
|
Version byte `json:"version" struc:"[1]byte"`
|
2025-07-15 11:24:49 +08:00
|
|
|
|
CMD uint32 `json:"cmdId" struc:"uint32"`
|
|
|
|
|
|
UserID uint32 `json:"userId"`
|
2025-06-24 22:09:05 +08:00
|
|
|
|
//Error uint32 `json:"error" struc:"[0]pad"`
|
|
|
|
|
|
|
|
|
|
|
|
Result uint32 `json:"result"`
|
2025-07-14 11:10:16 +08:00
|
|
|
|
Data []byte `json:"data" struc:"skip"` //组包忽略此字段// struc:"[0]pad"
|
2025-07-25 07:46:31 +00:00
|
|
|
|
//Return []byte `struc:"[0]pad"` //返回记录
|
2025-06-20 17:00:56 +08:00
|
|
|
|
}
|
2025-07-06 19:31:30 +08:00
|
|
|
|
|
2025-08-24 17:33:19 +08:00
|
|
|
|
func NewTomeeHeader(cmd uint32, userid uint32) *TomeeHeader {
|
2025-07-06 19:31:30 +08:00
|
|
|
|
|
|
|
|
|
|
return &TomeeHeader{
|
2025-08-24 17:33:19 +08:00
|
|
|
|
CMD: cmd,
|
2025-07-06 19:31:30 +08:00
|
|
|
|
// Len: 0,
|
2025-10-28 02:28:15 +08:00
|
|
|
|
Version: 49,
|
2025-08-24 17:33:19 +08:00
|
|
|
|
Result: 0,
|
2025-07-06 19:31:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-14 11:08:30 +08:00
|
|
|
|
// Pack 将给定的数据打包成一个字节切片。
|
|
|
|
|
|
// 该方法处理的数据类型包括指针、切片和结构体。
|
|
|
|
|
|
// 对于指针类型,会解引用以获取实际值。
|
|
|
|
|
|
// 切片类型直接转换为字节切片。
|
|
|
|
|
|
// 结构体类型使用struc库进行序列化。
|
|
|
|
|
|
// 最后,将数据长度、版本号、命令码、用户ID和结果代码一并打包进返回的字节切片中。
|
2025-07-14 11:07:13 +08:00
|
|
|
|
func (h *TomeeHeader) Pack(data any) []byte { //组包
|
2025-08-17 21:55:15 +08:00
|
|
|
|
//h.Result = 0//默认置0
|
2025-07-14 11:07:13 +08:00
|
|
|
|
|
|
|
|
|
|
//t := reflect.TypeOf(data)
|
|
|
|
|
|
tv := reflect.ValueOf(data)
|
|
|
|
|
|
var datar []byte
|
|
|
|
|
|
|
|
|
|
|
|
// 处理指针类型
|
|
|
|
|
|
if tv.Kind() == reflect.Ptr {
|
|
|
|
|
|
//tv = t.Elem() // 获取指针指向的类型
|
|
|
|
|
|
tv = tv.Elem() // 获取指针指向的值
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-10 00:41:09 +08:00
|
|
|
|
switch tv.Kind() {
|
|
|
|
|
|
case reflect.String:
|
|
|
|
|
|
datar = []byte(tv.String())
|
2025-07-14 11:07:13 +08:00
|
|
|
|
case reflect.Slice:
|
|
|
|
|
|
datar = data.([]byte)
|
|
|
|
|
|
//p.Conn.Write(p.pack(cmd, data.([]byte))) //写入数据
|
|
|
|
|
|
|
|
|
|
|
|
case reflect.Struct:
|
|
|
|
|
|
var data1 bytes.Buffer
|
|
|
|
|
|
err := struc.Pack(&data1, data)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
datar = data1.Bytes()
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
datar = []byte{}
|
|
|
|
|
|
// fmt.Println(err, datar)
|
|
|
|
|
|
// p.Conn.Write(p.pack(cmd, data))
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
h.Len = uint32(len(datar) + 17)
|
|
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
by := bytearray.CreateByteArray(nil)
|
2025-07-14 11:07:13 +08:00
|
|
|
|
by.WriteUInt32(h.Len)
|
2025-10-28 02:28:15 +08:00
|
|
|
|
by.WriteByte(h.Version)
|
2025-07-14 11:07:13 +08:00
|
|
|
|
by.WriteUInt32(uint32(h.CMD))
|
|
|
|
|
|
by.WriteUInt32(h.UserID)
|
|
|
|
|
|
by.WriteUInt32(h.Result)
|
|
|
|
|
|
by.Write(datar)
|
|
|
|
|
|
|
|
|
|
|
|
return by.Bytes()
|
|
|
|
|
|
|
2025-07-15 21:11:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-27 09:36:49 +00:00
|
|
|
|
// 遍历结构体方法并执行RECV_cmd
|
2025-10-30 01:37:53 +00:00
|
|
|
|
func (h *ClientData) Recv(data TomeeHeader) {
|
2025-10-27 09:36:49 +00:00
|
|
|
|
|
|
|
|
|
|
cmdlister, ok := cool.CmdCache.Load(data.CMD)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
|
|
|
|
glog.Debug(context.Background(), data.CMD, "cmd未注册")
|
|
|
|
|
|
return //TODO 待实现cmd未注册
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params := []reflect.Value{}
|
|
|
|
|
|
|
|
|
|
|
|
//funct := cmdlister.Type().NumIn()
|
|
|
|
|
|
|
|
|
|
|
|
// 如果需要可设置的变量(用于修改值),创建指针并解引用
|
|
|
|
|
|
ptrValue := reflect.New(cmdlister.Type().In(0).Elem())
|
|
|
|
|
|
|
|
|
|
|
|
tt1 := ptrValue.Elem().Addr().Interface()
|
|
|
|
|
|
// fmt.Println(tt1)
|
|
|
|
|
|
err := struc.Unpack(bytes.NewBuffer(data.Data), tt1)
|
|
|
|
|
|
if err != nil {
|
2025-11-01 14:31:19 +08:00
|
|
|
|
cool.Loger.Error(context.Background(), data.UserID, data.CMD, "解包失败")
|
|
|
|
|
|
return
|
2025-10-27 09:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
//fmt.Println(tt1)
|
|
|
|
|
|
ptrValue1 := ptrValue.Elem().Addr()
|
|
|
|
|
|
// 设置 Name 字段
|
|
|
|
|
|
nameField := ptrValue.Elem().Field(0) //首个为header
|
|
|
|
|
|
if nameField.IsValid() && nameField.CanSet() {
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
2025-10-30 01:37:53 +00:00
|
|
|
|
params = append(params, ptrValue1, reflect.ValueOf(h.Conn.Context().(*ClientData).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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ret := cmdlister.Call(params)
|
|
|
|
|
|
|
|
|
|
|
|
if len(ret) <= 0 { //如果判断没有参数,那就说明这个包没有返回参数
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
aa, ok := ret[1].Interface().(errorcode.ErrorCode) //判断错误
|
|
|
|
|
|
data.Result = uint32(aa)
|
|
|
|
|
|
if aa == -1 {
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-01 00:40:19 +08:00
|
|
|
|
t := h.Conn.Context().(*ClientData)
|
2025-10-30 01:37:53 +00:00
|
|
|
|
|
2025-10-27 09:36:49 +00:00
|
|
|
|
if ok && aa != 0 { //这里实现回复错误包
|
|
|
|
|
|
|
|
|
|
|
|
cool.Loger.Error(context.Background(), aa.Code())
|
|
|
|
|
|
t.SendPack(data.Pack(nil))
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 01:37:53 +00:00
|
|
|
|
//data.Version = 49
|
2025-10-27 09:36:49 +00:00
|
|
|
|
t.SendPack(data.Pack(ret[0].Interface()))
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
type ClientData struct {
|
2025-11-01 14:31:19 +08:00
|
|
|
|
IsCrossDomain sync.Once //是否跨域过
|
|
|
|
|
|
Player *Player //客户实体
|
|
|
|
|
|
//Mu sync.RWMutex
|
|
|
|
|
|
ERROR_CONNUT int
|
|
|
|
|
|
Wsmsg *WsCodec
|
|
|
|
|
|
Conn gnet.Conn
|
|
|
|
|
|
SaveL sync.Once //保存锁
|
2025-11-05 22:07:46 +00:00
|
|
|
|
LF *lockfree.Lockfree[[]byte]
|
|
|
|
|
|
//SaveDone chan struct{}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewClientData(c gnet.Conn) *ClientData {
|
|
|
|
|
|
// 创建事件处理器
|
2025-11-05 22:07:46 +00:00
|
|
|
|
// 创建消费端串行处理的Lockfree
|
2025-10-30 16:54:20 +00:00
|
|
|
|
|
2025-11-05 22:17:03 +00:00
|
|
|
|
cd := &ClientData{
|
2025-11-01 14:31:19 +08:00
|
|
|
|
|
2025-11-05 22:34:03 +00:00
|
|
|
|
Conn: c,
|
|
|
|
|
|
Wsmsg: &WsCodec{},
|
2025-10-30 16:54:20 +00:00
|
|
|
|
}
|
2025-11-05 22:17:03 +00:00
|
|
|
|
cd.LF = lockfree.NewLockfree[[]byte](
|
2025-11-15 13:20:42 +08:00
|
|
|
|
8,
|
2025-11-05 22:17:03 +00:00
|
|
|
|
cd,
|
2025-11-15 13:20:42 +08:00
|
|
|
|
lockfree.NewConditionBlockStrategy(),
|
2025-11-05 22:17:03 +00:00
|
|
|
|
)
|
|
|
|
|
|
// 启动Lockfree
|
|
|
|
|
|
if err := cd.LF.Start(); err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return cd
|
2025-10-30 16:54:20 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
func (h *ClientData) OnEvent(v []byte) {
|
2025-11-05 22:18:29 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
|
|
|
|
|
// 1. 打印错误信息
|
2025-10-30 16:54:20 +00:00
|
|
|
|
|
2025-11-05 22:18:29 +00:00
|
|
|
|
cool.Loger.Error(context.TODO(), "panic 错误:", err)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2025-11-15 22:17:43 +00:00
|
|
|
|
var header 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 = XORDecrypt(v[17:], "CWF")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
header.Data = []byte{} // 数据部分为空时显式初始化
|
|
|
|
|
|
}
|
2025-10-30 16:56:07 +00:00
|
|
|
|
if header.CMD > 1001 {
|
2025-10-30 16:54:20 +00:00
|
|
|
|
if h.Conn.Context().(*ClientData).Player == nil {
|
2025-10-30 16:56:07 +00:00
|
|
|
|
cool.Loger.Error(context.TODO(), header.UserID, "账号未注册")
|
2025-10-30 16:54:20 +00:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if h.Conn.Context().(*ClientData).Player.Info == nil {
|
2025-10-30 16:56:07 +00:00
|
|
|
|
cool.Loger.Error(context.TODO(), header.UserID, "未创建角色")
|
2025-10-30 16:54:20 +00:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-30 16:56:07 +00:00
|
|
|
|
cool.Loger.Debug(context.TODO(), "接收数据", header.UserID, header.CMD)
|
2025-10-30 16:54:20 +00:00
|
|
|
|
h.Recv(header)
|
2025-10-30 16:56:07 +00:00
|
|
|
|
|
2025-10-30 16:54:20 +00:00
|
|
|
|
}
|
2025-11-01 00:40:19 +08:00
|
|
|
|
func (p *ClientData) SendPack(b []byte) error {
|
2025-11-01 14:31:19 +08:00
|
|
|
|
if _, ok := p.Conn.Context().(*ClientData); !ok {
|
|
|
|
|
|
return fmt.Errorf("链接错误,取消发包")
|
2025-11-01 00:40:19 +08:00
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
}
|
2025-11-01 00:40:19 +08:00
|
|
|
|
|
|
|
|
|
|
if p.Conn.Context().(*ClientData).Wsmsg.Upgraded {
|
|
|
|
|
|
// This is the echo server
|
|
|
|
|
|
err := wsutil.WriteServerMessage(p.Conn, ws.OpBinary, b)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logging.Infof("conn[%v] [err=%v]", p.Conn.RemoteAddr().String(), err.Error())
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
2025-11-01 14:31:19 +08:00
|
|
|
|
err := p.Conn.AsyncWrite(b, nil)
|
2025-11-01 00:40:19 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
glog.Debug(context.Background(), err)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|