GateSessionKeyComponent.cs 895 B

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