Example #1
0
        /// <summary>
        /// 创建一个RepeatedTimer
        /// </summary>
        private long NewRepeatedTimerInner(long time, Action action)
        {
#if SERVER
            if (time < 100)
            {
                throw new Exception($"repeated timer < 100, timerType: time: {time}");
            }
#endif
            long        tillTime = TimeHelper.ServerNow() + time;
            TimerAction timer    = Entity.Create <TimerAction, TimerClass, long, object>(this, TimerClass.RepeatedTimer, time, action, true);
            this.AddTimer(tillTime, timer);
            return(timer.Id);
        }
Example #2
0
 private static void AddTimer(this TimerComponent self, long tillTime, TimerAction timer)
 {
     if (timer.TimerClass == TimerClass.RepeatedTimer && timer.Time == 0)
     {
         self.everyFrameTimer.Enqueue(timer.Id);
         return;
     }
     self.TimeId.Add(tillTime, timer.Id);
     if (tillTime < self.minTime)
     {
         self.minTime = tillTime;
     }
 }
Example #3
0
            protected override void Update(TimerComponent self)
            {
                if (self.TimeId.Count == 0)
                {
                    return;
                }

                long timeNow = TimeHelper.ServerNow();

                if (timeNow < self.minTime)
                {
                    return;
                }

                foreach (KeyValuePair <long, List <long> > kv in self.TimeId)
                {
                    long k = kv.Key;
                    if (k > timeNow)
                    {
                        self.minTime = k;
                        break;
                    }

                    self.timeOutTime.Enqueue(k);
                }

                while (self.timeOutTime.Count > 0)
                {
                    long time = self.timeOutTime.Dequeue();
                    var  list = self.TimeId[time];
                    for (int i = 0; i < list.Count; ++i)
                    {
                        long timerId = list[i];
                        self.timeOutTimerIds.Enqueue(timerId);
                    }

                    self.TimeId.Remove(time);
                }

                while (self.timeOutTimerIds.Count > 0)
                {
                    long timerId = self.timeOutTimerIds.Dequeue();

                    TimerAction timerAction = self.GetChild <TimerAction>(timerId);
                    if (timerAction == null)
                    {
                        continue;
                    }
                    self.Run(timerAction);
                }
            }
Example #4
0
        /// <summary>
        /// 创建一个RepeatedTimer
        /// </summary>
        private long NewRepeatedTimerInner(long time, Action action)
        {
#if NOT_CLIENT
            if (time < 100)
            {
                throw new Exception($"repeated timer < 100, timerType: time: {time}");
            }
#endif
            Log.Debug("NewRepeatedTimerInner");
            long        tillTime = TimeHelper.ServerNow() + time;
            TimerAction timer    = EntityFactory.CreateWithParent <TimerAction, TimerClass, long, object>(this, TimerClass.RepeatedTimer, time, action, true);
            this.AddTimer(tillTime, timer);
            return(timer.Id);
        }
Example #5
0
        public void Update()
        {
            if (this.TimeId.Count == 0)
            {
                return;
            }

            long timeNow = TimeHelper.ServerNow();

            if (timeNow < this.minTime)
            {
                return;
            }

            foreach (KeyValuePair <long, List <long> > kv in this.TimeId)
            {
                long k = kv.Key;
                if (k > timeNow)
                {
                    minTime = k;
                    break;
                }

                this.timeOutTime.Enqueue(k);
            }

            while (this.timeOutTime.Count > 0)
            {
                long time = this.timeOutTime.Dequeue();
                foreach (long timerId in this.TimeId[time])
                {
                    this.timeOutTimerIds.Enqueue(timerId);
                }

                this.TimeId.Remove(time);
            }

            while (this.timeOutTimerIds.Count > 0)
            {
                long timerId = this.timeOutTimerIds.Dequeue();

                TimerAction timerAction = this.GetChild <TimerAction>(timerId);
                if (timerAction == null)
                {
                    continue;
                }
                Run(timerAction);
            }
        }
Example #6
0
        /// <summary>
        /// 创建一个RepeatedTimer
        /// </summary>
        private static long NewRepeatedTimerInner(this TimerComponent self, long time, int type, object args)
        {
#if NOT_UNITY
            if (time < 100)
            {
                throw new Exception($"repeated timer < 100, timerType: time: {time}");
            }
#endif
            long        tillTime = TimeHelper.ServerNow() + time;
            TimerAction timer    = self.AddChild <TimerAction, TimerClass, long, int, object>(TimerClass.RepeatedTimer, time, type, args, true);

            // 每帧执行的不用加到timerId中,防止遍历
            self.AddTimer(tillTime, timer);
            return(timer.Id);
        }
Example #7
0
        private static bool Remove(this TimerComponent self, long id)
        {
            if (id == 0)
            {
                return(false);
            }

            TimerAction timerAction = self.GetChild <TimerAction>(id);

            if (timerAction == null)
            {
                return(false);
            }
            timerAction.Dispose();
            return(true);
        }
Example #8
0
        private bool Remove(long id)
        {
            if (id == 0)
            {
                return(false);
            }

            TimerAction timerAction = this.GetChild <TimerAction>(id);

            if (timerAction == null)
            {
                return(false);
            }
            timerAction.Dispose();
            return(true);
        }
Example #9
0
        private static void Run(this TimerComponent self, TimerAction timerAction)
        {
            switch (timerAction.TimerClass)
            {
            case TimerClass.OnceTimer:
            {
                int    type  = timerAction.Type;
                ITimer timer = self.timerActions[type];
                if (timer == null)
                {
                    Log.Error($"not found timer action: {type}");
                    return;
                }
                timer.Handle(timerAction.Object);
                break;
            }

            case TimerClass.OnceWaitTimer:
            {
                ETTask <bool> tcs = timerAction.Object as ETTask <bool>;
                self.Remove(timerAction.Id);
                tcs.SetResult(true);
                break;
            }

            case TimerClass.RepeatedTimer:
            {
                int  type     = timerAction.Type;
                long tillTime = TimeHelper.ServerNow() + timerAction.Time;
                self.AddTimer(tillTime, timerAction);

                ITimer timer = self.timerActions[type];
                if (timer == null)
                {
                    Log.Error($"not found timer action: {type}");
                    return;
                }
                timer.Handle(timerAction.Object);
                break;
            }
            }
        }
Example #10
0
        public async ETTask <bool> WaitTillAsync(long tillTime, ETCancellationToken cancellationToken = null)
        {
            long timeNow = TimeHelper.ServerNow();

            if (timeNow >= tillTime)
            {
                return(true);
            }

            ETTask <bool> tcs = ETTask <bool> .Create(true);

            TimerAction timer = this.AddChild <TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, tillTime - timeNow, 0, tcs, true);

            this.AddTimer(tillTime, timer);
            long timerId = timer.Id;

            void CancelAction()
            {
                if (this.Remove(timerId))
                {
                    tcs.SetResult(false);
                }
            }

            bool ret;

            try
            {
                cancellationToken?.Add(CancelAction);
                ret = await tcs;
            }
            finally
            {
                cancellationToken?.Remove(CancelAction);
            }
            return(ret);
        }
Example #11
0
        public static async ETTask <bool> WaitAsync(this TimerComponent self, long time, ETCancellationToken cancellationToken = null)
        {
            if (time == 0)
            {
                return(true);
            }
            long tillTime = TimeHelper.ServerNow() + time;

            ETTask <bool> tcs = ETTask <bool> .Create(true);

            TimerAction timer = self.AddChild <TimerAction, TimerClass, long, int, object>(TimerClass.OnceWaitTimer, time, 0, tcs, true);

            self.AddTimer(tillTime, timer);
            long timerId = timer.Id;

            void CancelAction()
            {
                if (self.Remove(timerId))
                {
                    tcs.SetResult(false);
                }
            }

            bool ret;

            try
            {
                cancellationToken?.Add(CancelAction);
                ret = await tcs;
            }
            finally
            {
                cancellationToken?.Remove(CancelAction);
            }
            return(ret);
        }
Example #12
0
        public async ETTask <bool> WaitAsync(long time, ETCancellationToken cancellationToken = null)
        {
            if (time == 0)
            {
                return(true);
            }
            long tillTime = TimeHelper.ServerNow() + time;

            ETTask <bool> tcs = ETTask <bool> .Create(true);

            TimerAction timer = EntityFactory.CreateWithParent <TimerAction, TimerClass, long, object>(this, TimerClass.OnceWaitTimer, 0, tcs, true);

            this.AddTimer(tillTime, timer);
            long timerId = timer.Id;

            void CancelAction()
            {
                if (this.Remove(timerId))
                {
                    tcs.SetResult(false);
                }
            }

            bool ret;

            try
            {
                cancellationToken?.Add(CancelAction);
                ret = await tcs;
            }
            finally
            {
                cancellationToken?.Remove(CancelAction);
            }
            return(ret);
        }