Skip to content

Commit

Permalink
added mutex for access to m_handshake everywhere except count requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Lidgren committed Jan 21, 2015
1 parent bc5b0a0 commit 4b7b53b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
46 changes: 28 additions & 18 deletions Lidgren.Network/NetPeer.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,26 +340,32 @@ private void Heartbeat()
// 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;
lock (m_handshakes)
{
foreach (var kvp in m_handshakes)
{
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!");
// 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)
{
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);
}
// 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
break; // collection has been modified
}
}
}
}
}
Expand Down Expand Up @@ -710,7 +716,8 @@ private void ReceivedUnconnectedLibraryMessage(double now, IPEndPoint senderEndP
// 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;

Expand All @@ -729,8 +736,11 @@ internal void AcceptConnection(NetConnection conn)
// 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)
{
Expand Down
3 changes: 2 additions & 1 deletion Lidgren.Network/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ public virtual NetConnection Connect(IPEndPoint remoteEndPoint, NetOutgoingMessa
conn.m_connectRequested = true;
conn.m_connectionInitiator = true;

m_handshakes.Add(remoteEndPoint, conn);
lock(m_handshakes)
m_handshakes.Add(remoteEndPoint, conn);

return conn;
}
Expand Down
1 change: 0 additions & 1 deletion Samples/Chat/ChatClient/ChatClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
1 change: 0 additions & 1 deletion Samples/Chat/ChatServer/ChatServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down

0 comments on commit 4b7b53b

Please sign in to comment.