完善登录流程的错误处理

This commit is contained in:
flswld
2023-02-11 21:08:44 +08:00
parent 4380aea9aa
commit b5b5cf59a5
17 changed files with 253 additions and 144 deletions

View File

@@ -185,6 +185,7 @@ func (g *GameManager) gameMainLoop() {
tickCost := int64(0)
localEventCost := int64(0)
commandCost := int64(0)
routeCount := int64(0)
runtime.LockOSThread()
for {
// 消耗CPU时间性能统计
@@ -194,7 +195,7 @@ func (g *GameManager) gameMainLoop() {
tickCost /= 1e6
localEventCost /= 1e6
commandCost /= 1e6
logger.Info("[GAME MAIN LOOP] cpu time cost detail, routeCost: %vms, tickCost: %vms, localEventCost: %vms, commandCost: %vms",
logger.Info("[GAME MAIN LOOP] cpu time cost detail, routeCost: %v ms, tickCost: %v ms, localEventCost: %v ms, commandCost: %v ms",
routeCost, tickCost, localEventCost, commandCost)
totalCost := routeCost + tickCost + localEventCost + commandCost
logger.Info("[GAME MAIN LOOP] cpu time cost percent, routeCost: %v%%, tickCost: %v%%, localEventCost: %v%%, commandCost: %v%%",
@@ -202,15 +203,17 @@ func (g *GameManager) gameMainLoop() {
float32(tickCost)/float32(totalCost)*100.0,
float32(localEventCost)/float32(totalCost)*100.0,
float32(commandCost)/float32(totalCost)*100.0)
logger.Info("[GAME MAIN LOOP] total cpu time cost detail, totalCost: %vms",
logger.Info("[GAME MAIN LOOP] total cpu time cost detail, totalCost: %v ms",
totalCost)
logger.Info("[GAME MAIN LOOP] total cpu time cost percent, totalCost: %v%%",
float32(totalCost)/float32(intervalTime/1e6)*100.0)
logger.Info("[GAME MAIN LOOP] avg route cost: %v ms", float32(routeCost)/float32(routeCount))
lastTime = now
routeCost = 0
tickCost = 0
localEventCost = 0
commandCost = 0
routeCount = 0
}
select {
case netMsg := <-MESSAGE_QUEUE.GetNetMsg():
@@ -219,6 +222,7 @@ func (g *GameManager) gameMainLoop() {
ROUTE_MANAGER.RouteHandle(netMsg)
end := time.Now().UnixNano()
routeCost += end - start
routeCount++
case <-TICK_MANAGER.GetGlobalTick().C:
// 游戏服务器定时帧
start := time.Now().UnixNano()
@@ -232,12 +236,12 @@ func (g *GameManager) gameMainLoop() {
end := time.Now().UnixNano()
localEventCost += end - start
case command := <-COMMAND_MANAGER.GetCommandTextInput():
// 处理传入的命令(普通玩家 GM命令)
// 处理GM命令
start := time.Now().UnixNano()
COMMAND_MANAGER.HandleCommand(command)
end := time.Now().UnixNano()
commandCost += end - start
logger.Info("run gm cmd cost: %v ns", commandCost)
logger.Info("run gm cmd cost: %v ns", end-start)
}
}
}

View File

@@ -4,6 +4,7 @@ import (
"time"
"hk4e/common/mq"
"hk4e/gate/kcp"
"hk4e/gs/model"
"hk4e/pkg/logger"
"hk4e/protocol/cmd"
@@ -87,6 +88,13 @@ func (g *GameManager) ClientTimeNotify(userId uint32, clientTime uint32) {
}
logger.Debug("client time notify, uid: %v, time: %v", userId, clientTime)
player.ClientTime = clientTime
now := time.Now().Unix()
// 客户端与服务器时间相差太过严重
if now-int64(player.ClientTime) > 60 || int64(player.ClientTime)-now > 60 {
g.KickPlayer(player.PlayerID, kcp.EnetServerKick)
logger.Error("abs of client time and server time above 60, uid: %v", userId)
}
player.LastKeepaliveTime = uint32(now)
}
func (g *GameManager) ServerAnnounceNotify(announceId uint32, announceMsg string) {

View File

@@ -85,6 +85,17 @@ func (t *TickManager) onUserTickSecond(userId uint32, now int64) {
}
func (t *TickManager) onUserTickMinute(userId uint32, now int64) {
player := USER_MANAGER.GetOnlineUser(userId)
if player == nil {
logger.Error("player is nil, uid: %v", userId)
return
}
if uint32(now/1000)-player.LastKeepaliveTime > 60 {
logger.Error("remove keepalive timeout user, uid: %v", userId)
GAME_MANAGER.OnUserOffline(userId, &ChangeGsInfo{
IsChangeGs: false,
})
}
}
// 玩家定时任务常量
@@ -182,15 +193,6 @@ func (t *TickManager) onTickMinute(now int64) {
count := random.GetRandomInt32(0, 4)
i := int32(0)
for itemId := range allItemDataConfig {
itemDataConfig, ok := allItemDataConfig[itemId]
if !ok {
logger.Error("config is nil, itemId: %v", itemId)
return
}
// TODO 3.0.0REL版本中 发送某些无效家具 可能会导致客户端背包家具界面卡死
if uint16(itemDataConfig.Type) == constant.ITEM_TYPE_FURNITURE {
continue
}
num := random.GetRandomInt32(1, 9)
GAME_MANAGER.AddUserItem(player.PlayerID, []*UserItem{{ItemId: uint32(itemId), ChangeCount: uint32(num)}}, true, 0)
i++

View File

@@ -16,7 +16,7 @@ import (
const (
ENTITY_NUM_UNLIMIT = false // 是否不限制场景内实体数量
ENTITY_MAX_SEND_NUM = 200 // 场景内最大实体数量
MAX_MULTIPLAYER_WORLD_NUM = 10 // 本服务器最大多人世界数量
MAX_MULTIPLAYER_WORLD_NUM = 2 // 本服务器最大多人世界数量
)
type WorldManager struct {