You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

222 lines
7.3 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using ProtoCSStruct;
using Sog;
using Sog.IO;
using Sog.Log;
using Sog.Service;
namespace Operation
{
// 服务器入口
public class OperationServer : IScript
{
private Guid m_guid = Guid.NewGuid();
private ServerApp m_app;
private OperationMsgHandler m_messageHandler;
private long m_stopServerTime;
public static SyncHttpServerService m_httpService = new SyncHttpServerService();
private void CommInitOnCreateReload(ServerApp app)
{
m_app = app;
if (GameConfigMgr.Instance.m_isBin)
{
GameConfigMgr.Instance.ReadAllConfig();
AppTime.UpdateGameResetHour(CommParamDescMgr.Instance.CrossDaysTime.int_val);
}
DBServerSelect.Init(app);
RegisterAllService();
RoleInfoMap.LoadDataFromFile();
}
public IScriptHotfixCheck GetScriptHotfixCheck()
{
return new ServerScriptHotfixCheck();
}
public virtual void OnCreate(ServerApp app)
{
//打印消息统计日志
app.GetCluster().NeedLogMsgStat = true;
m_app = app;
RegisterAllDataObj();
CommInitOnCreateReload(app);
var http = OperationServerUtils.GetServerConfig();
if (http.startHttpserver == 1)
{
var route = HttpApiRootHandler.Instance.Init();
Console.WriteLine("HttpPortNumber :" + http.HttpPortNumber);
m_httpService.StartHttpServer(http.HttpPortNumber, route);
}
TraceLog.Debug("OperationServer start at {0}, guid {1}", DateTime.Now, m_guid);
TraceLog.Debug("OperationServer timezone {0}, nowsec {1}", AppTime.TimeZone, OperationServerUtils.GetTimeSecond());
}
public virtual void OnHotfix(ServerApp app)
{
m_app = app;
ReadServerConfig();
CommInitOnCreateReload(app);
var http = OperationServerUtils.GetServerConfig();
if (http.startHttpserver == 1)
{
var route = HttpApiRootHandler.Instance.Init();
m_httpService.StartHttpServer(http.HttpPortNumber, route);
}
TraceLog.Debug("OperationServer hotfix at {0}, guid {1}", DateTime.Now, m_guid);
}
public virtual void OnReloadConfig(string excelConfigFile)
{
TraceLog.Debug("OperationServer OnReloadConfig at {0}", DateTime.Now);
ReadServerConfig();
}
public virtual void OnTick(long nowMs)
{
ServerStat.Instance.Tick(nowMs);
// 定时发送邮件
OperationServerUtils.GetSendMailSvc().OnTick(nowMs);
m_httpService.Update();
Realmlist.OnTick(nowMs);
WebsiteSvc.OnTick(nowMs);
Realm.Refresh_RealmCache(nowMs);
OperationServerUtils.CleanTokenList(nowMs);
OperationServerUtils.GetSendNoticeSvc().OnTick(nowMs);
AdminSvc.OnTick(nowMs);
if ((m_stopServerTime > 0) && (nowMs / 1000 - m_stopServerTime > 5))
{
// 进程可以退出
TraceLog.Debug("OperationServer tick stop!!!");
m_app.SetStopSuccess();
}
}
public virtual void OnMessage(uint remoteAppID, MessageData message)
{
m_messageHandler.HandlerMessage(remoteAppID, message);
}
public virtual void OnStop()
{
//很多服务器停服可以直接退出
m_stopServerTime = OperationServerUtils.GetTimeSecond();
TraceLog.Debug("OperationServer set stop time");
RoleInfoMap.SaveDataToFile();
//关闭所有消息处理任务
MessageTaskDistributor.Instance.CloseAllTask();
}
//所有引用对象置空
public void Dispose()
{
TraceLog.Debug("OperationServer be disposed");
RoleInfoMap.SaveDataToFile();
m_app = null;
m_messageHandler = null;
HttpApiRootHandler.Instance.Dispose();
}
private void ReadServerConfig()
{
//旧的数据库配置
var oldServerConfig = ServerConfigMgr.Instance.m_serverConfig;
//读取配置文件
string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile;
ServerConfigMgr.Instance.ReloadServerConfig<OperationServerConfig>(m_app, strLogicServerConfig);
var serverConfig = ServerConfigMgr.Instance.ReloadServerConfig<OperationServerConfig>(m_app, strLogicServerConfig);
if (serverConfig != null && oldServerConfig != null && oldServerConfig is OperationServerConfig)
{
if (string.Compare(((OperationServerConfig)oldServerConfig).dbip, serverConfig.dbip) != 0)
{
TraceLog.Debug("OperationServer.ReadServerConfig Trans db {0} to {1}", ((OperationServerConfig)oldServerConfig).dbip, serverConfig.dbip);
//数据库切换
MessageTaskHandler.AfterReloadServerConfigChangeDB(m_app);
}
}
}
private void RegisterAllDataObj()
{
ReadServerConfig();
OperationServerData serverData = new OperationServerData(m_app);
ServerDataObjMgr.Instance.RegisterDataObj(serverData);
GameConfigMgr.Instance.ReadAllConfig();
AppTime.UpdateGameResetHour(CommParamDescMgr.Instance.CrossDaysTime.int_val);
MailDBData mailDBData = new MailDBData();
ServerDataObjMgr.Instance.RegisterDataObj(mailDBData);
}
private void RegisterAllService()
{
TraceLog.Debug("OperationServer.RegisterAllService");
MessageTaskHandler.InitAllTaskHandler(m_app);
m_messageHandler = new OperationMsgHandler(m_app);
ServiceMgr.Instance.RegisterService(m_messageHandler);
SendMailSvc sendMailSvc = new SendMailSvc();
ServiceMgr.Instance.RegisterService(sendMailSvc);
GmCmdSvc gmcmd = new GmCmdSvc();
ServiceMgr.Instance.RegisterService(gmcmd);
SendNoticeSvc sendNoticeSvc = new SendNoticeSvc();
ServiceMgr.Instance.RegisterService(sendNoticeSvc);
Realmlist Realmlist = new Realmlist();
ServiceMgr.Instance.RegisterService(Realmlist);
ExchangeCodeSvc ExchangeCodeSvc = new ExchangeCodeSvc();
ServiceMgr.Instance.RegisterService(ExchangeCodeSvc);
AdminSvc adminSvc = new AdminSvc();
ServiceMgr.Instance.RegisterService(adminSvc);
}
public static void UpdateCurrency()
{
var cfg = (OperationServerConfig)ServerConfigMgr.Instance.m_serverConfig;
int currency = cfg.currency;
if (currency == 0)
{
return;
}
TraceLog.Trace("OperationServer.UpdateCurrency currency {0}", (Currency)currency);
PayDiamondDescMgr.Instance.UpdateGameCurrency(currency);
}
}
}