CTT/Server/Hotfix/Game/System/Other/SessionCountCheckComponentS...

119 lines
3.8 KiB
C#
Raw Normal View History

2021-04-24 17:39:11 +08:00
using System;
using System.IO;
namespace ET
{
public class SessionCountCheckComponentAwakeSystem: AwakeSystem<SessionCountCheckComponent>
{
public override void Awake(SessionCountCheckComponent self)
{
self.timerId = TimerComponent.Instance.NewRepeatedTimer(1000, self.Check);
self.GetParent<NetKcpComponent>().Service.ReadCallback += self.OnRead;
}
}
public class SessionCountCheckComponentLoadSystem: LoadSystem<SessionCountCheckComponent>
{
public override void Load(SessionCountCheckComponent self)
{
TimerComponent.Instance.Remove(self.timerId);
self.timerId = TimerComponent.Instance.NewRepeatedTimer(1000, self.Check);
}
}
public class SessionCountCheckComponentDestorySystem: DestroySystem<SessionCountCheckComponent>
{
public override void Destroy(SessionCountCheckComponent self)
{
self.GetParent<NetKcpComponent>().Service.ReadCallback -= self.OnRead;
TimerComponent.Instance.Remove(ref self.timerId);
self.countDic.Clear();
}
}
public static class SessionCountCheckComponentSystem
{
public static void Check(this SessionCountCheckComponent self)
{
using var listComponent = ListComponent<long>.Create();
listComponent.List.AddRange(self.countDic.Keys);
foreach (long id in listComponent.List)
{
if (!self.countDic.TryGetValue(id, out int count))
{
continue;
}
self.countDic[id] = 0;
if (count > 5)
{
Session session = self.Parent.GetChild<Session>(id);
User user = session?.GetComponent<SessionPlayerComponent>()?.User;
Log.Error($"{user?.NickName} : "+count.ToString());
}
if (count <= 8)
{
continue;
}
try
{
CheckAsync(self, id, count).Coroutine();
}
catch (Exception e)
{
Log.Error(e);
}
}
}
private static async ETVoid CheckAsync(SessionCountCheckComponent self, long id, int count)
{
Session session = self.Parent.GetChild<Session>(id);
if (!session)
{
Log.Error("session is null");
return;
}
try
{
User user = null;
try
{
SessionPlayerComponent sessionPlayerComponent = session.GetComponent<SessionPlayerComponent>();
user = sessionPlayerComponent.User;
if (count >= 10)
{
session.Send(new G2C_ForceOffLine());
await TimerComponent.Instance.WaitAsync(300);
}
}
catch (Exception e)
{
Log.Error(e);
}
session.Dispose();
if (user != null)
{
Log.Error($"***********************\n\n【{user.NickName}({user.Id})】 点击次数特别快 :{count}");
}
}
catch (Exception e)
{
Log.Error(e);
}
}
public static void OnRead(this SessionCountCheckComponent self, long channelId, MemoryStream memoryStream)
{
if (!self.countDic.TryGetValue(channelId, out int oldCount))
{
self.countDic[channelId] = 0;
}
self.countDic[channelId] = oldCount + 1;
}
}
}