mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-06 09:32:55 +00:00
"Mark the blocking wrappers obsolete" left several callers of those wrappers under #pragma, because a property, a parameter handler or a state setter could not wait, and a few places that blocked without a wrapper. Each of those chains now runs through callers that can await. The emulation connector. A state change collects what its handlers throw and sends it out once the state lock is released; the disconnect that follows Stopping is awaited there as well. The change to Stopped moved from a handler of Disconnected into OnDisconnectedAsync, a step the connector now awaits before it raises that event. ExpressionIndexSecurity. Expression records the text and reads the instruments it names, which needs no compiler. The formula is compiled by GetFormulaAsync; Formula is an obsolete blocking wrapper over it. IBasketSecurityProcessor gets InitAsync, where the expression processor takes its formula, and CreateProcessorAsync creates a processor and prepares it; CreateProcessor and the IEnumerable ToBasket are obsolete wrappers, and the tests of ToBasket go through the IAsyncEnumerable overload. The index diagram element builds its index in OnPrepareAsync. MathDiagramElement. The inputs are named by the text of the formula; the formula and its validation are compiled in OnPrepareAsync. An element that was reset therefore compiles again at the next start instead of refusing to start. An input that is wired stays while the text is not known to compile, and goes at the preparation if the compiled formula does not name it. A text replaced under a running element is an error on every value until the element is prepared again. Candle patterns. A CandleExpressionCondition compiled its formula in the constructor or, for the registry, at the first evaluation. The formula is now compiled by PrepareAsync: on the condition, on ICandlePattern and, through the new IIndicator.PrepareAsync, on the indicator that holds a pattern. A pattern that was not prepared refuses to recognize instead of compiling on the spot. The checks of a pattern as a whole - a formula reaching outside it, no formulas at all - moved from the first Recognize to the preparation, and a failed check no longer leaves the pattern marked as checked. Whoever feeds an indicator prepares it first: IndicatorDiagramElement in OnPrepareAsync, a strategy for the indicators its start registered, the chart samples when an indicator is added. ICandlePatternProvider.Save became SaveAsync and prepares the pattern before it stores it. The compileOnDemand constructor is gone: no constructor compiles. Stopping a strategy from code that cannot wait. When an element was entered deeper than the overflow limit, DiagramElement.Process stopped the strategy through AsyncHelper.Run. Strategy gets RequestStop for such code: it reports the error, refuses new orders at once and leaves the stop itself to OnNewMessage, which carries it out when the connector hands over the next message. Releasing a connector. Connector.DisposeManaged waited for the disconnect, and for the message that takes the adapters apart, through AsyncHelper.Run. The release is now DisposeManagedAsync, a virtual chain that the connectors derived from Connector override. DisposeAsync awaits it, and the sealed DisposeManaged is the same chain with the caller blocked, as IDisposable requires. IConnector is IAsyncDisposable, so code that holds a connector by its interface releases it by await as well. Diagram elements that waited for their type. A composition read before one of its element types was registered keeps an empty node for each such element. Registering the type started their load unawaited, and a failure lost the settings at the next save. ICompositionRegistry gets LoadNotLoadedElementsAsync, to be awaited by whoever registered the types. It loads what waits in the given composition and in the compositions nested in it, one call at a time. The settings of an element that waits are kept by its node (ICompositionModelNode.ElementSettings). A loaded element joins its model: it is announced as an added one and the links of its node are connected. The sample windows. CombineHistoryRealtime, LiveTerminal and StoreDataLocal, WPF and Avalonia, flushed the storage executor, released the CSV registries and read them at startup through AsyncHelper.Run. A window now refuses the first attempt to close it, awaits the release and closes itself once the queued writes are on the disk. A shutdown that does not honour the refusal - the end of the Windows session, Application.Shutdown - is not waited for: what is still queued then is lost. LiveOptionsQuoting and MultiConnect called the blocking Strategy.Start, Stop and GetTimeFrames under a suppressed obsolete warning; they await the async ones now. What the rework uncovered: - Pausing the optimizer. An iteration that had passed the pause gate but whose connector had not started replaying yet ran to its end through the pause; cloning the strategy asynchronously made that window long enough for PauseHaltsBruteForce to see it on CI. An iteration now waits for its connector to come up and suspends it if the optimizer is paused by then. - Undo of diagram elements. An undo scope opened with no undo manager in sight took the element's level down when it closed, so the level was negative by the time the element reached a composition and no edit was ever reported. The scope remembers whether it was counted. - Undo of a chart panel. A snapshot keeps the series and axes of a panel by reference, since a part can only be saved asynchronously, so undo did not put back what a part was set to. IChartSnapshotPart is an optional interface for a part that can copy its settings in memory; the panel captures and gives back the settings of such parts. No chart part implements it yet - that is for the chart packages to do. - The dummy indicator wrapper was left without a chart element, so indicator values could not be wired into the panel that a run without a window uses. - Settings loaded before a composition existed are applied when the strategy starts; a test pins it. - AsyncMessageChannel looked at its state once per wake-up and then went through the queue until it was empty, so a message sent right after Suspend returned could still be started, and Suspend_PausesProcessing was red from time to time. The state is now read under the queue lock, where the next message is picked; a channel that is being closed stops starting what is queued the same way. DiagramElementParamTests no longer declares a value that persists itself through the obsolete IPersistable: declaring one needs a suppressed warning, and for that contract the parameter relies on SaveEntireAsync and LoadEntireAsync of Ecng, which are tested there.
848 lines
20 KiB
C#
848 lines
20 KiB
C#
namespace StockSharp.Messages;
|
|
|
|
using Nito.AsyncEx;
|
|
|
|
/// <summary>
|
|
/// Async message channel that processes messages via <see cref="IMessageTransport.NewOutMessageAsync"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Initializes a new instance of the <see cref="AsyncMessageChannel"/>.
|
|
/// </remarks>
|
|
/// <param name="adapter"><see cref="IMessageAdapter"/>.</param>
|
|
public class AsyncMessageChannel(IMessageAdapter adapter) : Disposable, IMessageChannel
|
|
{
|
|
private class MessageQueueItem
|
|
{
|
|
private TaskCompletionSource<bool> _completion;
|
|
private int _cleanupStarted;
|
|
private int _isCompleted;
|
|
|
|
public MessageQueueItem(Message message)
|
|
{
|
|
Message = message ?? throw new ArgumentNullException(nameof(message));
|
|
|
|
IsControl = Message.Type
|
|
is MessageTypes.Reset
|
|
or MessageTypes.Connect
|
|
or MessageTypes.Disconnect;
|
|
|
|
IsPing = Message.Type == MessageTypes.Time;
|
|
|
|
IsLookup = Message.IsLookup();
|
|
|
|
IsTransaction = Message.Type
|
|
is MessageTypes.OrderRegister
|
|
or MessageTypes.OrderReplace
|
|
or MessageTypes.OrderCancel
|
|
or MessageTypes.OrderGroupCancel;
|
|
}
|
|
|
|
public Message Message { get; }
|
|
|
|
public bool IsProcessing { get; set; }
|
|
|
|
public bool IsControl { get; }
|
|
public bool IsPing { get; }
|
|
public bool IsLookup { get; }
|
|
public bool IsTransaction { get; }
|
|
|
|
public CancellationTokenSource ProcessSource { get; set; }
|
|
public CancellationTokenSource Cts { get; set; }
|
|
|
|
public Task CompletionTask
|
|
{
|
|
get
|
|
{
|
|
if (Volatile.Read(ref _isCompleted) != 0)
|
|
return Task.CompletedTask;
|
|
|
|
var created = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var completion = Interlocked.CompareExchange(ref _completion, created, null) ?? created;
|
|
|
|
if (Volatile.Read(ref _isCompleted) != 0)
|
|
completion.TrySetResult(true);
|
|
|
|
return completion.Task;
|
|
}
|
|
}
|
|
|
|
public Task DisconnectPrerequisite { get; set; }
|
|
|
|
public bool TryBeginCleanup() => Interlocked.Exchange(ref _cleanupStarted, 1) == 0;
|
|
|
|
public void Complete()
|
|
{
|
|
if (Interlocked.Exchange(ref _isCompleted, 1) == 0)
|
|
Volatile.Read(ref _completion)?.TrySetResult(true);
|
|
}
|
|
|
|
public override string ToString() => Message.ToString();
|
|
}
|
|
|
|
private readonly SynchronizedList<MessageQueueItem> _messages = [];
|
|
private readonly SynchronizedDictionary<MessageQueueItem, Task> _childTasks = [];
|
|
private readonly SynchronizedDictionary<long, MessageQueueItem> _subscriptionItems = [];
|
|
|
|
private readonly AsyncManualResetEvent _processMessageEvt = new(false);
|
|
private readonly Lock _stateLock = new();
|
|
private CancellationTokenSource _globalCts = new();
|
|
private CancellationTokenSource _processorCts = new();
|
|
private Task _processorTask;
|
|
|
|
private bool _isConnectionStarted, _isDisconnecting;
|
|
|
|
private readonly IMessageAdapter _adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
|
|
private ChannelStates _state = ChannelStates.Stopped;
|
|
|
|
/// <inheritdoc />
|
|
public ChannelStates State
|
|
{
|
|
get => _state;
|
|
private set
|
|
{
|
|
if (_state == value)
|
|
return;
|
|
|
|
if (!_state.ValidateChannelState(value))
|
|
return;
|
|
|
|
_state = value;
|
|
StateChanged?.Invoke();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public event Action StateChanged;
|
|
|
|
/// <inheritdoc />
|
|
public void Open()
|
|
{
|
|
State = ChannelStates.Starting;
|
|
|
|
CancellationToken token;
|
|
|
|
lock (_stateLock)
|
|
token = _processorCts.Token;
|
|
|
|
_processorTask = Task.Run(() => ProcessMessagesAsync(token), token);
|
|
|
|
State = ChannelStates.Started;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Close()
|
|
{
|
|
State = ChannelStates.Stopping;
|
|
|
|
try
|
|
{
|
|
_processMessageEvt.Set();
|
|
}
|
|
catch { }
|
|
|
|
CancelAndReplaceGlobalCts();
|
|
CancelAndReplaceProcessorCts();
|
|
|
|
foreach (var kv in _subscriptionItems.CopyAndClear())
|
|
{
|
|
var item = kv.Value;
|
|
|
|
try
|
|
{
|
|
item.Cts?.Cancel();
|
|
}
|
|
catch { }
|
|
finally
|
|
{
|
|
try { item.Cts?.Dispose(); }
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
_processorTask?.Wait(TimeSpan.FromSeconds(5));
|
|
}
|
|
catch { }
|
|
|
|
_messages.Clear();
|
|
_childTasks.Clear();
|
|
|
|
lock (_stateLock)
|
|
{
|
|
_isConnectionStarted = false;
|
|
_isDisconnecting = false;
|
|
}
|
|
|
|
State = ChannelStates.Stopped;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Suspend()
|
|
{
|
|
State = ChannelStates.Suspending;
|
|
State = ChannelStates.Suspended;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Resume()
|
|
{
|
|
State = ChannelStates.Starting;
|
|
State = ChannelStates.Started;
|
|
_processMessageEvt.Set();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Clear()
|
|
{
|
|
_messages.Clear();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ValueTask SendInMessageAsync(Message message, CancellationToken cancellationToken)
|
|
{
|
|
if (IsDisposed)
|
|
throw new ObjectDisposedException(nameof(AsyncMessageChannel));
|
|
|
|
if (!this.IsOpened())
|
|
return default;
|
|
|
|
using (_messages.EnterScope())
|
|
{
|
|
if (message is ResetMessage)
|
|
{
|
|
_messages.Clear();
|
|
CancelAndReplaceGlobalCts();
|
|
}
|
|
|
|
_messages.Add(new(message));
|
|
}
|
|
|
|
_processMessageEvt.Set();
|
|
return default;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public event Func<Message, CancellationToken, ValueTask> NewOutMessageAsync;
|
|
|
|
private ValueTask RaiseNewOutMessage(Message message, CancellationToken cancellationToken)
|
|
{
|
|
return NewOutMessageAsync.InvokeAsync(message, cancellationToken);
|
|
}
|
|
|
|
private Task ProcessMessagesAsync(CancellationToken token)
|
|
{
|
|
bool nextMessage()
|
|
{
|
|
MessageQueueItem item;
|
|
|
|
using (_messages.EnterScope())
|
|
{
|
|
// The state is read with the queue locked: a message put there after Suspend returned is
|
|
// seen together with the state Suspend left, so it is not started until Resume.
|
|
if (State != ChannelStates.Started)
|
|
return false;
|
|
|
|
var isControlProcessing = false;
|
|
var isPingProcessing = false;
|
|
var isLookupProcessing = false;
|
|
var isTransactionProcessing = false;
|
|
var numProcessing = 0;
|
|
|
|
foreach (var m in _messages.Where(m => m.IsProcessing))
|
|
{
|
|
isControlProcessing |= m.IsControl;
|
|
isPingProcessing |= m.IsPing;
|
|
isLookupProcessing |= m.IsLookup;
|
|
isTransactionProcessing |= m.IsTransaction;
|
|
++numProcessing;
|
|
}
|
|
|
|
// cant process anything in parallel while connect/disconnect/reset is processing
|
|
if (isControlProcessing)
|
|
return false;
|
|
|
|
var nonProcessing = _messages.Where(i => !i.IsProcessing);
|
|
|
|
//
|
|
// priority order:
|
|
//
|
|
// controls messages - 1
|
|
// heartbeat(=ping) - 2
|
|
// unsubscribe - 3
|
|
// lookup - 4
|
|
// transactions - 5
|
|
// other - 6
|
|
//
|
|
|
|
item = nonProcessing.FirstOrDefault(m => m.IsControl);
|
|
|
|
if (item?.Message is DisconnectMessage)
|
|
{
|
|
var disconnectItem = item;
|
|
List<Task> prerequisites = null;
|
|
|
|
foreach (var priorItem in _messages)
|
|
{
|
|
if (ReferenceEquals(priorItem, disconnectItem))
|
|
break;
|
|
|
|
if (priorItem.Message is not ISubscriptionMessage { IsSubscribe: false })
|
|
continue;
|
|
|
|
if (!priorItem.IsProcessing)
|
|
{
|
|
item = priorItem;
|
|
break;
|
|
}
|
|
|
|
(prerequisites ??= []).Add(priorItem.CompletionTask);
|
|
}
|
|
|
|
if (ReferenceEquals(item, disconnectItem))
|
|
{
|
|
disconnectItem.DisconnectPrerequisite = prerequisites?.Count switch
|
|
{
|
|
null or 0 => Task.CompletedTask,
|
|
1 => prerequisites[0],
|
|
_ => Task.WhenAll(prerequisites),
|
|
};
|
|
}
|
|
}
|
|
|
|
if (item is null)
|
|
{
|
|
if (isPingProcessing)
|
|
{
|
|
// can't process parallel pings, applying filter
|
|
nonProcessing = nonProcessing.Where(m => !m.IsPing);
|
|
}
|
|
else
|
|
item = nonProcessing.FirstOrDefault(m => m.IsPing);
|
|
}
|
|
|
|
item ??= nonProcessing.FirstOrDefault(m => m.Message is ISubscriptionMessage { IsSubscribe: false });
|
|
|
|
// all other message types are MaxParallelMessages tolerant
|
|
if (item is null && numProcessing < _adapter.MaxParallelMessages)
|
|
{
|
|
if (isLookupProcessing)
|
|
{
|
|
// can't process parallel lookup, applying filter
|
|
nonProcessing = nonProcessing.Where(m => !m.IsLookup);
|
|
}
|
|
else
|
|
item = nonProcessing.FirstOrDefault(m => m.IsLookup);
|
|
|
|
if (item is null)
|
|
{
|
|
if (isTransactionProcessing)
|
|
item = nonProcessing.FirstOrDefault(m => !m.IsTransaction);
|
|
else
|
|
item = nonProcessing.FirstOrDefault(m => m.IsTransaction) ?? nonProcessing.FirstOrDefault();
|
|
}
|
|
}
|
|
|
|
if (item is null)
|
|
return false;
|
|
|
|
if (item.IsProcessing)
|
|
throw new InvalidOperationException($"processing is already started for {item.Message}");
|
|
|
|
lock (_stateLock)
|
|
item.ProcessSource = _globalCts;
|
|
|
|
item.IsProcessing = true;
|
|
}
|
|
|
|
var msg = item.Message;
|
|
|
|
async ValueTask wrapperInner()
|
|
{
|
|
var processSource = item.ProcessSource;
|
|
var localToken = processSource.Token;
|
|
long? registeredSubscriptionId = null;
|
|
|
|
void done()
|
|
{
|
|
if (!item.TryBeginCleanup())
|
|
return;
|
|
|
|
try
|
|
{
|
|
if (!item.IsControl)
|
|
_childTasks.Remove(item);
|
|
|
|
if (registeredSubscriptionId is long subscriptionId)
|
|
_subscriptionItems.Remove(subscriptionId);
|
|
|
|
item.Cts?.Dispose();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_adapter.AddErrorLog(ex);
|
|
}
|
|
finally
|
|
{
|
|
using (_messages.EnterScope())
|
|
{
|
|
try
|
|
{
|
|
_messages.Remove(item);
|
|
}
|
|
finally
|
|
{
|
|
if (msg is ISubscriptionMessage)
|
|
item.Complete();
|
|
}
|
|
}
|
|
|
|
_processMessageEvt.Set();
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
if (localToken.IsCancellationRequested)
|
|
{
|
|
if (item.IsTransaction)
|
|
await _adapter.SendOutMessageAsync(msg.CreateErrorResponse(new OperationCanceledException(), _adapter), localToken);
|
|
|
|
return;
|
|
}
|
|
|
|
if (msg.Type != MessageTypes.Time)
|
|
_adapter.AddVerboseLog("beginprocess: {0}", msg.Type);
|
|
|
|
if (!item.IsControl)
|
|
{
|
|
bool isConnectionStarted;
|
|
bool isDisconnecting;
|
|
|
|
lock (_stateLock)
|
|
{
|
|
isConnectionStarted = _isConnectionStarted;
|
|
isDisconnecting = _isDisconnecting;
|
|
}
|
|
|
|
if (!isConnectionStarted || isDisconnecting)
|
|
{
|
|
_adapter.AddDebugLog($"unable to process {msg.Type} in this state. connStarted={isConnectionStarted}, disconnecting={isDisconnecting}");
|
|
return;
|
|
}
|
|
|
|
if (msg is ISubscriptionMessage subMsg)
|
|
{
|
|
if (subMsg.IsSubscribe)
|
|
{
|
|
var (cts, childToken) = localToken.CreateChildToken();
|
|
localToken = childToken;
|
|
item.Cts = cts;
|
|
_subscriptionItems.Add(subMsg.TransactionId, item);
|
|
registeredSubscriptionId = subMsg.TransactionId;
|
|
}
|
|
else
|
|
{
|
|
// The unsubscribe item owns the acknowledgement for a long-running subscribe.
|
|
// Wait for its full cleanup before acknowledging the queued unsubscribe.
|
|
if (_subscriptionItems.TryGetAndRemove(subMsg.OriginalTransactionId, out var subItem))
|
|
{
|
|
var subscriptionCompletion = subItem.CompletionTask;
|
|
|
|
try
|
|
{
|
|
subItem.Cts.Cancel();
|
|
}
|
|
catch (ObjectDisposedException)
|
|
{
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_adapter.AddErrorLog(ex);
|
|
}
|
|
|
|
try
|
|
{
|
|
await subscriptionCompletion.WithCancellation(localToken);
|
|
}
|
|
catch (OperationCanceledException) when (localToken.IsCancellationRequested)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await _adapter.SendOutMessageAsync(subMsg.TransactionId.CreateSubscriptionResponse(), localToken);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ValueTask _()
|
|
=> msg switch
|
|
{
|
|
ConnectMessage m => ConnectAsync(m, localToken, processSource),
|
|
DisconnectMessage m => DisconnectAsync(m, item),
|
|
ResetMessage m => ResetAsync(m),
|
|
|
|
_ => RaiseNewOutMessage(msg, localToken)
|
|
};
|
|
|
|
try
|
|
{
|
|
// A source-backed ValueTask can only be consumed once. Track and await the same Task.
|
|
var task = _().AsTask();
|
|
|
|
if (!task.IsCompleted)
|
|
{
|
|
if (!item.IsControl)
|
|
_childTasks.Add(item, task);
|
|
|
|
await task;
|
|
|
|
if (!item.IsControl)
|
|
_childTasks.Remove(item);
|
|
}
|
|
|
|
if (task.IsFaulted)
|
|
throw task.Exception;
|
|
else if (task.IsCanceled)
|
|
throw new OperationCanceledException();
|
|
|
|
if (msg.Type != MessageTypes.Time)
|
|
_adapter.AddVerboseLog("endprocess: {0}", msg.Type);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var responseToken = item.IsControl ? item.ProcessSource.Token : localToken;
|
|
|
|
try
|
|
{
|
|
if (item.IsControl)
|
|
{
|
|
lock (_stateLock)
|
|
{
|
|
if (responseToken.IsCancellationRequested || !ReferenceEquals(_globalCts, item.ProcessSource))
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (msg is ISubscriptionMessage)
|
|
{
|
|
if (localToken.IsCancellationRequested)
|
|
{
|
|
// Cancellation is not an error for subscriptions. Fast-path unsubscribe
|
|
// acknowledgements are emitted by the unsubscribe queue item.
|
|
return;
|
|
}
|
|
|
|
_adapter.AddVerboseLog("endprocess: {0} ({1})", msg.Type, ex);
|
|
|
|
await _adapter.FaultDelay.Delay(localToken);
|
|
}
|
|
|
|
await _adapter.SendOutMessageAsync(msg.CreateErrorResponse(ex, _adapter), responseToken);
|
|
}
|
|
catch (Exception ex2)
|
|
{
|
|
if (!responseToken.IsCancellationRequested)
|
|
_adapter.AddErrorLog(ex2);
|
|
}
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
done();
|
|
}
|
|
}
|
|
|
|
async ValueTask wrapper()
|
|
{
|
|
try
|
|
{
|
|
await wrapperInner();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_adapter.AddErrorLog(ex);
|
|
}
|
|
}
|
|
|
|
#pragma warning disable CA2012
|
|
_ = wrapper();
|
|
#pragma warning restore CA2012
|
|
|
|
return true;
|
|
}
|
|
|
|
return Do.InvariantAsync(async () =>
|
|
{
|
|
while (true)
|
|
{
|
|
await _processMessageEvt.WaitAsync(token);
|
|
_processMessageEvt.Reset();
|
|
|
|
if (IsDisposeStarted)
|
|
break;
|
|
|
|
if (State != ChannelStates.Started)
|
|
{
|
|
if (State == ChannelStates.Stopping)
|
|
break;
|
|
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
while (nextMessage()) { }
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_adapter.AddErrorLog("error processing message: {0}", e);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private async ValueTask ConnectAsync(ConnectMessage msg, CancellationToken token, CancellationTokenSource processSource)
|
|
{
|
|
lock (_stateLock)
|
|
{
|
|
if (!ReferenceEquals(_globalCts, processSource) || processSource.IsCancellationRequested)
|
|
throw new OperationCanceledException(processSource.Token);
|
|
|
|
if (_isConnectionStarted)
|
|
throw new InvalidOperationException(LocalizedStrings.NotDisconnectPrevTime);
|
|
}
|
|
|
|
await RaiseNewOutMessage(msg, token);
|
|
|
|
lock (_stateLock)
|
|
{
|
|
if (!ReferenceEquals(_globalCts, processSource) || processSource.IsCancellationRequested)
|
|
throw new OperationCanceledException(processSource.Token);
|
|
|
|
_isConnectionStarted = true;
|
|
}
|
|
}
|
|
|
|
private async ValueTask DisconnectAsync(DisconnectMessage msg, MessageQueueItem item)
|
|
{
|
|
var processSource = item.ProcessSource;
|
|
|
|
lock (_stateLock)
|
|
{
|
|
if (!ReferenceEquals(_globalCts, processSource) || processSource.IsCancellationRequested)
|
|
throw new OperationCanceledException(processSource.Token);
|
|
|
|
if (!_isConnectionStarted)
|
|
throw new InvalidOperationException("not connected");
|
|
|
|
if (_isDisconnecting)
|
|
throw new InvalidOperationException("already disconnecting");
|
|
|
|
_isDisconnecting = true;
|
|
}
|
|
|
|
var prerequisite = item.DisconnectPrerequisite ?? Task.CompletedTask;
|
|
var operationSource = processSource;
|
|
|
|
try
|
|
{
|
|
using var timeoutCts = _adapter.DisconnectTimeout.CreateTimeout();
|
|
using var prerequisiteCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, processSource.Token);
|
|
CancellationTokenSource disconnectSource = null;
|
|
|
|
try
|
|
{
|
|
try
|
|
{
|
|
await prerequisite.WithCancellation(prerequisiteCts.Token);
|
|
}
|
|
catch (OperationCanceledException ex) when (timeoutCts.IsCancellationRequested && !processSource.IsCancellationRequested)
|
|
{
|
|
throw new TimeoutException("Unable to complete disconnect. Prior unsubscribe requests are still running.", ex);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
// Only the disconnect that still owns this cancellation generation may replace it.
|
|
// Close or Reset can already have installed a fresh source for subsequent processing.
|
|
disconnectSource = TryCancelAndReplaceGlobalCts(processSource);
|
|
|
|
if (disconnectSource is not null)
|
|
{
|
|
operationSource = disconnectSource;
|
|
item.ProcessSource = disconnectSource;
|
|
}
|
|
}
|
|
|
|
if (disconnectSource is null)
|
|
throw new OperationCanceledException(processSource.Token);
|
|
|
|
using var completionCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, disconnectSource.Token);
|
|
|
|
if (!await WhenChildrenComplete(completionCts.Token))
|
|
{
|
|
lock (_stateLock)
|
|
{
|
|
if (disconnectSource.IsCancellationRequested || !ReferenceEquals(_globalCts, disconnectSource))
|
|
throw new OperationCanceledException(disconnectSource.Token);
|
|
}
|
|
|
|
throw new InvalidOperationException("unable to complete disconnect. some tasks are still running.");
|
|
}
|
|
|
|
lock (_stateLock)
|
|
{
|
|
if (disconnectSource.IsCancellationRequested || !ReferenceEquals(_globalCts, disconnectSource))
|
|
throw new OperationCanceledException(disconnectSource.Token);
|
|
}
|
|
|
|
await RaiseNewOutMessage(msg, disconnectSource.Token);
|
|
|
|
lock (_stateLock)
|
|
{
|
|
if (disconnectSource.IsCancellationRequested || !ReferenceEquals(_globalCts, disconnectSource))
|
|
throw new OperationCanceledException(disconnectSource.Token);
|
|
|
|
_isConnectionStarted = false;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
lock (_stateLock)
|
|
{
|
|
if (ReferenceEquals(_globalCts, operationSource))
|
|
_isDisconnecting = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async ValueTask ResetAsync(ResetMessage msg)
|
|
{
|
|
lock (_stateLock)
|
|
_isDisconnecting = true;
|
|
|
|
// token is already canceled in SendInMessage
|
|
await AsyncHelper.CatchHandle((Func<Task>)(async () =>
|
|
{
|
|
using var cts = _adapter.DisconnectTimeout.CreateTimeout();
|
|
await WhenChildrenComplete(cts.Token);
|
|
}), default);
|
|
|
|
foreach (var kv in _subscriptionItems.CopyAndClear())
|
|
{
|
|
var item = kv.Value;
|
|
|
|
try { item.Cts?.Cancel(); }
|
|
catch (ObjectDisposedException) { }
|
|
|
|
try { item.Cts?.Dispose(); }
|
|
catch (ObjectDisposedException) { }
|
|
}
|
|
|
|
try
|
|
{
|
|
await RaiseNewOutMessage(msg, default);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_adapter.AddErrorLog(ex);
|
|
}
|
|
|
|
lock (_stateLock)
|
|
_isDisconnecting = _isConnectionStarted = false;
|
|
}
|
|
|
|
private void CancelAndReplaceGlobalCts()
|
|
{
|
|
// Atomically swap in a fresh source and cancel the old one. The old source is intentionally NOT
|
|
// disposed here: the processor task may still be reading its token (see ProcessMessagesAsync), and
|
|
// disposing it concurrently races into an ObjectDisposedException. A CancellationTokenSource without a
|
|
// timer holds no unmanaged resource, so letting the GC reclaim the abandoned one once the processor
|
|
// drains is safe and removes the use-after-dispose race.
|
|
CancellationTokenSource old;
|
|
|
|
lock (_stateLock)
|
|
{
|
|
old = _globalCts;
|
|
_globalCts = new();
|
|
}
|
|
|
|
CancelGlobalCts(old);
|
|
}
|
|
|
|
private void CancelAndReplaceProcessorCts()
|
|
{
|
|
CancellationTokenSource old;
|
|
|
|
lock (_stateLock)
|
|
{
|
|
old = _processorCts;
|
|
_processorCts = new();
|
|
}
|
|
|
|
CancelGlobalCts(old);
|
|
}
|
|
|
|
private CancellationTokenSource TryCancelAndReplaceGlobalCts(CancellationTokenSource expected)
|
|
{
|
|
var replacement = new CancellationTokenSource();
|
|
|
|
lock (_stateLock)
|
|
{
|
|
if (!ReferenceEquals(_globalCts, expected))
|
|
{
|
|
replacement.Dispose();
|
|
return null;
|
|
}
|
|
|
|
_globalCts = replacement;
|
|
}
|
|
|
|
CancelGlobalCts(expected);
|
|
return replacement;
|
|
}
|
|
|
|
private static void CancelGlobalCts(CancellationTokenSource source)
|
|
{
|
|
try
|
|
{
|
|
source?.Cancel();
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private async Task<bool> WhenChildrenComplete(CancellationToken token)
|
|
{
|
|
var tasks = _childTasks.CopyAndClear();
|
|
|
|
var allComplete = true;
|
|
|
|
await Task.WhenAll(tasks.Select(t => t.Value.WithCancellation(token))).CatchHandle(token, finalizer: () =>
|
|
{
|
|
var incomplete = tasks.Where(t => !t.Value.IsCompleted).Select(t => t.Key.ToString()).ToArray();
|
|
if(incomplete.Any())
|
|
{
|
|
allComplete = false;
|
|
_adapter.AddErrorLog("following tasks were not completed:\n" + incomplete.JoinN());
|
|
}
|
|
});
|
|
|
|
return allComplete;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void DisposeManaged()
|
|
{
|
|
Close();
|
|
base.DisposeManaged();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IMessageChannel Clone() => new AsyncMessageChannel(_adapter);
|
|
|
|
object ICloneable.Clone() => Clone();
|
|
}
|