完善登录流程的错误处理

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

@@ -21,13 +21,17 @@ func (d *Dao) GetRedisPlayerKey(userId uint32) string {
}
func (d *Dao) GetRedisPlayer(userId uint32) *model.Player {
startTime := time.Now().UnixNano()
playerDataLz4, err := d.redis.Get(context.TODO(), d.GetRedisPlayerKey(userId)).Result()
if err != nil {
logger.Error("get player from redis error: %v", err)
return nil
}
endTime := time.Now().UnixNano()
costTime := endTime - startTime
logger.Debug("get player from redis cost time: %v ns", costTime)
// 解压
startTime := time.Now().UnixNano()
startTime = time.Now().UnixNano()
in := bytes.NewReader([]byte(playerDataLz4))
out := new(bytes.Buffer)
lz4Reader := lz4.NewReader(in)
@@ -37,8 +41,8 @@ func (d *Dao) GetRedisPlayer(userId uint32) *model.Player {
return nil
}
playerData := out.Bytes()
endTime := time.Now().UnixNano()
costTime := endTime - startTime
endTime = time.Now().UnixNano()
costTime = endTime - startTime
logger.Debug("lz4 decode cost time: %v ns, before len: %v, after len: %v, ratio lz4/raw: %v",
costTime, len(playerDataLz4), len(playerData), float64(len(playerDataLz4))/float64(len(playerData)))
player := new(model.Player)
@@ -76,11 +80,15 @@ func (d *Dao) SetRedisPlayer(player *model.Player) {
costTime := endTime - startTime
logger.Debug("lz4 encode cost time: %v ns, before len: %v, after len: %v, ratio lz4/raw: %v",
costTime, len(playerData), len(playerDataLz4), float64(len(playerDataLz4))/float64(len(playerData)))
startTime = time.Now().UnixNano()
err = d.redis.Set(context.TODO(), d.GetRedisPlayerKey(player.PlayerID), playerDataLz4, time.Hour*24*30).Err()
if err != nil {
logger.Error("set player to redis error: %v", err)
return
}
endTime = time.Now().UnixNano()
costTime = endTime - startTime
logger.Debug("set player to redis cost time: %v ns", costTime)
}
func (d *Dao) SetRedisPlayerList(playerList []*model.Player) {

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 {

View File

@@ -27,12 +27,12 @@ type Avatar struct {
FetterLevel uint8 `bson:"fetterLevel"` // 好感度等级
FetterExp uint32 `bson:"fetterExp"` // 好感度经验
PromoteRewardMap map[uint32]bool `bson:"promoteRewardMap"` // 突破奖励 map[突破等级]是否已被领取
Guid uint64 `bson:"-"`
EquipGuidMap map[uint64]uint64 `bson:"-"`
EquipWeapon *Weapon `bson:"-"`
EquipReliquaryList []*Reliquary `bson:"-"`
FightPropMap map[uint32]float32 `bson:"-"`
ExtraAbilityEmbryos map[string]bool `bson:"-"`
Guid uint64 `bson:"-" msgpack:"-"`
EquipGuidMap map[uint64]uint64 `bson:"-" msgpack:"-"`
EquipWeapon *Weapon `bson:"-" msgpack:"-"`
EquipReliquaryList []*Reliquary `bson:"-" msgpack:"-"`
FightPropMap map[uint32]float32 `bson:"-" msgpack:"-"`
ExtraAbilityEmbryos map[string]bool `bson:"-" msgpack:"-"`
}
func (p *Player) InitAllAvatar() {

View File

@@ -5,7 +5,7 @@ import "hk4e/common/constant"
type Item struct {
ItemId uint32 `bson:"itemId"` // 道具id
Count uint32 `bson:"count"` // 道具数量
Guid uint64 `bson:"-"`
Guid uint64 `bson:"-" msgpack:"-"`
}
func (p *Player) InitAllItem() {

View File

@@ -62,6 +62,7 @@ type Player struct {
DbState int `bson:"-" msgpack:"-"` // 数据库存档状态
WorldId uint32 `bson:"-" msgpack:"-"` // 所在的世界id
GameObjectGuidCounter uint64 `bson:"-" msgpack:"-"` // 游戏对象guid计数器
LastKeepaliveTime uint32 `bson:"-" msgpack:"-"` // 上一次保持活跃时间
ClientTime uint32 `bson:"-" msgpack:"-"` // 玩家客户端的本地时钟
ClientRTT uint32 `bson:"-" msgpack:"-"` // 玩家客户端往返时延
GameObjectGuidMap map[uint64]GameObject `bson:"-" msgpack:"-"` // 游戏对象guid映射表

View File

@@ -15,7 +15,7 @@ type Reliquary struct {
AffixIdList []uint32 `bson:"affixIdList"` // 词缀
MainPropId uint32 `bson:"mainPropId"` // 主词条id
AvatarId uint32 `bson:"avatarId"` // 装备角色id
Guid uint64 `bson:"-"`
Guid uint64 `bson:"-" msgpack:"-"`
}
func (p *Player) InitReliquary(reliquary *Reliquary) {

View File

@@ -36,8 +36,8 @@ type TeamInfo struct {
TeamList []*Team `bson:"teamList"`
CurrTeamIndex uint8 `bson:"currTeamIndex"`
CurrAvatarIndex uint8 `bson:"currAvatarIndex"`
TeamResonances map[uint16]bool `bson:"-"`
TeamResonancesConfig map[int32]bool `bson:"-"`
TeamResonances map[uint16]bool `bson:"-" msgpack:"-"`
TeamResonancesConfig map[int32]bool `bson:"-" msgpack:"-"`
}
func NewTeamInfo() (r *TeamInfo) {

View File

@@ -15,7 +15,7 @@ type Weapon struct {
AffixIdList []uint32 `bson:"affixIdList"` // 词缀
Refinement uint8 `bson:"refinement"` // 精炼等阶
AvatarId uint32 `bson:"avatarId"` // 装备角色id
Guid uint64 `bson:"-"`
Guid uint64 `bson:"-" msgpack:"-"`
}
func (p *Player) InitWeapon(weapon *Weapon) {