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.
 
 
 
 
 
 

140 lines
3.8 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using Sog;
using Sog.Service;
using Sog.Log;
namespace DB
{
// 服务器入口
public class DBServer : IScript
{
private Guid m_guid;
private ServerApp m_app;
private DBMsgHandler m_messageHandler;
public DBServer()
{
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);
TraceLog.Debug("DBServer start at {0}, guid {1}", DateTime.Now, m_guid);
}
public virtual void OnHotfix(ServerApp app)
{
m_app = app;
ReadServerConfig();
CommInitOnCreateReload(app);
TraceLog.Debug("DBServer hotfix at {0}, guid {1}", DateTime.Now, m_guid);
}
public virtual void OnReloadConfig(string excelConfigFile)
{
TraceLog.Debug("Server OnReloadConfig at {0}", DateTime.Now);
ReadServerConfig();
}
public virtual void OnTick(long nowMs)
{
ServerStat.Instance.Tick(nowMs);
}
public virtual void OnMessage(uint remoteAppID, MessageData message)
{
m_messageHandler.HandlerMessage(remoteAppID, message);
}
public virtual void OnStop()
{
//很多服务器停服可以直接退出
m_app.SetStopSuccess();
//关闭所有消息处理任务
MessageTaskDistributor.Instance.CloseAllTask();
}
//所有引用对象置空
public void Dispose()
{
TraceLog.Debug("DBServer be disposed");
m_app = null;
m_messageHandler = null;
MessageTaskHandler.DisposeAllHandler();
}
private void ReadServerConfig()
{
string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile;
var oldServerConfig = ServerConfigMgr.Instance.m_serverConfig;
DBServerConfig serverConfig = ServerConfigMgr.Instance.ReloadServerConfig<DBServerConfig>(m_app,strLogicServerConfig);
if (serverConfig != null && oldServerConfig != null && oldServerConfig is DBServerConfig)
{
//数据库切换
if (string.Compare(((DBServerConfig) oldServerConfig).dbip, serverConfig.dbip) != 0)
{
TraceLog.Debug("ReadServerConfig dbip {0} change to {1}", ((DBServerConfig) oldServerConfig).dbip, serverConfig.dbip);
MessageTaskHandler.AfterReloadServerConfigChangeDB(m_app);
}
}
}
private void RegisterAllDataObj()
{
TraceLog.Debug("DBServer RegisterAllDataObj");
DBServerData dbServerData = new DBServerData(m_app);
ServerDataObjMgr.Instance.RegisterDataObj(dbServerData);
//读取配置文件,需要在DBServerData注册之后
ReadServerConfig();
}
private void RegisterAllService()
{
TraceLog.Debug("DBServer RegisterAllService");
MessageTaskHandler.InitAllTaskHandler(m_app);
m_messageHandler = new DBMsgHandler();
ServiceMgr.Instance.RegisterService(m_messageHandler);
}
}
}