From 2239e336f56a13cdfaf6e8073e008efd361b8bed Mon Sep 17 00:00:00 2001 From: teascade Date: Tue, 9 May 2017 08:15:24 +0300 Subject: [PATCH] Add timestamps to movement packets --- Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs | 7 +++++++ Assets/Scripts/Networking/NetworkHelper.cs | 10 ++++++++++ Assets/Scripts/Networking/Serverside/Server.cs | 1 + 3 files changed, 18 insertions(+) diff --git a/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs b/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs index cc17820..92298bd 100644 --- a/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs +++ b/Assets/Scripts/Networking/Messages/MoveCreaturePkt.cs @@ -19,6 +19,11 @@ namespace Cyber.Networking.Messages { /// public int SyncBaseID; + /// + /// Time when server received this request. Used for compensating ping. + /// + public double Timestamp; + /// /// Creates a MoveCreaturePkt which contains the direction of desired movement (or (0, 0, 0) for stopping) /// @@ -43,6 +48,7 @@ namespace Cyber.Networking.Messages { public override void Deserialize(NetworkReader reader) { Direction = reader.ReadVector3(); SyncBaseID = reader.ReadInt32(); + Timestamp = reader.ReadDouble(); } /// @@ -52,6 +58,7 @@ namespace Cyber.Networking.Messages { public override void Serialize(NetworkWriter writer) { writer.Write(Direction); writer.Write(SyncBaseID); + writer.Write(Timestamp); } } diff --git a/Assets/Scripts/Networking/NetworkHelper.cs b/Assets/Scripts/Networking/NetworkHelper.cs index 4f7ab0f..cc12fa3 100644 --- a/Assets/Scripts/Networking/NetworkHelper.cs +++ b/Assets/Scripts/Networking/NetworkHelper.cs @@ -51,5 +51,15 @@ namespace Cyber.Networking { return IntArray; } + /// + /// Returns the current system time in seconds. + /// + /// The system time in seconds. + public static double GetCurrentSystemTime() { + return DateTime.Now.ToUniversalTime().Subtract( + new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)) + .TotalMilliseconds * 1.0 / 1000; + } + } } diff --git a/Assets/Scripts/Networking/Serverside/Server.cs b/Assets/Scripts/Networking/Serverside/Server.cs index e63444e..6d3e9ef 100644 --- a/Assets/Scripts/Networking/Serverside/Server.cs +++ b/Assets/Scripts/Networking/Serverside/Server.cs @@ -141,6 +141,7 @@ namespace Cyber.Networking.Serverside { if (Player.Value.ConnectionID == msg.conn.connectionId) { continue; } + MoveCreature.Timestamp = NetworkHelper.GetCurrentSystemTime(); NetworkServer.SendToClient(Player.Value.ConnectionID, PktType.MoveCreature, MoveCreature); } break;