public void OnMessageReceived(UserToken token, SocketModel sm) { switch (sm.command) { case TimeProtocol.CHECK_CREQ: //客户端发起对时请求 DelayCheckDTO delayCheckDTO = sm.GetMessage <DelayCheckDTO>(); if (delayCheckDTO.timeStamps.Count < delayCheckDTO.checkNum * 2) { delayCheckDTO.timeStamps.Add(DateTime.Now.Ticks); Send(token, TimeProtocol.CHECK_SRES, delayCheckDTO); } else { //对时次数够了,计算时差和延迟,这里注意最后一个时间戳是client发来的,因此需要再把现在的时间加上进行计算 delayCheckDTO.timeStamps.Add(DateTime.Now.Ticks); DelayAndFloating df = GetDelayAndFloatingEven(delayCheckDTO.timeStamps); userBiz.CacheDelayAndFloating(GetUserId(token), df); } break; case TimeProtocol.CHECK_CRES: //客户端的回应 DelayCheckDTO delayCheck = sm.GetMessage <DelayCheckDTO>(); if (delayCheck.timeStamps.Count <= delayCheck.checkNum * 2) { //对时次数不够,就继续对够 delayCheck.timeStamps.Add(DateTime.Now.Ticks); Send(token, TimeProtocol.CHECK_SREQ, delayCheck); } if (delayCheck.timeStamps.Count == delayCheck.checkNum * 2 + 1) { //对时次数够了,计算时差和延迟 DelayAndFloating df = GetDelayAndFloatingOdd(delayCheck.timeStamps); userBiz.CacheDelayAndFloating(GetUserId(token), df); } break; } }
private DelayAndFloating GetDelayAndFloatingOdd(List <long> timeStamps) { long delay = 0; long floating = 0; for (int i = timeStamps.Count - 1; i >= 2; i -= 2) { delay += (timeStamps[i] - timeStamps[i - 2]); floating += (timeStamps[i] - timeStamps[i - 1]); } delay /= ((timeStamps.Count - 1) / 2); floating = floating / (timeStamps.Count / 2) - delay;//server-client DelayAndFloating delayAndFloating = new DelayAndFloating(delay, floating); return(delayAndFloating); }
public void CacheDelayAndFloating(int userId, DelayAndFloating df) { if (df.delay < 0) { return; } if (!uIdToDelayFloating.ContainsKey(userId)) { uIdToDelayFloating.Add(userId, df); } else { uIdToDelayFloating[userId] = df; } }
/// <summary> /// 对时包总数为偶数的情况 /// </summary> /// <param name="timeStamps"></param> /// <returns></returns> private DelayAndFloating GetDelayAndFloatingEven(List <long> timeStamps) { long delay = 0; long floating = 0; for (int i = timeStamps.Count - 1; i >= 2; i -= 2) { delay += (timeStamps[i] - timeStamps[i - 2]); } for (int j = timeStamps.Count - 1; j >= 1; j -= 2) { floating += (timeStamps[j] - timeStamps[j - 1]);//client-server } delay /= ((timeStamps.Count - 1) / 2); floating = floating / (timeStamps.Count / 2) - delay; DelayAndFloating delayAndFloating = new DelayAndFloating(delay, floating); return(delayAndFloating); }
/// <summary> /// 根据预测进行移动 /// </summary> /// <param name="token"></param> /// <param name="moveDTO"></param> private void Move(UserToken token, PosSyncDTO moveDTO) { DelayAndFloating delayAndFloating = userBiz.GetDelayAndFloating(token); //time: floating = server - client //server = floating + client long time = delayAndFloating.floating + moveDTO.timeStamp;//将client的发送时间,转换为server时间 float timeBtwClientSendAndNow = (DateTime.Now.Ticks - time) * 0.0000001f; float speed = instances[moveDTO.instanceId].speed; if (timeBtwClientSendAndNow < 0) { Console.WriteLine("time floating is negative : " + timeBtwClientSendAndNow.ToString()); DelayCheckDTO delayCheckDTO = new DelayCheckDTO(5); delayCheckDTO.timeStamps.Add(DateTime.Now.Ticks); Send(token, Protocol.Protocol.TYPE_TIME, area, TimeProtocol.CHECK_SREQ, delayCheckDTO); } else { Console.WriteLine("time floating is : " + timeBtwClientSendAndNow.ToString()); //TODO 如果位置与之前存储的位置相差太大,就发送位置校正消息 Vector3 posNow = GetFuturePos(new Vector3(moveDTO.x, moveDTO.y, moveDTO.z), new Vector3(moveDTO.dirX, moveDTO.dirY, moveDTO.dirZ), timeBtwClientSendAndNow, speed); if (!instancePos.ContainsKey(moveDTO.instanceId)) { instancePos.Add(moveDTO.instanceId, new EntityMoveInfo(posNow, true)); } else { instancePos[moveDTO.instanceId].cVector3.x = posNow.x; instancePos[moveDTO.instanceId].cVector3.y = posNow.y; instancePos[moveDTO.instanceId].cVector3.z = posNow.z; instancePos[moveDTO.instanceId].moveStatus = MoveStatus.Moving; } PosSyncDTO posSyncDTO = new PosSyncDTO(moveDTO.instanceId, posNow.x, posNow.y, posNow.z, moveDTO.dirX, moveDTO.dirY, moveDTO.dirZ, DateTime.Now.Ticks); brocast(FightProtocol.POS_SYNC_BRO, posSyncDTO, token); } }
public void CacheDelayAndFloating(int userId, DelayAndFloating delay) { userCache.CacheDelayAndFloating(userId, delay); }
public static MsgDelayAndFloating GetMsgDelayAndFloating(ushort msgId, DelayAndFloating df) { msgDelayAndFloating.SetMsgDelayAndFloating(msgId, df); return(msgDelayAndFloating); }
public void SetMsgDelayAndFloating(ushort msgId, DelayAndFloating delayAndFloating) { this.msgId = msgId; this.DelayFloating = delayAndFloating; }
public MsgDelayAndFloating(ushort msgId, DelayAndFloating df) { this.msgId = msgId; this.DelayFloating = df; }