private static void MessageReceivePump(object state)
            {
                bool flag;

                DuplexRequestBindingElement.DuplexRequestSessionChannel duplexRequestSessionChannel = (DuplexRequestBindingElement.DuplexRequestSessionChannel)state;
                try
                {
                    do
                    {
                        flag = false;
                        if (duplexRequestSessionChannel.State != CommunicationState.Opened && duplexRequestSessionChannel.State != CommunicationState.Closing)
                        {
                            continue;
                        }
                        IAsyncResult asyncResult = duplexRequestSessionChannel.innerChannel.BeginTryReceive(TimeSpan.MaxValue, DuplexRequestBindingElement.DuplexRequestSessionChannel.onMessageReceived, duplexRequestSessionChannel);
                        if (!asyncResult.CompletedSynchronously)
                        {
                            continue;
                        }
                        flag = duplexRequestSessionChannel.HandleMessageReceived(asyncResult);
                    }while (flag);
                }
                catch (CommunicationException communicationException1)
                {
                    CommunicationException communicationException = communicationException1;
                    MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRuntimeChannelFaulting(duplexRequestSessionChannel.innerChannel.GetType().Name, duplexRequestSessionChannel.innerChannel.LocalAddress.Uri.AbsoluteUri, duplexRequestSessionChannel.innerChannel.RemoteAddress.Uri.AbsoluteUri, duplexRequestSessionChannel.innerChannel.Via.AbsoluteUri, duplexRequestSessionChannel.innerChannel.Session.Id, string.Concat("MessageReceivePump: ", communicationException.ToString())));
                    duplexRequestSessionChannel.Fault();
                }
            }
 private static void EndPing(IAsyncResult result)
 {
     DuplexRequestBindingElement.DuplexRequestSessionChannel asyncState = (DuplexRequestBindingElement.DuplexRequestSessionChannel)result.AsyncState;
     try
     {
         asyncState.innerChannel.EndSend(result);
         lock (asyncState.pingTimer)
         {
             CommunicationState state = asyncState.State;
             if (state == CommunicationState.Opened && asyncState.inflightRequests.Count > 0)
             {
                 asyncState.pingTimer.Set(SbmpConstants.ConnectionPingTimeout);
             }
             else if (state != CommunicationState.Opened)
             {
                 MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRuntimeChannelStopPingWithIncorrectState(asyncState.innerChannel.GetType().Name, asyncState.innerChannel.LocalAddress.Uri.AbsoluteUri, (asyncState.innerChannel.RemoteAddress == null ? "Null" : asyncState.innerChannel.RemoteAddress.Uri.AbsoluteUri), (asyncState.innerChannel.Via == null ? "Null" : asyncState.innerChannel.Via.AbsoluteUri), asyncState.innerChannel.Session.Id, state.ToString(), asyncState.inflightRequests.Count));
             }
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRuntimeChannelPingFailed(asyncState.innerChannel.GetType().Name, asyncState.innerChannel.LocalAddress.Uri.AbsoluteUri, (asyncState.innerChannel.RemoteAddress == null ? "Null" : asyncState.innerChannel.RemoteAddress.Uri.AbsoluteUri), (asyncState.innerChannel.Via == null ? "Null" : asyncState.innerChannel.Via.AbsoluteUri), asyncState.innerChannel.Session.Id, exception.ToString()));
     }
 }
            private static void BeginPing(object state)
            {
                DuplexRequestBindingElement.DuplexRequestSessionChannel duplexRequestSessionChannel = (DuplexRequestBindingElement.DuplexRequestSessionChannel)state;
                CommunicationState communicationState = duplexRequestSessionChannel.State;

                if (communicationState != CommunicationState.Opened)
                {
                    MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRuntimeChannelPingIncorrectState(duplexRequestSessionChannel.innerChannel.GetType().Name, duplexRequestSessionChannel.innerChannel.LocalAddress.Uri.AbsoluteUri, (duplexRequestSessionChannel.innerChannel.RemoteAddress == null ? "Null" : duplexRequestSessionChannel.innerChannel.RemoteAddress.Uri.AbsoluteUri), (duplexRequestSessionChannel.innerChannel.Via == null ? "Null" : duplexRequestSessionChannel.innerChannel.Via.AbsoluteUri), duplexRequestSessionChannel.innerChannel.Session.Id, communicationState.ToString()));
                    return;
                }
                Message message = Message.CreateMessage(duplexRequestSessionChannel.messageVersion, "http://schemas.microsoft.com/servicebus/2010/08/protocol/Ping", new Microsoft.ServiceBus.Messaging.Channels.PingMessage());

                try
                {
                    duplexRequestSessionChannel.innerChannel.BeginSend(message, SbmpConstants.ConnectionPingOperationTimeout, DuplexRequestBindingElement.DuplexRequestSessionChannel.endPing, duplexRequestSessionChannel);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRuntimeChannelPingFailed(duplexRequestSessionChannel.innerChannel.GetType().Name, duplexRequestSessionChannel.innerChannel.LocalAddress.Uri.AbsoluteUri, (duplexRequestSessionChannel.innerChannel.RemoteAddress == null ? "Null" : duplexRequestSessionChannel.innerChannel.RemoteAddress.Uri.AbsoluteUri), (duplexRequestSessionChannel.innerChannel.Via == null ? "Null" : duplexRequestSessionChannel.innerChannel.Via.AbsoluteUri), duplexRequestSessionChannel.innerChannel.Session.Id, exception.ToString()));
                }
            }
 private static void OnMessageReceived(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         DuplexRequestBindingElement.DuplexRequestSessionChannel asyncState = (DuplexRequestBindingElement.DuplexRequestSessionChannel)result.AsyncState;
         if (asyncState.HandleMessageReceived(result))
         {
             DuplexRequestBindingElement.DuplexRequestSessionChannel.MessageReceivePump(asyncState);
         }
     }
 }
 public CloseAsyncResult(DuplexRequestBindingElement.DuplexRequestSessionChannel correlator, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
 {
     this.correlator         = correlator;
     this.timeoutHelper      = new Microsoft.ServiceBus.Common.TimeoutHelper(timeout);
     this.shutdownWaitHandle = ThreadPool.RegisterWaitForSingleObject(this.correlator.serverShutdownEvent, DuplexRequestBindingElement.DuplexRequestSessionChannel.CloseAsyncResult.serverShutdownCallback, this, Microsoft.ServiceBus.Common.TimeoutHelper.ToMilliseconds(this.timeoutHelper.RemainingTime()), true);
     this.TryOperation(true, (DuplexRequestBindingElement.DuplexRequestSessionChannel.CloseAsyncResult t) => {
         IAsyncResult asyncResult = t.correlator.innerChannel.Session.BeginCloseOutputSession(t.timeoutHelper.RemainingTime(), DuplexRequestBindingElement.DuplexRequestSessionChannel.CloseAsyncResult.closeOutputSessionCallback, t);
         if (asyncResult.CompletedSynchronously)
         {
             t.correlator.innerChannel.Session.EndCloseOutputSession(asyncResult);
         }
     });
 }
                public DuplexCorrelationAsyncResult(DuplexRequestBindingElement.DuplexRequestSessionChannel parent, Message wcfRequest, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
                {
                    TrackingIdHeader trackingIdHeader;

                    DuplexRequestBindingElement.DuplexRequestSessionChannel.DuplexCorrelationAsyncResult duplexCorrelationAsyncResult;
                    this.parent           = parent;
                    this.OriginalTimeout  = timeout;
                    this.wcfRequest       = wcfRequest;
                    this.RequestMessageId = this.wcfRequest.Headers.MessageId;
                    if (this.RequestMessageId == null)
                    {
                        this.RequestMessageId             = new UniqueId();
                        this.wcfRequest.Headers.MessageId = this.RequestMessageId;
                    }
                    this.requestAction = this.wcfRequest.Headers.Action;
                    if (!TrackingIdHeader.TryRead(this.wcfRequest.Headers, out trackingIdHeader))
                    {
                        this.clientTrackingId = this.RequestMessageId.ToString();
                    }
                    else
                    {
                        this.clientTrackingId = trackingIdHeader.Id;
                    }
                    this.parent.inflightRequests[this.RequestMessageId] = this;
                    this.parent.OnBeginRequest();
                    if (timeout != TimeSpan.MaxValue)
                    {
                        this.timer = new IOThreadTimer(DuplexRequestBindingElement.DuplexRequestSessionChannel.DuplexCorrelationAsyncResult.timerCallback, this, false);
                        this.timer.Set(timeout);
                    }
                    lock (base.ThisLock)
                    {
                        try
                        {
                            if (base.SyncContinue(this.parent.innerChannel.BeginSend(this.wcfRequest, timeout, base.PrepareAsyncCompletion(DuplexRequestBindingElement.DuplexRequestSessionChannel.DuplexCorrelationAsyncResult.sendCompletion), this)))
                            {
                                this.TryComplete(true, null);
                            }
                        }
                        catch (Exception exception1)
                        {
                            Exception exception = exception1;
                            if (Fx.IsFatal(exception))
                            {
                                throw;
                            }
                            this.parent.inflightRequests.TryRemove(this.RequestMessageId, out duplexCorrelationAsyncResult);
                            this.TryComplete(true, exception);
                        }
                    }
                }