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.
86 lines
2.8 KiB
C#
86 lines
2.8 KiB
C#
namespace StockSharp.Configuration;
|
|
|
|
using System.Threading;
|
|
|
|
using Ecng.Common;
|
|
|
|
using Nito.AsyncEx;
|
|
|
|
/// <summary>
|
|
/// Default implementation of <see cref="ICredentialsProvider"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="DefaultCredentialsProvider"/>.
|
|
/// </remarks>
|
|
/// <param name="fileSystem">File system. If null, uses <see cref="Paths.FileSystem"/>.</param>
|
|
/// <param name="credentialsFile">Credentials file path. If null, uses <see cref="Paths.CredentialsFile"/>.</param>
|
|
/// <param name="companyPath">Company directory path. If null, uses <see cref="Paths.CompanyPath"/>.</param>
|
|
public class DefaultCredentialsProvider(
|
|
IFileSystem fileSystem = null,
|
|
string credentialsFile = null,
|
|
string companyPath = null) : ICredentialsProvider
|
|
{
|
|
private readonly AsyncLock _lock = new();
|
|
private readonly IFileSystem _fileSystem = fileSystem ?? Paths.FileSystem;
|
|
private readonly string _credentialsFile = credentialsFile ?? Paths.CredentialsFile;
|
|
private readonly string _companyPath = companyPath ?? Paths.CompanyPath;
|
|
|
|
private ServerCredentials _credentials;
|
|
|
|
async ValueTask<ServerCredentials> ICredentialsProvider.TryLoadAsync(CancellationToken cancellationToken)
|
|
{
|
|
using (await _lock.LockAsync(cancellationToken))
|
|
{
|
|
if (_credentials != null)
|
|
return await _credentials.CopyAsync(cancellationToken);
|
|
|
|
try
|
|
{
|
|
if (!_credentialsFile.IsConfigExists(_fileSystem))
|
|
return null;
|
|
|
|
var credentials = new ServerCredentials();
|
|
await credentials.LoadIfNotNullAsync(await _credentialsFile.DeserializeAsync<SettingsStorage>(_fileSystem, cancellationToken), cancellationToken);
|
|
|
|
_credentials = await credentials.CopyAsync(cancellationToken);
|
|
|
|
return credentials;
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested)
|
|
{
|
|
ex.LogError();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
async ValueTask ICredentialsProvider.SaveAsync(ServerCredentials credentials, bool keepSecret, CancellationToken cancellationToken)
|
|
{
|
|
if (credentials is null)
|
|
throw new ArgumentNullException(nameof(credentials));
|
|
|
|
using (await _lock.LockAsync(cancellationToken))
|
|
{
|
|
_credentials = await credentials.CopyAsync(cancellationToken);
|
|
|
|
_fileSystem.CreateDirectory(_companyPath);
|
|
|
|
var clone = await credentials.CopyAsync(cancellationToken);
|
|
if (!keepSecret)
|
|
clone.Password = clone.Token = null;
|
|
|
|
await (await clone.SaveAsync(cancellationToken)).SerializeAsync(_fileSystem, _credentialsFile, true, cancellationToken);
|
|
}
|
|
}
|
|
|
|
async ValueTask ICredentialsProvider.DeleteAsync(CancellationToken cancellationToken)
|
|
{
|
|
using (await _lock.LockAsync(cancellationToken))
|
|
{
|
|
if (_fileSystem.FileExists(_credentialsFile))
|
|
_fileSystem.DeleteFile(_credentialsFile);
|
|
|
|
_credentials = null;
|
|
}
|
|
}
|
|
} |