Fix a crash with BlockingCollection in Connection
The BlockingCollection would have `IsAddingCompleted` to true, but `IsComplete` to false, slipping through the cracks and causing an InvalidOperationException ("The collection has been marked as complete with regards to additions.") when trying to add to it.
We now add a check on `(Try)SendData` to only try to add if we can. The collection is still viable for reading until empty/`IsComplete`.
This commit is contained in:
@@ -154,10 +154,8 @@ namespace OpenRA.Server
|
|||||||
|
|
||||||
// Regularly check player ping
|
// Regularly check player ping
|
||||||
if (lastPingSent.ElapsedMilliseconds > 1000)
|
if (lastPingSent.ElapsedMilliseconds > 1000)
|
||||||
{
|
if (TrySendData(CreatePingFrame()))
|
||||||
sendQueue.Add(CreatePingFrame());
|
|
||||||
lastPingSent.Restart();
|
lastPingSent.Restart();
|
||||||
}
|
|
||||||
|
|
||||||
// Send all data immediately, we will block again on read
|
// Send all data immediately, we will block again on read
|
||||||
while (sendQueue.TryTake(out var data, 0))
|
while (sendQueue.TryTake(out var data, 0))
|
||||||
@@ -195,9 +193,21 @@ namespace OpenRA.Server
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendData(byte[] data)
|
public bool TrySendData(byte[] data)
|
||||||
|
{
|
||||||
|
if (sendQueue.IsAddingCompleted)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
sendQueue.Add(data);
|
sendQueue.Add(data);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException)
|
||||||
|
{
|
||||||
|
// Occurs if the collection is marked completed for adding by another thread.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -434,7 +434,7 @@ namespace OpenRA.Server
|
|||||||
var ms = new MemoryStream(8);
|
var ms = new MemoryStream(8);
|
||||||
ms.WriteArray(BitConverter.GetBytes(ProtocolVersion.Handshake));
|
ms.WriteArray(BitConverter.GetBytes(ProtocolVersion.Handshake));
|
||||||
ms.WriteArray(BitConverter.GetBytes(newConn.PlayerIndex));
|
ms.WriteArray(BitConverter.GetBytes(newConn.PlayerIndex));
|
||||||
newConn.SendData(ms.ToArray());
|
newConn.TrySendData(ms.ToArray());
|
||||||
|
|
||||||
// Dispatch a handshake order
|
// Dispatch a handshake order
|
||||||
var request = new HandshakeRequest
|
var request = new HandshakeRequest
|
||||||
@@ -737,14 +737,10 @@ namespace OpenRA.Server
|
|||||||
|
|
||||||
void DispatchFrameToClient(Connection c, int client, byte[] frameData)
|
void DispatchFrameToClient(Connection c, int client, byte[] frameData)
|
||||||
{
|
{
|
||||||
try
|
if (!c.TrySendData(frameData))
|
||||||
{
|
|
||||||
c.SendData(frameData);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
{
|
||||||
DropClient(c);
|
DropClient(c);
|
||||||
Log.Write("server", $"Dropping client {client.ToString(CultureInfo.InvariantCulture)} because dispatching orders failed: {e}");
|
Log.Write("server", $"Dropping client {client.ToString(CultureInfo.InvariantCulture)} because dispatching orders failed!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user