using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; namespace Gate { public static class GateServerUtils { public static GateServerData GetGateServerData() { return ServerDataObjMgr.GetDataObj(GateDataObjType.GateServerData); } /// /// 获取服务器配置方法 /// public static GateServerConfig GetServerConfig() { return (GateServerConfig)ServerConfigMgr.Instance.m_serverConfig; } public static ServerRoute GetServerRoute() { return ServiceMgr.GetService(GateServiceType.ServerRoute); } public static GateClientService GetGateClientService() { return ServiceMgr.GetService(GateServiceType.GateClientService); } public static ServerMsgTrans GetServerMsgTrans() { return ServiceMgr.GetService(GateServiceType.ServerMsgTrans); } public static long GetTimeSecond() { return GateServerUtils.GetGateServerData().m_app.Time.GetTimeSecond(); } public static void OnAddOneMessage(GateClientInfo info, GateServerConfig serverConfig, int messageId, int messageNum, int messageLenth) { var gateData = GetGateServerData(); long now = GetTimeSecond(); if (info.FlowCheckInfo == null) { info.FlowCheckInfo = new FlowCheckInfo(); } if (serverConfig.messageIgnoreLimitList != null && serverConfig.messageIgnoreLimitList.Contains(messageId)) { return; } var checkInfo = info.FlowCheckInfo; if (checkInfo.nextResetTime < now) { checkInfo.CleanData(); checkInfo.nextResetTime = now + serverConfig.checkFlowSecCd; } //数量限制 if(gateData.m_messageLimitMap.TryGetValue(messageId, out int limitCount)) { if(false == checkInfo.messageNumMap.ContainsKey(messageId)) { checkInfo.messageNumMap.Add(messageId, 0); } checkInfo.messageNumMap[messageId] += messageNum; int nowNum = checkInfo.messageNumMap[messageId]; if (nowNum >= limitCount) { checkInfo.CleanData(); checkInfo.nextResetTime = now + serverConfig.checkFlowSecCd; ++checkInfo.aboveLimitTimes; TraceLog.Error("GateServerUtils.OnAddOneMessage SessionID:{0} messageID:{1} above num:{2}", info.SessionID, messageId, nowNum); return; } } else { checkInfo.messageNum += messageNum; int nowNum = checkInfo.messageNum; if (checkInfo.messageNum >= serverConfig.limitMessageNum) { checkInfo.CleanData(); checkInfo.nextResetTime = now + serverConfig.checkFlowSecCd; ++checkInfo.aboveLimitTimes; TraceLog.Error("GateServerUtils.OnAddOneMessage SessionID:{0} messageNum:{1}", info.SessionID, nowNum); return; } } //流量限制 checkInfo.messageLenth += messageLenth; int nowLenth = checkInfo.messageLenth; if (nowLenth >= serverConfig.limitMessageLenth) { checkInfo.CleanData(); checkInfo.nextResetTime = now + serverConfig.checkFlowSecCd; ++checkInfo.aboveLimitTimes; TraceLog.Error("GateServerUtils.OnAddOneMessage SessionID:{0} messageLenth:{1}", info.SessionID, nowLenth); return; } } } }