private static void OnCompleteMessageCompletion(IAsyncResult result)
        {
            MessageReceivePump.BatchCompleteAsyncResult batchCompleteAsyncResult = (MessageReceivePump.BatchCompleteAsyncResult)result;
            MessageReceivePump pump            = batchCompleteAsyncResult.Pump;
            TrackingContext    trackingContext = batchCompleteAsyncResult.TrackingContext;

            lock (pump.completionList)
            {
                try
                {
                    try
                    {
                        AsyncResult <MessageReceivePump.BatchCompleteAsyncResult> .End(batchCompleteAsyncResult);
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpFailedToComplete(trackingContext.Activity, trackingContext.TrackingId, trackingContext.SystemTracker, exception.ToStringSlim()));
                        pump.RaiseExceptionReceivedEvent(exception, "Complete");
                    }
                }
                finally
                {
                    pump.completionInProgress = false;
                }
            }
            pump.ScheduleMessageCompletion(null, trackingContext);
        }
 public RenewLockLoopAsyncResult(MessageReceivePump pump, TrackingContext trackingContext, BrokeredMessage message, CancellationToken cancellationToken, AsyncCallback callback, object state) : base(TimeSpan.MaxValue, callback, state)
 {
     this.pump              = pump;
     this.trackingContext   = trackingContext;
     this.message           = message;
     this.cancellationToken = cancellationToken;
 }
 public PumpAsyncResult(MessageReceivePump owner, BrokeredMessage firstMessage, AsyncCallback callback, object state) : base(TimeSpan.MaxValue, callback, state)
 {
     this.owner           = owner;
     this.firstMessage    = firstMessage;
     this.trackingContext = TrackingContext.GetInstance(Guid.NewGuid(), owner.receiver.Path);
     MessageReceivePump.PumpAsyncResult pumpAsyncResult = this;
     pumpAsyncResult.OnCompleting = (Action <AsyncResult, Exception>)Delegate.Combine(pumpAsyncResult.OnCompleting, MessageReceivePump.PumpAsyncResult.CompletingAction);
 }
            protected override IEnumerator <IteratorAsyncResult <MessageReceivePump.PumpAsyncResult> .AsyncStep> GetAsyncSteps()
            {
                TimeSpan timeSpan;

                while (this.owner.receiver.IsOpened)
                {
                    if (!this.owner.semaphore.TryEnter())
                    {
                        MessageReceivePump.PumpAsyncResult pumpAsyncResult = this;
                        IteratorAsyncResult <MessageReceivePump.PumpAsyncResult> .BeginCall beginCall = (MessageReceivePump.PumpAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.semaphore.BeginEnter(c, s);
                        yield return(pumpAsyncResult.CallAsync(beginCall, (MessageReceivePump.PumpAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.semaphore.EndEnter(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Transfer));
                    }
                    this.message = null;
                    if (this.firstMessage != null)
                    {
                        this.message      = this.firstMessage;
                        this.firstMessage = null;
                    }
                    else
                    {
                        MessageReceivePump.PumpAsyncResult pumpAsyncResult1 = this;
                        IteratorAsyncResult <MessageReceivePump.PumpAsyncResult> .BeginCall beginCall1 = (MessageReceivePump.PumpAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.receiver.BeginReceive(thisPtr.owner.OnMessageOptions.ReceiveTimeOut, c, s);
                        yield return(pumpAsyncResult1.CallAsync(beginCall1, (MessageReceivePump.PumpAsyncResult thisPtr, IAsyncResult r) => thisPtr.message = thisPtr.owner.receiver.EndReceive(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));
                    }
                    if (base.LastAsyncStepException != null)
                    {
                        MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpReceiveException(this.trackingContext.Activity, this.trackingContext.TrackingId, this.trackingContext.SystemTracker, base.LastAsyncStepException.ToString()));
                        this.owner.RaiseExceptionReceivedEvent(base.LastAsyncStepException, "Receive");
                        if (MessageReceivePump.ShouldBackoff(base.LastAsyncStepException, out timeSpan))
                        {
                            MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpBackoff(this.trackingContext.Activity, this.trackingContext.TrackingId, this.trackingContext.SystemTracker, (int)MessageReceivePump.ServerBusyExceptionBackoffAmount.TotalMilliseconds, base.LastAsyncStepException.ToString()));
                            yield return(base.CallAsyncSleep(timeSpan));
                        }
                    }
                    if (this.message != null)
                    {
                        try
                        {
                            (new MessageReceivePump.DispatchAsyncResult(this.owner, this.trackingContext, this.message, MessageReceivePump.PumpAsyncResult.onDispatchCompleted, this.owner)).Start();
                        }
                        catch (Exception exception)
                        {
                            if (Fx.IsFatal(exception))
                            {
                                throw;
                            }
                            this.owner.semaphore.Exit();
                        }
                    }
                    else
                    {
                        this.owner.semaphore.Exit();
                    }
                }
                MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpStopped(this.trackingContext.Activity, this.trackingContext.TrackingId, this.trackingContext.SystemTracker));
            }
 public DispatchAsyncResult(MessageReceivePump owner, TrackingContext trackingContext, BrokeredMessage message, AsyncCallback callback, object state) : base(TimeSpan.MaxValue, callback, state)
 {
     this.owner           = owner;
     this.message         = message;
     this.trackingContext = trackingContext;
     this.processingTimer = new IOThreadTimer(MessageReceivePump.DispatchAsyncResult.StaticOnProcessingTimeout, this, true);
     this.renewCancellationTokenSource = new CancellationTokenSource();
     MessageReceivePump.DispatchAsyncResult dispatchAsyncResult = this;
     dispatchAsyncResult.OnCompleting = (Action <AsyncResult, Exception>)Delegate.Combine(dispatchAsyncResult.OnCompleting, MessageReceivePump.DispatchAsyncResult.CompletingAction);
 }
            protected override IEnumerator <IteratorAsyncResult <MessageReceivePump.DispatchAsyncResult> .AsyncStep> GetAsyncSteps()
            {
                TimeSpan timeSpan;

                if (this.ShouldRenewLock())
                {
                    this.owner.ScheduleRenewLockLoop(this.trackingContext, this.message, this.renewCancellationTokenSource.Token);
                    this.processingTimer.Set(this.owner.OnMessageOptions.AutoRenewTimeout);
                }
                MessageReceivePump.DispatchAsyncResult dispatchAsyncResult = this;
                IteratorAsyncResult <MessageReceivePump.DispatchAsyncResult> .BeginCall beginCall = (MessageReceivePump.DispatchAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.beginCallback(thisPtr.message, c, s);
                yield return(dispatchAsyncResult.CallAsync(beginCall, (MessageReceivePump.DispatchAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.endCallback(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                if (this.ShouldRenewLock())
                {
                    this.processingTimer.Cancel();
                    this.renewCancellationTokenSource.Cancel();
                    this.renewCancellationTokenSource.Dispose();
                }
                if (base.LastAsyncStepException != null)
                {
                    MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpUserCallbackException(this.trackingContext.Activity, this.trackingContext.TrackingId, this.trackingContext.SystemTracker, base.LastAsyncStepException.ToString()));
                    this.owner.RaiseExceptionReceivedEvent(base.LastAsyncStepException, "UserCallback");
                }
                if (this.owner.receiver.Mode == ReceiveMode.PeekLock)
                {
                    if (base.LastAsyncStepException != null)
                    {
                        MessageReceivePump.DispatchAsyncResult dispatchAsyncResult1 = this;
                        IteratorAsyncResult <MessageReceivePump.DispatchAsyncResult> .BeginCall beginCall1 = (MessageReceivePump.DispatchAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.receiver.BeginAbandon(thisPtr.message.LockToken, c, s);
                        yield return(dispatchAsyncResult1.CallAsync(beginCall1, (MessageReceivePump.DispatchAsyncResult thisPtr, IAsyncResult r) => thisPtr.owner.receiver.EndAbandon(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                        if (base.LastAsyncStepException != null)
                        {
                            MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpFailedToAbandon(this.trackingContext.Activity, this.trackingContext.TrackingId, this.trackingContext.SystemTracker, base.LastAsyncStepException.ToString()));
                            this.owner.RaiseExceptionReceivedEvent(base.LastAsyncStepException, "Abandon");
                        }
                    }
                    else if (this.owner.OnMessageOptions.AutoComplete)
                    {
                        this.owner.ScheduleMessageCompletion(this.message, this.trackingContext);
                    }
                    bool lastAsyncStepException = base.LastAsyncStepException != null;
                    if (lastAsyncStepException && MessageReceivePump.ShouldBackoff(base.LastAsyncStepException, out timeSpan))
                    {
                        MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpBackoff(this.trackingContext.Activity, this.trackingContext.TrackingId, this.trackingContext.SystemTracker, (int)MessageReceivePump.ServerBusyExceptionBackoffAmount.TotalMilliseconds, base.LastAsyncStepException.ToString()));
                        yield return(base.CallAsyncSleep(timeSpan));
                    }
                }
            }
        private static void OnRenewLockLoopCompleted(IAsyncResult result)
        {
            MessageReceivePump asyncState = (MessageReceivePump)result.AsyncState;

            try
            {
                asyncState.EndRenewLockLoop(result);
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
                TrackingContext instance = TrackingContext.GetInstance(Guid.NewGuid(), asyncState.receiver.Path);
                MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.MessageReceivePumpUnexpectedException(instance.Activity, instance.TrackingId, instance.SystemTracker, exception.ToString()));
                throw;
            }
        }
            protected override IEnumerator <IteratorAsyncResult <MessageReceivePump.StartAsyncResult> .AsyncStep> GetAsyncSteps()
            {
                TimeSpan timeSpan;

                while (this.currentRetryCount <= 3)
                {
                    MessageReceivePump.StartAsyncResult startAsyncResult = this;
                    startAsyncResult.currentRetryCount = startAsyncResult.currentRetryCount + 1;
                    MessageReceivePump.StartAsyncResult startAsyncResult1 = this;
                    IteratorAsyncResult <MessageReceivePump.StartAsyncResult> .BeginCall beginCall = (MessageReceivePump.StartAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.owner.receiver.BeginReceive(TimeSpan.Zero, c, s);
                    yield return(startAsyncResult1.CallAsync(beginCall, (MessageReceivePump.StartAsyncResult thisPtr, IAsyncResult r) => thisPtr.InitialMessage = thisPtr.owner.receiver.EndReceive(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                    if (base.LastAsyncStepException == null)
                    {
                        break;
                    }
                    if (this.ShouldRetry(base.LastAsyncStepException))
                    {
                        if (!MessageReceivePump.ShouldBackoff(base.LastAsyncStepException, out timeSpan))
                        {
                            continue;
                        }
                        yield return(base.CallAsyncSleep(timeSpan));
                    }
                    else
                    {
                        base.Complete(base.LastAsyncStepException);
                        goto Label0;
                    }
                }
                try
                {
                    (new MessageReceivePump.PumpAsyncResult(this.owner, this.InitialMessage, this.onPumpCompleted, this)).Start();
                }
                catch (Exception exception)
                {
                    Environment.FailFast(exception.ToString());
                }
Label0:
                yield break;
            }
            private static void OnDispatchCompleted(IAsyncResult asyncResult)
            {
                MessageReceivePump asyncState = (MessageReceivePump)asyncResult.AsyncState;

                try
                {
                    try
                    {
                        AsyncResult <MessageReceivePump.DispatchAsyncResult> .End(asyncResult);
                    }
                    catch (Exception exception)
                    {
                        if (Fx.IsFatal(exception))
                        {
                            throw;
                        }
                    }
                }
                finally
                {
                    asyncState.semaphore.Exit();
                }
            }
 public SyncCallbackAsyncResult(MessageReceivePump owner, BrokeredMessage message, AsyncCallback callback, object state) : base(callback, state)
 {
     this.owner   = owner;
     this.message = message;
     IOThreadScheduler.ScheduleCallbackNoFlow(MessageReceivePump.SyncCallbackAsyncResult.executeSyncCallback, this);
 }
 public StartAsyncResult(MessageReceivePump owner, AsyncCallback callback, object state) : base(TimeSpan.MaxValue, callback, state)
 {
     this.owner = owner;
 }
 public BatchCompleteAsyncResult(MessageReceivePump owner, TrackingContext trackingContext, TimeSpan timeout, AsyncCallback callback, object state) : base(timeout, callback, state)
 {
     this.owner           = owner;
     this.lockTokens      = new List <Guid>();
     this.trackingContext = trackingContext;
 }