Store last command sent for context in reply converter

This commit is contained in:
Benito Palacios Sánchez 2015-03-15 12:45:46 +01:00
parent 5951284dfa
commit 4adfb24542
4 changed files with 9 additions and 6 deletions

View File

@ -182,7 +182,8 @@ namespace UnitTests
byte[] expected = new byte[] { 0xCA, 0xFE, 0xBE, 0xBE, 0x00, 0x10, 0x20 };
string dataString = BitConverter.ToString(expected).Replace("-", "");
ReplyPacket reply = ReplyPacketFactory.CreateReplyPacket(dataString);
ReadMemoryCommand readMemory = new ReadMemoryCommand(0x00, 0x00);
ReplyPacket reply = ReplyPacketFactory.CreateReplyPacket(dataString, readMemory);
Assert.IsInstanceOf<DataReply>(reply);
Assert.AreEqual(expected, ((DataReply)reply).GetData());

View File

@ -46,7 +46,7 @@ namespace NitroDebugger.RSP
return TextEncoding.GetBytes(binPacket.ToString());
}
public static ReplyPacket FromBinary(byte[] data)
public static ReplyPacket FromBinary(byte[] data, CommandPacket commandSent = null)
{
if (!ValidateBinary(data))
throw new FormatException("[BIN] Invalid packet");
@ -61,7 +61,7 @@ namespace NitroDebugger.RSP
if (receivedChecksum != calculatedChecksum.ToString("x2"))
throw new FormatException("[BIN] Invalid checksum");
return ReplyPacketFactory.CreateReplyPacket(packetData);
return ReplyPacketFactory.CreateReplyPacket(packetData, commandSent);
}
private static bool ValidateBinary(byte[] data)

View File

@ -36,6 +36,7 @@ namespace NitroDebugger.RSP
private Session session;
private CancellationTokenSource cts;
private CommandPacket lastCommandSent;
public Presentation(string hostname, int port)
{
@ -50,6 +51,7 @@ namespace NitroDebugger.RSP
public void SendCommand(CommandPacket command)
{
this.lastCommandSent = command;
this.SendData(PacketBinConverter.ToBinary(command));
}
@ -101,7 +103,7 @@ namespace NitroDebugger.RSP
try {
// Get data
byte[] packet = this.session.ReadPacket(PacketSeparator);
response = PacketBinConverter.FromBinary(packet);
response = PacketBinConverter.FromBinary(packet, lastCommandSent);
// Send ACK
this.session.Write(RawPacket.Ack);

View File

@ -26,7 +26,7 @@ namespace NitroDebugger.RSP
{
public static class ReplyPacketFactory
{
public static ReplyPacket CreateReplyPacket(string data)
public static ReplyPacket CreateReplyPacket(string data, CommandPacket commandSent = null)
{
if (data == "OK")
return new OkReply();
@ -37,7 +37,7 @@ namespace NitroDebugger.RSP
if (data.Length == 3 && data[0] == 'E')
return new ErrorReply(Convert.ToInt32(data.Substring(1), 16));
if (data.Length % 2 == 0) {
if (commandSent is ReadMemoryCommand) {
try {
byte[] dataBytes = Enumerable.Range(0, data.Length / 2)
.Select(i => data.Substring(i * 2, 2))