Reworking ReplayRecorderConnection to no longer be an IConnection but rather attached to IConnection
This commit is contained in:
@@ -44,6 +44,7 @@ namespace OpenRA.Network
|
||||
}
|
||||
|
||||
protected List<ReceivedPacket> receivedPackets = new List<ReceivedPacket>();
|
||||
public ReplayRecorder Recorder { get; private set; }
|
||||
|
||||
public virtual int LocalClientId
|
||||
{
|
||||
@@ -99,10 +100,26 @@ namespace OpenRA.Network
|
||||
}
|
||||
|
||||
foreach (var p in packets)
|
||||
{
|
||||
packetFn(p.FromClient, p.Data);
|
||||
if (Recorder != null)
|
||||
Recorder.Receive(p.FromClient, p.Data);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing) { }
|
||||
public void StartRecording(Func<string> chooseFilename)
|
||||
{
|
||||
// If we have a previous recording then save/dispose it and start a new one.
|
||||
if (Recorder != null)
|
||||
Recorder.Dispose();
|
||||
Recorder = new ReplayRecorder(chooseFilename);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && Recorder != null)
|
||||
Recorder.Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
@@ -16,11 +16,9 @@ using OpenRA.FileFormats;
|
||||
|
||||
namespace OpenRA.Network
|
||||
{
|
||||
sealed class ReplayRecorderConnection : IConnection
|
||||
sealed class ReplayRecorder
|
||||
{
|
||||
public ReplayMetadata Metadata;
|
||||
|
||||
IConnection inner;
|
||||
BinaryWriter writer;
|
||||
Func<string> chooseFilename;
|
||||
MemoryStream preStartBuffer = new MemoryStream();
|
||||
@@ -36,10 +34,9 @@ namespace OpenRA.Network
|
||||
return frame == 0 && data.ToOrderList(null).Any(o => o.OrderString == "StartGame");
|
||||
}
|
||||
|
||||
public ReplayRecorderConnection(IConnection inner, Func<string> chooseFilename)
|
||||
public ReplayRecorder(Func<string> chooseFilename)
|
||||
{
|
||||
this.chooseFilename = chooseFilename;
|
||||
this.inner = inner;
|
||||
|
||||
writer = new BinaryWriter(preStartBuffer);
|
||||
}
|
||||
@@ -70,30 +67,19 @@ namespace OpenRA.Network
|
||||
this.writer = new BinaryWriter(file);
|
||||
}
|
||||
|
||||
public int LocalClientId { get { return inner.LocalClientId; } }
|
||||
public ConnectionState ConnectionState { get { return inner.ConnectionState; } }
|
||||
|
||||
public void Send(int frame, List<byte[]> orders) { inner.Send(frame, orders); }
|
||||
public void SendImmediate(List<byte[]> orders) { inner.SendImmediate(orders); }
|
||||
public void SendSync(int frame, byte[] syncData) { inner.SendSync(frame, syncData); }
|
||||
|
||||
public void Receive(Action<int, byte[]> packetFn)
|
||||
public void Receive(int clientID, byte[] data)
|
||||
{
|
||||
inner.Receive((client, data) =>
|
||||
{
|
||||
if (preStartBuffer != null && IsGameStart(data))
|
||||
{
|
||||
writer.Flush();
|
||||
var preStartData = preStartBuffer.ToArray();
|
||||
preStartBuffer = null;
|
||||
StartSavingReplay(preStartData);
|
||||
}
|
||||
if (preStartBuffer != null && IsGameStart(data))
|
||||
{
|
||||
writer.Flush();
|
||||
var preStartData = preStartBuffer.ToArray();
|
||||
preStartBuffer = null;
|
||||
StartSavingReplay(preStartData);
|
||||
}
|
||||
|
||||
writer.Write(client);
|
||||
writer.Write(data.Length);
|
||||
writer.Write(data);
|
||||
packetFn(client, data);
|
||||
});
|
||||
writer.Write(clientID);
|
||||
writer.Write(data.Length);
|
||||
writer.Write(data);
|
||||
}
|
||||
|
||||
bool disposed;
|
||||
@@ -114,7 +100,6 @@ namespace OpenRA.Network
|
||||
if (preStartBuffer != null)
|
||||
preStartBuffer.Dispose();
|
||||
writer.Close();
|
||||
inner.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user