From f9053998cdf2a985d59dec4c6f8f1b0e18c7b12d Mon Sep 17 00:00:00 2001 From: AgentFire Date: Mon, 5 Oct 2015 16:33:46 +0300 Subject: [PATCH] Update NetPeer.cs Fixed an issue in the `WaitMessage(int)` when `MessageReceivedEvent` is signaled, yet `ReadMessage()` returns null. --- Lidgren.Network/NetPeer.cs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Lidgren.Network/NetPeer.cs b/Lidgren.Network/NetPeer.cs index 3e59e2b..bfb32f5 100644 --- a/Lidgren.Network/NetPeer.cs +++ b/Lidgren.Network/NetPeer.cs @@ -180,15 +180,24 @@ namespace Lidgren.Network /// /// Read a pending message from any connection, blocking up to maxMillis if needed /// - public NetIncomingMessage WaitMessage(int maxMillis) - { - var msg = ReadMessage(); - if (msg != null) - return msg; // no need to wait; we already have a message to deliver - var msgEvt = MessageReceivedEvent; - msgEvt.WaitOne(maxMillis); - return ReadMessage(); - } + public NetIncomingMessage WaitMessage(int maxMillis) + { + NetIncomingMessage msg = ReadMessage(); + + while (msg == null) + { + // This could return true... + if (!MessageReceivedEvent.WaitOne(maxMillis)) + { + return null; + } + + // ... while this will still returns null. That's why we need to cycle. + msg = ReadMessage(); + } + + return msg; + } /// /// Read a pending message from any connection, if any