Code cleanup.

This commit is contained in:
José Pedro 2018-02-10 19:38:02 +00:00
parent e970ebfbd2
commit 591c23392b
No known key found for this signature in database
GPG key ID: B8247B9301707B83
4 changed files with 61 additions and 154 deletions

View file

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

View file

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

View file

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

View file

@ -9,7 +9,8 @@ using System.Threading;
namespace Cosmos.Debug.DebugConnectors
{
/// <summary>Handles the dialog between the Debug Stub embedded in a debugged Cosmos Kernel and
/// <summary>
/// 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.
/// </summary>
@ -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);
}
/// <summary>Descendants must invoke this method whenever they detect an incoming connection.</summary>
public void DoConnected()
{
if (Connected != null)
{
Connected();
}
}
public void DoConnected() => Connected?.Invoke();
/// <summary>Defines the handler to be invoked when a connection occurs on this condector. This
/// method is for use by the AD7Process instance.</summary>
/// <param name="handler">The handler to be notified when a connection occur.</param>
public void SetConnectionHandler(Action handler)
{
Connected = handler;
}
public void SetConnectionHandler(Action handler) => Connected = handler;
/// <summary>
/// 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<byte[]> MethodContextDatas = new List<byte[]>();
private List<byte[]> MemoryDatas = new List<byte[]>();
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));
}
}