GateSessionKeyComponent.cs 994 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections.Generic;
  2. namespace Model
  3. {
  4. [ObjectEvent]
  5. public class GateSessionKeyComponentEvent : ObjectEvent<GateSessionKeyComponent>, IAwake
  6. {
  7. public void Awake()
  8. {
  9. this.Get().Awake();
  10. }
  11. }
  12. public class GateSessionKeyComponent : Component
  13. {
  14. private TimerComponent timerComponent;
  15. private readonly HashSet<long> sessionKey = new HashSet<long>();
  16. public void Awake()
  17. {
  18. this.timerComponent = Game.Scene.GetComponent<TimerComponent>();
  19. }
  20. public long Get()
  21. {
  22. long key = RandomHelper.RandInt64();
  23. this.sessionKey.Add(key);
  24. this.TimeoutRemoveKey(key);
  25. return key;
  26. }
  27. public bool Check(long key)
  28. {
  29. bool ret = this.sessionKey.Contains(key);
  30. if (ret)
  31. {
  32. this.sessionKey.Remove(key);
  33. }
  34. return ret;
  35. }
  36. public void Remove(long key)
  37. {
  38. this.sessionKey.Remove(key);
  39. }
  40. private async void TimeoutRemoveKey(long key)
  41. {
  42. await this.timerComponent.WaitAsync(20000);
  43. this.sessionKey.Remove(key);
  44. }
  45. }
  46. }