SessionIdleCheckerComponentSystem.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. namespace ET
  3. {
  4. [Invoke(TimerInvokeType.SessionIdleChecker)]
  5. public class SessionIdleChecker: ATimer<SessionIdleCheckerComponent>
  6. {
  7. protected override void Run(SessionIdleCheckerComponent self)
  8. {
  9. try
  10. {
  11. self.Check();
  12. }
  13. catch (Exception e)
  14. {
  15. Log.Error($"move timer error: {self.Id}\n{e}");
  16. }
  17. }
  18. }
  19. [ObjectSystem]
  20. public class SessionIdleCheckerComponentAwakeSystem: AwakeSystem<SessionIdleCheckerComponent>
  21. {
  22. protected override void Awake(SessionIdleCheckerComponent self)
  23. {
  24. self.RepeatedTimer = TimerComponent.Instance.NewRepeatedTimer(ConstValue.SessionTimeoutTime / 2 + 100, TimerInvokeType.SessionIdleChecker, self);
  25. }
  26. }
  27. [ObjectSystem]
  28. public class SessionIdleCheckerComponentDestroySystem: DestroySystem<SessionIdleCheckerComponent>
  29. {
  30. protected override void Destroy(SessionIdleCheckerComponent self)
  31. {
  32. TimerComponent.Instance?.Remove(ref self.RepeatedTimer);
  33. }
  34. }
  35. public static class SessionIdleCheckerComponentSystem
  36. {
  37. public const int CheckInteral = 2000;
  38. public static void Check(this SessionIdleCheckerComponent self)
  39. {
  40. Session session = self.GetParent<Session>();
  41. long timeNow = TimeHelper.ClientNow();
  42. if (timeNow - session.LastRecvTime < ConstValue.SessionTimeoutTime && timeNow - session.LastSendTime < ConstValue.SessionTimeoutTime)
  43. {
  44. return;
  45. }
  46. Log.Info($"session timeout: {session.Id} {timeNow} {session.LastRecvTime} {session.LastSendTime} {timeNow - session.LastRecvTime} {timeNow - session.LastSendTime}");
  47. session.Error = ErrorCore.ERR_SessionSendOrRecvTimeout;
  48. session.Dispose();
  49. }
  50. }
  51. }