Files
StockSharp/Messages/IMessageAdapterWrapper.cs
mika@stocksharp.com 6e7a7dae22 Move instrument lookups and persistence to async
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.
2026-10-04 11:28:17 +03:00

399 lines
12 KiB
C#

namespace StockSharp.Messages;
/// <summary>
/// Wrapping based adapter.
/// </summary>
public interface IMessageAdapterWrapper : IMessageAdapter
{
/// <summary>
/// Underlying adapter.
/// </summary>
IMessageAdapter InnerAdapter { get; set; }
}
/// <summary>
/// Base implementation of <see cref="IMessageAdapterWrapper"/>.
/// </summary>
public abstract class MessageAdapterWrapper : IMessageAdapterWrapper
{
/// <summary>
/// Create a copy of the adapter.
/// </summary>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>Copy.</returns>
public abstract ValueTask<IMessageAdapter> CloneAsync(CancellationToken cancellationToken);
private IMessageAdapter _innerAdapter;
/// <summary>
/// Initialize <see cref="MessageAdapterWrapper"/>.
/// </summary>
/// <param name="innerAdapter">Underlying adapter.</param>
protected MessageAdapterWrapper(IMessageAdapter innerAdapter)
{
InnerAdapter = innerAdapter ?? throw new ArgumentNullException(nameof(innerAdapter));
_innerAdapterName = GetUnderlyingAdapter(InnerAdapter).Name;
}
private static IMessageAdapter GetUnderlyingAdapter(IMessageAdapter adapter)
{
if (adapter == null)
throw new ArgumentNullException(nameof(adapter));
if (adapter is IMessageAdapterWrapper wrapper && wrapper.InnerAdapter is IMessageAdapter inner)
return GetUnderlyingAdapter(inner);
return adapter;
}
private void Unsub()
{
_innerAdapter.NewOutMessageAsync -= InnerAdapterNewOutMessageAsync;
}
/// <inheritdoc />
public IMessageAdapter InnerAdapter
{
get => _innerAdapter;
set
{
if (_innerAdapter == value)
return;
if (_innerAdapter != null)
Unsub();
_innerAdapter = value;
if (_innerAdapter != null)
{
_innerAdapter.NewOutMessageAsync += InnerAdapterNewOutMessageAsync;
}
}
}
/// <summary>
/// Control <see cref="InnerAdapter"/> lifetime.
/// </summary>
public bool OwnInnerAdapter { get; set; }
/// <summary>
/// Process <see cref="InnerAdapter"/> output message.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">Cancellation token.</param>
protected virtual ValueTask InnerAdapterNewOutMessageAsync(Message message, CancellationToken cancellationToken)
{
if (message.IsBack())
return RaiseNewOutMessageAsync(message, cancellationToken);
else
return OnInnerAdapterNewOutMessageAsync(message, cancellationToken);
}
/// <summary>
/// Process <see cref="InnerAdapter"/> output message.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">Cancellation token.</param>
protected virtual ValueTask OnInnerAdapterNewOutMessageAsync(Message message, CancellationToken cancellationToken)
{
return RaiseNewOutMessageAsync(message, cancellationToken);
}
/// <summary>
/// To call the event <see cref="NewOutMessageAsync"/>.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="cancellationToken">Cancellation token.</param>
protected ValueTask RaiseNewOutMessageAsync(Message message, CancellationToken cancellationToken)
=> NewOutMessageAsync.InvokeAsync(message, cancellationToken);
/// <summary>
/// Auto send <see cref="Message.BackMode"/> messages to <see cref="InnerAdapter"/>.
/// </summary>
protected virtual bool SendInBackFurther => true;
/// <inheritdoc />
public virtual async ValueTask SendInMessageAsync(Message message, CancellationToken cancellationToken)
{
if (message.IsBack())
{
if (message.Adapter == this)
{
message.UndoBack();
}
else
{
if (SendInBackFurther)
{
await InnerAdapter.SendInMessageAsync(message, cancellationToken);
return;
}
}
}
try
{
await OnSendInMessageAsync(message, cancellationToken);
}
catch (OperationCanceledException)
{
// operation was cancelled by caller
}
catch (Exception ex) when (message.Type == MessageTypes.Disconnect && ex is ObjectDisposedException)
{
// adapter may have already cleaned up during disconnect
}
catch (Exception ex)
{
await RaiseNewOutMessageAsync(message.CreateErrorResponse(ex, this), cancellationToken);
throw;
}
}
/// <summary>
/// Send message.
/// </summary>
/// <param name="message">Message.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
protected virtual ValueTask OnSendInMessageAsync(Message message, CancellationToken cancellationToken)
{
return InnerAdapter.SendInMessageAsync(message, cancellationToken);
}
/// <inheritdoc />
public event Func<Message, CancellationToken, ValueTask> NewOutMessageAsync;
/// <summary>
/// Load settings.
/// </summary>
/// <param name="storage">Settings storage.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns><see cref="Task"/></returns>
public virtual async Task LoadAsync(SettingsStorage storage, CancellationToken cancellationToken)
{
await InnerAdapter.LoadAsync(storage, cancellationToken);
}
/// <summary>
/// Save settings.
/// </summary>
/// <param name="storage">Settings storage.</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns><see cref="Task"/></returns>
public virtual async Task SaveAsync(SettingsStorage storage, CancellationToken cancellationToken)
{
await InnerAdapter.SaveAsync(storage, cancellationToken);
}
Guid ILogSource.Id => InnerAdapter.Id;
private readonly string _innerAdapterName;
string ILogSource.Name
{
get => _innerAdapterName + $" ({GetType().Name.Remove(nameof(MessageAdapter))})";
set { }
}
/// <inheritdoc />
public virtual ILogSource Parent
{
get => InnerAdapter.Parent;
set => InnerAdapter.Parent = value;
}
/// <inheritdoc />
public event Action<ILogSource> ParentRemoved
{
add { }
remove { }
}
LogLevels ILogSource.LogLevel
{
get => InnerAdapter.LogLevel;
set => InnerAdapter.LogLevel = value;
}
/// <inheritdoc />
public DateTime CurrentTime => InnerAdapter.CurrentTime;
bool ILogSource.IsRoot => InnerAdapter.IsRoot;
event Action<LogMessage> ILogSource.Log
{
add => InnerAdapter.Log += value;
remove => InnerAdapter.Log -= value;
}
void ILogReceiver.AddLog(LogMessage message)
{
InnerAdapter.AddLog(message);
}
/// <inheritdoc />
public bool CheckTimeFrameByRequest => InnerAdapter.CheckTimeFrameByRequest;
/// <inheritdoc />
public ReConnectionSettings ReConnectionSettings => InnerAdapter.ReConnectionSettings;
/// <inheritdoc />
public IdGenerator TransactionIdGenerator => InnerAdapter.TransactionIdGenerator;
/// <inheritdoc />
public virtual IEnumerable<MessageTypeInfo> PossibleSupportedMessages => InnerAdapter.PossibleSupportedMessages;
/// <inheritdoc />
public virtual IEnumerable<MessageTypes> SupportedInMessages
{
get => InnerAdapter.SupportedInMessages;
set => InnerAdapter.SupportedInMessages = value;
}
/// <inheritdoc />
public virtual IEnumerable<MessageTypes> NotSupportedResultMessages => InnerAdapter.NotSupportedResultMessages;
/// <inheritdoc />
public virtual IAsyncEnumerable<DataType> GetSupportedMarketDataTypesAsync(SecurityId securityId, DateTime? from, DateTime? to)
=> InnerAdapter.GetSupportedMarketDataTypesAsync(securityId, from, to);
/// <inheritdoc />
public TimeSpan HeartbeatInterval
{
get => InnerAdapter.HeartbeatInterval;
set => InnerAdapter.HeartbeatInterval = value;
}
/// <inheritdoc />
public string StorageName => InnerAdapter.StorageName;
/// <inheritdoc />
public virtual bool IsNativeIdentifiersPersistable => InnerAdapter.IsNativeIdentifiersPersistable;
/// <inheritdoc />
public virtual bool IsNativeIdentifiers => InnerAdapter.IsNativeIdentifiers;
/// <inheritdoc />
public virtual bool IsFullCandlesOnly => InnerAdapter.IsFullCandlesOnly;
/// <inheritdoc />
public virtual bool IsSupportSubscriptions => InnerAdapter.IsSupportSubscriptions;
/// <inheritdoc />
public virtual bool IsSupportCandlesUpdates(MarketDataMessage subscription) => InnerAdapter.IsSupportCandlesUpdates(subscription);
/// <inheritdoc />
public virtual bool IsSupportCandlesPriceLevels(MarketDataMessage subscription) => InnerAdapter.IsSupportCandlesPriceLevels(subscription);
/// <inheritdoc />
public virtual MessageAdapterCategories Categories => InnerAdapter.Categories;
/// <inheritdoc />
public virtual IEnumerable<int> SupportedOrderBookDepths => InnerAdapter.SupportedOrderBookDepths;
/// <inheritdoc />
public virtual bool IsSupportOrderBookIncrements => InnerAdapter.IsSupportOrderBookIncrements;
/// <inheritdoc />
public virtual bool IsSupportExecutionsPnL => InnerAdapter.IsSupportExecutionsPnL;
/// <inheritdoc />
public virtual bool IsSecurityNewsOnly => InnerAdapter.IsSecurityNewsOnly;
/// <inheritdoc />
public IEnumerable<Level1Fields> CandlesBuildFrom => InnerAdapter.CandlesBuildFrom;
/// <inheritdoc />
public virtual bool IsSupportTransactionLog => InnerAdapter.IsSupportTransactionLog;
Type IMessageAdapter.OrderConditionType => InnerAdapter.OrderConditionType;
bool IMessageAdapter.HeartbeatBeforeConnect => InnerAdapter.HeartbeatBeforeConnect;
Uri IMessageAdapter.Icon => InnerAdapter.Icon;
bool IMessageAdapter.IsAutoReplyOnTransactonalUnsubscription => InnerAdapter.IsAutoReplyOnTransactonalUnsubscription;
bool IMessageAdapter.IsReplaceCommandEditCurrent => InnerAdapter.IsReplaceCommandEditCurrent;
bool IMessageAdapter.EnqueueSubscriptions
{
get => InnerAdapter.EnqueueSubscriptions;
set => InnerAdapter.EnqueueSubscriptions = value;
}
bool IMessageAdapter.UseInChannel => InnerAdapter.UseInChannel;
bool IMessageAdapter.UseOutChannel => InnerAdapter.UseOutChannel;
TimeSpan IMessageAdapter.IterationInterval => InnerAdapter.IterationInterval;
TimeSpan? IMessageAdapter.LookupTimeout => InnerAdapter.LookupTimeout;
string IMessageAdapter.FeatureName => InnerAdapter.FeatureName;
bool IMessageAdapter.ExtraSetup => InnerAdapter.ExtraSetup;
/// <inheritdoc />
public virtual bool? IsPositionsEmulationRequired => InnerAdapter.IsPositionsEmulationRequired;
string[] IMessageAdapter.AssociatedBoards => InnerAdapter.AssociatedBoards;
TimeSpan IMessageAdapter.DisconnectTimeout => InnerAdapter.DisconnectTimeout;
int IMessageAdapter.MaxParallelMessages { get => InnerAdapter.MaxParallelMessages; set => InnerAdapter.MaxParallelMessages = value; }
TimeSpan IMessageAdapter.FaultDelay { get => InnerAdapter.FaultDelay; set => InnerAdapter.FaultDelay = value; }
IOrderLogMarketDepthBuilder IMessageAdapter.CreateOrderLogMarketDepthBuilder(SecurityId securityId)
=> InnerAdapter.CreateOrderLogMarketDepthBuilder(securityId);
/// <inheritdoc />
public virtual bool IsAllDownloadingSupported(DataType dataType)
=> InnerAdapter.IsAllDownloadingSupported(dataType);
/// <inheritdoc />
public virtual bool IsSecurityRequired(DataType dataType)
=> InnerAdapter.IsSecurityRequired(dataType);
ValueTask IMessageAdapter.SendOutMessageAsync(Message message, CancellationToken cancellationToken)
=> InnerAdapter.SendOutMessageAsync(message, cancellationToken);
/// <inheritdoc />
public virtual void Dispose()
{
if (InnerAdapter is null)
return;
Unsub();
if (OwnInnerAdapter)
InnerAdapter.Dispose();
GC.SuppressFinalize(this);
}
/// <inheritdoc />
public override string ToString() => InnerAdapter.ToString();
/// <inheritdoc />
public void LogVerbose(string message, params object[] args)
=> this.AddVerboseLog(message, args);
/// <inheritdoc />
public void LogDebug(string message, params object[] args)
=> this.AddDebugLog(message, args);
/// <inheritdoc />
public void LogInfo(string message, params object[] args)
=> this.AddInfoLog(message, args);
/// <inheritdoc />
public void LogWarning(string message, params object[] args)
=> this.AddWarningLog(message, args);
/// <inheritdoc />
public void LogError(string message, params object[] args)
=> this.AddErrorLog(message, args);
}