Fixed IDisposable implementation and usage.
- Implement IDisposable interface correctly, with sealed classes where possible for simplicity. - Add using statement around undisposed local variables.
This commit is contained in:
@@ -86,7 +86,7 @@ namespace OpenRA.Network
|
||||
if (packet.Length == 0)
|
||||
throw new NotImplementedException();
|
||||
lock (this)
|
||||
receivedPackets.Add(new ReceivedPacket { FromClient = LocalClientId, Data = packet } );
|
||||
receivedPackets.Add(new ReceivedPacket { FromClient = LocalClientId, Data = packet });
|
||||
}
|
||||
|
||||
public virtual void Receive(Action<int, byte[]> packetFn)
|
||||
@@ -102,10 +102,16 @@ namespace OpenRA.Network
|
||||
packetFn(p.FromClient, p.Data);
|
||||
}
|
||||
|
||||
public virtual void Dispose() { }
|
||||
protected virtual void Dispose(bool disposing) { }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
class NetworkConnection : EchoConnection
|
||||
sealed class NetworkConnection : EchoConnection
|
||||
{
|
||||
TcpClient socket;
|
||||
int clientId;
|
||||
@@ -193,22 +199,27 @@ namespace OpenRA.Network
|
||||
|
||||
bool disposed = false;
|
||||
|
||||
public override void Dispose()
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed) return;
|
||||
if (disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
|
||||
t.Abort();
|
||||
if (socket != null)
|
||||
socket.Client.Close();
|
||||
if (disposing)
|
||||
if (socket != null)
|
||||
socket.Client.Close();
|
||||
using (new PerfSample("Thread.Join"))
|
||||
{
|
||||
if (!t.Join(1000))
|
||||
return;
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
~NetworkConnection() { Dispose(); }
|
||||
~NetworkConnection()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
public class OrderManager : IDisposable
|
||||
public sealed class OrderManager : IDisposable
|
||||
{
|
||||
readonly SyncReport syncReport;
|
||||
readonly FrameData frameData = new FrameData();
|
||||
@@ -197,22 +197,10 @@ namespace OpenRA.Network
|
||||
++NetFrameNumber;
|
||||
}
|
||||
|
||||
bool disposed;
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
Connection.Dispose();
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
if (Connection != null)
|
||||
Connection.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
public class ReplayConnection : IConnection
|
||||
public sealed class ReplayConnection : IConnection
|
||||
{
|
||||
class Chunk
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
class ReplayRecorderConnection : IConnection, IDisposable
|
||||
sealed class ReplayRecorderConnection : IConnection
|
||||
{
|
||||
public ReplayMetadata Metadata;
|
||||
|
||||
@@ -98,30 +98,24 @@ namespace OpenRA.Network
|
||||
}
|
||||
|
||||
bool disposed;
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
if (Metadata != null)
|
||||
{
|
||||
if (Metadata.GameInfo != null)
|
||||
Metadata.GameInfo.EndTimeUtc = DateTime.UtcNow;
|
||||
Metadata.Write(writer);
|
||||
}
|
||||
writer.Close();
|
||||
inner.Dispose();
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
if (disposed)
|
||||
return;
|
||||
disposed = true;
|
||||
|
||||
if (Metadata != null)
|
||||
{
|
||||
if (Metadata.GameInfo != null)
|
||||
Metadata.GameInfo.EndTimeUtc = DateTime.UtcNow;
|
||||
Metadata.Write(writer);
|
||||
}
|
||||
|
||||
if (preStartBuffer != null)
|
||||
preStartBuffer.Dispose();
|
||||
writer.Close();
|
||||
inner.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user