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

NetQueue.TryDequeue failure will now only throw in DEBUG

WaitMessage will now create a wait event object if needed
This commit is contained in:
lidgren
2014-05-21 07:18:08 +00:00
parent 865a7600f2
commit fbddaf8962
3 changed files with 30 additions and 17 deletions

View File

@@ -14,6 +14,7 @@ namespace Lidgren.Network
private int m_listenPort;
private object m_tag;
private object m_messageReceivedEventCreationLock = new object();
internal readonly List<NetConnection> m_connections;
private readonly Dictionary<IPEndPoint, NetConnection> m_connectionLookup;
@@ -36,7 +37,13 @@ namespace Lidgren.Network
get
{
if (m_messageReceivedEvent == null)
m_messageReceivedEvent = new AutoResetEvent(false);
{
lock (m_messageReceivedEventCreationLock) // make sure we don't create more than one event object
{
if (m_messageReceivedEvent == null)
m_messageReceivedEvent = new AutoResetEvent(false);
}
}
return m_messageReceivedEvent;
}
}
@@ -174,8 +181,8 @@ namespace Lidgren.Network
var msg = ReadMessage();
if (msg != null)
return msg; // no need to wait; we already have a message to deliver
if (m_messageReceivedEvent != null)
m_messageReceivedEvent.WaitOne(maxMillis);
var msgEvt = MessageReceivedEvent;
msgEvt.WaitOne(maxMillis);
return ReadMessage();
}

View File

@@ -193,6 +193,15 @@ namespace Lidgren.Network
return true;
}
catch
{
#if DEBUG
throw;
#else
item = default(T);
return false;
#endif
}
finally
{
m_lock.ExitWriteLock();
@@ -200,7 +209,7 @@ namespace Lidgren.Network
}
/// <summary>
/// Gets an item from the head of the queue, or returns default(T) if empty
/// Gets all items from the head of the queue, or returns number of items popped
/// </summary>
public int TryDrain(IList<T> addTo)
{

View File

@@ -42,20 +42,17 @@ namespace UnitTests
peer.Shutdown("bye");
// read all message
NetIncomingMessage inc;
while((inc = peer.ReadMessage()) != null)
NetIncomingMessage inc = peer.WaitMessage(5000);
switch(inc.MessageType)
{
switch(inc.MessageType)
{
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine("Peer message: " + inc.ReadString());
break;
case NetIncomingMessageType.Error:
throw new Exception("Received error message!");
}
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine("Peer message: " + inc.ReadString());
break;
case NetIncomingMessageType.Error:
throw new Exception("Received error message!");
}
Console.WriteLine("Done");