示意图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210608192818152.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYxMDI2MA==,size_16,color_FFFFFF,t_70)
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
}
计算方法
![在这里插入图片描述](https://img-blog.csdnimg.cn/c75a493f293f46069e3a1b99f17058fb.png)
--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)
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