From 73fd174b1ef014f5fbaecd0a841ded9b6663d5b2 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 19 May 2023 15:46:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=A7=E5=90=8C=E6=AD=A5=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E5=8F=91=E9=80=81=E5=B8=A7=E5=8F=8A=E7=A9=BA?= =?UTF-8?q?=E5=8C=85=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component/components/lockstep.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/component/components/lockstep.go b/component/components/lockstep.go index b2d4f0e..fd0337d 100644 --- a/component/components/lockstep.go +++ b/component/components/lockstep.go @@ -3,10 +3,8 @@ package components import ( "encoding/json" "github.com/kercylan98/minotaur/component" - "github.com/kercylan98/minotaur/utils/log" "github.com/kercylan98/minotaur/utils/synchronization" "github.com/kercylan98/minotaur/utils/timer" - "go.uber.org/zap" "sync" "time" ) @@ -64,17 +62,27 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() { frames := slf.frames.Map() for clientId, client := range slf.clients.Map() { - for i := slf.clientCurrentFrame[clientId]; i <= currentFrame; i++ { - if err := client.SyncSend(slf.serialization(i, frames[i])); err != nil { - log.Error("Lockstep.StartBroadcast", zap.Any("ClientID", client.GetID()), zap.Int("Frame", i), zap.Error(err)) - break - } - slf.clientCurrentFrame[clientId] = i + var i = slf.clientCurrentFrame[clientId] + for ; i < currentFrame; i++ { + client.Send(slf.serialization(i, frames[i])) } + slf.clientCurrentFrame[clientId] = i + } }) } +func (slf *Lockstep[ClientID, Command]) Stop() { + slf.ticker.StopTimer("lockstep") + slf.frameMutex.Lock() + slf.currentFrame = 0 + for key := range slf.clientCurrentFrame { + delete(slf.clientCurrentFrame, key) + } + slf.frames.Clear() + slf.frameMutex.Unlock() +} + func (slf *Lockstep[ClientID, Command]) AddCommand(command Command) { slf.frames.AtomGetSet(slf.currentFrame, func(value []Command, exist bool) (newValue []Command, isSet bool) { return append(value, command), true