mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-06 01:22:11 +00:00
WHAT LEAVES A SOCKET. The order element's Matched and Finished sockets are both declared as order sockets, and both emitted what caused the ending rather than the order itself - a MyTrade and an OrderFail. Everything wired behind them is entitled to an order; the cancel element casts its input straight to one, and the cast throws somewhere that names neither the element that emitted nor the socket it left by. Cancelled had been emitting the order all along, so the two were simply out of line with their neighbour. That promise is now checked wherever a diagram test records an output, rather than one element at a time: a value arriving at the recording element was made when it crossed the link, and the value it was made from is the one the upstream element emitted, under the socket it emitted from - so the declared type is there to compare against. Sockets declared as Any promise nothing and are skipped. Fifty-five elements live here and eighteen are built by a test at all, so this covers what is driven and leaves the rest where they were. WHICH PROPERTY A CONVERTER READS. The author picks a property from the socket's type, and it was read back off the object without that type. A message implements an interface member explicitly, and one name means different values on different interfaces of the same class, so the value read was not always the value offered. The read now names the type it was chosen from. A TRADE COUNTED ONCE. The protection element acted on every trade it was handed, and a trade re-reported - which is ordinary order flow - protected the same fill twice. Trades are now remembered per order, by id where the venue gives one and by string id where it gives that instead, and forgotten on reset so a backtest's second run starts as the first did. AND A DEBUGGER THAT CAN BE CONTINUED FROM ITS OWN BREAK. The break notification fired before the object marked itself as waiting, so a handler that continued immediately - the plain way to drive a debugger - continued something that did not yet consider itself stopped, and the run waited for a second continue nobody knew to give. The state is set first and the notification follows.
205 lines
5.2 KiB
C#
205 lines
5.2 KiB
C#
namespace StockSharp.Diagram;
|
|
|
|
using Ecng.Configuration;
|
|
|
|
/// <summary>
|
|
/// The synchronization object for the composite elements debugger.
|
|
/// </summary>
|
|
public class DebuggerSyncObject : ViewModelBase
|
|
{
|
|
private readonly Func<DiagramSocket, bool> _isBreak;
|
|
private readonly Action<DebuggerSyncObject> _breakAction;
|
|
private readonly Action<DebuggerSyncObject> _errorAction;
|
|
private readonly ManualResetEventSlim _inputSync = new(false);
|
|
private readonly DiagramDebugger _debugger;
|
|
private readonly CompositionDiagramElement _rootElement;
|
|
|
|
private DiagramElement _currentElement;
|
|
private bool _waitOnNext;
|
|
private DiagramSocket _currentSocket;
|
|
private Exception _currentError;
|
|
|
|
private class DebuggerSyncObjectGuiWrapper(DebuggerSyncObject obj)
|
|
: DispatcherNotifiableObject<DebuggerSyncObject>(ConfigManager.GetService<IDispatcher>(), obj, TimeSpan.FromSeconds(0.5))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gui wrapper for property binding.
|
|
/// </summary>
|
|
public INotifyPropertyChanged GuiWrapper {get;}
|
|
|
|
/// <summary>
|
|
/// The current element.
|
|
/// </summary>
|
|
public DiagramElement CurrentElement
|
|
{
|
|
get => _currentElement;
|
|
private set => SetField(ref _currentElement, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The current socket.
|
|
/// </summary>
|
|
public DiagramSocket CurrentSocket
|
|
{
|
|
get => _currentSocket;
|
|
private set => SetField(ref _currentSocket, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The current error.
|
|
/// </summary>
|
|
public Exception CurrentError
|
|
{
|
|
get => _currentError;
|
|
private set => SetField(ref _currentError, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DebuggerSyncObject"/>.
|
|
/// </summary>
|
|
/// <param name="debugger"><see cref="DiagramDebugger"/></param>
|
|
/// <param name="rootElement">The root diagram element.</param>
|
|
/// <param name="isBreak">The handler that returns a stop flag for the socket.</param>
|
|
/// <param name="breakAction">The action with the element at stop.</param>
|
|
/// <param name="errorAction">The action with the element at error.</param>
|
|
public DebuggerSyncObject(DiagramDebugger debugger, CompositionDiagramElement rootElement, Func<DiagramSocket, bool> isBreak, Action<DebuggerSyncObject> breakAction, Action<DebuggerSyncObject> errorAction)
|
|
{
|
|
_debugger = debugger ?? throw new ArgumentNullException(nameof(debugger));
|
|
_rootElement = rootElement ?? throw new ArgumentNullException(nameof(rootElement));
|
|
_isBreak = isBreak ?? throw new ArgumentNullException(nameof(isBreak));
|
|
_breakAction = breakAction ?? throw new ArgumentNullException(nameof(breakAction));
|
|
_errorAction = errorAction ?? throw new ArgumentNullException(nameof(errorAction));
|
|
|
|
GuiWrapper = new DebuggerSyncObjectGuiWrapper(this);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void DisposeManaged()
|
|
{
|
|
_inputSync.Dispose();
|
|
GuiWrapper.DoDispose();
|
|
base.DisposeManaged();
|
|
}
|
|
|
|
/// <summary>
|
|
/// <see langword="true" />, if the debugger is stopped at the entry of the diagram element. Otherwise, <see langword="false" />.
|
|
/// </summary>
|
|
public bool IsWaitingOnInput { get; private set; }
|
|
|
|
/// <summary>
|
|
/// <see langword="true" />, if the debugger is stopped at the exit of the diagram element. Otherwise, <see langword="false" />.
|
|
/// </summary>
|
|
public bool IsWaitingOnOutput { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Try wait on socket.
|
|
/// </summary>
|
|
/// <param name="socket"><see cref="DiagramSocket"/></param>
|
|
/// <param name="isOnInput">Is wait on input.</param>
|
|
/// <returns>Operation result.</returns>
|
|
public bool TryWait(DiagramSocket socket, bool isOnInput)
|
|
{
|
|
if (_debugger.IsDisabled)
|
|
return false;
|
|
|
|
if (!_isBreak(socket) && !_waitOnNext)
|
|
{
|
|
if (socket.Parent == CurrentElement)
|
|
CurrentElement = null;
|
|
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
socket.IsBreakActive = true;
|
|
CurrentSocket = socket;
|
|
CurrentElement = socket.Parent;
|
|
|
|
SetIsWaiting(isOnInput, true);
|
|
|
|
_breakAction?.Invoke(this);
|
|
|
|
_waitOnNext = false;
|
|
_inputSync.Wait();
|
|
_inputSync.Reset();
|
|
}
|
|
finally
|
|
{
|
|
socket.IsBreakActive = false;
|
|
CurrentSocket = null;
|
|
CurrentElement = null;
|
|
}
|
|
|
|
SetIsWaiting(isOnInput, false);
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Try wait on error.
|
|
/// </summary>
|
|
/// <param name="element"><see cref="DiagramElement"/></param>
|
|
/// <param name="error">Error.</param>
|
|
/// <returns>Operation result.</returns>
|
|
public bool TryWaitOnError(DiagramElement element, Exception error)
|
|
{
|
|
if (element is null)
|
|
throw new ArgumentNullException(nameof(element));
|
|
|
|
if (error is null)
|
|
throw new ArgumentNullException(nameof(error));
|
|
|
|
if (_debugger.IsDisabled)
|
|
return false;
|
|
|
|
if (element == _rootElement)
|
|
return false;
|
|
|
|
CurrentError = error;
|
|
CurrentElement = element;
|
|
|
|
SetIsWaiting(true, true);
|
|
|
|
_errorAction?.Invoke(this);
|
|
|
|
_waitOnNext = false;
|
|
_inputSync.Wait();
|
|
_inputSync.Reset();
|
|
|
|
CurrentError = null;
|
|
CurrentElement = null;
|
|
|
|
SetIsWaiting(true, false);
|
|
|
|
return true;
|
|
}
|
|
|
|
private void SetIsWaiting(bool isOnInput, bool value)
|
|
{
|
|
if (isOnInput)
|
|
IsWaitingOnInput = value;
|
|
else
|
|
IsWaitingOnOutput = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// To set the flag for waiting at the entry of the next diagram element.
|
|
/// </summary>
|
|
public void SetWaitOnNext()
|
|
{
|
|
_waitOnNext = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Continue.
|
|
/// </summary>
|
|
public void Continue()
|
|
{
|
|
if (IsWaitingOnInput || IsWaitingOnOutput)
|
|
_inputSync.Set();
|
|
}
|
|
}
|