diff --git a/Lidgren.Network/NetBuffer.Read.cs b/Lidgren.Network/NetBuffer.Read.cs
index a49b385..d083240 100644
--- a/Lidgren.Network/NetBuffer.Read.cs
+++ b/Lidgren.Network/NetBuffer.Read.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Net;
+using System.Threading;
#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
@@ -17,6 +18,8 @@ namespace Lidgren.Network
{
private const string c_readOverflowError = "Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.";
+ private static byte[] s_buffer;
+
///
/// Reads a boolean value (stored as a single bit) written using Write(bool)
///
@@ -352,8 +355,11 @@ namespace Lidgren.Network
return retval;
}
- byte[] bytes = ReadBytes(4);
- return BitConverter.ToSingle(bytes, 0);
+ byte[] bytes = Interlocked.Exchange(ref s_buffer, null) ?? new byte[8];
+ ReadBytes(bytes, 0, 4);
+ float res = BitConverter.ToSingle(bytes, 0);
+ s_buffer = bytes;
+ return res;
}
///
@@ -374,8 +380,10 @@ namespace Lidgren.Network
return true;
}
- byte[] bytes = ReadBytes(4);
+ byte[] bytes = Interlocked.Exchange(ref s_buffer, null) ?? new byte[8];
+ ReadBytes(bytes, 0, 4);
result = BitConverter.ToSingle(bytes, 0);
+ s_buffer = bytes;
return true;
}
@@ -394,8 +402,11 @@ namespace Lidgren.Network
return retval;
}
- byte[] bytes = ReadBytes(8);
- return BitConverter.ToDouble(bytes, 0);
+ byte[] bytes = Interlocked.Exchange(ref s_buffer, null) ?? new byte[8];
+ ReadBytes(bytes, 0, 8);
+ double res = BitConverter.ToDouble(bytes, 0);
+ s_buffer = bytes;
+ return res;
}
//
diff --git a/Lidgren.Network/NetServer.cs b/Lidgren.Network/NetServer.cs
index e6ebf62..99a198f 100644
--- a/Lidgren.Network/NetServer.cs
+++ b/Lidgren.Network/NetServer.cs
@@ -24,7 +24,8 @@ namespace Lidgren.Network
/// How to deliver the message
public void SendToAll(NetOutgoingMessage msg, NetDeliveryMethod method)
{
- var all = this.Connections;
+ // Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
+ var all = m_connections;
if (all.Count <= 0) {
if (msg.m_isSent == false)
Recycle(msg);
@@ -43,7 +44,8 @@ namespace Lidgren.Network
/// Which sequence channel to use for the message
public void SendToAll(NetOutgoingMessage msg, NetConnection except, NetDeliveryMethod method, int sequenceChannel)
{
- var all = this.Connections;
+ // Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
+ var all = m_connections;
if (all.Count <= 0) {
if (msg.m_isSent == false)
Recycle(msg);