Reworking ReplayRecorderConnection to no longer be an IConnection but rather attached to IConnection

This commit is contained in:
Whinis
2015-12-24 10:07:05 -05:00
parent 7cc8846d72
commit 155c74fc5f
5 changed files with 37 additions and 33 deletions

View File

@@ -49,9 +49,9 @@ namespace OpenRA
public static OrderManager JoinServer(string host, int port, string password, bool recordReplay = true)
{
IConnection connection = new NetworkConnection(host, port);
var connection = new NetworkConnection(host, port);
if (recordReplay)
connection = new ReplayRecorderConnection(connection, TimestampedFilename);
connection.StartRecording(TimestampedFilename);
var om = new OrderManager(host, port, password, connection);
JoinInner(om);

View File

@@ -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()
{

View File

@@ -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();
}
}
}

View File

@@ -188,7 +188,7 @@
<Compile Include="World.cs" />
<Compile Include="WorldUtils.cs" />
<Compile Include="VoiceExts.cs" />
<Compile Include="Network\ReplayRecorderConnection.cs" />
<Compile Include="Network\ReplayRecorder.cs" />
<Compile Include="Traits\DebugPauseState.cs" />
<Compile Include="Network\UPnP.cs" />
<Compile Include="Graphics\Renderable.cs" />

View File

@@ -211,7 +211,9 @@ namespace OpenRA
foreach (var player in Players)
gameInfo.AddPlayer(player, OrderManager.LobbyInfo);
var rc = OrderManager.Connection as ReplayRecorderConnection;
var echo = OrderManager.Connection as EchoConnection;
var rc = echo != null ? echo.Recorder : null;
if (rc != null)
rc.Metadata = new ReplayMetadata(gameInfo);
}