From dfdf5ebdb18924ecc0812d663bae826c20e55b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benito=20Palacios=20S=C3=A1nchez?= Date: Wed, 10 Dec 2014 23:43:53 +0100 Subject: [PATCH] Refactoring --- NitroDebugger.UnitTests/GdbClientTests.cs | 60 ++++++++++------------- NitroDebugger/RSP/GdbClient.cs | 35 ++++++++----- 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/NitroDebugger.UnitTests/GdbClientTests.cs b/NitroDebugger.UnitTests/GdbClientTests.cs index 20bfddb..6761a4f 100644 --- a/NitroDebugger.UnitTests/GdbClientTests.cs +++ b/NitroDebugger.UnitTests/GdbClientTests.cs @@ -129,49 +129,29 @@ namespace UnitTests this.serverStream.Write(packetBin, 0, packetBin.Length); } - private void Read() - { - Thread.Sleep(50); - byte[] buffer = new byte[10 * 1024]; - this.serverStream.Read(buffer, 0, buffer.Length); - } - [Test] - public void AskHaltedReason() - { - this.SendPacket("S", "02"); - StopSignal reason = this.client.AskHaltedReason(); - this.Read(); - - Assert.IsTrue(reason.HasFlag(StopSignal.HostBreak)); - } - - [Test] - public void CommandError() + public void CommandUnknownReplyPacket() { + // Send as many times as the maximum counter of Presentation layer for (int i = 0; i < 10; i++) this.SendPacket("@", ""); StopSignal reason = this.client.AskHaltedReason(); - this.Read(); - Assert.IsFalse(this.client.IsConnected); Assert.AreEqual(StopSignal.Unknown, reason); } [Test] - public void CommandInvalidReply() + public void CommandUnexepectedReply() { this.SendPacket("OK", ""); StopSignal reason = this.client.AskHaltedReason(); - this.Read(); - Assert.IsFalse(this.client.IsConnected); Assert.AreEqual(StopSignal.Unknown, reason); } [Test] - public void ConnectionClosedRaiseHandle() + public void CommandConnectionLostRaiseHandle() { this.serverClient.Close(); this.client.LostConnection += new LostConnectionEventHandle(LostConnection); @@ -185,7 +165,7 @@ namespace UnitTests } [Test] - public void ConnectionClosedDoesNotRaiseHandle() + public void CommandConnectionLostDoesNotRaiseHandle() { this.serverClient.Close(); Assert.DoesNotThrow(() => this.client.AskHaltedReason()); @@ -196,42 +176,54 @@ namespace UnitTests { this.SendPacket("S", "02"); bool stopped = this.client.StopExecution(); - this.Read(); - Assert.IsTrue(stopped); } [Test] - public void InterruptError() + public void InterruptInvalidSignal() { + this.SendPacket("S", "03"); + bool stopped = this.client.StopExecution(); + Assert.IsFalse(stopped); + } + + [Test] + public void InterruptUnknownReplyPacket() + { + // Send as many times as the maximum counter of Presentation layer for (int i = 0; i < 10; i++) this.SendPacket("@", ""); - bool stopped = this.client.StopExecution(); - this.Read(); + bool stopped = this.client.StopExecution(); Assert.IsFalse(this.client.IsConnected); Assert.IsFalse(stopped); } [Test] - public void InterruptInvalidReply() + public void InterruptUnexepectedReply() { this.SendPacket("OK", ""); bool stopped = this.client.StopExecution(); - this.Read(); - Assert.IsFalse(this.client.IsConnected); Assert.IsFalse(stopped); } [Test] - public void InterruptConnectionLost() + public void InterruptConnectionLostRaiseHandle() { this.serverClient.Close(); this.client.LostConnection += new LostConnectionEventHandle(LostConnection); bool stopped = this.client.StopExecution(); Assert.IsFalse(stopped); } + + [Test] + public void AskHaltedReason() + { + this.SendPacket("S", "02"); + StopSignal reason = this.client.AskHaltedReason(); + Assert.IsTrue(reason.HasFlag(StopSignal.HostBreak)); + } } } diff --git a/NitroDebugger/RSP/GdbClient.cs b/NitroDebugger/RSP/GdbClient.cs index 64776e6..2121504 100644 --- a/NitroDebugger/RSP/GdbClient.cs +++ b/NitroDebugger/RSP/GdbClient.cs @@ -112,18 +112,20 @@ namespace NitroDebugger.RSP private ReplyPacket SafeInterruption() { ReplyPacket response = null; + bool error = false; try { response = this.presentation.SendInterrupt(); } catch (SocketException) { - OnLostConnection(EventArgs.Empty); + error = true; } catch (ProtocolViolationException) { - this.Disconnect(); - OnLostConnection(EventArgs.Empty); + error = true; } - if (response != null && !(response is StopSignalReply)) { - this.Disconnect(); - OnLostConnection(EventArgs.Empty); + if (response != null && !(response is StopSignalReply)) + error = true; + + if (error) { + NetworkError(); response = null; } @@ -133,19 +135,22 @@ namespace NitroDebugger.RSP private ReplyPacket SafeSending(CommandPacket command, params Type[] validReplyTypes) { ReplyPacket response = null; + bool error = false; + try { this.presentation.SendCommand(command); response = this.presentation.ReceiveReply(); } catch (SocketException) { - OnLostConnection(EventArgs.Empty); + error = true; } catch (ProtocolViolationException) { - this.Disconnect(); - OnLostConnection(EventArgs.Empty); + error = true; } - if (response != null && !ValidateType(response, validReplyTypes)) { - this.Disconnect(); - OnLostConnection(EventArgs.Empty); + if (response != null && !ValidateType(response, validReplyTypes)) + error = true; + + if (error) { + NetworkError(); response = null; } @@ -156,6 +161,12 @@ namespace NitroDebugger.RSP { return validReplyTypes.Contains(reply.GetType()); } + + private void NetworkError() + { + this.Disconnect(); + OnLostConnection(EventArgs.Empty); + } } }