mirror of
https://github.com/pleonex/NitroDebugger.git
synced 2025-06-20 14:15:39 -04:00
Max attempts for receive messages too
This commit is contained in:
parent
4f698709a0
commit
3a06b1aa6b
@ -116,39 +116,46 @@ namespace UnitTests
|
|||||||
() => SendCommand("test2", "arg1,arg2", "f5", 11));
|
() => SendCommand("test2", "arg1,arg2", "f5", 11));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
private void ReceiveMessageBad(int attempts)
|
||||||
public void ReceiveMessage()
|
|
||||||
{
|
{
|
||||||
// $ O K # 9 a
|
// $ O K # 9 b
|
||||||
byte[] response = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x61 };
|
byte[] responseBad = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x62 };
|
||||||
connection.GetStream().Write(response, 0, response.Length);
|
|
||||||
|
// $ O K # 9 a
|
||||||
|
byte[] responseGood = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x61 };
|
||||||
|
|
||||||
|
for (int i = 0; i < attempts - 1; i++)
|
||||||
|
connection.GetStream().Write(responseBad, 0, responseBad.Length);
|
||||||
|
connection.GetStream().Write(responseGood, 0, responseGood.Length);
|
||||||
|
|
||||||
ReplyPacket responsePacket = presentation.ReceiveReply();
|
ReplyPacket responsePacket = presentation.ReceiveReply();
|
||||||
Assert.IsInstanceOf<OkReply>(responsePacket);
|
Assert.IsInstanceOf<OkReply>(responsePacket);
|
||||||
|
|
||||||
|
for (int i = 0; i < attempts - 1; i++) {
|
||||||
|
byte nack = (byte)connection.GetStream().ReadByte();
|
||||||
|
Assert.AreEqual(RawPacket.Nack, nack);
|
||||||
|
}
|
||||||
|
|
||||||
byte ack = (byte)connection.GetStream().ReadByte();
|
byte ack = (byte)connection.GetStream().ReadByte();
|
||||||
Assert.AreEqual(RawPacket.Ack, ack);
|
Assert.AreEqual(RawPacket.Ack, ack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ReceiveMessage()
|
||||||
|
{
|
||||||
|
ReceiveMessageBad(1);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void RequestResent()
|
public void RequestResent()
|
||||||
{
|
{
|
||||||
// $ O K # 9 b
|
ReceiveMessageBad(3);
|
||||||
byte[] responseBad = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x62 };
|
}
|
||||||
connection.GetStream().Write(responseBad, 0, responseBad.Length);
|
|
||||||
|
|
||||||
// $ O K # 9 a
|
[Test]
|
||||||
byte[] responseGood = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x61 };
|
public void ReceiveTooManyBadMessages()
|
||||||
connection.GetStream().Write(responseGood, 0, responseGood.Length);
|
{
|
||||||
|
Assert.Throws<ProtocolViolationException>(() => ReceiveMessageBad(11));
|
||||||
ReplyPacket responsePacket = presentation.ReceiveReply();
|
|
||||||
Assert.IsInstanceOf<OkReply>(responsePacket);
|
|
||||||
|
|
||||||
byte firstNack = (byte)connection.GetStream().ReadByte();
|
|
||||||
Assert.AreEqual(RawPacket.Nack, firstNack);
|
|
||||||
|
|
||||||
byte ack = (byte)connection.GetStream().ReadByte();
|
|
||||||
Assert.AreEqual(RawPacket.Ack, ack);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -73,19 +73,32 @@ namespace NitroDebugger.RSP
|
|||||||
public ReplyPacket ReceiveReply()
|
public ReplyPacket ReceiveReply()
|
||||||
{
|
{
|
||||||
ReplyPacket response = null;
|
ReplyPacket response = null;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
while (response == null) {
|
do {
|
||||||
try {
|
if (count == MaxWriteAttemps)
|
||||||
// Get data
|
throw new ProtocolViolationException("[PRES] Can not receive correctly");
|
||||||
byte[] packet = this.session.ReadPacket(PacketSeparator);
|
count++;
|
||||||
response = PacketBinConverter.FromBinary(packet);
|
|
||||||
|
|
||||||
// Send ACK
|
response = this.NextReply();
|
||||||
this.session.Write(RawPacket.Ack);
|
} while (response == null);
|
||||||
} catch (FormatException) {
|
|
||||||
// Error... send NACK
|
return response;
|
||||||
this.session.Write(RawPacket.Nack);
|
}
|
||||||
}
|
|
||||||
|
private ReplyPacket NextReply()
|
||||||
|
{
|
||||||
|
ReplyPacket response = null;
|
||||||
|
try {
|
||||||
|
// Get data
|
||||||
|
byte[] packet = this.session.ReadPacket(PacketSeparator);
|
||||||
|
response = PacketBinConverter.FromBinary(packet);
|
||||||
|
|
||||||
|
// Send ACK
|
||||||
|
this.session.Write(RawPacket.Ack);
|
||||||
|
} catch (FormatException) {
|
||||||
|
// Error... send NACK
|
||||||
|
this.session.Write(RawPacket.Nack);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
|
Loading…
Reference in New Issue
Block a user