Skip to content

Commit

Permalink
[+] 时配网络延迟bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JiepengTan committed Sep 6, 2019
1 parent 53299b0 commit 07bae21
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 74 deletions.
45 changes: 14 additions & 31 deletions Server/Src/SimpleServer/Src/Server/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Lockstep.Game;
using Lockstep.Logging;
using Lockstep.Math;
using Lockstep.Serialization;
Expand Down Expand Up @@ -167,6 +168,7 @@ public void DoUpdate(float deltaTime){
_timeSinceLoaded += deltaTime;
_waitTimer += deltaTime;
if (State != EGameState.Playing) return;
if(_gameStartTimestampMs <=0) return;
while (Tick < _tickSinceGameStart) {
_CheckBorderServerFrame(true);
}
Expand All @@ -191,29 +193,6 @@ private bool _CheckBorderServerFrame(bool isForce = false){
if (State != EGameState.Playing) return false;
var frame = GetOrCreateFrame(Tick);
var inputs = frame.Inputs;
//超时等待 移除超时玩家
//if (_waitTimer > (NetworkDefine.MAX_DELAY_TIME_MS / 1000.0f)) {
// _waitTimer = 0;
// //移除还没有到来的帧的Player
// for (int i = 0; i < inputs.Length; i++) {
// if (inputs[i] == null) {
// if (Players[i] != null) {
// Log($"Overtime wait remove localId = {i}");
// }
//
// _allNeedWaitInputPlayerIds.Remove((byte) i);
// }
// }
//}

//if (!isForce) {
// //是否所有的输入 都已经等到
// foreach (var id in _allNeedWaitInputPlayerIds) {
// if (inputs[id] == null) {
// return false;
// }
// }
//}
if (!isForce) {
//是否所有的输入 都已经等到
for (int i = 0; i < inputs.Length; i++) {
Expand All @@ -226,11 +205,14 @@ private bool _CheckBorderServerFrame(bool isForce = false){
//将所有未到的包 给予默认的输入
for (int i = 0; i < inputs.Length; i++) {
if (inputs[i] == null) {
inputs[i] = new Msg_PlayerInput(Tick, (byte) i);
inputs[i] = new Msg_PlayerInput(Tick, (byte) i,new InputCmd[]{
new InputCmd() {
content = new PlayerInput().ToBytes(),
}});
}
}

//Debug.Log("Border input " + Tick);
//Debug.Log($" Border input {Tick} isUpdate:{isForce} _tickSinceGameStart:{_tickSinceGameStart}");
var msg = new Msg_ServerFrames();
int count = Tick < 2 ? Tick + 1 : 3;
var frames = new ServerFrame[count];
Expand All @@ -245,6 +227,10 @@ private bool _CheckBorderServerFrame(bool isForce = false){
_firstFrameTimeStamp = _timeSinceLoaded;
}

if (_gameStartTimestampMs < 0) {
_gameStartTimestampMs = LTime.realtimeSinceStartupMS + NetworkDefine.UPDATE_DELTATIME * _ServerTickDealy;
}

Tick++;
return true;
}
Expand Down Expand Up @@ -521,8 +507,8 @@ public void OnNetMsg(Player player, ushort opcode, BaseMsg msg){
}
}

public long _gameStartTimestampMs;
public int _ServerTickDealy = 2;
public long _gameStartTimestampMs = -1;
public int _ServerTickDealy = 0;

public int _tickSinceGameStart =>
(int) ((LTime.realtimeSinceStartupMS - _gameStartTimestampMs) / NetworkDefine.UPDATE_DELTATIME);
Expand All @@ -532,8 +518,6 @@ void C2G_PlayerInput(Player player, BaseMsg data){
if (State == EGameState.PartLoaded) {
Log("First input: game start playing");
State = EGameState.Playing;
_gameStartTimestampMs =
LTime.realtimeSinceStartupMS + NetworkDefine.UPDATE_DELTATIME * _ServerTickDealy;
}

var input = data as Msg_PlayerInput;
Expand All @@ -560,8 +544,7 @@ void C2G_PlayerInput(Player player, BaseMsg data){
}

frame.Inputs[id] = input;

while (_CheckBorderServerFrame(false)) { }
_CheckBorderServerFrame(false);
}

ServerFrame GetOrCreateFrame(int tick){
Expand Down
2 changes: 1 addition & 1 deletion Server/Src/SimpleServer/Src/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Server : IMessageDispatcher {
private NetOuterProxy _netProxy = new NetOuterProxy();

//update
private const double UpdateInterval = 0.015; //frame rate = 30
private const double UpdateInterval = NetworkDefine.UPDATE_DELTATIME /1000.0f; //frame rate = 30
private DateTime _lastUpdateTimeStamp;
private DateTime _startUpTimeStamp;
private double _deltaTime;
Expand Down
Binary file modified Unity/Assets/Scenes/Demo.unity
Binary file not shown.
2 changes: 1 addition & 1 deletion Unity/Assets/Scripts/Logic/EntityComponent/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void DoUpdate(LFloat deltaTime){
}

public bool Fire(int idx = 0){
return skillBox.Fire(idx);
return skillBox.Fire(idx-1);
}

public void StopSkill(int idx = -1){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected override void BindRef(){
}
public override void DoUpdate(LFloat deltaTime){
base.DoUpdate(deltaTime);
if (input.skillId != -1) {
if (input.skillId != 0) {
Fire(input.skillId);
}
}
Expand Down
5 changes: 4 additions & 1 deletion Unity/Assets/Scripts/Logic/Framework/Simulator/DumpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public DumpHelper(IServiceContainer serviceContainer, World world) : base(servic

private string dumpAllPath => "/Users/jiepengtan/Projects/Tutorial/LockstepTutorial/DumpLog";
private HashHelper _hashHelper;

public bool enable = false;
public void DumpFrame(bool isNewFrame){
if(!enable) return;
var data = DumpFrame();
if (isNewFrame) {
_tick2RawFrameData[Tick] = data;
Expand All @@ -30,6 +31,7 @@ public void DumpFrame(bool isNewFrame){
}

public void DumpToFile(){
if(!enable) return;
#if UNITY_EDITOR
var path = dumpPath + "/cur.txt";
var dir = Path.GetDirectoryName(path);
Expand All @@ -51,6 +53,7 @@ public void DumpToFile(){
}

public void DumpAll(){
if(!enable) return;
var path = dumpAllPath + "/cur.txt";
var dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir)) {
Expand Down
10 changes: 5 additions & 5 deletions Unity/Assets/Scripts/Logic/Framework/Simulator/FrameBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IFrameBuffer {
void SetClientTick(int tick);
void SendInput(Msg_PlayerInput input);

void DoUpdate(float deltaTime);
void DoUpdate(float deltaTime,int worldTick);
int NextTickToCheck { get; }
int MaxServerTickInBuffer { get; }
bool IsNeedRollback { get; }
Expand Down Expand Up @@ -87,11 +87,10 @@ public void PushMissServerFrames(ServerFrame[] frames, bool isNeedDebugCheck = t
}

public void PushServerFrames(ServerFrame[] frames, bool isNeedDebugCheck = true){
//Debug.Log("PushServerFrames");
var count = frames.Length;
for (int i = 0; i < count; i++) {
var data = frames[i];

//Debug.Log("PushServerFrames" + data.tick);
if (_tick2SendTimestamp.TryGetValue(data.tick, out var sendTick)) {
var delay = LTime.realtimeSinceStartupMS - sendTick;
_delays.Add(delay);
Expand All @@ -112,6 +111,7 @@ public void PushServerFrames(ServerFrame[] frames, bool isNeedDebugCheck = true)
return;
}

//Debug.Log("PushServerFramesSucc" + data.tick);
if (data.tick > MaxServerTickInBuffer) {
MaxServerTickInBuffer = data.tick;
}
Expand All @@ -123,13 +123,13 @@ public void PushServerFrames(ServerFrame[] frames, bool isNeedDebugCheck = true)
}
}

public void DoUpdate(float deltaTime){
public void DoUpdate(float deltaTime,int worldTick){
UpdatePingVal(deltaTime);

//Debug.Assert(nextTickToCheck <= nextClientTick, "localServerTick <= localClientTick ");
//Confirm frames
IsNeedRollback = false;
while (NextTickToCheck <= MaxServerTickInBuffer) {
while (NextTickToCheck <= MaxServerTickInBuffer && NextTickToCheck<worldTick) {
var sIdx = NextTickToCheck % _bufferSize;
var cFrame = _clientBuffer[sIdx];
var sFrame = _serverBuffer[sIdx];
Expand Down
4 changes: 2 additions & 2 deletions Unity/Assets/Scripts/Logic/Framework/Simulator/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public class World : BaseSystem {
private bool _hasStart = false;


public void RollbackTo(int tick, int missFrameTick, bool isNeedClear = true){
public void RollbackTo(int tick, int maxContinueServerTick, bool isNeedClear = true){
if (tick < 0) {
Debug.LogError("Target Tick invalid!" + tick);
return;
}

Debug.Log($" curTick {Tick} RevertTo {tick} {missFrameTick} {isNeedClear}");
Debug.Log($" Rollback diff:{Tick - tick} From{Tick}->{tick} maxContinueServerTick:{maxContinueServerTick} {isNeedClear}");
_timeMachineService.RollbackTo(tick);
_commonStateService.SetTick(tick);
Tick = tick;
Expand Down
Loading

0 comments on commit 07bae21

Please sign in to comment.