diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs index bc1e9b5..ead750c 100644 --- a/Lidgren.Network/NetPeer.Internal.cs +++ b/Lidgren.Network/NetPeer.Internal.cs @@ -340,26 +340,32 @@ namespace Lidgren.Network // do handshake heartbeats if ((m_frameCounter % 3) == 0) { - foreach (var kvp in m_handshakes) + if (m_handshakes.Count > 0) { - NetConnection conn = kvp.Value as NetConnection; -#if DEBUG - // sanity check - if (kvp.Key != kvp.Key) - LogWarning("Sanity fail! Connection in handshake list under wrong key!"); -#endif - conn.UnconnectedHeartbeat(now); - if (conn.m_status == NetConnectionStatus.Connected || conn.m_status == NetConnectionStatus.Disconnected) + lock (m_handshakes) { -#if DEBUG - // sanity check - if (conn.m_status == NetConnectionStatus.Disconnected && m_handshakes.ContainsKey(conn.RemoteEndPoint)) + foreach (var kvp in m_handshakes) { - LogWarning("Sanity fail! Handshakes list contained disconnected connection!"); - m_handshakes.Remove(conn.RemoteEndPoint); - } + NetConnection conn = kvp.Value as NetConnection; +#if DEBUG + // sanity check + if (kvp.Key != kvp.Key) + LogWarning("Sanity fail! Connection in handshake list under wrong key!"); #endif - break; // collection has been modified + conn.UnconnectedHeartbeat(now); + if (conn.m_status == NetConnectionStatus.Connected || conn.m_status == NetConnectionStatus.Disconnected) + { +#if DEBUG + // sanity check + if (conn.m_status == NetConnectionStatus.Disconnected && m_handshakes.ContainsKey(conn.RemoteEndPoint)) + { + LogWarning("Sanity fail! Handshakes list contained disconnected connection!"); + m_handshakes.Remove(conn.RemoteEndPoint); + } +#endif + break; // collection has been modified + } + } } } } @@ -710,7 +716,8 @@ namespace Lidgren.Network // Ok, start handshake! NetConnection conn = new NetConnection(this, senderEndPoint); conn.m_status = NetConnectionStatus.ReceivedInitiation; - m_handshakes.Add(senderEndPoint, conn); + lock(m_handshakes) + m_handshakes.Add(senderEndPoint, conn); conn.ReceivedHandshake(now, tp, ptr, payloadByteLength); return; @@ -729,8 +736,11 @@ namespace Lidgren.Network // LogDebug("Accepted connection " + conn); conn.InitExpandMTU(NetTime.Now); - if (m_handshakes.Remove(conn.m_remoteEndPoint) == false) - LogWarning("AcceptConnection called but m_handshakes did not contain it!"); + lock (m_handshakes) + { + if (m_handshakes.Remove(conn.m_remoteEndPoint) == false) + LogWarning("AcceptConnection called but m_handshakes did not contain it!"); + } lock (m_connections) { diff --git a/Lidgren.Network/NetPeer.cs b/Lidgren.Network/NetPeer.cs index f09eed7..2617a3c 100644 --- a/Lidgren.Network/NetPeer.cs +++ b/Lidgren.Network/NetPeer.cs @@ -306,7 +306,8 @@ namespace Lidgren.Network conn.m_connectRequested = true; conn.m_connectionInitiator = true; - m_handshakes.Add(remoteEndPoint, conn); + lock(m_handshakes) + m_handshakes.Add(remoteEndPoint, conn); return conn; } diff --git a/Samples/Chat/ChatClient/ChatClient.csproj b/Samples/Chat/ChatClient/ChatClient.csproj index 0b79074..363645c 100644 --- a/Samples/Chat/ChatClient/ChatClient.csproj +++ b/Samples/Chat/ChatClient/ChatClient.csproj @@ -61,7 +61,6 @@ Resources.resx True - SettingsSingleFileGenerator Settings.Designer.cs diff --git a/Samples/Chat/ChatServer/ChatServer.csproj b/Samples/Chat/ChatServer/ChatServer.csproj index 682480c..9d22874 100644 --- a/Samples/Chat/ChatServer/ChatServer.csproj +++ b/Samples/Chat/ChatServer/ChatServer.csproj @@ -61,7 +61,6 @@ Resources.resx True - SettingsSingleFileGenerator Settings.Designer.cs