From d1c788bb060852b0e1606d965e6cc5a6cc0a3f73 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 27 Aug 2020 23:24:51 +0800 Subject: [PATCH] 1 --- timer.go | 104 ++++++++++++++++++++++++++++++++++++++++++++++----- timerlist.go | 6 +-- xparams.go | 4 ++ 3 files changed, 99 insertions(+), 15 deletions(-) diff --git a/timer.go b/timer.go index 32b1fdd..b14566a 100644 --- a/timer.go +++ b/timer.go @@ -1,5 +1,7 @@ package q5 +import "time" + const CONFIG_BASE_SMALL = false const TVN_BITS = 6 const TVR_BITS = 8 @@ -132,7 +134,7 @@ func (this *Timer) Update() { func (this *Timer) AddDeadLineTimer( milli_seconds int32, init_func func (params* XParams), - timer_func func (params* XParams)) ITimerList { + timer_func func (params* XParams)) *TimerList { return this.AddDeadLineTimerEx(milli_seconds, init_func, timer_func, nil) } @@ -140,17 +142,23 @@ func (this *Timer) AddDeadLineTimerEx( milli_seconds int32, init_func func (params* XParams), timer_func func (params* XParams), - install_func func (timer_list ITimerList, params* XParams)) ITimerList { + install_func func (timerList *TimerList, params* XParams)) *TimerList { + timerList := this.newTimerList() + timerList.params.Reset() if init_func != nil { - init_func(new(XParams)) + init_func(&timerList.params) } - return nil + timerList.InitTimerList(this, DEADLINE_TIMER, milli_seconds, timer_func) + if install_func != nil { + install_func(timerList, &timerList.params) + } + return timerList } func (this *Timer) AddRepeatTimer( milli_seconds int32, init_func func (params* XParams), - timer_func func (params* XParams)) ITimerList { + timer_func func (params* XParams)) *TimerList { return this.AddRepeatTimerEx(milli_seconds, init_func, timer_func, nil) } @@ -158,23 +166,88 @@ func (this *Timer) AddRepeatTimerEx( milli_seconds int32, init_func func (params* XParams), timer_func func (params* XParams), - install_func func (timer_list ITimerList, params* XParams)) ITimerList { + install_func func (timer_list *TimerList, params* XParams)) *TimerList { + timerList := this.newTimerList() if init_func != nil { - init_func(new(XParams)) + init_func(&timerList.params) } + timerList.InitTimerList(this, REPEAT_TIMER, milli_seconds, timer_func) + if install_func != nil { + install_func(timerList, &timerList.params) + } + return timerList +} + +func (this *Timer) AddFixedTimer( + milli_seconds int32, + init_func func (params* XParams), + timer_func func (params* XParams)) *TimerList { + return this.AddFixedTimerEx(milli_seconds, init_func, timer_func, nil) +} + +func (this *Timer) AddFixedTimerEx( + milli_seconds int32, + init_func func (params* XParams), + timer_func func (params* XParams), + install_func func (timer_list *TimerList, params* XParams)) *TimerList { + timerList := this.newTimerList() + if init_func != nil { + init_func(&timerList.params) + } + timerList.InitTimerList(this, FIXED_TIMER, milli_seconds, timer_func) + if install_func != nil { + install_func(timerList, &timerList.params) + } + return timerList +} + +func (this *Timer) ModifyTimer(timerList *TimerList, milliSeconds int32) { + this.detachTimer(timerList) + timerList.milliSeconds = milliSeconds + if timerList.timerType == FIXED_TIMER { + tick := this.getTickCount() + nowTime := time.Now().Unix() + todayPassedSeconds := nowTime - GetDaySeconds(nowTime) + timerList.expires = (tick - todayPassedSeconds * 1000) + int64(milliSeconds) + if timerList.fixedTimierExecuteTimes > 0 { + if timerList.expires <= tick { + timerList.expires += 1000 * 3600 * 24 + } + } + } else { + timerList.expires = this.getTickCount() + int64(milliSeconds) + } + this.internalAddTimer(timerList) +} + +func (this *Timer) DeleteTimer(timerList *TimerList) { +} + +func (this *Timer) GetTimerByAtttach(attachEntry *ListHead) *TimerList { return nil } -func (this *Timer) ModifyTimer(timerList *TimerList, milli_seconds int32) { +func (this *Timer) MutableParams(timerList *TimerList) *XParams { + return nil +} +func (this *Timer) GetRemainTime(timerList *TimerList) int64 { + return 0 +} + +func (this *Timer) GetRunningTimer() *TimerList { + return nil } func (this *Timer) detachTimer(timerList *TimerList) { - + if !timerList.entry.Empty() { + timerList.entry.DelInit() + } } func (this *Timer) addToFreeList(timerList *TimerList) { - + this.freeTimer.AddTail(&timerList.entry) + this.freeTimerNum += 1 } func (this *Timer) cascade(tv *[TVN_SIZE]ListHead, index uint32) uint32 { @@ -225,6 +298,17 @@ func (this *Timer) getTimerIndex(index uint32) uint32 { return (uint32)((this.timerTick >> (TVR_BITS + index * TVN_BITS)) & TVN_MASK); } +func (this *Timer) newTimerList() *TimerList { + if !this.freeTimer.Empty() { + timerList := this.freeTimer.FirstEntry().(*TimerList) + timerList.entry.DelInit() + this.freeTimerNum -= 1 + return timerList + } else { + return new(TimerList) + } +} + func (this *Timer) gcTimerFunc(params *XParams) { for i := 0; !this.freeTimer.Empty() && this.freeTimerNum > this.cacheTimerNum && i < 1000; i += 1 { timerList := this.freeTimer.FirstEntry().(*TimerList) diff --git a/timerlist.go b/timerlist.go index 085d1ee..9895dfe 100644 --- a/timerlist.go +++ b/timerlist.go @@ -21,13 +21,9 @@ func (this *TimerList) InitTimerList( timer *Timer, timerType int8, millSeconds int32, - params XParams, - timerFunc func (params *XParams), - timerAfterFunc func (params *XParams)) { + timerFunc func (params *XParams)) { this.timerType = timerType this.milliSeconds = millSeconds this.fixedTimierExecuteTimes = 0 this.timerFunc = timerFunc - this.timerAfterFunc = timerAfterFunc - this.params = params } diff --git a/xparams.go b/xparams.go index 7136b68..f560117 100644 --- a/xparams.go +++ b/xparams.go @@ -7,6 +7,10 @@ type XParams struct { Param3 XValue } +func (this *XParams) Reset() *XParams { + return this +} + func (this *XParams) SetInt32(val int32) *XParams { return this }