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

major update; gen 3.5

This commit is contained in:
lidgren
2010-10-19 17:45:55 +00:00
parent baaa5926f8
commit 609bc1afe2
167 changed files with 4065 additions and 9640 deletions

View File

@@ -1,161 +1,71 @@
/* Copyright (c) 2010 Michael Lidgren
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System;
namespace Lidgren.Network
{
public partial class NetConnection
{
//
// Connection keepalive and latency calculation
//
internal float m_averageRoundtripTime = 0.05f;
private byte m_lastSentPingNumber;
private double m_lastPingSendTime;
private double m_nextPing;
internal double m_lastSendRespondedTo;
// remote + diff = local
// diff = local - remote
// local - diff = remote
internal double m_remoteToLocalNetTime = double.MinValue;
private float m_sentPingTime;
private int m_sentPingNumber;
private float m_averageRoundtripTime;
private float m_timeoutDeadline = float.MaxValue;
public float AverageRoundtripTime { get { return m_averageRoundtripTime; } }
internal void InitializeLatency(float rtt, float remoteNetTime)
internal void SendPing()
{
m_averageRoundtripTime = Math.Max(0.005f, rtt - 0.005f); // TODO: find out why initial ping always overshoots
m_nextPing = NetTime.Now + m_peerConfiguration.m_pingFrequency / 2.0f;
m_peer.VerifyNetworkThread();
double currentRemoteNetTime = remoteNetTime - (rtt * 0.5);
m_remoteToLocalNetTime = (float)(NetTime.Now - currentRemoteNetTime);
m_sentPingNumber++;
if (m_sentPingNumber >= 256)
m_sentPingNumber = 0;
m_sentPingTime = (float)NetTime.Now;
NetOutgoingMessage om = m_peer.CreateMessage(1);
om.Write((byte)m_sentPingNumber);
om.m_messageType = NetMessageType.Ping;
StringBuilder bdr = new StringBuilder();
bdr.Append("Initialized average roundtrip time to: ");
bdr.Append(NetTime.ToReadable(m_averageRoundtripTime));
bdr.Append(" remote time diff to ");
bdr.Append(NetTime.ToReadable(m_remoteToLocalNetTime));
m_owner.LogDebug(bdr.ToString());
int len = om.Encode(m_peer.m_sendBuffer, 0, 0);
bool connectionReset;
m_peer.SendPacket(len, m_remoteEndpoint, 1, out connectionReset);
}
internal void HandleIncomingPing(byte pingNumber)
internal void SendPong(int pingNumber)
{
double now = NetTime.Now;
m_peer.VerifyNetworkThread();
// send pong
NetOutgoingMessage pong = m_owner.CreateMessage(1 + 8);
pong.m_libType = NetMessageLibraryType.Pong;
pong.Write((byte)pingNumber);
pong.Write(now);
NetOutgoingMessage om = m_peer.CreateMessage(1);
om.Write((byte)pingNumber);
om.m_messageType = NetMessageType.Pong;
m_owner.LogDebug("Sending pong for remote ping #" + pingNumber);
m_owner.SendLibraryImmediately(pong, m_remoteEndpoint);
int len = om.Encode(m_peer.m_sendBuffer, 0, 0);
bool connectionReset;
m_peer.SendPacket(len, m_remoteEndpoint, 1, out connectionReset);
}
internal void HandleIncomingPong(double receiveNow, byte pingNumber, double remoteNetTime)
internal void ReceivedPong(float now, int pongNumber)
{
// verify it´s the correct ping number
if (pingNumber != m_lastSentPingNumber)
if (pongNumber != m_sentPingNumber)
{
m_owner.LogError("Received wrong pong number; got " + pingNumber + " expected " + m_lastSentPingNumber);
// but still, a pong must have been triggered by a ping...
double diff = receiveNow - m_lastSendRespondedTo;
if (diff > 0)
m_lastSendRespondedTo += (diff / 2); // postpone timing out a bit
m_peer.LogVerbose("Ping/Pong mismatch; dropped message?");
return;
}
double now = NetTime.Now; // need exact number
m_lastHeardFrom = now;
if (m_lastPingSendTime > m_lastSendRespondedTo)
m_lastSendRespondedTo = m_lastPingSendTime;
m_timeoutDeadline = now + m_peerConfiguration.m_connectionTimeout;
double rtt = now - m_lastPingSendTime;
float rtt = now - m_sentPingTime;
NetException.Assert(rtt >= 0);
// update clock sync
double currentRemoteNetTime = remoteNetTime - (rtt * 0.5);
double foundDiffRemoteToLocal = receiveNow - currentRemoteNetTime;
if (m_remoteToLocalNetTime == double.MinValue)
m_remoteToLocalNetTime = foundDiffRemoteToLocal;
if (m_averageRoundtripTime < 0)
{
m_averageRoundtripTime = rtt; // initial estimate
m_peer.LogDebug("Initiated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime));
}
else
m_remoteToLocalNetTime = ((m_remoteToLocalNetTime + foundDiffRemoteToLocal) * 0.5);
m_averageRoundtripTime = (m_averageRoundtripTime * 0.7f) + (float)(rtt * 0.3f);
m_owner.LogVerbose("Found rtt: " + NetTime.ToReadable(rtt) + ", new average: " + NetTime.ToReadable(m_averageRoundtripTime) + " new time diff: " + NetTime.ToReadable(m_remoteToLocalNetTime));
}
internal void KeepAliveHeartbeat(double now)
{
// do keepalive and latency pings
if (m_status == NetConnectionStatus.Disconnected || m_status == NetConnectionStatus.None)
return;
// force ack sending?
if (now > m_nextForceAckTime)
{
// send keepalive thru regular channels to give acks something to piggyback on
NetOutgoingMessage pig = m_owner.CreateMessage(1);
pig.m_libType = NetMessageLibraryType.KeepAlive;
SendLibrary(pig);
m_nextForceAckTime = now + m_peerConfiguration.m_maxAckDelayTime;
m_averageRoundtripTime = (m_averageRoundtripTime * 0.7f) + (float)(rtt * 0.3f);
m_peer.LogVerbose("Updated average roundtrip time to " + NetTime.ToReadable(m_averageRoundtripTime));
}
// timeout
if (now > m_lastSendRespondedTo + m_peerConfiguration.m_connectionTimeout)
{
m_owner.LogDebug(this + " timed out; now is " + now + " last responded to is: " + m_lastSendRespondedTo);
Disconnect("Timed out after " + (now - m_lastSendRespondedTo) + " seconds");
return;
}
// ping time?
if (now >= m_nextPing)
{
//
// send ping
//
now = NetTime.Now; // need exact number
m_lastSentPingNumber++;
m_lastPingSendTime = now;
m_owner.LogDebug("Sending ping #" + m_lastSentPingNumber);
// in case of not heard for a while
if (m_lastSendRespondedTo > 0 && (now > m_lastSendRespondedTo + (m_owner.Configuration.m_pingFrequency * 1.5f)))
m_nextPing = now + (m_owner.Configuration.m_pingFrequency * 0.5f); // double ping rate
else
m_nextPing = now + m_owner.Configuration.m_pingFrequency;
NetOutgoingMessage ping = m_owner.CreateMessage(1);
ping.m_libType = NetMessageLibraryType.Ping;
ping.Write((byte)m_lastSentPingNumber);
m_owner.SendLibraryImmediately(ping, m_remoteEndpoint);
}
m_peer.LogVerbose("Timeout deadline pushed to " + m_timeoutDeadline);
}
}
}