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-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-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-07-16 11:30:37 +08:00
|
|
|
|
// var _ Blazingservice = (*TomeeHeader)(nil)
|
|
|
|
|
|
|
|
|
|
|
|
// type Blazingservice interface {
|
|
|
|
|
|
// Ret() []byte
|
|
|
|
|
|
// }
|
2025-07-15 11:24:49 +08:00
|
|
|
|
|
|
|
|
|
|
// MergeBytes 将多个字节数组合并为一个
|
|
|
|
|
|
func MergeBytes(arrays ...[]byte) []byte {
|
|
|
|
|
|
// 计算所有数组的总长度
|
|
|
|
|
|
totalLen := 0
|
|
|
|
|
|
for _, arr := range arrays {
|
|
|
|
|
|
totalLen += len(arr)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 创建结果切片
|
|
|
|
|
|
result := make([]byte, totalLen)
|
|
|
|
|
|
|
|
|
|
|
|
// 逐个复制数组内容
|
|
|
|
|
|
currentIndex := 0
|
|
|
|
|
|
for _, arr := range arrays {
|
|
|
|
|
|
copy(result[currentIndex:], arr)
|
|
|
|
|
|
currentIndex += len(arr)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
}
|
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-03 03:59:59 +08:00
|
|
|
|
SaveDone chan struct{}
|
2025-10-30 16:54:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewClientData(c gnet.Conn) *ClientData {
|
|
|
|
|
|
// 创建事件处理器
|
|
|
|
|
|
|
|
|
|
|
|
cd := ClientData{
|
2025-11-01 14:31:19 +08:00
|
|
|
|
|
|
|
|
|
|
Player: nil,
|
|
|
|
|
|
Conn: c,
|
|
|
|
|
|
Wsmsg: &WsCodec{},
|
2025-10-30 16:54:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
return &cd
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (h *ClientData) OnEvent(v []byte) {
|
|
|
|
|
|
|
|
|
|
|
|
header := TomeeHeader{}
|
|
|
|
|
|
|
|
|
|
|
|
tempdata := bytearray.CreateByteArray(v)
|
|
|
|
|
|
header.Len, _ = tempdata.ReadUInt32()
|
|
|
|
|
|
header.Version, _ = tempdata.ReadByte()
|
|
|
|
|
|
header.CMD, _ = tempdata.ReadUInt32()
|
|
|
|
|
|
//header.CMD = cmd.EnumCommandID(_CMD)
|
|
|
|
|
|
header.UserID, _ = tempdata.ReadUInt32()
|
|
|
|
|
|
|
|
|
|
|
|
header.Result, _ = tempdata.ReadUInt32()
|
|
|
|
|
|
header.Data = tempdata.BytesAvailable()
|
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
|
|
|
|
|
|
}
|