public void Close() { if (this.sessionReceivePump != null) { this.sessionPumpCancellationTokenSource?.Cancel(); this.sessionPumpCancellationTokenSource?.Dispose(); this.sessionReceivePump = null; } }
public void Close() { if (this.sessionReceivePump != null) { this.sessionPumpCancellationTokenSource?.Cancel(); this.sessionPumpCancellationTokenSource?.Dispose(); // For back-compatibility this.runningTaskCancellationTokenSource?.Cancel(); this.runningTaskCancellationTokenSource?.Dispose(); this.sessionReceivePump = null; } }
public void OnSessionHandler( Func <IMessageSession, Message, CancellationToken, Task> callback, SessionHandlerOptions sessionHandlerOptions) { MessagingEventSource.Log.RegisterOnSessionHandlerStart(this.ClientId, sessionHandlerOptions); lock (this.syncLock) { if (this.sessionReceivePump != null) { throw new InvalidOperationException(Resources.SessionHandlerAlreadyRegistered); } this.sessionPumpCancellationTokenSource = new CancellationTokenSource(); // Running task cancellation token source can be reused if previously UnregisterSessionHandlerAsync was called if (this.runningTaskCancellationTokenSource == null) { this.runningTaskCancellationTokenSource = new CancellationTokenSource(); } this.sessionReceivePump = new SessionReceivePump( this.ClientId, this.SessionClient, this.ReceiveMode, sessionHandlerOptions, callback, this.endpoint, this.sessionPumpCancellationTokenSource.Token, this.runningTaskCancellationTokenSource.Token); } try { this.sessionReceivePump.StartPump(); } catch (Exception exception) { MessagingEventSource.Log.RegisterOnSessionHandlerException(this.ClientId, exception); if (this.sessionReceivePump != null) { this.sessionPumpCancellationTokenSource.Cancel(); this.sessionPumpCancellationTokenSource.Dispose(); this.sessionReceivePump = null; } throw; } MessagingEventSource.Log.RegisterOnSessionHandlerStop(this.ClientId); }
public async Task UnregisterSessionHandlerAsync(TimeSpan inflightSessionHandlerTasksWaitTimeout) { if (inflightSessionHandlerTasksWaitTimeout <= TimeSpan.Zero) { throw Fx.Exception.ArgumentOutOfRange(nameof(inflightSessionHandlerTasksWaitTimeout), inflightSessionHandlerTasksWaitTimeout, Resources.TimeoutMustBePositiveNonZero.FormatForUser(nameof(inflightSessionHandlerTasksWaitTimeout), inflightSessionHandlerTasksWaitTimeout)); } MessagingEventSource.Log.UnregisterSessionHandlerStart(this.ClientId); lock (this.syncLock) { if (this.sessionReceivePump == null || this.sessionPumpCancellationTokenSource.IsCancellationRequested) { // Silently return if handler has already been unregistered. return; } this.sessionPumpCancellationTokenSource.Cancel(); this.sessionPumpCancellationTokenSource.Dispose(); } Stopwatch stopWatch = Stopwatch.StartNew(); while (this.sessionReceivePump != null && stopWatch.Elapsed < inflightSessionHandlerTasksWaitTimeout && (this.sessionReceivePump.maxConcurrentSessionsSemaphoreSlim.CurrentCount < this.sessionReceivePump.sessionHandlerOptions.MaxConcurrentSessions || this.sessionReceivePump.maxPendingAcceptSessionsSemaphoreSlim.CurrentCount < this.sessionReceivePump.sessionHandlerOptions.MaxConcurrentAcceptSessionCalls)) { // We can proceed when the inflight tasks are done. if (this.sessionReceivePump.maxConcurrentSessionsSemaphoreSlim.CurrentCount == this.sessionReceivePump.sessionHandlerOptions.MaxConcurrentSessions && this.sessionReceivePump.maxPendingAcceptSessionsSemaphoreSlim.CurrentCount == this.sessionReceivePump.sessionHandlerOptions.MaxConcurrentAcceptSessionCalls) { break; } await Task.Delay(10).ConfigureAwait(false); } lock (this.sessionPumpCancellationTokenSource) { this.runningTaskCancellationTokenSource.Cancel(); this.runningTaskCancellationTokenSource.Dispose(); this.sessionReceivePump = null; } MessagingEventSource.Log.UnregisterSessionHandlerStop(this.ClientId); }
public async Task OnSessionHandlerAsync( Func <IMessageSession, Message, CancellationToken, Task> callback, SessionHandlerOptions sessionHandlerOptions) { MessagingEventSource.Log.RegisterOnSessionHandlerStart(this.ClientId, sessionHandlerOptions); lock (this.syncLock) { if (this.sessionReceivePump != null) { throw new InvalidOperationException(Resources.SessionHandlerAlreadyRegistered); } this.sessionPumpCancellationTokenSource = new CancellationTokenSource(); this.sessionReceivePump = new SessionReceivePump( this.ClientId, this.SessionClient, this.ReceiveMode, sessionHandlerOptions, callback, this.sessionPumpCancellationTokenSource.Token); } try { await this.sessionReceivePump.StartPumpAsync().ConfigureAwait(false); } catch (Exception exception) { MessagingEventSource.Log.RegisterOnSessionHandlerException(this.ClientId, exception); if (this.sessionReceivePump != null) { this.sessionPumpCancellationTokenSource.Cancel(); this.sessionPumpCancellationTokenSource.Dispose(); this.sessionReceivePump = null; } throw; } MessagingEventSource.Log.RegisterOnSessionHandlerStop(this.ClientId); }