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.
 
 
 
 
 
 

180 lines
5.3 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sog;
using Sog.Log;
namespace Gate
{
public class GateServer : IScript
{
private Guid m_guid;
private ServerApp m_app;
GateClientService m_gateClientService;
public GateServer()
{
m_guid = Guid.NewGuid();
}
public IScriptHotfixCheck GetScriptHotfixCheck()
{
return new ServerScriptHotfixCheck();
}
private void CommInitOnCreateReload(ServerApp app)
{
m_app = app;
RegisterAllService();
// 默认消息超过50k压缩
int zipMsgSize = GateServerUtils.GetServerConfig().zipMsgSize;
if (zipMsgSize == 0)
{
zipMsgSize = 10 * 1024;
}
m_gateClientService.SetZipMsgSize(zipMsgSize);
}
public virtual void OnCreate(ServerApp app)
{
//打印消息统计日志
app.GetCluster().NeedLogMsgStat = true;
m_app = app;
RegisterAllDataObj();
CommInitOnCreateReload(app);
app.GetCluster().ReInitAllChannel();
m_gateClientService.StartListen();
TraceLog.Debug("GateServer start at {0}, guid {1}", DateTime.Now, m_guid);
}
public virtual void OnHotfix(ServerApp app)
{
m_app = app;
ReadServerConfig();
CommInitOnCreateReload(app);
TraceLog.Debug("GateServer 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)
{
if(m_gateClientService == null)
{
TraceLog.Assert("GateServer m_gateClientService is null, guid {0}", m_guid);
return;
}
m_gateClientService.Update(nowMs);
ServerStat.Instance.SetValue("onlinePlayer", m_gateClientService.GetClientCount());
ServerStat.Instance.Tick(nowMs);
}
public virtual void OnMessage(uint remoteAppID, MessageData message)
{
GetClusterChannelHandler().HandlerMessage(remoteAppID, message);
}
public virtual void OnStop()
{
//很多服务器停服可以直接退出
m_app.SetStopSuccess();
m_gateClientService.CloseListen();
}
//所有引用对象置空
public void Dispose()
{
TraceLog.Debug("GateServer be disposed");
m_app = null;
m_gateClientService = null;
}
private void ReadServerConfig()
{
GateServerData serverData = GateServerUtils.GetGateServerData();
//读取配置文件
string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile;
GateServerConfig serverConfig = ServerConfigMgr.Instance.ReloadServerConfig<GateServerConfig>(m_app,strLogicServerConfig);
//读取消息频率限制表
serverData.m_messageLimitMap.Clear();
if(serverConfig.messageLimitList != null)
{
for (int i = 0; i < serverConfig.messageLimitList.Length; ++i)
{
var one = serverConfig.messageLimitList[i];
serverData.m_messageLimitMap.TryAdd(one.Id, one.limitCount);
}
TraceLog.Debug("GateServer ReadServerConfig add message rate limit count {0}", serverData.m_messageLimitMap.Count);
}
// 默认消息超过50k压缩
int zipMsgSize = serverConfig.zipMsgSize == 0 ? 50 * 1024 : serverConfig.zipMsgSize;
if (m_gateClientService != null)
{
m_gateClientService.SetZipMsgSize(zipMsgSize);
}
}
private void RegisterAllDataObj()
{
GateServerData serverData = new GateServerData(m_app);
ServerDataObjMgr.Instance.RegisterDataObj(serverData);
ReadServerConfig();
ClientServiceData clientServiceData = new ClientServiceData();
ServerDataObjMgr.Instance.RegisterDataObj(clientServiceData);
}
private void RegisterAllService()
{
TraceLog.Debug("GateServer RegisterAllService");
m_gateClientService = new GateClientService(m_app);
ServiceMgr.Instance.RegisterService(m_gateClientService);
ServerMsgTrans serverMsgTrans = new ServerMsgTrans(m_app);
ServiceMgr.Instance.RegisterService(serverMsgTrans);
ServerRoute serverRoute = new ServerRoute(m_app);
ServiceMgr.Instance.RegisterService(serverRoute);
ClusterChannelHandler channelHandler = new ClusterChannelHandler(m_app, m_gateClientService);
ServiceMgr.Instance.RegisterService(channelHandler);
}
private ClusterChannelHandler GetClusterChannelHandler()
{
return ServiceMgr.GetService<ClusterChannelHandler>(GateServiceType.ClusterChannelHandler);
}
}
}