feat(rpc): 优化客户端连接管理,使用 sync.Map 替代普通 map
将 `Clientmap` 从普通 map 改为 `sync.Map`,提升并发安全性。新增 `addClient` 和 `getClient` 方法封装存取逻辑,并在多处调用点进行了替换。 fix(fight): 修复战斗逻辑中技能ID与攻击时间字段引用错误 将 `attacker.AttackValue.SkillID` 和 `attacker.AttackValue.AttackTime` 的访问方式修正为正确的字段路径。 refactor(fight): 调整战斗结束信息处理流程 合并 `FightOverInfo` 结构到 `FightC` 中,简化广播发送逻辑,统一通过 `f.FightOverInfo` 发送战斗结果。 refactor(effect): 修改效果叠加判断逻辑并增强健壮性 更新效果节点比较方法,增加参数匹配检查以支持更精确的效果识别;同时添加 `equalInts` 工具函数用于数组内容对比。
This commit is contained in:
@@ -20,6 +20,7 @@ func GetServerInfoList() []ServerInfo {
|
||||
var ret1 []ServerInfo
|
||||
ip, _ := service.NewBaseSysParamService().DataByKey(context.Background(), "server_ip")
|
||||
testip, _ := service.NewBaseSysParamService().DataByKey(context.Background(), "test_ip")
|
||||
|
||||
for _, v := range ret {
|
||||
tt := newServerInfo()
|
||||
tt.OnlineID = uint32(v.OnlineID)
|
||||
@@ -30,7 +31,8 @@ func GetServerInfoList() []ServerInfo {
|
||||
tt.IP = testip
|
||||
}
|
||||
tt.Port = v.Port
|
||||
t, ok := Clientmap[v.Port]
|
||||
|
||||
t, ok := getClient(v.Port)
|
||||
|
||||
if ok {
|
||||
cool.Loger.Info(context.TODO(), "服务器假踢人")
|
||||
|
||||
@@ -3,6 +3,7 @@ package rpc
|
||||
import (
|
||||
"blazing/common/data/share"
|
||||
"blazing/cool"
|
||||
"sync"
|
||||
|
||||
"blazing/modules/base/service"
|
||||
"context"
|
||||
@@ -18,7 +19,26 @@ import (
|
||||
|
||||
var rpcport = gconv.String(cool.Config.RPC)
|
||||
|
||||
var Clientmap = make(map[uint16]*ClientHandler) //客户端map
|
||||
// 定义改为sync.Map
|
||||
var Clientmap sync.Map
|
||||
|
||||
// 存值示例
|
||||
func addClient(id uint16, client *ClientHandler) {
|
||||
// 普通map:Clientmap[id] = client
|
||||
Clientmap.Store(id, client) // sync.Map存值
|
||||
}
|
||||
|
||||
// 取值示例
|
||||
func getClient(id uint16) (*ClientHandler, bool) {
|
||||
// 普通map:client, ok := Clientmap[id]
|
||||
val, ok := Clientmap.Load(id) // sync.Map取值
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
// 类型断言(确保value是*ClientHandler)
|
||||
client, ok := val.(*ClientHandler)
|
||||
return client, ok
|
||||
}
|
||||
|
||||
type ClientHandler struct {
|
||||
KickPerson func(uint32) error //踢人,这里是返回具体的logic
|
||||
@@ -37,7 +57,8 @@ func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("user not found", err)
|
||||
}
|
||||
cl, ok := Clientmap[useid1]
|
||||
|
||||
cl, ok := getClient(useid1)
|
||||
if ok {
|
||||
err := cl.KickPerson(userid) //实现指定服务器踢人
|
||||
if err != nil {
|
||||
@@ -57,11 +78,11 @@ func (h *ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) erro
|
||||
return fmt.Errorf("no reverse client")
|
||||
}
|
||||
t, _ := blservice.NewLoginServiceService().GetServerID(id)
|
||||
aa, ok := Clientmap[t]
|
||||
aa, ok := getClient(t)
|
||||
if ok && aa != nil { //如果已经存在且这个端口已经被存过
|
||||
aa.QuitSelf(0)
|
||||
}
|
||||
Clientmap[port] = &revClient
|
||||
addClient(port, &revClient)
|
||||
|
||||
//Refurh()
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user