GateSessionKeyComponent.cs 1010 B

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