From b6d2111380a9b2c5d433f289f9729377cfade04c Mon Sep 17 00:00:00 2001 From: lidgren Date: Mon, 5 Nov 2012 13:39:57 +0000 Subject: [PATCH] Release build fixed for NetPeerSettingsWindow ManyClients project excluded missing app.config file Reduced number of calls to NetTime.Now when receiving multiple messages Moved magic numbers to constants in NetConnection --- Lidgren.Network/NetConnection.cs | 7 +++++-- Lidgren.Network/NetPeer.Internal.cs | 11 +++++++---- Lidgren.Network/NetPeer.cs | 10 +++++++++- .../ManySample/ManyClients/ManyClients.csproj | 1 - Samples/SamplesCommon/NetPeerSettingsWindow.cs | 10 +++++++++- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Lidgren.Network/NetConnection.cs b/Lidgren.Network/NetConnection.cs index 82087b1..b0365f7 100644 --- a/Lidgren.Network/NetConnection.cs +++ b/Lidgren.Network/NetConnection.cs @@ -11,6 +11,9 @@ namespace Lidgren.Network [DebuggerDisplay("RemoteUniqueIdentifier={RemoteUniqueIdentifier} RemoteEndPoint={remoteEndPoint}")] public partial class NetConnection { + private const int m_infrequentEventsSkipFrames = 8; // number of heartbeats to skip checking for infrequent events (ping, timeout etc) + private const int m_messageCoalesceFrames = 3; // number of heartbeats to wait for more incoming messages before sending packet + internal NetPeer m_peer; internal NetPeerConfiguration m_peerConfiguration; internal NetConnectionStatus m_status; @@ -138,7 +141,7 @@ namespace Lidgren.Network NetException.Assert(m_status != NetConnectionStatus.InitiatedConnect && m_status != NetConnectionStatus.RespondedConnect); - if ((frameCounter % 5) == 0) + if ((frameCounter % m_infrequentEventsSkipFrames) == 0) { if (now > m_timeoutDeadline) { @@ -175,7 +178,7 @@ namespace Lidgren.Network byte[] sendBuffer = m_peer.m_sendBuffer; int mtu = m_currentMTU; - if ((frameCounter % 3) == 0) // coalesce a few frames + if ((frameCounter % m_messageCoalesceFrames) == 0) // coalesce a few frames { // // send ack messages diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs index e1fc47f..42b200b 100644 --- a/Lidgren.Network/NetPeer.Internal.cs +++ b/Lidgren.Network/NetPeer.Internal.cs @@ -36,7 +36,7 @@ namespace Lidgren.Network internal long m_uniqueIdentifier; internal bool m_executeFlushSendQueue; - private AutoResetEvent m_messageReceivedEvent = new AutoResetEvent(false); + private AutoResetEvent m_messageReceivedEvent; private List> m_receiveCallbacks; /// @@ -369,6 +369,10 @@ namespace Lidgren.Network //if (m_socket == null || m_socket.Available < 1) // return; + // update now + dnow = NetTime.Now; + now = (float)dnow; + do { int bytesReceived = 0; @@ -418,7 +422,6 @@ namespace Lidgren.Network NetConnection sender = null; m_connectionLookup.TryGetValue(ipsender, out sender); - double receiveTime = NetTime.Now; // // parse packet into messages // @@ -460,7 +463,7 @@ namespace Lidgren.Network if (sender != null) sender.ReceivedLibraryMessage(tp, ptr, payloadByteLength); else - ReceivedUnconnectedLibraryMessage(receiveTime, ipsender, tp, ptr, payloadByteLength); + ReceivedUnconnectedLibraryMessage(dnow, ipsender, tp, ptr, payloadByteLength); } else { @@ -469,7 +472,7 @@ namespace Lidgren.Network NetIncomingMessage msg = CreateIncomingMessage(NetIncomingMessageType.Data, payloadByteLength); msg.m_isFragment = isFragment; - msg.m_receiveTime = receiveTime; + msg.m_receiveTime = dnow; msg.m_sequenceNumber = sequenceNumber; msg.m_receivedMessageType = tp; msg.m_senderConnection = sender; diff --git a/Lidgren.Network/NetPeer.cs b/Lidgren.Network/NetPeer.cs index 5823c4c..547b872 100644 --- a/Lidgren.Network/NetPeer.cs +++ b/Lidgren.Network/NetPeer.cs @@ -31,7 +31,15 @@ namespace Lidgren.Network /// find the message in the queue. Other user created threads could be preempted and dequeue /// the message before the waiting thread wakes up. /// - public AutoResetEvent MessageReceivedEvent { get { return m_messageReceivedEvent; } } + public AutoResetEvent MessageReceivedEvent + { + get + { + if (m_messageReceivedEvent == null) + m_messageReceivedEvent = new AutoResetEvent(false); + return m_messageReceivedEvent; + } + } /// /// Gets a unique identifier for this NetPeer based on Mac address and ip/port. Note! Not available until Start() has been called! diff --git a/Samples/LibraryTestSamples/ManySample/ManyClients/ManyClients.csproj b/Samples/LibraryTestSamples/ManySample/ManyClients/ManyClients.csproj index da0975e..7fdd06c 100644 --- a/Samples/LibraryTestSamples/ManySample/ManyClients/ManyClients.csproj +++ b/Samples/LibraryTestSamples/ManySample/ManyClients/ManyClients.csproj @@ -96,7 +96,6 @@ Resources.resx True - SettingsSingleFileGenerator Settings.Designer.cs diff --git a/Samples/SamplesCommon/NetPeerSettingsWindow.cs b/Samples/SamplesCommon/NetPeerSettingsWindow.cs index 332dc21..2a58bff 100644 --- a/Samples/SamplesCommon/NetPeerSettingsWindow.cs +++ b/Samples/SamplesCommon/NetPeerSettingsWindow.cs @@ -45,6 +45,7 @@ namespace SamplesCommon { var pc = Peer.Configuration; +#if DEBUG var loss = (pc.SimulatedLoss * 100.0f).ToString(); label5.Text = loss + " %"; LossTextBox.Text = loss; @@ -55,7 +56,12 @@ namespace SamplesCommon var minLat = (pc.SimulatedMinimumLatency * 1000.0f).ToString(); var maxLat = ((pc.SimulatedMinimumLatency + pc.SimulatedRandomLatency) * 1000.0f).ToString(); - +#else + var loss = 0; + var dupes = 0; + var minLat = ""; + var maxLat = ""; +#endif label4.Text = minLat + " to " + maxLat + " ms"; MinLatencyTextBox.Text = minLat; MaxLatencyTextBox.Text = maxLat; @@ -84,6 +90,7 @@ namespace SamplesCommon { Peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.DebugMessage, DebugCheckBox.Checked); Peer.Configuration.SetMessageTypeEnabled(NetIncomingMessageType.VerboseDebugMessage, VerboseCheckBox.Checked); +#if DEBUG float f; if (Single.TryParse(LossTextBox.Text, out f)) Peer.Configuration.SimulatedLoss = (float)((double)f / 100.0); @@ -105,6 +112,7 @@ namespace SamplesCommon MaxLatencyTextBox.Text = ((int)(max * 1000)).ToString(); } } +#endif }