//取下一帧执行 public FrameInfo NextTurn() { if (headIdx == -1) { return(null); } FrameInfo turn = null; turn = turnQ[headIdx]; if (turn == null) { if (tailIdx > headIdx) //缺帧,请求重发。否则只是新的帧还未到 { GameMsgSender.SendMissTurnReq((uint)curExecTurnID + 1); } } else { if (turn.frameNum != curExecTurnID + 1) { Debug.LogError("turn.frameNum != curExecTurnID + 1, Impossible!"); } turnQ[headIdx] = null; headIdx = WrapIncIndex(headIdx, 1); curExecTurnID = (int)turn.frameNum; } return(turn); }
//新的一帧到来 public void NewTurn(PlayFrame playFrame) { if (headIdx == -1) { headIdx = 0; } //计算需求的空间 int maxTurnID = (int)playFrame.frames[playFrame.frames.Count - 1].frameNum; int turnNum = maxTurnID - curExecTurnID; if (turnNum > turnQ.Capacity) //当前turnQ空间已经存不下,分配更多空间 { ReallocTurnQ(turnNum); } foreach (FrameInfo turn in playFrame.frames) { int turnID = (int)turn.frameNum; int diff = turnID - curServerTurnID; //帧编号差距 int index = WrapIncIndex(tailIdx, diff); //安放位置 turnQ[index] = turn; //安放 if (diff > 1) //丢包,请求重发 { List <uint> missedTurnIDs = new List <uint>(); for (int i = curServerTurnID + 1; i < turnID; ++i) { missedTurnIDs.Add((uint)i); } GameMsgSender.SendMissTurnReq(missedTurnIDs); } if (diff > 0) //非补帧 { tailIdx = index; curServerTurnID = (int)turn.frameNum; } //Logger.Log("Receive new turn: " + info.frameNum + " " + Time.time); /* * foreach (ClientInput input in turn.info) * { * Logger.Log(string.Format("New turn:{0} Acc:{1} Dir:{2} Cmd:{3}", * // Logger.Log("Input: " + input.acc_id + " " + input.dir + " " + input.cmd); * } * //*/ //通知 if (onNewTurn != null) { onNewTurn(turn); } } CalcAverageTurnInterval(Time.time); }