From 591c23392be4f79fc5072f98b425eea1c145e0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Sat, 10 Feb 2018 19:38:02 +0000 Subject: [PATCH] Code cleanup. --- .../DebugConnector.Helpers.cs | 8 +- .../DebugConnector.Receiving.cs | 73 +++---------- .../DebugConnector.Thread.cs | 34 +++--- .../DebugConnector.cs | 100 ++++-------------- 4 files changed, 61 insertions(+), 154 deletions(-) diff --git a/source/Cosmos.Debug.DebugConnectors/DebugConnector.Helpers.cs b/source/Cosmos.Debug.DebugConnectors/DebugConnector.Helpers.cs index 2878783b3..a2bfb8130 100644 --- a/source/Cosmos.Debug.DebugConnectors/DebugConnector.Helpers.cs +++ b/source/Cosmos.Debug.DebugConnectors/DebugConnector.Helpers.cs @@ -5,7 +5,13 @@ namespace Cosmos.Debug.DebugConnectors { partial class DebugConnector { - internal static string BytesToString(byte[] bytes, int index, int count) + protected static ushort GetUInt16(byte[] aBytes, int aOffset) => BitConverter.ToUInt16(aBytes, aOffset); + protected static uint GetUInt32(byte[] aBytes, int aOffset) => BitConverter.ToUInt32(aBytes, aOffset); + protected static ulong GetUInt64(byte[] aBytes, int aOffset) => BitConverter.ToUInt64(aBytes, aOffset); + protected static float GetSingle(byte[] aBytes, int aOffset) => BitConverter.ToSingle(aBytes, aOffset); + protected static double GetDouble(byte[] aBytes, int aOffset) => BitConverter.ToDouble(aBytes, aOffset); + + protected static string BytesToString(byte[] bytes, int index, int count) { if (count > 100 || count <= 0 || bytes.Length == 0) { diff --git a/source/Cosmos.Debug.DebugConnectors/DebugConnector.Receiving.cs b/source/Cosmos.Debug.DebugConnectors/DebugConnector.Receiving.cs index 2f01ba7b2..1bf90f822 100644 --- a/source/Cosmos.Debug.DebugConnectors/DebugConnector.Receiving.cs +++ b/source/Cosmos.Debug.DebugConnectors/DebugConnector.Receiving.cs @@ -1,25 +1,16 @@ -using System; -using System.Linq; +using System.Linq; using System.Text; namespace Cosmos.Debug.DebugConnectors { partial class DebugConnector { - protected void WaitForMessage() - { - Next(1, PacketMsg); - } + protected void WaitForMessage() => Next(1, PacketMsg); - protected void PacketTextSize(byte[] aPacket) - { - Next(GetUInt16(aPacket, 0), PacketText); - } + protected void PacketTextSize(byte[] aPacket) => Next(GetUInt16(aPacket, 0), PacketText); - protected void PacketOtherChannelCommand(byte aChannel, byte[] aPacket) - { + protected void PacketOtherChannelCommand(byte aChannel, byte[] aPacket) => Next(4, data => PacketOtherChannelSize(aChannel, aPacket[0], data)); - } protected void PacketOtherChannelSize(byte aChannel, byte aCommand, byte[] aPacket) { @@ -28,10 +19,7 @@ namespace Cosmos.Debug.DebugConnectors Next(xPacketSize, data => PacketChannel(aChannel, aCommand, data)); } - protected void PacketMessageBoxTextSize(byte[] aPacket) - { - Next(GetUInt16(aPacket, 0), PacketMessageBoxText); - } + protected void PacketMessageBoxTextSize(byte[] aPacket) => Next(GetUInt16(aPacket, 0), PacketMessageBoxText); protected void PacketMethodContext(byte[] aPacket) { @@ -47,28 +35,19 @@ namespace Cosmos.Debug.DebugConnectors protected void PacketRegisters(byte[] aPacket) { - if (CmdRegisters != null) - { - CmdRegisters(aPacket.ToArray()); - } + CmdRegisters?.Invoke(aPacket.ToArray()); WaitForMessage(); } protected void PacketFrame(byte[] aPacket) { - if (CmdFrame != null) - { - CmdFrame(aPacket.ToArray()); - } + CmdFrame?.Invoke(aPacket.ToArray()); WaitForMessage(); } protected void PacketPong(byte[] aPacket) { - if (CmdPong != null) - { - CmdPong(aPacket.ToArray()); - } + CmdPong?.Invoke(aPacket.ToArray()); WaitForMessage(); } @@ -76,20 +55,15 @@ namespace Cosmos.Debug.DebugConnectors { if (SigReceived) { - if (CmdChannel != null) - { - CmdChannel(channel, command, aPacket); - } + CmdChannel?.Invoke(channel, command, aPacket); } + WaitForMessage(); } protected void PacketNullReferenceOccurred(byte[] aPacket) { - if (CmdNullReferenceOccurred != null) - { - CmdNullReferenceOccurred(GetUInt32(aPacket, 0)); - } + CmdNullReferenceOccurred?.Invoke(GetUInt32(aPacket, 0)); WaitForMessage(); } @@ -125,37 +99,25 @@ namespace Cosmos.Debug.DebugConnectors protected void PacketStackCorruptionOccurred(byte[] aPacket) { - if (CmdStackCorruptionOccurred != null) - { - CmdStackCorruptionOccurred(GetUInt32(aPacket, 0)); - } + CmdStackCorruptionOccurred?.Invoke(GetUInt32(aPacket, 0)); WaitForMessage(); } protected void PacketStackOverflowOccurred(byte[] aPacket) { - if (CmdStackOverflowOccurred != null) - { - CmdStackOverflowOccurred(GetUInt32(aPacket, 0)); - } + CmdStackOverflowOccurred?.Invoke(GetUInt32(aPacket, 0)); WaitForMessage(); } protected void PacketInterruptOccurred(byte[] aPacket) { - if (CmdInterruptOccurred != null) - { - CmdInterruptOccurred(GetUInt32(aPacket, 0)); - } + CmdInterruptOccurred?.Invoke(GetUInt32(aPacket, 0)); WaitForMessage(); } protected void PacketStack(byte[] aPacket) { - if (CmdStack != null) - { - CmdStack(aPacket.ToArray()); - } + CmdStack?.Invoke(aPacket.ToArray()); WaitForMessage(); } @@ -203,10 +165,7 @@ namespace Cosmos.Debug.DebugConnectors protected void PacketCoreDump(byte[] aPacket) { - if (CmdCoreDump != null) - { - CmdCoreDump(aPacket.ToArray()); - } + CmdCoreDump?.Invoke(aPacket.ToArray()); WaitForMessage(); } diff --git a/source/Cosmos.Debug.DebugConnectors/DebugConnector.Thread.cs b/source/Cosmos.Debug.DebugConnectors/DebugConnector.Thread.cs index 029db1922..ca1bb950d 100644 --- a/source/Cosmos.Debug.DebugConnectors/DebugConnector.Thread.cs +++ b/source/Cosmos.Debug.DebugConnectors/DebugConnector.Thread.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Threading; namespace Cosmos.Debug.DebugConnectors @@ -118,37 +117,34 @@ namespace Cosmos.Debug.DebugConnectors InitializeBackground(); DoConnected(); Next(1, WaitForSignature); + while (true) { - aCancellationToken.ThrowIfCancellationRequested(); + if (aCancellationToken.IsCancellationRequested) + { + while (mPendingWrites.TryTake(out var xPendingOutgoing)) + { + xPendingOutgoing.Packet = null; + xPendingOutgoing.Completed.Set(); + } + + return; + } if (!GetIsConnectedToDebugStub()) { ConnectionLost(null); return; } + ProcessPendingActions(); } } - catch (OperationCanceledException) + catch (Exception e) { - while (true) - { - Outgoing xPendingOutgoing; - if (!mPendingWrites.TryTake(out xPendingOutgoing)) - { - break; - } - xPendingOutgoing.Packet = null; - xPendingOutgoing.Completed.Set(); - } + CmdMessageBox("Error occurred in DebugConnector.ThreadMethod: " + e.ToString()); + } - return; - } - catch (Exception E) - { - CmdMessageBox("Error occurred in DebugConnector.ThreadMethod: " + E.ToString()); - } ConnectionLost(null); } diff --git a/source/Cosmos.Debug.DebugConnectors/DebugConnector.cs b/source/Cosmos.Debug.DebugConnectors/DebugConnector.cs index fc6b57a82..8eb4fda5e 100644 --- a/source/Cosmos.Debug.DebugConnectors/DebugConnector.cs +++ b/source/Cosmos.Debug.DebugConnectors/DebugConnector.cs @@ -9,7 +9,8 @@ using System.Threading; namespace Cosmos.Debug.DebugConnectors { - /// Handles the dialog between the Debug Stub embedded in a debugged Cosmos Kernel and + /// + /// Handles the dialog between the Debug Stub embedded in a debugged Cosmos Kernel and /// our Debug Engine hosted in Visual Studio. This abstract class is communication protocol /// independent. Sub-classes exist that manage the wire level details of the communications. /// @@ -53,32 +54,21 @@ namespace Cosmos.Debug.DebugConnectors protected void HandleError(Exception E) { - if (Error != null) - { - Error(E); - } - else + if (Error == null) { throw new Exception("Unhandled exception occurred!", E); } + + Error(E); } /// Descendants must invoke this method whenever they detect an incoming connection. - public void DoConnected() - { - if (Connected != null) - { - Connected(); - } - } + public void DoConnected() => Connected?.Invoke(); /// Defines the handler to be invoked when a connection occurs on this condector. This /// method is for use by the AD7Process instance. /// The handler to be notified when a connection occur. - public void SetConnectionHandler(Action handler) - { - Connected = handler; - } + public void SetConnectionHandler(Action handler) => Connected = handler; /// /// Method to do debug logging of the debug connector itself. @@ -96,58 +86,22 @@ namespace Cosmos.Debug.DebugConnectors if (IsConnected || aOnlyIfConnected == false) { System.Diagnostics.Debug.WriteLine(aMsg); - if (OnDebugMsg != null) - { - OnDebugMsg(aMsg); - } + OnDebugMsg?.Invoke(aMsg); } } protected bool mSigReceived = false; - public bool SigReceived - { - get { return mSigReceived; } - } + public bool SigReceived => mSigReceived; // Is stream alive? Other side may not be responsive yet, use SigReceived instead for this instead - public abstract bool IsConnected - { - get; - } + public abstract bool IsConnected { get; } private int mDataSize; private List MethodContextDatas = new List(); private List MemoryDatas = new List(); - public void DeleteBreakpoint(int aID) - { - SetBreakpoint(aID, 0); - } + public void DeleteBreakpoint(int aID) => SetBreakpoint(aID, 0); - protected ushort GetUInt16(byte[] aBytes, int aOffset) - { - return BitConverter.ToUInt16(aBytes, aOffset); - } - - protected uint GetUInt32(byte[] aBytes, int aOffset) - { - return BitConverter.ToUInt32(aBytes, aOffset); - } - - protected ulong GetUInt64(byte[] aBytes, int aOffset) - { - return BitConverter.ToUInt64(aBytes, aOffset); - } - - protected float GetSingle(byte[] aBytes, int aOffset) - { - return BitConverter.ToSingle(aBytes, aOffset); - } - - protected double GetDouble(byte[] aBytes, int aOffset) - { - return BitConverter.ToDouble(aBytes, aOffset); - } protected void PacketMsg(byte[] aPacket) { mCurrentMsgType = aPacket[0]; @@ -308,19 +262,20 @@ namespace Cosmos.Debug.DebugConnectors } } - public virtual void Dispose() + protected virtual void Dispose(bool disposing) { - if (mDebugWriter != null) - { - mDebugWriter.Dispose(); - mDebugWriter = null; - } - if (mBackgroundThread != null) + if (disposing) { + mDebugWriter?.Dispose(); + mCancellationTokenSource.Cancel(); - mBackgroundThread.Join(); - mBackgroundThread = null; + mBackgroundThread?.Join(); } + } + + public void Dispose() + { + Dispose(true); GC.SuppressFinalize(this); } @@ -351,16 +306,7 @@ namespace Cosmos.Debug.DebugConnectors } } - protected void SendPacketToConsole(byte[] aPacket) - { - CmdChannel(129, 0, aPacket); - } - - protected void SendTextToConsole(string aText) - { - SendPacketToConsole(Encoding.UTF8.GetBytes(aText)); - } - - + protected void SendPacketToConsole(byte[] aPacket) => CmdChannel(129, 0, aPacket); + protected void SendTextToConsole(string aText) => SendPacketToConsole(Encoding.UTF8.GetBytes(aText)); } }