/* Sog 游戏基础库 2016 by zouwei */ using System; using Sog; using Sog.Service; using Sog.Log; namespace Mail { // 服务器入口 public class MailServer : IScript { private Guid m_guid; private ServerApp m_app; private MailMsgHandler m_messageHandler; private long m_oneMinute; public MailServer() { m_guid = Guid.NewGuid(); } public IScriptHotfixCheck GetScriptHotfixCheck() { return new ServerScriptHotfixCheck(); } private void CommInitOnCreateReload(ServerApp app) { m_app = app; RegisterAllService(); } public virtual void OnCreate(ServerApp app) { //打印消息统计日志 app.GetCluster().NeedLogMsgStat = true; //多线程特殊需求 app.GetCluster().EnableMultiThreadSendSafe(); m_app = app; RegisterAllDataObj(); CommInitOnCreateReload(app); MailSendWithRule.LoadDataFromFile(); TraceLog.Debug("MailServer start at {0}, guid {1}", DateTime.Now, m_guid); } public virtual void OnHotfix(ServerApp app) { m_app = app; ReadServerConfig(); CommInitOnCreateReload(app); TraceLog.Debug("MailServer hotfix at {0}, guid {1}", DateTime.Now, m_guid); } public virtual void OnReloadConfig(string excelConfigFile) { TraceLog.Debug("Server OnReloadConfig at {0}", DateTime.Now); ReadServerConfig(); MessageTaskHandler.ResetTaskStat(); } public virtual void OnTick(long nowMs) { long nowSec = m_app.GetTimeSecond(); TickOneSecond(nowMs,nowSec); TickOneMinute(nowMs, nowSec); ServerStat.Instance.Tick(nowMs); MessageTaskHandler.Tick(nowMs); m_messageHandler.Tick(); } public virtual void OnMessage(uint remoteAppID, MessageData message) { m_messageHandler.HandlerMessage(remoteAppID, message); } public virtual void OnStop() { MailSendWithRule.SaveDataToFile(); } //所有引用对象置空 public void Dispose() { TraceLog.Debug("MailServer be disposed"); m_app = null; m_messageHandler = null; //保存邮件文件 MailSendWithRule.SaveDataToFile(); MessageTaskHandler.DisposeAllHandler(); } private void ReadServerConfig() { //读取配置文件 string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile; var oldServerConfig = ServerConfigMgr.Instance.m_serverConfig; MailServerConfig serverConfig = ServerConfigMgr.Instance.ReloadServerConfig(m_app,strLogicServerConfig); if (serverConfig != null && oldServerConfig != null && oldServerConfig is MailServerConfig) { if (string.Compare(((MailServerConfig)oldServerConfig).dbip, serverConfig.dbip) != 0) { TraceLog.Debug("ReadServerConfig DB Trans {0} to {1}", ((MailServerConfig)oldServerConfig).dbip, serverConfig.dbip); //数据库切换 MessageTaskHandler.AfterReloadServerConfigChangeDB(m_app); } } } private void RegisterAllDataObj() { TraceLog.Debug("MailServer RegisterAllDataObj"); MailServerData dbServerData = new MailServerData(m_app); ServerDataObjMgr.Instance.RegisterDataObj(dbServerData); ReadServerConfig(); //运营日志 BillLogWriter.Instance.Init(m_app.ServerID, MailServerUtils.GetPacketSender()); //读取游戏数据文件 GameConfigMgr.Instance.ReadAllConfig(); AppTime.UpdateGameResetHour(CommParamDescMgr.Instance.CrossDaysTime.int_val); MailOp.MaxMailCount = CommParamDescMgr.Instance.MailCountMax.int_val; } private void RegisterAllService() { TraceLog.Debug("MailServer RegisterAllService"); MessageTaskHandler.InitAllTaskHandler(m_app); m_messageHandler = new MailMsgHandler(); ServiceMgr.Instance.RegisterService(m_messageHandler); GmCmdSvc gmCmdSvc = new GmCmdSvc(); ServiceMgr.Instance.RegisterService(gmCmdSvc); } private long m_oneSecondMs = 0; private void TickOneSecond(long nowMs, long nowSec) { if (nowMs < m_oneSecondMs + 1000) { return; } m_oneSecondMs = nowMs; PushSvc.OnTick(nowSec); PlayerMailVerSeqSvc.Tick(nowMs); TickServerStop(); } private void TickOneMinute(long nowMs, long nowSec) { if (nowSec < m_oneMinute + 60) { return; } m_oneMinute = nowSec; MailSendWithRule.TickSendMailWithRule(nowSec); MailMsgHandler.TickOneMinute(nowSec); MailSendWithRule.TickGetMailWithRule(nowSec); } private void TickServerStop() { if (m_app.IsStopping) { bool canStop = PlayerMailVerSeqSvc.IsCanStop(); canStop = canStop && m_messageHandler.IsCanStop(); TraceLog.Debug("ArenaServer.MailServer canStop {0}", canStop); if (canStop) { m_app.SetStopSuccess(); } } } } }