Documentation ¶
Index ¶
- type Client
- type Lockstep
- func (slf *Lockstep[ClientID, Command]) AddCommand(command Command)
- func (slf *Lockstep[ClientID, Command]) GetClientCurrentFrame(clientId ClientID) int
- func (slf *Lockstep[ClientID, Command]) GetCurrentFrame() int
- func (slf *Lockstep[ClientID, Command]) GetFrameLimit() int
- func (slf *Lockstep[ClientID, Command]) GetFrames() [][]Command
- func (slf *Lockstep[ClientID, Command]) JoinClient(client Client[ClientID])
- func (slf *Lockstep[ClientID, Command]) JoinClientWithFrame(client Client[ClientID], frameIndex int)
- func (slf *Lockstep[ClientID, Command]) LeaveClient(clientId ClientID)
- func (slf *Lockstep[ClientID, Command]) OnLockstepStoppedEvent()
- func (slf *Lockstep[ClientID, Command]) RegLockstepStoppedEvent(handle component.LockstepStoppedEventHandle[ClientID, Command])
- func (slf *Lockstep[ClientID, Command]) StartBroadcast()
- func (slf *Lockstep[ClientID, Command]) StopBroadcast()
- type Option
- func WithFrameLimit[ClientID comparable, Command any](frameLimit int) Option[ClientID, Command]
- func WithFrameRate[ClientID comparable, Command any](frameRate int) Option[ClientID, Command]
- func WithSerialization[ClientID comparable, Command any](handle func(frame int, commands []Command) server.Packet) Option[ClientID, Command]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client[ID comparable] interface { // GetID 用户玩家ID GetID() ID // Write 写入数据包 Write(packet server.Packet) }
Client 帧同步客户端接口定义
- 客户端应该具备ID及写入数据包的实现
type Lockstep ¶
type Lockstep[ClientID comparable, Command any] struct { // contains filtered or unexported fields }
Lockstep 锁步(帧)同步默认实现
- 支持最大帧上限 WithFrameLimit
- 自定逻辑帧频率,默认为每秒15帧(帧/66ms) WithFrameRate
- 自定帧序列化方式 WithSerialization
- 从特定帧开始追帧
- 兼容各种基于TCP/UDP/Unix的网络类型,可通过客户端实现其他网络类型同步
func NewLockstep ¶
func NewLockstep[ClientID comparable, Command any](options ...Option[ClientID, Command]) *Lockstep[ClientID, Command]
NewLockstep 创建一个锁步(帧)同步默认实现的组件(Lockstep)进行返回
func (*Lockstep[ClientID, Command]) AddCommand ¶
func (slf *Lockstep[ClientID, Command]) AddCommand(command Command)
AddCommand 添加命令到当前帧
func (*Lockstep[ClientID, Command]) GetClientCurrentFrame ¶
GetClientCurrentFrame 获取客户端当前帧
func (*Lockstep[ClientID, Command]) GetCurrentFrame ¶
GetCurrentFrame 获取当前帧
func (*Lockstep[ClientID, Command]) GetFrames ¶
func (slf *Lockstep[ClientID, Command]) GetFrames() [][]Command
GetFrames 获取所有帧数据
func (*Lockstep[ClientID, Command]) JoinClient ¶
JoinClient 加入客户端到广播队列中
func (*Lockstep[ClientID, Command]) JoinClientWithFrame ¶
func (slf *Lockstep[ClientID, Command]) JoinClientWithFrame(client Client[ClientID], frameIndex int)
JoinClientWithFrame 加入客户端到广播队列中,并从特定帧开始追帧
- 可用于重连及状态同步、帧同步混用的情况
- 混用:服务端记录指令时同时做一次状态计算,新客户端加入时直接同步当前状态,之后从特定帧开始广播
func (*Lockstep[ClientID, Command]) LeaveClient ¶
func (slf *Lockstep[ClientID, Command]) LeaveClient(clientId ClientID)
LeaveClient 将客户端从广播队列中移除
func (*Lockstep[ClientID, Command]) OnLockstepStoppedEvent ¶
func (slf *Lockstep[ClientID, Command]) OnLockstepStoppedEvent()
func (*Lockstep[ClientID, Command]) RegLockstepStoppedEvent ¶
func (slf *Lockstep[ClientID, Command]) RegLockstepStoppedEvent(handle component.LockstepStoppedEventHandle[ClientID, Command])
RegLockstepStoppedEvent 当广播停止时将触发被注册的事件处理函数
func (*Lockstep[ClientID, Command]) StartBroadcast ¶
func (slf *Lockstep[ClientID, Command]) StartBroadcast()
StartBroadcast 开始广播
- 在开始广播后将持续按照设定的帧率进行帧数推进,并在每一帧推进时向客户端进行同步,需提前将客户端加入广播队列 JoinClient
- 广播过程中使用 AddCommand 将该帧数据追加到当前帧中
func (*Lockstep[ClientID, Command]) StopBroadcast ¶
func (slf *Lockstep[ClientID, Command]) StopBroadcast()
StopBroadcast 停止广播
type Option ¶
type Option[ClientID comparable, Command any] func(lockstep *Lockstep[ClientID, Command])
func WithFrameLimit ¶
func WithFrameLimit[ClientID comparable, Command any](frameLimit int) Option[ClientID, Command]
WithFrameLimit 通过特定逻辑帧上限创建锁步(帧)同步组件
- 当达到上限时将停止广播
func WithFrameRate ¶
func WithFrameRate[ClientID comparable, Command any](frameRate int) Option[ClientID, Command]
WithFrameRate 通过特定逻辑帧率创建锁步(帧)同步组件
- 默认情况下为 15/s
func WithSerialization ¶
func WithSerialization[ClientID comparable, Command any](handle func(frame int, commands []Command) server.Packet) Option[ClientID, Command]
WithSerialization 通过特定的序列化方式将每一帧的数据进行序列化
默认情况下为将被序列化为以下结构体的JSON字符串
type Frame struct { Frame int `json:"frame"` Commands []Command `json:"commands"` }
Click to show internal directories.
Click to hide internal directories.