From e0ae331d178d9971f34bd93d14903beb8ef65a04 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 8 Sep 2020 17:36:26 +0800 Subject: [PATCH] 1 --- timer.go | 37 ++++++++++++++++++++++++++----------- timerlist.go | 19 +++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/timer.go b/timer.go index b761ace..aea3566 100644 --- a/timer.go +++ b/timer.go @@ -213,22 +213,37 @@ func (this *Timer) modifyTimerEx(timerList *TimerList, milliSeconds int32, isFir } func (this *Timer) DeleteTimer(timerList *TimerList) { + if timerList == nil { + return + } + if this.runningTimer == timerList { + this.runningTimer = nil + } + if timerList.timerAfterFunc != nil { + timerList.timerAfterFunc(&timerList.params) + } + this.detachTimer(timerList) + this.addToFreeList(timerList) } func (this *Timer) GetTimerByAtttach(attachEntry *ListHead) *TimerList { return nil } -func (this *Timer) MutableParams(timerList *TimerList) *XParams { - return nil -} - func (this *Timer) GetRemainTime(timerList *TimerList) int64 { - return 0 + if timerList == nil { + return 0 + } + remainTime := timerList.expires - this.getTickCount(this.context) + if remainTime < 0 { + return 0 + } else { + return remainTime + } } func (this *Timer) GetRunningTimer() *TimerList { - return nil + return this.runningTimer } func (this *Timer) GetIdleMilliSeconds() int64 { @@ -264,9 +279,9 @@ func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 { return index } -func (this *Timer) internalAddTimer(timer_list *TimerList) { - timer_list.entry.data = timer_list - expires := timer_list.expires +func (this *Timer) internalAddTimer(timerList *TimerList) { + timerList.entry.data = timerList + expires := timerList.expires idx := expires - this.timerTick var vec *ListHead var index uint32 @@ -290,7 +305,7 @@ func (this *Timer) internalAddTimer(timer_list *TimerList) { index = (uint32)((expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK) vec = &this.tv5[index] } - vec.AddTail(&timer_list.entry) + vec.AddTail(&timerList.entry) } func (this *Timer) getTimerIndex(index uint32) uint32 { @@ -304,7 +319,7 @@ func (this *Timer) newTimerList() *TimerList { return timerList } else { timerList := new(TimerList) - timerList.entry.Init() + timerList.Init() return timerList } } diff --git a/timerlist.go b/timerlist.go index d808489..75fcac2 100644 --- a/timerlist.go +++ b/timerlist.go @@ -1,5 +1,19 @@ package q5 +type TimerAttacher struct { + timer *Timer + timers ListHead +} + +func (this *TimerAttacher) ClearTimers() { + var workList ListHead + this.timers.ReplaceInit(&workList) + for !workList.Empty() { + timerList := workList.FirstEntry().(*TimerList) + this.timer.DeleteTimer(timerList) + } +} + type TimerList struct { entry ListHead attachEntry ListHead @@ -42,3 +56,8 @@ func (this *TimerList) Init() { func (this *TimerList) SetTimerAfterFunc(timerAfterFunc func(*XParams)) { this.timerAfterFunc = timerAfterFunc } + +func (this *TimerList) Attach(timerAttacher *TimerAttacher) { + this.attachEntry.data = this + timerAttacher.timers.AddTail(&this.attachEntry) +}