/* Sog 游戏基础库 2016 by zouwei */ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using ProtoCSStruct; using Sog.IO; namespace Game { public static class StopServerSvc { private static int STOP_SERVER_TIME_NOTICE = 15; private static long m_gmStartStopServerTime = 0; private static long m_gmStopServerTime = 0; private static int m_lastNoticeMintue = 0; private static long m_lastTimeForCheckAllDataSaveToQuit; public static void StartStopServer(int stopAfterMintue) { if(m_gmStartStopServerTime != 0) { TraceLog.Debug("StopServerSvc.StartStopServer already stop server at {0}" , AppTime.ConvertUnixTimeToDateTime(m_gmStartStopServerTime*1000)); return; } var notify = new SSGmStopServerNotify(); GameServerUtils.GetPacketSender().SendToWorldServer((int)SSGameMsgID.GmStopServerNotify, ref notify, 0); GameServerUtils.GetApp().PrepareStop(); STOP_SERVER_TIME_NOTICE = stopAfterMintue; m_gmStartStopServerTime = GameServerUtils.GetTimeSecond(); // 正式停服时间 m_gmStopServerTime = m_gmStartStopServerTime + STOP_SERVER_TIME_NOTICE * 60 - 1; TraceLog.Debug("StopServerSvc.StartStopServer stop server after {0} mintue, at {1}" , STOP_SERVER_TIME_NOTICE, AppTime.ConvertUnixTimeToDateTime(m_gmStopServerTime * 1000)); m_lastNoticeMintue = STOP_SERVER_TIME_NOTICE; AddNotice(m_lastNoticeMintue); } private static void AddNotice(int mintue) { SSSysNoticeLamp notify = new SSSysNoticeLamp(); notify.Id = 1; notify.Params.Add(mintue.ToString()); GameServerUtils.GetPacketSender().SendToWorldServer((int)SSGameMsgID.SysNoticeLamp, ref notify, 0); } public static void Tick(long nowMs) { TickGmCmdStopCommand(nowMs); TickCheckAllDataSaveToExit(nowMs); } private static void TickGmCmdStopCommand(long nowMs) { if(m_gmStartStopServerTime == 0) { return; } int leftMintue = (int)(m_gmStopServerTime - GameServerUtils.GetTimeSecond()) / 60 + 1; if(leftMintue < m_lastNoticeMintue && leftMintue > 0) { m_lastNoticeMintue = leftMintue; AddNotice(m_lastNoticeMintue); } //执行停服,延迟1秒 if(GameServerUtils.GetTimeSecond() >= m_gmStopServerTime + 1 && GameServerUtils.GetApp().CurrStopStage != ServerStopStage.stopping) { TraceLog.Debug("StopServerSvc.TickGmCmdStopCommand do stop server now"); GameServerUtils.GetApp().StopServer(); } } private static void TickCheckAllDataSaveToExit(long nowMs) { //没有停服 if (GameServerUtils.GetApp().CurrStopStage != ServerStopStage.stopping) { return; } //1秒检查一次 if (nowMs - m_lastTimeForCheckAllDataSaveToQuit < 1000) { return; } m_lastTimeForCheckAllDataSaveToQuit = nowMs; if (IsAllPlayerSaveSuccess()) { TraceLog.Debug("StopServerSvc:TickCheckAllDataSaveToQuit all player data save success"); GameServerUtils.GetApp().SetStopSuccess(); //保存文件数据库 ServerDataFileDbSvc.SaveDataToFile(); } } private static bool IsAllPlayerSaveSuccess() { int iPlayerCount = GameServerUtils.GetPlayerTableOp().Table.m_uidTable.Count; TraceLog.Trace("StopServerSvc:IsAllPlayerSaveSuccess player object count {0}", iPlayerCount); return iPlayerCount == 0; } } }