void OnInactivityElapsed(object state)
        {
            WsrmFault fault;
            Exception e;
            string    exceptionMessage = SR.GetString(SR.SequenceTerminatedInactivityTimeoutExceeded, this.settings.InactivityTimeout);

            if (TD.InactivityTimeoutIsEnabled())
            {
                TD.InactivityTimeout(exceptionMessage);
            }

            if (this.SequenceID != null)
            {
                string faultReason = SR.GetString(SR.SequenceTerminatedInactivityTimeoutExceeded, this.settings.InactivityTimeout);
                fault = SequenceTerminatedFault.CreateCommunicationFault(this.SequenceID, faultReason, exceptionMessage);
                e     = fault.CreateException();
            }
            else
            {
                fault = null;
                e     = new CommunicationException(exceptionMessage);
            }

            OnLocalFault(e, fault, null);
        }
        bool InternalAddMessage(Message message, TimeSpan timeout, object state, bool isLast)
        {
            MessageAttemptInfo attemptInfo;
            TimeoutHelper      helper = new TimeoutHelper(timeout);

            try
            {
                if (isLast)
                {
                    if (state != null)
                    {
                        throw Fx.AssertAndThrow("The isLast overload does not take a state.");
                    }

                    attemptInfo = this.strategy.AddLast(message, helper.RemainingTime(), null);
                }
                else if (!this.strategy.Add(message, helper.RemainingTime(), state, out attemptInfo))
                {
                    return(false);
                }
            }
            catch (TimeoutException)
            {
                if (isLast)
                {
                    this.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.id, SR.GetString(SR.SequenceTerminatedAddLastToWindowTimedOut), null));
                }
                // else - RM does not fault the channel based on a timeout exception trying to add a sequenced message to the window.

                throw;
            }
            catch (Exception e)
            {
                if (!Fx.IsFatal(e))
                {
                    this.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.id, SR.GetString(SR.SequenceTerminatedUnknownAddToWindowError), null));
                }

                throw;
            }

            if (sendGuard.Enter())
            {
                try
                {
                    this.sendHandler(attemptInfo, helper.RemainingTime(), false);
                }
                catch (QuotaExceededException)
                {
                    this.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.id));
                    throw;
                }
                finally
                {
                    this.sendGuard.Exit();
                }
            }

            return(true);
        }
        public virtual void Abort()
        {
            this.guard.Abort();
            this.inactivityTimer.Abort();

            // Try to send a fault.
            bool sendFault;

            lock (this.ThisLock)
            {
                // Faulted thread already cleaned up. No need to to anything more.
                if (this.faulted == SessionFaultState.CleanedUp)
                {
                    return;
                }

                // Can only send a fault if the other side did not send one already.
                sendFault    = this.canSendFault && (this.faulted != SessionFaultState.RemotelyFaulted); // NotFaulted || LocallyFaulted
                this.faulted = SessionFaultState.CleanedUp;
            }

            if (sendFault)
            {
                if ((this.binder.State == CommunicationState.Opened) &&
                    this.binder.Connected &&
                    (this.binder.CanSendAsynchronously || (this.replyFaultContext != null)))
                {
                    if (this.terminatingFault == null)
                    {
                        UniqueId sequenceId = this.InputID ?? this.OutputID;
                        if (sequenceId != null)
                        {
                            WsrmFault fault = SequenceTerminatedFault.CreateCommunicationFault(sequenceId, SR.GetString(SR.SequenceTerminatedOnAbort), null);
                            this.terminatingFault = fault.CreateMessage(this.settings.MessageVersion,
                                                                        this.settings.ReliableMessagingVersion);
                        }
                    }

                    if (this.terminatingFault != null)
                    {
                        this.AddFinalRanges();
                        this.faultHelper.SendFaultAsync(this.binder, this.replyFaultContext, this.terminatingFault);
                        return;
                    }
                }
            }

            // Got here so the session did not actually send a fault, must clean up resources.
            if (this.terminatingFault != null)
            {
                this.terminatingFault.Close();
            }
            if (this.replyFaultContext != null)
            {
                this.replyFaultContext.Abort();
            }
            this.binder.Abort();
        }
            public AddAsyncResult(Message message, bool isLast, TimeSpan timeout, object state,
                                  ReliableOutputConnection connection, AsyncCallback callback, object asyncState)
                : base(callback, asyncState)
            {
                this.connection    = connection;
                this.timeoutHelper = new TimeoutHelper(timeout);
                this.isLast        = isLast;

                bool         complete = false;
                IAsyncResult result;

                try
                {
                    if (isLast)
                    {
                        if (state != null)
                        {
                            throw Fx.AssertAndThrow("The isLast overload does not take a state.");
                        }

                        result = this.connection.strategy.BeginAddLast(message, this.timeoutHelper.RemainingTime(), state, addCompleteStatic, this);
                    }
                    else
                    {
                        result = this.connection.strategy.BeginAdd(message, this.timeoutHelper.RemainingTime(), state, addCompleteStatic, this);
                    }
                }
                catch (TimeoutException)
                {
                    if (isLast)
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedAddLastToWindowTimedOut), null));
                    }
                    // else - RM does not fault the channel based on a timeout exception trying to add a sequenced message to the window.

                    throw;
                }
                catch (Exception e)
                {
                    if (!Fx.IsFatal(e))
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedUnknownAddToWindowError), null));
                    }

                    throw;
                }

                if (result.CompletedSynchronously)
                {
                    complete = this.CompleteAdd(result);
                }

                if (complete)
                {
                    this.Complete(true);
                }
            }
        private bool InternalAddMessage(Message message, TimeSpan timeout, object state, bool isLast)
        {
            MessageAttemptInfo info;
            TimeoutHelper      helper = new TimeoutHelper(timeout);

            try
            {
                if (isLast)
                {
                    if (state != null)
                    {
                        throw Fx.AssertAndThrow("The isLast overload does not take a state.");
                    }
                    info = this.strategy.AddLast(message, helper.RemainingTime(), null);
                }
                else if (!this.strategy.Add(message, helper.RemainingTime(), state, out info))
                {
                    return(false);
                }
            }
            catch (TimeoutException)
            {
                if (isLast)
                {
                    this.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.id, System.ServiceModel.SR.GetString("SequenceTerminatedAddLastToWindowTimedOut"), null));
                }
                throw;
            }
            catch (Exception exception)
            {
                if (!Fx.IsFatal(exception))
                {
                    this.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.id, System.ServiceModel.SR.GetString("SequenceTerminatedUnknownAddToWindowError"), null));
                }
                throw;
            }
            if (this.sendGuard.Enter())
            {
                try
                {
                    this.sendHandler(info, helper.RemainingTime(), false);
                }
                catch (QuotaExceededException)
                {
                    this.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.id));
                    throw;
                }
                finally
                {
                    this.sendGuard.Exit();
                }
            }
            return(true);
        }
            public AddAsyncResult(Message message, bool isLast, TimeSpan timeout, object state, ReliableOutputConnection connection, AsyncCallback callback, object asyncState) : base(callback, asyncState)
            {
                IAsyncResult result;

                this.connection    = connection;
                this.timeoutHelper = new TimeoutHelper(timeout);
                this.isLast        = isLast;
                bool flag = false;

                try
                {
                    if (isLast)
                    {
                        if (state != null)
                        {
                            throw Fx.AssertAndThrow("The isLast overload does not take a state.");
                        }
                        result = this.connection.strategy.BeginAddLast(message, this.timeoutHelper.RemainingTime(), state, addCompleteStatic, this);
                    }
                    else
                    {
                        result = this.connection.strategy.BeginAdd(message, this.timeoutHelper.RemainingTime(), state, addCompleteStatic, this);
                    }
                }
                catch (TimeoutException)
                {
                    if (isLast)
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, System.ServiceModel.SR.GetString("SequenceTerminatedAddLastToWindowTimedOut"), null));
                    }
                    throw;
                }
                catch (Exception exception)
                {
                    if (!Fx.IsFatal(exception))
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, System.ServiceModel.SR.GetString("SequenceTerminatedUnknownAddToWindowError"), null));
                    }
                    throw;
                }
                if (result.CompletedSynchronously)
                {
                    flag = this.CompleteAdd(result);
                }
                if (flag)
                {
                    base.Complete(true);
                }
            }
        private void OnInactivityElapsed(object state)
        {
            WsrmFault fault;
            Exception exception;
            string    exceptionMessage = System.ServiceModel.SR.GetString("SequenceTerminatedInactivityTimeoutExceeded", new object[] { this.settings.InactivityTimeout });

            if (this.SequenceID != null)
            {
                string faultReason = System.ServiceModel.SR.GetString("SequenceTerminatedInactivityTimeoutExceeded", new object[] { this.settings.InactivityTimeout });
                fault     = SequenceTerminatedFault.CreateCommunicationFault(this.SequenceID, faultReason, exceptionMessage);
                exception = fault.CreateException();
            }
            else
            {
                fault     = null;
                exception = new CommunicationException(exceptionMessage);
            }
            this.OnLocalFault(exception, fault, null);
        }
        public virtual void Abort()
        {
            bool flag;

            this.guard.Abort();
            this.inactivityTimer.Abort();
            lock (this.ThisLock)
            {
                if (this.faulted == SessionFaultState.CleanedUp)
                {
                    return;
                }
                flag         = this.canSendFault && (this.faulted != SessionFaultState.RemotelyFaulted);
                this.faulted = SessionFaultState.CleanedUp;
            }
            if (((flag && (this.binder.State == CommunicationState.Opened)) && this.binder.Connected) && (this.binder.CanSendAsynchronously || (this.replyFaultContext != null)))
            {
                if (this.terminatingFault == null)
                {
                    UniqueId sequenceID = this.InputID ?? this.OutputID;
                    if (sequenceID != null)
                    {
                        this.terminatingFault = SequenceTerminatedFault.CreateCommunicationFault(sequenceID, System.ServiceModel.SR.GetString("SequenceTerminatedOnAbort"), null).CreateMessage(this.settings.MessageVersion, this.settings.ReliableMessagingVersion);
                    }
                }
                if (this.terminatingFault != null)
                {
                    this.AddFinalRanges();
                    this.faultHelper.SendFaultAsync(this.binder, this.replyFaultContext, this.terminatingFault);
                    return;
                }
            }
            if (this.terminatingFault != null)
            {
                this.terminatingFault.Close();
            }
            if (this.replyFaultContext != null)
            {
                this.replyFaultContext.Abort();
            }
            this.binder.Abort();
        }
            public Message WaitForReply(TimeSpan timeout)
            {
                bool throwing = true;

                try
                {
                    bool expired = false;

                    if (!this.completed)
                    {
                        bool wait = false;

                        lock (this.ThisLock)
                        {
                            if (!this.completed)
                            {
                                wait = true;
                                this.completedHandle = new ManualResetEvent(false);
                            }
                        }

                        if (wait)
                        {
                            expired = !TimeoutHelper.WaitOne(this.completedHandle, timeout);

                            lock (this.ThisLock)
                            {
                                if (!this.completed)
                                {
                                    this.completed = true;
                                }
                                else
                                {
                                    expired = false;
                                }
                            }

                            this.completedHandle.Close();
                        }
                    }

                    if (this.aborted)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.parent.CreateClosedException());
                    }
                    else if (this.faulted)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.parent.GetTerminalException());
                    }
                    else if (expired)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.GetString(SR.TimeoutOnRequest, this.originalTimeout)));
                    }
                    else
                    {
                        throwing = false;
                        return(this.reply);
                    }
                }
                finally
                {
                    if (throwing)
                    {
                        WsrmFault fault = SequenceTerminatedFault.CreateCommunicationFault(this.parent.session.InputID,
                                                                                           SR.GetString(SR.SequenceTerminatedReliableRequestThrew), null);
                        this.parent.session.OnLocalFault(null, fault, null);
                        if (this.completedHandle != null)
                        {
                            this.completedHandle.Close();
                        }
                    }
                }
            }
            public Message WaitForReply(TimeSpan timeout)
            {
                Message reply;
                bool    flag = true;

                try
                {
                    bool flag2 = false;
                    if (!this.completed)
                    {
                        bool flag3 = false;
                        lock (this.ThisLock)
                        {
                            if (!this.completed)
                            {
                                flag3 = true;
                                this.completedHandle = new ManualResetEvent(false);
                            }
                        }
                        if (flag3)
                        {
                            flag2 = !TimeoutHelper.WaitOne(this.completedHandle, timeout);
                            lock (this.ThisLock)
                            {
                                if (!this.completed)
                                {
                                    this.completed = true;
                                }
                                else
                                {
                                    flag2 = false;
                                }
                            }
                            this.completedHandle.Close();
                        }
                    }
                    if (this.aborted)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.parent.CreateClosedException());
                    }
                    if (this.faulted)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.parent.GetTerminalException());
                    }
                    if (flag2)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(System.ServiceModel.SR.GetString("TimeoutOnRequest", new object[] { this.originalTimeout })));
                    }
                    flag  = false;
                    reply = this.reply;
                }
                finally
                {
                    if (flag)
                    {
                        WsrmFault fault = SequenceTerminatedFault.CreateCommunicationFault(this.parent.session.InputID, System.ServiceModel.SR.GetString("SequenceTerminatedReliableRequestThrew"), null);
                        this.parent.session.OnLocalFault(null, fault, null);
                        if (this.completedHandle != null)
                        {
                            this.completedHandle.Close();
                        }
                    }
                }
                return(reply);
            }
            private bool CompleteAdd(IAsyncResult result)
            {
                MessageAttemptInfo attemptInfo = new MessageAttemptInfo();

                this.validAdd = true;
                try
                {
                    if (this.isLast)
                    {
                        attemptInfo = this.connection.strategy.EndAddLast(result);
                    }
                    else if (!this.connection.strategy.EndAdd(result, out attemptInfo))
                    {
                        this.validAdd = false;
                        return(true);
                    }
                }
                catch (TimeoutException)
                {
                    if (this.isLast)
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, System.ServiceModel.SR.GetString("SequenceTerminatedAddLastToWindowTimedOut"), null));
                    }
                    throw;
                }
                catch (Exception exception)
                {
                    if (!Fx.IsFatal(exception))
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, System.ServiceModel.SR.GetString("SequenceTerminatedUnknownAddToWindowError"), null));
                    }
                    throw;
                }
                if (this.connection.sendGuard.Enter())
                {
                    bool flag = true;
                    try
                    {
                        result = this.connection.beginSendHandler(attemptInfo, this.timeoutHelper.RemainingTime(), false, sendCompleteStatic, this);
                        flag   = false;
                    }
                    catch (QuotaExceededException)
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.connection.id));
                        throw;
                    }
                    finally
                    {
                        if (flag)
                        {
                            this.connection.sendGuard.Exit();
                        }
                    }
                }
                else
                {
                    return(true);
                }
                if (result.CompletedSynchronously)
                {
                    this.CompleteSend(result);
                    return(true);
                }
                return(false);
            }
            bool CompleteAdd(IAsyncResult result)
            {
                MessageAttemptInfo attemptInfo = default(MessageAttemptInfo);

                this.validAdd = true;

                try
                {
                    if (this.isLast)
                    {
                        attemptInfo = this.connection.strategy.EndAddLast(result);
                    }
                    else if (!this.connection.strategy.EndAdd(result, out attemptInfo))
                    {
                        this.validAdd = false;
                        return(true);
                    }
                }
                catch (TimeoutException)
                {
                    if (this.isLast)
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedAddLastToWindowTimedOut), null));
                    }
                    // else - RM does not fault the channel based on a timeout exception trying to add a sequenced message to the window.

                    throw;
                }
                catch (Exception e)
                {
                    if (!Fx.IsFatal(e))
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateCommunicationFault(this.connection.id, SR.GetString(SR.SequenceTerminatedUnknownAddToWindowError), null));
                    }

                    throw;
                }

                if (this.connection.sendGuard.Enter())
                {
                    bool throwing = true;

                    try
                    {
                        result   = this.connection.beginSendHandler(attemptInfo, this.timeoutHelper.RemainingTime(), false, sendCompleteStatic, this);
                        throwing = false;
                    }
                    catch (QuotaExceededException)
                    {
                        this.connection.RaiseFault(null, SequenceTerminatedFault.CreateQuotaExceededFault(this.connection.id));
                        throw;
                    }
                    finally
                    {
                        if (throwing)
                        {
                            this.connection.sendGuard.Exit();
                        }
                    }
                }
                else
                {
                    return(true);
                }

                if (result.CompletedSynchronously)
                {
                    this.CompleteSend(result);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Exemple #13
0
            public async Task <Message> ReceiveReplyAsync(TimeoutHelper timeoutHelper)
            {
                bool throwing = true;

                try
                {
                    bool expired = false;

                    if (!_completed)
                    {
                        bool wait = false;

                        lock (ThisLock)
                        {
                            if (!_completed)
                            {
                                wait = true;
                                _tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
                            }
                        }

                        if (wait)
                        {
                            expired = !await TaskHelpers.AwaitWithTimeout(_tcs.Task, timeoutHelper.RemainingTime());

                            lock (ThisLock)
                            {
                                if (!_completed)
                                {
                                    _completed = true;
                                }
                                else
                                {
                                    expired = false;
                                }
                            }
                        }
                    }

                    if (_aborted)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_parent.CreateClosedException());
                    }
                    else if (_faulted)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(_parent.GetTerminalException());
                    }
                    else if (expired)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.TimeoutOnRequest, _originalTimeout)));
                    }
                    else
                    {
                        throwing = false;
                        return(_reply);
                    }
                }
                finally
                {
                    if (throwing)
                    {
                        WsrmFault fault = SequenceTerminatedFault.CreateCommunicationFault(_parent.session.InputID,
                                                                                           SR.SequenceTerminatedReliableRequestThrew, null);
                        _parent.session.OnLocalFault(null, fault, null);
                    }
                }
            }