/* Sog 游戏基础库 2016 by zouwei */ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Sog; using Sog.Service; using Sog.Log; using ProtoCSStruct; namespace Rank { // 服务器入口 public class RankServer : IScript { private Guid m_guid; private ServerApp m_app; private RankMsgHandler m_messageHandler; private long m_oneSecondMs; private long m_5SecondMs; private long m_10SecondMs; public static bool m_loadrealm; public static RankServerConfig serverConfig { get { return RankServerUtils.GetRankServerData().m_RankConfig; } } public RankServer() { m_guid = Guid.NewGuid(); } public IScriptHotfixCheck GetScriptHotfixCheck() { return new ServerScriptHotfixCheck(); } private void CommInitOnCreateReload(ServerApp app) { m_app = app; RegisterAllService(); GameDBServerIDUtils.Init(m_app); RankServerUtils.GetEventHandlerMgr().RegisterAllEventHandler(); } public virtual void OnCreate(ServerApp app) { //打印消息统计日志 app.GetCluster().NeedLogMsgStat = true; m_app = app; RegisterAllDataObj(); CommInitOnCreateReload(app); TraceLog.Debug("RankServer.OnCreate start at {0}, guid {1}", DateTime.Now, m_guid); } public virtual void OnHotfix(ServerApp app) { m_app = app; CommInitOnCreateReload(app); TraceLog.Debug("RankServer.OnHotfix reload dll at {0}, guid {1}", DateTime.Now, m_guid); } public virtual void OnReloadConfig(string excelConfigFile) { ReadServerConfig(); TraceLog.Debug("Server OnReloadConfig at {0}", DateTime.Now); } // TickNewDay每天只触发一次,如果内部调用的逻辑出错,要等到明天才有更新的机会 private void TickNewDay(long nowSec) { var svrData = RankServerUtils.GetRankServerData(); // world启动时会从文件WorldSvrData读入LastDayStartTime if (AppTime.IsSameDay(nowSec, svrData.m_lastDayStartTime)) { return; } RankSvc.TALogRank(nowSec); svrData.m_lastDayStartTime = nowSec; } private void TickOneSecond(long nowMs, long nowSec) { if (nowMs < m_oneSecondMs + 1000) { return; } m_oneSecondMs = nowMs; RankSvc.OnTick(nowSec); TickServerStop(); } private void Tick5Second(long nowMs, long nowSec) { if (nowMs < m_5SecondMs + 5000) { return; } m_5SecondMs = nowMs; TickNewDay(nowSec); } private void Tick10Second(long nowMs, long nowSec) { if (nowMs < m_10SecondMs + 10000) { return; } m_10SecondMs = nowMs; RankSvc.On10secondTick(nowSec); RankSvc.SaveRankData(); } private void TickServerStop() { if (m_app.IsStopping) { bool canStop = true; canStop = canStop && WaitAckStructRequestSender.Instance.IsEmpty(); canStop = canStop && RankSvc.CheckCanStop(); TraceLog.Debug("RankServer.TickServerStop canStop {0}", canStop); if (canStop) { m_app.SetStopSuccess(); } } } public virtual void OnTick(long nowMs) { //m_stopwatch = Stopwatch.StartNew(); long nowSec = m_app.GetTimeSecond(); TickOneSecond(nowMs, nowSec); Tick5Second(nowMs, nowSec); Tick10Second(nowMs, nowSec); WaitAckStructRequestSender.Instance.Tick(nowMs); ServerStat.Instance.Tick(nowMs); TickGetRealmList(nowSec); } public virtual void OnMessage(uint remoteAppID, MessageData message) { m_messageHandler.HandlerMessage(remoteAppID, message); } public virtual void OnStop() { RankSvc.ForceUpdateAllRank(); //停服强制更新排行榜 RankSvc.SaveRankData(); } //所有引用对象置空 public void Dispose() { TraceLog.Debug("RankServer be disposed"); m_app = null; m_messageHandler = null; } private void ReadServerConfig() { //读取配置文件 string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile; RankServerConfig serverConfig = ServerConfigMgr.Instance.ReloadServerConfig(m_app, strLogicServerConfig); RankServerUtils.GetRankServerData().m_RankConfig = serverConfig; } private void RegisterAllDataObj() { TraceLog.Debug("RankServer RegisterAllDataObj"); RankServerData serverData = new RankServerData(m_app); ServerDataObjMgr.Instance.RegisterDataObj(serverData); WaitAckStructRequestSender.Instance.Init(RankServerUtils.GetPacketSender(), m_app.ServerID, m_app); ReadServerConfig(); TraceLog.Debug("RegisterAllDataObj, read WorldServerConfig"); //运营日志 BillLogWriter.Instance.Init(m_app.ServerID, RankServerUtils.GetPacketSender()); //读取游戏数据文件 GameConfigMgr.Instance.ReadAllConfig(); AppTime.UpdateGameResetHour(CommParamDescMgr.Instance.CrossDaysTime.int_val); PlayerTable playerTable = new PlayerTable(); ServerDataObjMgr.Instance.RegisterDataObj(playerTable); RankSvc.LoadRankFromFile(); } private void RegisterAllService() { TraceLog.Debug("RankServer RegisterAllService"); m_messageHandler = new RankMsgHandler(m_app); ServiceMgr.Instance.RegisterService(m_messageHandler); EventHandlerMgr eventHandlerMgr = new EventHandlerMgr(); ServiceMgr.Instance.RegisterService(eventHandlerMgr); GmCmdSvc gmCmdSvc = new GmCmdSvc(); ServiceMgr.Instance.RegisterService(gmCmdSvc); } public void TickGetRealmList(long nowSec) { var svrData = RankServerUtils.GetRankServerData(); if (nowSec - svrData.m_lastGetRealmTime < 60) { return; } svrData.m_lastGetRealmTime = nowSec; TraceLog.Trace("RankServer.TickGetRealmList get RealmList From Realmlist Server"); SSRankRealmlistReq req = new SSRankRealmlistReq(); RankServerUtils.GetPacketSender().SendToWorldServer((int)SSGameMsgID.RankRealmlistReq,ref req,0); } } }