帧同步修复重复发送帧及空包的bug

This commit is contained in:
kercylan98 2023-05-19 15:46:52 +08:00
parent ba252155ff
commit 73fd174b1e
1 changed files with 16 additions and 8 deletions

View File

@ -3,10 +3,8 @@ package components
import ( import (
"encoding/json" "encoding/json"
"github.com/kercylan98/minotaur/component" "github.com/kercylan98/minotaur/component"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/synchronization" "github.com/kercylan98/minotaur/utils/synchronization"
"github.com/kercylan98/minotaur/utils/timer" "github.com/kercylan98/minotaur/utils/timer"
"go.uber.org/zap"
"sync" "sync"
"time" "time"
) )
@ -64,17 +62,27 @@ func (slf *Lockstep[ClientID, Command]) StartBroadcast() {
frames := slf.frames.Map() frames := slf.frames.Map()
for clientId, client := range slf.clients.Map() { for clientId, client := range slf.clients.Map() {
for i := slf.clientCurrentFrame[clientId]; i <= currentFrame; i++ { var i = slf.clientCurrentFrame[clientId]
if err := client.SyncSend(slf.serialization(i, frames[i])); err != nil { for ; i < currentFrame; i++ {
log.Error("Lockstep.StartBroadcast", zap.Any("ClientID", client.GetID()), zap.Int("Frame", i), zap.Error(err)) client.Send(slf.serialization(i, frames[i]))
break
} }
slf.clientCurrentFrame[clientId] = 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) { func (slf *Lockstep[ClientID, Command]) AddCommand(command Command) {
slf.frames.AtomGetSet(slf.currentFrame, func(value []Command, exist bool) (newValue []Command, isSet bool) { slf.frames.AtomGetSet(slf.currentFrame, func(value []Command, exist bool) (newValue []Command, isSet bool) {
return append(value, command), true return append(value, command), true