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

Resend bug fixed, unit tests expanded, misc cleanup

This commit is contained in:
lidgren
2010-06-01 18:28:14 +00:00
parent 09c241f428
commit 38c4a7a446
10 changed files with 120 additions and 39 deletions

View File

@@ -16,7 +16,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>

View File

@@ -29,6 +29,7 @@ namespace Lidgren.Network
private ushort[] m_lastReceivedSequenced;
internal readonly Dictionary<ushort, NetOutgoingMessage>[] m_storedMessages = new Dictionary<ushort, NetOutgoingMessage>[NetConstants.NumReliableChannels];
internal readonly Dictionary<NetOutgoingMessage, ushort>[] m_inverseStored = new Dictionary<NetOutgoingMessage, ushort>[NetConstants.NumReliableChannels];
internal readonly NetBitVector m_storedMessagesNotEmpty = new NetBitVector(NetConstants.NumReliableChannels);
private readonly ushort[] m_nextExpectedReliableSequence = new ushort[NetConstants.NumReliableChannels];
@@ -50,6 +51,18 @@ namespace Lidgren.Network
return retval;
}
public int GetWithheldMessagesCount()
{
int retval = 0;
for (int i = 0; i < m_withheldMessages.Length; i++)
{
var list = m_withheldMessages[i];
if (list != null)
retval += list.Count;
}
return retval;
}
private void InitializeReliability()
{
int num = ((int)NetMessageType.UserReliableOrdered + NetConstants.NetChannelsPerDeliveryMethod) - (int)NetMessageType.UserSequenced;
@@ -91,22 +104,50 @@ namespace Lidgren.Network
{
m_owner.VerifyNetworkThread();
ushort seqNr = GetSendSequenceNumber(msg.m_type);
int seqNr = -1;
int reliableSlot = (int)msg.m_type - (int)NetMessageType.UserReliableUnordered;
Dictionary<ushort, NetOutgoingMessage> slotDict = m_storedMessages[reliableSlot];
Dictionary<NetOutgoingMessage, ushort> invSlotDict = m_inverseStored[reliableSlot];
if (slotDict == null)
{
slotDict = new Dictionary<ushort, NetOutgoingMessage>();
m_storedMessages[reliableSlot] = slotDict;
invSlotDict = new Dictionary<NetOutgoingMessage, ushort>();
m_inverseStored[reliableSlot] = invSlotDict;
// (cannot be a resend here)
}
else
{
// we assume there's a invSlotDict if there's a slotDict
// is it a resend? if so, return the old sequence number
ushort oldSeqNr;
if (invSlotDict.TryGetValue(msg, out oldSeqNr))
seqNr = oldSeqNr;
}
Interlocked.Increment(ref msg.m_inQueueCount);
slotDict.Add(seqNr, msg);
if (seqNr != -1)
{
// resend!
// m_owner.LogDebug("Resending " + msg.m_type + "|" + seqNr);
m_statistics.MessageResent();
}
else
{
// first send
seqNr = GetSendSequenceNumber(msg.m_type);
if (slotDict.Count > 0)
m_storedMessagesNotEmpty.Set(reliableSlot, true);
//m_owner.LogDebug("Sending " + msg.m_type + "|" + seqNr);
Interlocked.Increment(ref msg.m_inQueueCount);
slotDict.Add((ushort)seqNr, msg);
invSlotDict.Add(msg, (ushort)seqNr);
if (slotDict.Count > 0)
m_storedMessagesNotEmpty.Set(reliableSlot, true);
}
// schedule next resend
int numSends = msg.m_numSends;
@@ -114,7 +155,7 @@ namespace Lidgren.Network
float[] multiplers = m_peerConfiguration.m_resendRTTMultiplier;
msg.m_nextResendTime = now + baseTimes[numSends] + (m_averageRoundtripTime * multiplers[numSends]);
return seqNr;
return (ushort)seqNr;
}
private void Resend(double now, ushort seqNr, NetOutgoingMessage msg)
@@ -128,14 +169,14 @@ namespace Lidgren.Network
// no more resends! We failed!
int reliableSlot = (int)msg.m_type - (int)NetMessageType.UserReliableUnordered;
m_storedMessages[reliableSlot].Remove(seqNr);
m_owner.LogWarning("Failed to deliver reliable message " + msg);
m_owner.LogWarning("Failed to deliver reliable message " + msg + " (seqNr " + seqNr + ")");
Disconnect("Failed to deliver reliable message!");
return; // bye
}
m_owner.LogVerbose("Resending " + msg);
m_owner.LogVerbose("Resending " + msg + " (seqNr " + seqNr + ")");
Interlocked.Increment(ref msg.m_inQueueCount);
m_unsentMessages.EnqueueFirst(msg);
@@ -160,7 +201,7 @@ namespace Lidgren.Network
{
ushort seqNr = (ushort)(buffer[ptr++] | (buffer[ptr++] << 8));
NetMessageType tp = (NetMessageType)buffer[ptr++];
// m_owner.LogDebug("Got ack for " + tp + " " + seqNr);
//m_owner.LogDebug("Got ack for " + tp + "|" + seqNr);
// remove stored message
int reliableSlot = (int)tp - (int)NetMessageType.UserReliableUnordered;
@@ -175,6 +216,8 @@ namespace Lidgren.Network
{
// found!
dict.Remove(seqNr);
m_inverseStored[reliableSlot].Remove(om);
Interlocked.Decrement(ref om.m_inQueueCount);
NetException.Assert(om.m_lastSentTime != 0);

View File

@@ -316,7 +316,7 @@ namespace Lidgren.Network
{
// Expected sequence number
AcceptMessage(mtp, isFragment, channelSequenceNumber, ptr, payloadLengthBits);
ExpectedReliableSequenceArrived(reliableSlot);
return;
}

View File

@@ -36,6 +36,8 @@ namespace Lidgren.Network
internal int m_sentBytes;
internal int m_receivedBytes;
internal int m_resentMessages;
internal NetConnectionStatistics(NetConnection conn)
{
m_connection = conn;
@@ -86,18 +88,32 @@ namespace Lidgren.Network
m_receivedMessages += numMessages;
}
[Conditional("DEBUG")]
internal void MessageResent()
{
m_resentMessages++;
}
public override string ToString()
{
StringBuilder bdr = new StringBuilder();
bdr.AppendLine("Average roundtrip time: " + NetTime.ToReadable(m_connection.m_averageRoundtripTime));
bdr.AppendLine("Sent " + m_sentBytes + " bytes in " + m_sentMessages + " messages in " + m_sentPackets + " packets");
bdr.AppendLine("Received " + m_receivedBytes + " bytes in " + m_receivedMessages + " messages in " + m_receivedPackets + " packets");
if (m_resentMessages > 0)
bdr.AppendLine("Resent messages: " + m_resentMessages);
int numUnsent = m_connection.m_unsentMessages.Count;
if (numUnsent > 0)
bdr.AppendLine("Unsent messages: " + numUnsent);
int numStored = m_connection.GetStoredMessagesCount();
if (numStored > 0)
bdr.AppendLine("Stored messages: " + numStored);
int numWithheld = m_connection.GetWithheldMessagesCount();
if (numWithheld > 0)
bdr.AppendLine("Withheld messages: " + numWithheld);
return bdr.ToString();
}
}

View File

@@ -45,7 +45,7 @@ namespace Lidgren.Network
{
if (NetRandom.Instance.Chance(m_configuration.m_loss))
{
LogVerbose("Sending packet " + numBytes + " bytes - SIMULATED LOST!");
//LogVerbose("Sending packet " + numBytes + " bytes - SIMULATED LOST!");
return; // packet "lost"
}
}
@@ -58,7 +58,7 @@ namespace Lidgren.Network
if (m == 0.0f && r == 0.0f)
{
// no latency simulation
LogVerbose("Sending packet " + numBytes + " bytes");
//LogVerbose("Sending packet " + numBytes + " bytes");
ActuallySendPacket(m_sendBuffer, numBytes, target);
return;
}
@@ -82,7 +82,7 @@ namespace Lidgren.Network
m_delayedPackets.Add(p);
}
LogVerbose("Sending packet " + numBytes + " bytes - delayed " + NetTime.ToReadable(delay));
// LogVerbose("Sending packet " + numBytes + " bytes - delayed " + NetTime.ToReadable(delay));
}
private void SendDelayedPackets()

View File

@@ -23,7 +23,7 @@ using System.Diagnostics;
namespace Lidgren.Network
{
/// <summary>
/// Thread safe queue with TryDequeue()
/// Thread safe (blocking) queue with TryDequeue() and EnqueueFirst()
/// </summary>
[DebuggerDisplay("Count={m_size}")]
public sealed class NetQueue<T>
@@ -98,38 +98,34 @@ namespace Lidgren.Network
}
}
// must be called from within a lock(m_lock) !
private void SetCapacity(int newCapacity)
{
if (m_size == 0)
{
lock (m_lock)
if (m_size == 0)
{
if (m_size == 0)
{
m_items = new T[newCapacity];
m_head = 0;
return;
}
m_items = new T[newCapacity];
m_head = 0;
return;
}
}
T[] newItems = new T[newCapacity];
lock (m_lock)
if (m_head + m_size - 1 < m_items.Length)
{
if (m_head + m_size - 1 < m_items.Length)
{
Array.Copy(m_items, m_head, newItems, 0, m_size);
}
else
{
Array.Copy(m_items, m_head, newItems, 0, m_items.Length - m_head);
Array.Copy(m_items, 0, newItems, m_items.Length - m_head, (m_size - (m_items.Length - m_head)));
}
m_items = newItems;
m_head = 0;
Array.Copy(m_items, m_head, newItems, 0, m_size);
}
else
{
Array.Copy(m_items, m_head, newItems, 0, m_items.Length - m_head);
Array.Copy(m_items, 0, newItems, m_items.Length - m_head, (m_size - (m_items.Length - m_head)));
}
m_items = newItems;
m_head = 0;
}
/// <summary>