mirror of
https://github.com/danbulant/Cosmos
synced 2026-06-06 16:22:40 +00:00
Code cleanup.
This commit is contained in:
parent
e970ebfbd2
commit
591c23392b
4 changed files with 61 additions and 154 deletions
|
|
@ -5,7 +5,13 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
{
|
{
|
||||||
partial class DebugConnector
|
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)
|
if (count > 100 || count <= 0 || bytes.Length == 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,16 @@
|
||||||
using System;
|
using System.Linq;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Cosmos.Debug.DebugConnectors
|
namespace Cosmos.Debug.DebugConnectors
|
||||||
{
|
{
|
||||||
partial class DebugConnector
|
partial class DebugConnector
|
||||||
{
|
{
|
||||||
protected void WaitForMessage()
|
protected void WaitForMessage() => Next(1, PacketMsg);
|
||||||
{
|
|
||||||
Next(1, PacketMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void PacketTextSize(byte[] aPacket)
|
protected void PacketTextSize(byte[] aPacket) => Next(GetUInt16(aPacket, 0), PacketText);
|
||||||
{
|
|
||||||
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));
|
Next(4, data => PacketOtherChannelSize(aChannel, aPacket[0], data));
|
||||||
}
|
|
||||||
|
|
||||||
protected void PacketOtherChannelSize(byte aChannel, byte aCommand, byte[] aPacket)
|
protected void PacketOtherChannelSize(byte aChannel, byte aCommand, byte[] aPacket)
|
||||||
{
|
{
|
||||||
|
|
@ -28,10 +19,7 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
Next(xPacketSize, data => PacketChannel(aChannel, aCommand, data));
|
Next(xPacketSize, data => PacketChannel(aChannel, aCommand, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketMessageBoxTextSize(byte[] aPacket)
|
protected void PacketMessageBoxTextSize(byte[] aPacket) => Next(GetUInt16(aPacket, 0), PacketMessageBoxText);
|
||||||
{
|
|
||||||
Next(GetUInt16(aPacket, 0), PacketMessageBoxText);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void PacketMethodContext(byte[] aPacket)
|
protected void PacketMethodContext(byte[] aPacket)
|
||||||
{
|
{
|
||||||
|
|
@ -47,28 +35,19 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
|
|
||||||
protected void PacketRegisters(byte[] aPacket)
|
protected void PacketRegisters(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdRegisters != null)
|
CmdRegisters?.Invoke(aPacket.ToArray());
|
||||||
{
|
|
||||||
CmdRegisters(aPacket.ToArray());
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketFrame(byte[] aPacket)
|
protected void PacketFrame(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdFrame != null)
|
CmdFrame?.Invoke(aPacket.ToArray());
|
||||||
{
|
|
||||||
CmdFrame(aPacket.ToArray());
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketPong(byte[] aPacket)
|
protected void PacketPong(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdPong != null)
|
CmdPong?.Invoke(aPacket.ToArray());
|
||||||
{
|
|
||||||
CmdPong(aPacket.ToArray());
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,20 +55,15 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
{
|
{
|
||||||
if (SigReceived)
|
if (SigReceived)
|
||||||
{
|
{
|
||||||
if (CmdChannel != null)
|
CmdChannel?.Invoke(channel, command, aPacket);
|
||||||
{
|
|
||||||
CmdChannel(channel, command, aPacket);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketNullReferenceOccurred(byte[] aPacket)
|
protected void PacketNullReferenceOccurred(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdNullReferenceOccurred != null)
|
CmdNullReferenceOccurred?.Invoke(GetUInt32(aPacket, 0));
|
||||||
{
|
|
||||||
CmdNullReferenceOccurred(GetUInt32(aPacket, 0));
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -125,37 +99,25 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
|
|
||||||
protected void PacketStackCorruptionOccurred(byte[] aPacket)
|
protected void PacketStackCorruptionOccurred(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdStackCorruptionOccurred != null)
|
CmdStackCorruptionOccurred?.Invoke(GetUInt32(aPacket, 0));
|
||||||
{
|
|
||||||
CmdStackCorruptionOccurred(GetUInt32(aPacket, 0));
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketStackOverflowOccurred(byte[] aPacket)
|
protected void PacketStackOverflowOccurred(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdStackOverflowOccurred != null)
|
CmdStackOverflowOccurred?.Invoke(GetUInt32(aPacket, 0));
|
||||||
{
|
|
||||||
CmdStackOverflowOccurred(GetUInt32(aPacket, 0));
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketInterruptOccurred(byte[] aPacket)
|
protected void PacketInterruptOccurred(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdInterruptOccurred != null)
|
CmdInterruptOccurred?.Invoke(GetUInt32(aPacket, 0));
|
||||||
{
|
|
||||||
CmdInterruptOccurred(GetUInt32(aPacket, 0));
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void PacketStack(byte[] aPacket)
|
protected void PacketStack(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdStack != null)
|
CmdStack?.Invoke(aPacket.ToArray());
|
||||||
{
|
|
||||||
CmdStack(aPacket.ToArray());
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,10 +165,7 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
|
|
||||||
protected void PacketCoreDump(byte[] aPacket)
|
protected void PacketCoreDump(byte[] aPacket)
|
||||||
{
|
{
|
||||||
if (CmdCoreDump != null)
|
CmdCoreDump?.Invoke(aPacket.ToArray());
|
||||||
{
|
|
||||||
CmdCoreDump(aPacket.ToArray());
|
|
||||||
}
|
|
||||||
WaitForMessage();
|
WaitForMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
namespace Cosmos.Debug.DebugConnectors
|
namespace Cosmos.Debug.DebugConnectors
|
||||||
|
|
@ -118,37 +117,34 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
InitializeBackground();
|
InitializeBackground();
|
||||||
DoConnected();
|
DoConnected();
|
||||||
Next(1, WaitForSignature);
|
Next(1, WaitForSignature);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
aCancellationToken.ThrowIfCancellationRequested();
|
if (aCancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
while (mPendingWrites.TryTake(out var xPendingOutgoing))
|
||||||
|
{
|
||||||
|
xPendingOutgoing.Packet = null;
|
||||||
|
xPendingOutgoing.Completed.Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!GetIsConnectedToDebugStub())
|
if (!GetIsConnectedToDebugStub())
|
||||||
{
|
{
|
||||||
ConnectionLost(null);
|
ConnectionLost(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessPendingActions();
|
ProcessPendingActions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
while (true)
|
CmdMessageBox("Error occurred in DebugConnector.ThreadMethod: " + e.ToString());
|
||||||
{
|
}
|
||||||
Outgoing xPendingOutgoing;
|
|
||||||
if (!mPendingWrites.TryTake(out xPendingOutgoing))
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
xPendingOutgoing.Packet = null;
|
|
||||||
xPendingOutgoing.Completed.Set();
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
catch (Exception E)
|
|
||||||
{
|
|
||||||
CmdMessageBox("Error occurred in DebugConnector.ThreadMethod: " + E.ToString());
|
|
||||||
}
|
|
||||||
ConnectionLost(null);
|
ConnectionLost(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ using System.Threading;
|
||||||
|
|
||||||
namespace Cosmos.Debug.DebugConnectors
|
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
|
/// 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.
|
/// independent. Sub-classes exist that manage the wire level details of the communications.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -53,32 +54,21 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
|
|
||||||
protected void HandleError(Exception E)
|
protected void HandleError(Exception E)
|
||||||
{
|
{
|
||||||
if (Error != null)
|
if (Error == null)
|
||||||
{
|
|
||||||
Error(E);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
throw new Exception("Unhandled exception occurred!", E);
|
throw new Exception("Unhandled exception occurred!", E);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Error(E);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Descendants must invoke this method whenever they detect an incoming connection.</summary>
|
/// <summary>Descendants must invoke this method whenever they detect an incoming connection.</summary>
|
||||||
public void DoConnected()
|
public void DoConnected() => Connected?.Invoke();
|
||||||
{
|
|
||||||
if (Connected != null)
|
|
||||||
{
|
|
||||||
Connected();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Defines the handler to be invoked when a connection occurs on this condector. This
|
/// <summary>Defines the handler to be invoked when a connection occurs on this condector. This
|
||||||
/// method is for use by the AD7Process instance.</summary>
|
/// method is for use by the AD7Process instance.</summary>
|
||||||
/// <param name="handler">The handler to be notified when a connection occur.</param>
|
/// <param name="handler">The handler to be notified when a connection occur.</param>
|
||||||
public void SetConnectionHandler(Action handler)
|
public void SetConnectionHandler(Action handler) => Connected = handler;
|
||||||
{
|
|
||||||
Connected = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Method to do debug logging of the debug connector itself.
|
/// Method to do debug logging of the debug connector itself.
|
||||||
|
|
@ -96,58 +86,22 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
if (IsConnected || aOnlyIfConnected == false)
|
if (IsConnected || aOnlyIfConnected == false)
|
||||||
{
|
{
|
||||||
System.Diagnostics.Debug.WriteLine(aMsg);
|
System.Diagnostics.Debug.WriteLine(aMsg);
|
||||||
if (OnDebugMsg != null)
|
OnDebugMsg?.Invoke(aMsg);
|
||||||
{
|
|
||||||
OnDebugMsg(aMsg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected bool mSigReceived = false;
|
protected bool mSigReceived = false;
|
||||||
public bool SigReceived
|
public bool SigReceived => mSigReceived;
|
||||||
{
|
|
||||||
get { return mSigReceived; }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is stream alive? Other side may not be responsive yet, use SigReceived instead for this instead
|
// Is stream alive? Other side may not be responsive yet, use SigReceived instead for this instead
|
||||||
public abstract bool IsConnected
|
public abstract bool IsConnected { get; }
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int mDataSize;
|
private int mDataSize;
|
||||||
private List<byte[]> MethodContextDatas = new List<byte[]>();
|
private List<byte[]> MethodContextDatas = new List<byte[]>();
|
||||||
private List<byte[]> MemoryDatas = new List<byte[]>();
|
private List<byte[]> MemoryDatas = new List<byte[]>();
|
||||||
|
|
||||||
public void DeleteBreakpoint(int aID)
|
public void DeleteBreakpoint(int aID) => SetBreakpoint(aID, 0);
|
||||||
{
|
|
||||||
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)
|
protected void PacketMsg(byte[] aPacket)
|
||||||
{
|
{
|
||||||
mCurrentMsgType = aPacket[0];
|
mCurrentMsgType = aPacket[0];
|
||||||
|
|
@ -308,19 +262,20 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Dispose()
|
protected virtual void Dispose(bool disposing)
|
||||||
{
|
{
|
||||||
if (mDebugWriter != null)
|
if (disposing)
|
||||||
{
|
|
||||||
mDebugWriter.Dispose();
|
|
||||||
mDebugWriter = null;
|
|
||||||
}
|
|
||||||
if (mBackgroundThread != null)
|
|
||||||
{
|
{
|
||||||
|
mDebugWriter?.Dispose();
|
||||||
|
|
||||||
mCancellationTokenSource.Cancel();
|
mCancellationTokenSource.Cancel();
|
||||||
mBackgroundThread.Join();
|
mBackgroundThread?.Join();
|
||||||
mBackgroundThread = null;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Dispose(true);
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -351,16 +306,7 @@ namespace Cosmos.Debug.DebugConnectors
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void SendPacketToConsole(byte[] aPacket)
|
protected void SendPacketToConsole(byte[] aPacket) => CmdChannel(129, 0, aPacket);
|
||||||
{
|
protected void SendTextToConsole(string aText) => SendPacketToConsole(Encoding.UTF8.GetBytes(aText));
|
||||||
CmdChannel(129, 0, aPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void SendTextToConsole(string aText)
|
|
||||||
{
|
|
||||||
SendPacketToConsole(Encoding.UTF8.GetBytes(aText));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue