1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-15 22:56:30 +09:00

Remote time diff now initialized as part of handshake

This commit is contained in:
lidgren
2010-12-23 12:24:01 +00:00
parent e2b66aca41
commit 3aa9fc0836
2 changed files with 23 additions and 3 deletions

View File

@@ -82,6 +82,7 @@ namespace Lidgren.Network
om.m_messageType = NetMessageType.Connect;
om.Write(m_peerConfiguration.AppIdentifier);
om.Write(m_peer.m_uniqueIdentifier);
om.Write((float)NetTime.Now);
WriteLocalHail(om);
@@ -97,6 +98,7 @@ namespace Lidgren.Network
om.m_messageType = NetMessageType.ConnectResponse;
om.Write(m_peerConfiguration.AppIdentifier);
om.Write(m_peer.m_uniqueIdentifier);
om.Write((float)NetTime.Now);
WriteLocalHail(om);
@@ -136,6 +138,7 @@ namespace Lidgren.Network
{
NetOutgoingMessage om = m_peer.CreateMessage(0);
om.m_messageType = NetMessageType.ConnectionEstablished;
om.Write((float)NetTime.Now);
m_peer.SendLibrary(om, m_remoteEndpoint);
InitializePing();
@@ -285,6 +288,10 @@ namespace Lidgren.Network
break;
case NetConnectionStatus.RespondedConnect:
// awesome
NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
InitializeRemoteTimeOffset(msg.ReadSingle());
m_peer.AcceptConnection(this);
InitializePing();
SetStatus(NetConnectionStatus.Connected, "Connected to " + NetUtility.ToHexString(m_remoteUniqueIdentifier));
@@ -320,6 +327,7 @@ namespace Lidgren.Network
{
string remoteAppIdentifier = msg.ReadString();
long remoteUniqueIdentifier = msg.ReadInt64();
InitializeRemoteTimeOffset(msg.ReadSingle());
int remainingBytes = payloadLength - (msg.PositionInBytes - ptr);
if (remainingBytes > 0)

View File

@@ -16,12 +16,24 @@ namespace Lidgren.Network
/// Gets the current average roundtrip time in seconds
/// </summary>
public float AverageRoundtripTime { get { return m_averageRoundtripTime; } }
// this might happen more than once
internal void InitializeRemoteTimeOffset(float remoteSendTime)
{
m_remoteTimeOffset = (remoteSendTime + (m_averageRoundtripTime / 2.0)) - NetTime.Now;
}
/// <summary>
/// Gets local time value comparable to NetTime.Now from a remote value
/// </summary>
public double GetLocalTime(double remoteTimestamp)
{
return remoteTimestamp - m_remoteTimeOffset;
}
/// <summary>
/// Gets the remote time value for a local time value produced by NetTime.Now
/// </summary>
public double GetRemoteTime(double localTimestamp)
{
return localTimestamp + m_remoteTimeOffset;
@@ -92,14 +104,14 @@ namespace Lidgren.Network
{
m_remoteTimeOffset = diff;
m_averageRoundtripTime = rtt * 1.15f; // initially over-estimate
m_peer.LogDebug("Initiated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime) + " Server time is: " + (now + diff));
m_peer.LogDebug("Initiated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime) + " Remote time is: " + (now + diff));
}
else
{
m_averageRoundtripTime = (m_averageRoundtripTime * 0.7f) + (float)(rtt * 0.3f);
m_remoteTimeOffset = ((m_remoteTimeOffset * (double)(m_sentPingNumber - 1)) + diff) / (double)m_sentPingNumber;
m_peer.LogVerbose("Updated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime) + ", server time to " + (now + m_remoteTimeOffset) + " (ie. diff " + m_remoteTimeOffset + ")");
m_peer.LogVerbose("Updated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime) + ", remote time to " + (now + m_remoteTimeOffset) + " (ie. diff " + m_remoteTimeOffset + ")");
}
// update resend delay for all channels