游戏方面NTP时间同步相关知识

示意图

在这里插入图片描述

protobuff包定义

message SceneClocksyncRequest
{
	int64	originate_timestamp = 1;客户端发送请求的时间T1
}

message SceneClocksyncRet
{
    int64   originate_timestamp = 1;客户端发送请求的时间 T1
    int64   receive_timestamp   = 2;服务器接收请求的时间 T2
    int64   scene_timestamp     = 3;场景同步时间戳【不参与NPT计算】
    int64   transmit_timestamp  = 4;包离开场景时候的时间 T3
}

计算方法

在这里插入图片描述

--destination_timestamp          T4        客户端接到返回包时间 T4其实就是当前的时间看图也应该明白
--网络延时d = (T2 - T1) + (T4 - T3)
--服务器与客户端的时差 t = [(T2 - T1) + (T3 - T4)] / 2

具体代码实现

    -- 同步请求
	local function SendClockSyncReqMsg()
    local sceneClocksyncRequest =
     {
        originate_timestamp = serviceCore.getClockRealtime()
    }

    serviceCore.send(m_channelID,"SceneClocksyncRequest",sceneClocksyncRequest)
end

    -- 同步反馈
	function robotStateMachine.SceneClocksyncRet(proto)
	    --proto.originate_timestamp      T1        客户端发送请求的时间
	    --proto.receive_timestamp        T2        服务器接收请求的时间
	    --proto.transmit_timestamp       T3        包离开场景时候的时间
	    --destination_timestamp          T4        客户端接到返回包时间
	    --网络延时d = (T2 - T1) + (T4 - T3)
	    --服务器与客户端的时差 t = [(T2 - T1) + (T3 - T4)] / 2
	    local destination_timestamp = serviceCore.getClockRealtime()
	
	    m_nNetDelay = (proto.receive_timestamp - proto.originate_timestamp) + ((destination_timestamp - proto.transmit_timestamp))
	    m_nDiftime = ((proto.receive_timestamp - proto.originate_timestamp) + (proto.transmit_timestamp - destination_timestamp) / 2)
	end
	
	-- 真正算出来的同步时间
	function GetServerTime()
		return serviceCore.getClockRealtime() + m_nNetDelay + m_nDiftime
	end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值