帧同步修复重复发送帧及空包的bug
This commit is contained in:
parent
ba252155ff
commit
73fd174b1e
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue