fix: lockstep 定时器导致空指针问题处理

This commit is contained in:
kercylan98 2023-12-23 20:02:34 +08:00
parent d27fa7c246
commit ceffa2e46f
1 changed files with 14 additions and 2 deletions

View File

@ -128,7 +128,18 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() {
slf.currentFrame = slf.initFrame slf.currentFrame = slf.initFrame
go func(ls *Lockstep[ClientID, Command]) { go func(ls *Lockstep[ClientID, Command]) {
for range ls.ticker.C { for {
ls.runningLock.RLock()
if ls.ticker == nil || !ls.running {
ls.runningLock.RUnlock()
break
}
_, ok := <-ls.ticker.C
ls.runningLock.RUnlock()
if !ok {
break
}
go func(ls *Lockstep[ClientID, Command]) { go func(ls *Lockstep[ClientID, Command]) {
ls.currentFrameLock.RLock() ls.currentFrameLock.RLock()
if ls.frameLimit > 0 && ls.currentFrame >= ls.frameLimit { if ls.frameLimit > 0 && ls.currentFrame >= ls.frameLimit {
@ -165,6 +176,7 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() {
ls.clientFrame[clientId] = currentFrame ls.clientFrame[clientId] = currentFrame
} }
}(ls) }(ls)
} }
}(slf) }(slf)
} }
@ -177,12 +189,12 @@ func (slf *Lockstep[ClientID, Command]) StopBroadcast() {
return return
} }
slf.running = false slf.running = false
slf.runningLock.Unlock()
if slf.ticker != nil { if slf.ticker != nil {
slf.ticker.Stop() slf.ticker.Stop()
} }
slf.ticker = nil slf.ticker = nil
slf.runningLock.Unlock()
slf.OnLockstepStoppedEvent() slf.OnLockstepStoppedEvent()