refactor(entity): 重构 ClientData 结构体并添加同步锁

- 在 ClientData 中添加 sync.Mutex 以确保并发安全
- 实现 SetPlayer 和 GetPlayer 等方法来封装 player 字段的操作
- 更新相关代码以使用新的 ClientData 结构和方法
This commit is contained in:
2025-07-06 22:58:39 +08:00
parent 53da82df82
commit 666557416d
9 changed files with 142 additions and 49 deletions

View File

@@ -7,6 +7,7 @@ import (
"blazing/common/socket/handler"
"blazing/cool"
"blazing/logic/service"
"time"
"bytes"
"context"
@@ -25,8 +26,8 @@ var Maincontroller = NewController() //注入service
type Controller struct {
Port uint32
RPCClient struct {
Kick func(int32) error
Quit func(int32) error
Kick func(int32) error
RegisterLogic func(int32) error
UserLogin func(int32, int32) error
UserLogout func(int32, int32) error
@@ -34,11 +35,27 @@ type Controller struct {
}
func (h *Controller) KickPerson(a int) error {
//TODO 这里待实现踢人
fmt.Println("检测到踢人请求", a)
service.KickPlayer(uint32(a))
return nil
}
func (h *Controller) QuitSelf(a int) error {
//TODO 这里待退出
fmt.Println("检测到退出请求")
go func() {
for {
//entity.ConutPlayer()
fmt.Println("当前在线人数", entity.ConutPlayer())
<-time.After((1000))
}
}()
//service.KickPlayer(uint32(a))
return nil
}
func NewController() *Controller {
return &Controller{}
}
@@ -176,7 +193,8 @@ func Recv(c gnet.Conn, data handler.TomeeHeader) {
params = append(params, ptrValue1, reflect.ValueOf(service.GetPlayer(c, data.UserID)))
} else {
// fmt.Println("远程地址", c.RemoteAddr(), c)
//c.SetContext("ahahha")
params = append(params, ptrValue1, reflect.ValueOf(c))
}
@@ -187,18 +205,5 @@ func Recv(c gnet.Conn, data handler.TomeeHeader) {
if err != nil {
fmt.Println(err)
}
// switch ret[0].Interface().(type) { //TODO 待修改
// case []byte: //原始包
// c.Write(ret[0].Interface().([]byte)) //这里直接发送原始包,应该是已经拼接过的原始包,通常不同使用
// //case uint32: //错误码 实际上这里包含在结构体里了 ,错误码应该构造在返回之前
// default:
// var data1 bytes.Buffer
// struc.Pack(&data1, &data)
// c.Write(data1.Bytes())
// }
}