Skip to content

Commit

Permalink
Merge pull request #110 from inworld-ai/yj380
Browse files Browse the repository at this point in the history
INTG-2356 1. Send to server by its own timestamp. 2. Add local time offset
  • Loading branch information
Juniverse authored Jan 10, 2025
2 parents 98b580d + b71c933 commit 81c2c19
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
10 changes: 9 additions & 1 deletion Runtime/Scripts/Data/ScriptableObjects/InworldAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ public static bool IsDebugMode
/// </summary>
public static Capabilities Capabilities
{
get => Instance.m_Capabilities;
get
{
Instance.m_Capabilities.logs = IsDebugMode;
Instance.m_Capabilities.logs_debug = IsDebugMode;
Instance.m_Capabilities.logs_warning = IsDebugMode;
Instance.m_Capabilities.logs_info = IsDebugMode;
return Instance.m_Capabilities;
}
set => Instance.m_Capabilities = value;
}

/// <summary>
/// The Input Action Asset that defines all the input controls utilized by the SDK.
/// </summary>
Expand Down
9 changes: 5 additions & 4 deletions Runtime/Scripts/InworldClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,9 @@ public virtual void SendPerceivedLatencyReport(float latencyPerceived, Precision
/// <summary>
/// Send PingPong Response for latency Test.
/// </summary>
/// <param name="packetID"></param>
public virtual void SendLatencyTestResponse(PacketId packetID)
/// <param name="packetID">the ping packet's packet ID.</param>
/// <param name="packetTimeStamp">the ping packet's timestamp</param>
public virtual void SendLatencyTestResponse(PacketId packetID, string packetTimeStamp)
{
LatencyReportPacket latencyReport = new LatencyReportPacket
{
Expand All @@ -508,7 +509,7 @@ public virtual void SendLatencyTestResponse(PacketId packetID)
{
type = PingPongType.PONG,
pingPacketId = packetID,
pingTimestamp = InworldDateTime.UtcNow
pingTimestamp = packetTimeStamp
}
}
};
Expand Down Expand Up @@ -1165,7 +1166,7 @@ bool _HandleRawPackets(InworldPacket receivedPacket)
if (latencyReportPacket.latencyReport is PingPongEvent)
{
m_PingpongLatency = InworldDateTime.ToLatency(receivedPacket.timestamp);
SendLatencyTestResponse(latencyReportPacket.packetId);
SendLatencyTestResponse(latencyReportPacket.packetId, receivedPacket.timestamp);
}
return false;
}
Expand Down
8 changes: 7 additions & 1 deletion Runtime/Scripts/Util/InworldDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Duration
}
public static class InworldDateTime
{
static TimeSpan s_TimeSpan = TimeSpan.Zero;
// YAN: In Unity we use the first format.
// And server will return the format with 9 digits.
// However, DotNet can only process 7 digits at most.
Expand Down Expand Up @@ -56,7 +57,12 @@ out DateTime outTime
public static int ToLatency(string timeStamp)
{
DateTime receivedTime = ToDateTime(timeStamp);
TimeSpan delta = DateTime.UtcNow - receivedTime;
if (s_TimeSpan == TimeSpan.Zero)
{
s_TimeSpan = DateTime.UtcNow - receivedTime;
return 20;
}
TimeSpan delta = DateTime.UtcNow - s_TimeSpan - receivedTime;
// YAN: Sometimes result can be even smaller than 0, due to the clock skew.
// < 20ms is not able to be perceived.
int result = delta.Seconds * 1000 + delta.Milliseconds;
Expand Down

0 comments on commit 81c2c19

Please sign in to comment.