``` refactor(socket): 重构客户端数据处理逻辑,优化玩家信息校验和包处理流程
This commit is contained in:
@@ -53,8 +53,18 @@ func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *player.Play
|
||||
result1 := maps.NewOutInfo()
|
||||
copier.CopyWithOption(result1, player.GetInfo(), copier.Option{DeepCopy: true})
|
||||
result.Player = append(result.Player, *result1)
|
||||
result.Player=LastFourElements(result.Player)
|
||||
})
|
||||
|
||||
c.Canmon = true //可以刷怪
|
||||
return
|
||||
}
|
||||
func LastFourElements[T any](s []T) []T {
|
||||
n := len(s)
|
||||
if n <= 30 {
|
||||
// 切片长度小于等于4时,返回整个切片
|
||||
return s
|
||||
}
|
||||
// 切片长度大于4时,返回最后4个元素(从n-4索引到末尾)
|
||||
return s[n-4:]
|
||||
}
|
||||
@@ -10,7 +10,7 @@ func (h Controller) Walk(data *maps.WalkInInfo, c *player.Player) (result *maps.
|
||||
result = &maps.WalkOutInfo{
|
||||
Flag: data.Flag,
|
||||
Point: data.Point,
|
||||
Reserve2: data.Reverse2,
|
||||
Path: data.Path,
|
||||
}
|
||||
|
||||
result.UserID = data.Head.UserID
|
||||
|
||||
@@ -16,8 +16,8 @@ type WalkInInfo struct {
|
||||
// Point: 直接给坐标x,y
|
||||
Point model.Pos `fieldDesc:"直接给坐标x,y"`
|
||||
|
||||
// Reverse2: 暂定 占位字符2
|
||||
Reverse2 string `struc:"[2]byte"`
|
||||
PathLen uint32 `struc:"sizeof=Path" `
|
||||
Path string `struc:"[2]byte"`
|
||||
}
|
||||
|
||||
func (t *WalkInInfo) Broadcast(mapid uint32, o WalkOutInfo) {
|
||||
@@ -41,6 +41,6 @@ type WalkOutInfo struct {
|
||||
// Point: 直接给坐标x,y
|
||||
Point model.Pos `fieldDesc:"直接给坐标x,y"`
|
||||
|
||||
// Reserve2: 这个字段同C2S_People_Walk中的reserve2
|
||||
Reserve2 string `struc:"[2]byte"`
|
||||
PathLen uint32 `struc:"sizeof=Path" `
|
||||
Path string `struc:"[2]byte"`
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/lunixbochs/struc"
|
||||
"github.com/panjf2000/gnet/v2"
|
||||
)
|
||||
|
||||
// TomeeHeader 结构体字段定义
|
||||
@@ -123,7 +122,7 @@ func MergeBytes(arrays ...[]byte) []byte {
|
||||
}
|
||||
|
||||
// 遍历结构体方法并执行RECV_cmd
|
||||
func Recv(c gnet.Conn, data TomeeHeader) {
|
||||
func (h *ClientData) Recv(data TomeeHeader) {
|
||||
|
||||
cmdlister, ok := cool.CmdCache.Load(data.CMD)
|
||||
if !ok {
|
||||
@@ -132,8 +131,6 @@ func Recv(c gnet.Conn, data TomeeHeader) {
|
||||
return //TODO 待实现cmd未注册
|
||||
}
|
||||
|
||||
// fmt.Println(cmdlister)
|
||||
//glog.Debug(context.Background(), "接收数据", data.UserID, data.CMD)
|
||||
params := []reflect.Value{}
|
||||
|
||||
//funct := cmdlister.Type().NumIn()
|
||||
@@ -154,20 +151,16 @@ func Recv(c gnet.Conn, data TomeeHeader) {
|
||||
if nameField.IsValid() && nameField.CanSet() {
|
||||
nameField.Set(reflect.ValueOf(data))
|
||||
}
|
||||
clientdata := c.Context().(*ClientData)
|
||||
if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
|
||||
|
||||
if data.CMD > 1000 { //if cmdlister.Type().In(1) == reflect.TypeOf(&Player{}) {
|
||||
//t := GetPlayer(c, data.UserID)
|
||||
|
||||
if clientdata.Player == nil {
|
||||
|
||||
return
|
||||
}
|
||||
// fmt.Println(data.CMD, "接收 变量的地址 ", &t.Info, t.Info.UserID)
|
||||
|
||||
params = append(params, ptrValue1, reflect.ValueOf(clientdata.Player))
|
||||
params = append(params, ptrValue1, reflect.ValueOf(h.Conn.Context().(*ClientData).Player))
|
||||
} else {
|
||||
|
||||
params = append(params, ptrValue1, reflect.ValueOf(c))
|
||||
params = append(params, ptrValue1, reflect.ValueOf(h.Conn))
|
||||
}
|
||||
|
||||
ret := cmdlister.Call(params)
|
||||
@@ -182,10 +175,8 @@ func Recv(c gnet.Conn, data TomeeHeader) {
|
||||
return
|
||||
|
||||
}
|
||||
t := clientdata.Player
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
t := h.Conn.Context().(*ClientData).Player
|
||||
|
||||
if ok && aa != 0 { //这里实现回复错误包
|
||||
|
||||
cool.Loger.Error(context.Background(), aa.Code())
|
||||
@@ -195,7 +186,7 @@ func Recv(c gnet.Conn, data TomeeHeader) {
|
||||
|
||||
}
|
||||
|
||||
data.Version = 49
|
||||
//data.Version = 49
|
||||
t.SendPack(data.Pack(ret[0].Interface()))
|
||||
|
||||
}
|
||||
|
||||
@@ -5,12 +5,9 @@ import (
|
||||
"blazing/common/data/xmlres"
|
||||
"blazing/common/socket/errorcode"
|
||||
"blazing/common/utils"
|
||||
"blazing/common/utils/bytearray"
|
||||
"blazing/cool"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"blazing/logic/service/common"
|
||||
@@ -21,7 +18,6 @@ import (
|
||||
"blazing/modules/blazing/model"
|
||||
blservice "blazing/modules/blazing/service"
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/antlabs/timer"
|
||||
@@ -44,58 +40,6 @@ func ConutPlayer() int {
|
||||
return count
|
||||
}
|
||||
|
||||
type RecvData struct {
|
||||
gnet.Conn
|
||||
Data []byte
|
||||
}
|
||||
type ClientData struct {
|
||||
IsCrossDomain bool //是否跨域过
|
||||
Player *Player //客户实体
|
||||
Mu sync.Mutex
|
||||
ERROR_CONNUT int
|
||||
Wsmsg *WsCodec
|
||||
Conn gnet.Conn
|
||||
|
||||
CloseChan chan struct{}
|
||||
}
|
||||
|
||||
func NewClientData(c gnet.Conn) *ClientData {
|
||||
// 创建事件处理器
|
||||
|
||||
cd := ClientData{
|
||||
IsCrossDomain: false,
|
||||
Player: nil,
|
||||
Conn: c,
|
||||
Wsmsg: &WsCodec{},
|
||||
}
|
||||
return &cd
|
||||
|
||||
}
|
||||
|
||||
func (h *ClientData) OnEvent(v []byte) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
||||
// 1. 打印错误信息
|
||||
log.Printf("捕获到 panic 错误:%v", err)
|
||||
// 2. 打印完整调用堆栈(debug.Stack() 获取堆栈字节流,转为字符串)
|
||||
log.Printf("panic 详细堆栈:\n%s", debug.Stack())
|
||||
}
|
||||
}()
|
||||
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()
|
||||
Recv(h.Conn, header)
|
||||
//fmt.Println("接收封包", header)
|
||||
}
|
||||
|
||||
var Mainplayer = &utils.SyncMap[uint32, *Player]{} //玩家数据
|
||||
|
||||
type OgreInfo struct {
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package player
|
||||
|
||||
import (
|
||||
"blazing/common/utils/bytearray"
|
||||
"blazing/cool"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/gobwas/ws"
|
||||
"github.com/gobwas/ws/wsutil"
|
||||
@@ -189,3 +193,54 @@ func (w *WsCodec) readWsMessages() (messages []wsutil.Message, err error) {
|
||||
msgBuf.curHeader = nil
|
||||
}
|
||||
}
|
||||
|
||||
type ClientData struct {
|
||||
IsCrossDomain bool //是否跨域过
|
||||
Player *Player //客户实体
|
||||
Mu sync.Mutex
|
||||
ERROR_CONNUT int
|
||||
Wsmsg *WsCodec
|
||||
Conn gnet.Conn
|
||||
|
||||
CloseChan chan struct{}
|
||||
}
|
||||
|
||||
func NewClientData(c gnet.Conn) *ClientData {
|
||||
// 创建事件处理器
|
||||
|
||||
cd := ClientData{
|
||||
IsCrossDomain: false,
|
||||
Player: nil,
|
||||
Conn: c,
|
||||
Wsmsg: &WsCodec{},
|
||||
}
|
||||
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()
|
||||
if header.CMD > 1000 {
|
||||
if h.Conn.Context().(*ClientData).Player == nil {
|
||||
cool.Loger.Error(context.TODO(), "player is nil")
|
||||
return
|
||||
}
|
||||
if h.Conn.Context().(*ClientData).Player.Info == nil {
|
||||
cool.Loger.Error(context.TODO(), "player info is nil")
|
||||
return
|
||||
}
|
||||
}
|
||||
h.Recv(header)
|
||||
//fmt.Println("接收封包", header)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user