mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +00:00
Instrument lookups, configuration, credentials, CSV entities, snapshots and the alert processing service are read and written asynchronously; the blocking synchronous forms are obsolete wrappers. IAsyncPersistable replaces IPersistable: every type that saves and loads its settings has LoadAsync and SaveAsync only, and nested objects are read with GetValueAsync, LoadEntireAsync and LoadAllAsync. The synchronous Load and Save are the obsolete Ecng extensions. The MultiConnect sample keeps IPersistable for two settings classes, because the settings window of the published StockSharp.Xaml package takes it. Copies that were made by saving and loading are made asynchronously. IMessageAdapter, IIndicator, the risk, commission, PnL, latency, slippage and position managers, IStorageBuffer, Strategy, DiagramElement, ICompositionModel and its nodes get CloneAsync and no longer implement ICloneable; Clone is an obsolete blocking wrapper. A strategy copy keeps every security and portfolio parameter the original holds. Messages, FieldMapping, BankDetails and WithdrawInfo are copied in memory. A CommissionRuleMessage shares its rule and the emulator takes a copy of it. Envelope gets a constructor that takes all three lines and CreateAsync. ToBruteForce and ToBruteForceRandom become ToBruteForceAsync and ToBruteForceRandomAsync and return IAsyncEnumerable; BruteForceOptimizer runs over it and takes the next strategy through an asynchronous delegate. Diagram elements keep their own state in SaveState, OnLoading and LoadState. Undo restores an in-memory snapshot; a chart element holds its series and axes in it by reference. An alert rule keeps its value in the form a message carries it. A diagram strategy applies composition settings that were loaded before the composition was set in SetCompositionAsync or at the start, and saves them while they wait. Strategies get an OnStartedAsync hook; diagram strategies resolve securities, underlyings and strikes before the start so value processing never looks up instruments. Paths.Serialize, SerializeInvariant, AppStartSettings.TrySave, ICredentialsProvider.Save, ISnapshotSerializer.Serialize and the composition registry gain async forms. ICredentialsProvider.Delete becomes DeleteAsync, so loading, saving and deleting the credentials wait for one another. Paths.InstalledVersion and AppNameWithVersion are obsolete in favour of GetInstalledVersionAsync and GetAppNameWithVersionAsync. A file is opened for writing only after the last cancellation check, because opening it empties it; a cancelled read reports the cancellation instead of an empty result. A snapshot flush that failed leaves what it took due for the next one. The indicator templates for C#, F# and Python override LoadAsync and SaveAsync.
129 lines
4.3 KiB
C#
129 lines
4.3 KiB
C#
namespace StockSharp.Algo;
|
|
|
|
/// <summary>
|
|
/// Security native id message adapter.
|
|
/// </summary>
|
|
public class SecurityNativeIdMessageAdapter : MessageAdapterWrapper
|
|
{
|
|
private readonly ISecurityNativeIdManager _manager;
|
|
private readonly bool _ownsManager;
|
|
private bool _isInitialized;
|
|
|
|
/// <summary>
|
|
/// Security native identifier storage provider.
|
|
/// </summary>
|
|
public INativeIdStorageProvider StorageProvider { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SecurityNativeIdMessageAdapter"/>.
|
|
/// </summary>
|
|
/// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
|
|
/// <param name="storageProvider">Security native identifier storage provider.</param>
|
|
public SecurityNativeIdMessageAdapter(IMessageAdapter innerAdapter, INativeIdStorageProvider storageProvider)
|
|
: base(innerAdapter)
|
|
{
|
|
StorageProvider = storageProvider ?? throw new ArgumentNullException(nameof(storageProvider));
|
|
_manager = new SecurityNativeIdManager(this, storageProvider, IsNativeIdentifiersPersistable);
|
|
_manager.ProcessSuspendedRequested += OnProcessSuspendedRequestedAsync;
|
|
_ownsManager = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SecurityNativeIdMessageAdapter"/> with a custom manager.
|
|
/// </summary>
|
|
/// <param name="innerAdapter">The adapter, to which messages will be directed.</param>
|
|
/// <param name="storageProvider">Security native identifier storage provider.</param>
|
|
/// <param name="manager">Security native id manager.</param>
|
|
public SecurityNativeIdMessageAdapter(IMessageAdapter innerAdapter, INativeIdStorageProvider storageProvider, ISecurityNativeIdManager manager)
|
|
: base(innerAdapter)
|
|
{
|
|
StorageProvider = storageProvider ?? throw new ArgumentNullException(nameof(storageProvider));
|
|
_manager = manager ?? throw new ArgumentNullException(nameof(manager));
|
|
_manager.ProcessSuspendedRequested += OnProcessSuspendedRequestedAsync;
|
|
_ownsManager = false;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void Dispose()
|
|
{
|
|
_manager.ProcessSuspendedRequested -= OnProcessSuspendedRequestedAsync;
|
|
|
|
if (_ownsManager)
|
|
_manager.Dispose();
|
|
|
|
base.Dispose();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override async ValueTask OnInnerAdapterNewOutMessageAsync(Message message, CancellationToken cancellationToken)
|
|
{
|
|
if (message.Type == MessageTypes.Connect && !_isInitialized)
|
|
{
|
|
// Initialize the manager from storage on connect
|
|
await _manager.InitializeAsync(StorageName, cancellationToken);
|
|
_isInitialized = true;
|
|
}
|
|
|
|
var (forward, extraOut, loopbackIn) = await _manager.ProcessOutMessageAsync(message, cancellationToken);
|
|
|
|
// Forward the main message first
|
|
if (forward != null)
|
|
await base.OnInnerAdapterNewOutMessageAsync(forward, cancellationToken);
|
|
|
|
// Forward extra output messages
|
|
if (extraOut.Length > 0)
|
|
{
|
|
foreach (var msg in extraOut)
|
|
await base.OnInnerAdapterNewOutMessageAsync(msg, cancellationToken);
|
|
}
|
|
|
|
// Send loopback input messages
|
|
if (loopbackIn.Length > 0)
|
|
{
|
|
foreach (var msg in loopbackIn)
|
|
{
|
|
msg.LoopBack(this);
|
|
await base.OnSendInMessageAsync(msg, cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override async ValueTask OnSendInMessageAsync(Message message, CancellationToken cancellationToken)
|
|
{
|
|
if (message.Type == MessageTypes.Reset)
|
|
{
|
|
_isInitialized = false;
|
|
}
|
|
|
|
var (toInner, toOut) = await _manager.ProcessInMessageAsync(message, cancellationToken);
|
|
|
|
if (toInner.Length > 0)
|
|
{
|
|
foreach (var msg in toInner)
|
|
await base.OnSendInMessageAsync(msg, cancellationToken);
|
|
}
|
|
|
|
if (toOut.Length > 0)
|
|
{
|
|
foreach (var msg in toOut)
|
|
await RaiseNewOutMessageAsync(msg, cancellationToken);
|
|
}
|
|
}
|
|
|
|
private async ValueTask OnProcessSuspendedRequestedAsync(SecurityId securityId, CancellationToken cancellationToken)
|
|
{
|
|
await RaiseNewOutMessageAsync(new ProcessSuspendedMessage(this, securityId), cancellationToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a copy of <see cref="SecurityNativeIdMessageAdapter"/>.
|
|
/// </summary>
|
|
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
|
|
/// <returns>Copy.</returns>
|
|
public override async ValueTask<IMessageAdapter> CloneAsync(CancellationToken cancellationToken)
|
|
{
|
|
return new SecurityNativeIdMessageAdapter(await InnerAdapter.CloneAsync(cancellationToken), StorageProvider);
|
|
}
|
|
}
|