From ceffa2e46fcf7aa246345af5a12e0c95fbaa50ab Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Sat, 23 Dec 2023 20:02:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20lockstep=20=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=A9=BA=E6=8C=87=E9=92=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/lockstep/lockstep.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server/lockstep/lockstep.go b/server/lockstep/lockstep.go index 640c093..e968111 100644 --- a/server/lockstep/lockstep.go +++ b/server/lockstep/lockstep.go @@ -128,7 +128,18 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() { slf.currentFrame = slf.initFrame 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]) { ls.currentFrameLock.RLock() if ls.frameLimit > 0 && ls.currentFrame >= ls.frameLimit { @@ -165,6 +176,7 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() { ls.clientFrame[clientId] = currentFrame } }(ls) + } }(slf) } @@ -177,12 +189,12 @@ func (slf *Lockstep[ClientID, Command]) StopBroadcast() { return } slf.running = false - slf.runningLock.Unlock() if slf.ticker != nil { slf.ticker.Stop() } slf.ticker = nil + slf.runningLock.Unlock() slf.OnLockstepStoppedEvent()