TimeModule.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using XGame.Framework.ThreadScheduler;
  3. namespace XGame.Framework.Time
  4. {
  5. public class TimeModule : ITimeModule, ITimeUpdate, IDisposable
  6. {
  7. private IClock _clock;
  8. private DateTime _utcStartTime;
  9. public void Initialize()
  10. {
  11. _utcStartTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  12. double nowTimestamp = (DateTime.UtcNow - _utcStartTime).TotalMilliseconds;
  13. _clock = new Clock((long)nowTimestamp);
  14. MainThreadUpdateRegister.update += Update;
  15. }
  16. #region ITimeModule接口实现
  17. public long GetNowTime(ClockType clockTyp = ClockType.Server)
  18. {
  19. return _clock.GetNowTime(clockTyp);
  20. }
  21. public void SetTime(long timestamp, ClockType clockTyp = ClockType.Server)
  22. {
  23. _clock.SetTime(timestamp, clockTyp);
  24. }
  25. public IAlarm AddAlarm(Action action, long triggerTimestamp, ClockType clockTyp = ClockType.Server)
  26. {
  27. return _clock.AddAlarm(triggerTimestamp, action, clockTyp);
  28. }
  29. public ITimer AddDelayTimer(int delay, Action action)
  30. {
  31. return _clock.AddDelayTimer(delay, action);
  32. }
  33. public ITimer AddLooperTimer(int interval, Action action)
  34. {
  35. return _clock.AddLooperTimer(interval,interval, action);
  36. }
  37. public ITimer AddLooperTimer(int interval, Action<int> action)
  38. {
  39. return _clock.AddLooperTimer(interval, interval, action);
  40. }
  41. public ITimer AddLooperTimer(int interval, int loopTimes, Action action)
  42. {
  43. return _clock.AddLooperTimer(interval, interval, loopTimes, action);
  44. }
  45. public ITimer AddLooperTimer(int interval, int loopTimes, Action<int> action)
  46. {
  47. return _clock.AddLooperTimer(interval, interval, loopTimes, action);
  48. }
  49. #endregion
  50. #region IUpdate接口实现
  51. public void Update(int elapse)
  52. {
  53. _clock.Update(elapse);
  54. }
  55. #endregion
  56. public void Dispose()
  57. {
  58. MainThreadUpdateRegister.update -= Update;
  59. _clock.Dispose();
  60. _clock = null;
  61. }
  62. public ITimer AddDelayLooperTimer(int delay, int interval, Action action)
  63. {
  64. return _clock.AddLooperTimer(delay, interval, action);
  65. }
  66. public ITimer AddDelayLooperTimer(int delay, int interval, Action<int> action)
  67. {
  68. return _clock.AddLooperTimer(delay, interval, action);
  69. }
  70. public ITimer AddDelayLooperTimer(int delay, int interval, int loopTimes, Action action)
  71. {
  72. return _clock.AddLooperTimer(delay, interval, loopTimes, action);
  73. }
  74. public ITimer AddDelayLooperTimer(int delay, int interval, int loopTimes, Action<int> action)
  75. {
  76. return _clock.AddLooperTimer(delay, interval, loopTimes, action);
  77. }
  78. }
  79. }