internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
        {
            msmqMessage  = null;
            msmqProperty = null;
            if (result == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }
            if (this.receiveParameters.ExactlyOnce)
            {
                TryTransactedReceiveAsyncResult result2 = result as TryTransactedReceiveAsyncResult;
                if (result2 == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("InvalidAsyncResult"));
                }
                return(TryTransactedReceiveAsyncResult.End(result2, out msmqMessage, out msmqProperty));
            }
            TryNonTransactedReceiveAsyncResult result3 = result as TryNonTransactedReceiveAsyncResult;

            if (result3 == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("InvalidAsyncResult"));
            }
            return(TryNonTransactedReceiveAsyncResult.End(result3, out msmqMessage, out msmqProperty));
        }
Esempio n. 2
0
        //
        internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
        {
            msmqMessage  = null;
            msmqProperty = null;

            if (null == result)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }

            if (this.receiveParameters.ExactlyOnce)
            {
                TryTransactedReceiveAsyncResult receiveResult = result as TryTransactedReceiveAsyncResult;
                if (null == receiveResult)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                }
                return(TryTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty));
            }
            else
            {
                TryNonTransactedReceiveAsyncResult receiveResult = result as TryNonTransactedReceiveAsyncResult;
                if (null == receiveResult)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                }
                return(TryNonTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty));
            }
        }
Esempio n. 3
0
        internal bool TryReceive(MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, out MsmqMessageProperty property)
        {
            property = null;

            MsmqQueue.ReceiveResult receiveResult = this.Queue.TryReceive(msmqMessage, timeout, transactionMode);
            if (MsmqQueue.ReceiveResult.OperationCancelled == receiveResult)
            {
                return(true);
            }
            if (MsmqQueue.ReceiveResult.Timeout == receiveResult)
            {
                return(false);
            }
            else
            {
                property = new MsmqMessageProperty(msmqMessage);
                if (this.Transactional)
                {
                    if (this.PoisonHandler.CheckAndHandlePoisonMessage(property))
                    {
                        long lookupId = property.LookupId;
                        property = null;
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new MsmqPoisonMessageException(lookupId));
                    }
                }
                return(true);
            }
        }
Esempio n. 4
0
 internal TryNonTransactedReceiveAsyncResult(MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.receiver    = receiver;
     this.msmqMessage = msmqMessage;
     receiver.Queue.BeginTryReceive(msmqMessage, timeout, onCompleteStatic, this);
 }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     MsmqReceiveHelper.TryTransactedReceiveAsyncResult result2 = AsyncResult.End <MsmqReceiveHelper.TryTransactedReceiveAsyncResult>(result);
     msmqMessage = result2.msmqMessage;
     property    = result2.messageProperty;
     return(!result2.expired);
 }
 internal IAsyncResult BeginTryReceive(MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, AsyncCallback callback, object state)
 {
     if (!this.receiveParameters.ExactlyOnce && !(this.queue is ILockingQueue))
     {
         return new TryNonTransactedReceiveAsyncResult(this, msmqMessage, timeout, callback, state);
     }
     return new TryTransactedReceiveAsyncResult(this, msmqMessage, timeout, transactionMode, callback, state);
 }
 internal IAsyncResult BeginTryReceive(MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, AsyncCallback callback, object state)
 {
     if (!this.receiveParameters.ExactlyOnce && !(this.queue is ILockingQueue))
     {
         return(new TryNonTransactedReceiveAsyncResult(this, msmqMessage, timeout, callback, state));
     }
     return(new TryTransactedReceiveAsyncResult(this, msmqMessage, timeout, transactionMode, callback, state));
 }
 public bool EndTryReceive(IAsyncResult result, out Message message)
 {
     bool flag2;
     message = null;
     if (result == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
     }
     DoneReceivingAsyncResult result2 = result as DoneReceivingAsyncResult;
     if (result2 != null)
     {
         return DoneReceivingAsyncResult.End(result2);
     }
     MsmqInputMessage msmqMessage = null;
     MsmqMessageProperty msmqProperty = null;
     try
     {
         bool flag = this.receiver.EndTryReceive(result, out msmqMessage, out msmqProperty);
         if (flag)
         {
             if (msmqProperty != null)
             {
                 message = this.DecodeMsmqMessage(msmqMessage, msmqProperty);
                 message.Properties["MsmqMessageProperty"] = msmqProperty;
                 if (this.receiveParameters.ReceiveContextSettings.Enabled)
                 {
                     message.Properties[ReceiveContext.Name] = this.receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                 }
                 MsmqDiagnostics.DatagramReceived(msmqMessage.MessageId, message);
                 this.listener.RaiseMessageReceived();
             }
             else if (CommunicationState.Opened == base.State)
             {
                 this.listener.FaultListener();
                 base.Fault();
             }
         }
         flag2 = flag;
     }
     catch (MsmqException exception)
     {
         if (exception.FaultReceiver)
         {
             this.listener.FaultListener();
             base.Fault();
         }
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
     }
     finally
     {
         if (msmqMessage != null)
         {
             this.receiver.ReturnMessage(msmqMessage);
         }
     }
     return flag2;
 }
 internal TryTransactedReceiveAsyncResult(MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, AsyncCallback callback, object state) : base(callback, state)
 {
     this.timeoutHelper   = new TimeoutHelper(timeout);
     this.txCurrent       = Transaction.Current;
     this.receiver        = receiver;
     this.msmqMessage     = msmqMessage;
     this.transactionMode = transactionMode;
     ActionItem.Schedule(onComplete, this);
 }
 public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
 {
     if (base.DoneReceivingInCurrentState())
     {
         return new DoneReceivingAsyncResult(callback, state);
     }
     MsmqInputMessage msmqMessage = this.receiver.TakeMessage();
     return this.receiver.BeginTryReceive(msmqMessage, timeout, this.ReceiveParameters.ExactlyOnce ? MsmqTransactionMode.CurrentOrNone : MsmqTransactionMode.None, callback, state);
 }
        //
        public override IInputSessionChannel EndAcceptChannel(IAsyncResult result)
        {
            if (null == result)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }

            DoneReceivingAsyncResult doneRecevingResult = result as DoneReceivingAsyncResult;

            if (doneRecevingResult != null)
            {
                DoneReceivingAsyncResult.End(doneRecevingResult);
                return(null);
            }

            MsmqInputMessage    msmqMessage = null;
            MsmqMessageProperty property    = null;

            try
            {
                bool retval = this.receiver.EndTryReceive(result, out msmqMessage, out property);
                if (retval)
                {
                    if (null != property)
                    {
                        return(MsmqDecodeHelper.DecodeTransportSessiongram(this, msmqMessage, property, this.receiveContextManager));
                    }
                    else
                    {
                        if (CommunicationState.Opened == this.State)
                        {
                            this.Fault();
                        }
                        return(null);
                    }
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
                }
            }
            catch (MsmqException ex)
            {
                if (ex.FaultReceiver)
                {
                    this.Fault();
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
            }
            finally
            {
                if (null != msmqMessage)
                {
                    this.receiver.ReturnMessage(msmqMessage);
                }
            }
        }
        // TryReceive
        public bool TryReceive(TimeSpan timeout, out Message message)
        {
            message = null;
            if (DoneReceivingInCurrentState())
            {
                return(true);
            }

            using (MsmqDiagnostics.BoundReceiveOperation(this.receiver))
            {
                MsmqInputMessage msmqMessage = this.receiver.TakeMessage();
                try
                {
                    MsmqMessageProperty property;
                    bool retval = this.receiver.TryReceive(
                        msmqMessage,
                        timeout,
                        this.ReceiveParameters.ExactlyOnce ? MsmqTransactionMode.CurrentOrNone : MsmqTransactionMode.None,
                        out property);
                    if (retval)
                    {
                        if (null != property)
                        {
                            message = DecodeMsmqMessage(msmqMessage, property);
                            message.Properties[MsmqMessageProperty.Name] = property;

                            if (this.receiveParameters.ReceiveContextSettings.Enabled)
                            {
                                message.Properties[MsmqReceiveContext.Name] = this.receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                            }
                            MsmqDiagnostics.DatagramReceived(msmqMessage.MessageId, message);
                            this.listener.RaiseMessageReceived();
                        }
                        else if (CommunicationState.Opened == this.State)
                        {
                            this.listener.FaultListener();
                            this.Fault();
                        }
                    }
                    return(retval);
                }
                catch (MsmqException ex)
                {
                    if (ex.FaultReceiver)
                    {
                        this.listener.FaultListener();
                        this.Fault();
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
                }
                finally
                {
                    this.receiver.ReturnMessage(msmqMessage);
                }
            }
        }
 protected override void OnGrowBuffers(MsmqInputMessage.SizeQuota bufferSizeQuota)
 {
     base.OnGrowBuffers(bufferSizeQuota);
     this.adminQueue.EnsureValueLength(this.adminQueueLength.Value);
     this.responseFormatName.EnsureValueLength(this.responseFormatNameLength.Value);
     this.destinationQueue.EnsureValueLength(this.destinationQueueLength.Value);
     this.label.EnsureValueLength(this.labelLength.Value);
     bufferSizeQuota.Alloc(this.extensionLength.Value);
     this.extension.EnsureBufferLength(this.extensionLength.Value);
 }
        //
        public override IInputSessionChannel AcceptChannel(TimeSpan timeout)
        {
            if (DoneReceivingInCurrentState())
            {
                return(null);
            }

            if (!this.ReceiveParameters.ReceiveContextSettings.Enabled && (Transaction.Current == null))
            {
                // In the absence of Receive context, Msmq Sessions can work only with the current transaction,
                this.Fault();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MsmqTransactionRequired)));
            }

            MsmqInputMessage msmqMessage = this.receiver.TakeMessage();

            try
            {
                MsmqMessageProperty property;
                bool retval = this.receiver.TryReceive(msmqMessage, timeout, MsmqTransactionMode.CurrentOrThrow, out property);
                if (retval)
                {
                    if (null != property)
                    {
                        return(MsmqDecodeHelper.DecodeTransportSessiongram(this, msmqMessage, property, this.receiveContextManager));
                    }
                    else
                    {
                        if (CommunicationState.Opened == this.State)
                        {
                            this.Fault();
                        }
                        return(null);
                    }
                }
                else
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
                }
            }
            catch (MsmqException ex)
            {
                if (ex.FaultReceiver)
                {
                    this.Fault();
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
            }
            finally
            {
                this.receiver.ReturnMessage(msmqMessage);
            }
        }
Esempio n. 15
0
        public override IInputSessionChannel EndAcceptChannel(IAsyncResult result)
        {
            IInputSessionChannel channel;

            if (result == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }
            DoneReceivingAsyncResult result2 = result as DoneReceivingAsyncResult;

            if (result2 != null)
            {
                DoneReceivingAsyncResult.End(result2);
                return(null);
            }
            MsmqInputMessage    msmqMessage  = null;
            MsmqMessageProperty msmqProperty = null;

            try
            {
                if (!this.receiver.EndTryReceive(result, out msmqMessage, out msmqProperty))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
                }
                if (msmqProperty != null)
                {
                    return(MsmqDecodeHelper.DecodeTransportSessiongram(this, msmqMessage, msmqProperty, this.receiveContextManager));
                }
                if (CommunicationState.Opened == base.State)
                {
                    base.Fault();
                }
                channel = null;
            }
            catch (MsmqException exception)
            {
                if (exception.FaultReceiver)
                {
                    base.Fault();
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
            }
            finally
            {
                if (msmqMessage != null)
                {
                    this.receiver.ReturnMessage(msmqMessage);
                }
            }
            return(channel);
        }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     MsmqReceiveHelper.TryNonTransactedReceiveAsyncResult result2 = AsyncResult.End <MsmqReceiveHelper.TryNonTransactedReceiveAsyncResult>(result);
     msmqMessage = result2.msmqMessage;
     property    = null;
     if (MsmqQueue.ReceiveResult.Timeout == result2.receiveResult)
     {
         return(false);
     }
     if (MsmqQueue.ReceiveResult.OperationCancelled != result2.receiveResult)
     {
         property = new MsmqMessageProperty(msmqMessage);
     }
     return(true);
 }
Esempio n. 17
0
        public override IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
        {
            if (base.DoneReceivingInCurrentState())
            {
                return(new DoneReceivingAsyncResult(callback, state));
            }
            if (!base.ReceiveParameters.ReceiveContextSettings.Enabled && (Transaction.Current == null))
            {
                base.Fault();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqTransactionRequired")));
            }
            MsmqInputMessage msmqMessage = this.receiver.TakeMessage();

            return(this.receiver.BeginTryReceive(msmqMessage, timeout, MsmqTransactionMode.CurrentOrThrow, callback, state));
        }
Esempio n. 18
0
        public override IInputSessionChannel AcceptChannel(TimeSpan timeout)
        {
            IInputSessionChannel channel;

            if (base.DoneReceivingInCurrentState())
            {
                return(null);
            }
            if (!base.ReceiveParameters.ReceiveContextSettings.Enabled && (Transaction.Current == null))
            {
                base.Fault();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqTransactionRequired")));
            }
            MsmqInputMessage msmqMessage = this.receiver.TakeMessage();

            try
            {
                MsmqMessageProperty property;
                if (!this.receiver.TryReceive(msmqMessage, timeout, MsmqTransactionMode.CurrentOrThrow, out property))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException());
                }
                if (property != null)
                {
                    return(MsmqDecodeHelper.DecodeTransportSessiongram(this, msmqMessage, property, this.receiveContextManager));
                }
                if (CommunicationState.Opened == base.State)
                {
                    base.Fault();
                }
                channel = null;
            }
            catch (MsmqException exception)
            {
                if (exception.FaultReceiver)
                {
                    base.Fault();
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
            }
            finally
            {
                this.receiver.ReturnMessage(msmqMessage);
            }
            return(channel);
        }
 internal MsmqMessageProperty(MsmqInputMessage msmqMessage)
 {
     if (msmqMessage == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("msmqMessage");
     }
     this.lookupId = msmqMessage.LookupId.Value;
     if (msmqMessage.AbortCount != null)
     {
         this.abortCount = msmqMessage.AbortCount.Value;
     }
     if (msmqMessage.MoveCount != null)
     {
         this.moveCount = msmqMessage.MoveCount.Value;
     }
     this.acknowledge = (ushort) msmqMessage.Class.Value;
     this.messageId = MsmqMessageId.ToString(msmqMessage.MessageId.Buffer);
 }
 internal MsmqMessageProperty(MsmqInputMessage msmqMessage)
 {
     if (msmqMessage == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("msmqMessage");
     }
     this.lookupId = msmqMessage.LookupId.Value;
     if (msmqMessage.AbortCount != null)
     {
         this.abortCount = msmqMessage.AbortCount.Value;
     }
     if (msmqMessage.MoveCount != null)
     {
         this.moveCount = msmqMessage.MoveCount.Value;
     }
     this.acknowledge = (ushort)msmqMessage.Class.Value;
     this.messageId   = MsmqMessageId.ToString(msmqMessage.MessageId.Buffer);
 }
Esempio n. 21
0
            internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
            {
                TryNonTransactedReceiveAsyncResult asyncResult = AsyncResult.End <TryNonTransactedReceiveAsyncResult>(result);

                msmqMessage = asyncResult.msmqMessage;
                property    = null;
                if (MsmqQueue.ReceiveResult.Timeout == asyncResult.receiveResult)
                {
                    return(false);
                }
                else if (MsmqQueue.ReceiveResult.OperationCancelled == asyncResult.receiveResult)
                {
                    return(true);
                }
                else
                {
                    property = new MsmqMessageProperty(msmqMessage);
                    return(true);
                }
            }
        //
        public override IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
        {
            if (DoneReceivingInCurrentState())
            {
                return(new DoneReceivingAsyncResult(callback, state));
            }

            if (!this.ReceiveParameters.ReceiveContextSettings.Enabled && (Transaction.Current == null))
            {
                // In the absence of Receive context, Msmq Sessions can work only with the current transaction,
                this.Fault();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.MsmqTransactionRequired)));
            }

            MsmqInputMessage msmqMessage = this.receiver.TakeMessage();

            return(this.receiver.BeginTryReceive(
                       msmqMessage,
                       timeout,
                       MsmqTransactionMode.CurrentOrThrow,
                       callback,
                       state));
        }
 protected MsmqIntegrationInputMessage(MsmqInputMessage.SizeQuota bufferSizeQuota) : base(0x16, bufferSizeQuota)
 {
     this.acknowledge = new NativeMsmqMessage.ByteProperty(this, 6);
     this.adminQueue = new NativeMsmqMessage.StringProperty(this, 0x11, 0x100);
     this.adminQueueLength = new NativeMsmqMessage.IntProperty(this, 0x12, 0x100);
     this.appSpecific = new NativeMsmqMessage.IntProperty(this, 8);
     this.arrivedTime = new NativeMsmqMessage.IntProperty(this, 0x20);
     this.senderIdType = new NativeMsmqMessage.IntProperty(this, 0x16);
     this.authenticated = new NativeMsmqMessage.ByteProperty(this, 0x19);
     this.bodyType = new NativeMsmqMessage.IntProperty(this, 0x2a);
     this.correlationId = new NativeMsmqMessage.BufferProperty(this, 3, 20);
     this.destinationQueue = new NativeMsmqMessage.StringProperty(this, 0x3a, 0x100);
     this.destinationQueueLength = new NativeMsmqMessage.IntProperty(this, 0x3b, 0x100);
     this.extension = new NativeMsmqMessage.BufferProperty(this, 0x23, bufferSizeQuota.AllocIfAvailable(0));
     this.extensionLength = new NativeMsmqMessage.IntProperty(this, 0x24, 0);
     this.label = new NativeMsmqMessage.StringProperty(this, 11, 0x80);
     this.labelLength = new NativeMsmqMessage.IntProperty(this, 12, 0x80);
     this.priority = new NativeMsmqMessage.ByteProperty(this, 4);
     this.responseFormatName = new NativeMsmqMessage.StringProperty(this, 0x36, 0x100);
     this.responseFormatNameLength = new NativeMsmqMessage.IntProperty(this, 0x37, 0x100);
     this.sentTime = new NativeMsmqMessage.IntProperty(this, 0x1f);
     this.timeToReachQueue = new NativeMsmqMessage.IntProperty(this, 13);
     this.privacyLevel = new NativeMsmqMessage.IntProperty(this, 0x17);
 }
 internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
 {
     Message message2;
     using (MsmqDiagnostics.BoundReceiveBytesOperation())
     {
         long num1 = msmqMessage.LookupId.Value;
         int size = msmqMessage.BodyLength.Value;
         int offset = 0;
         byte[] incoming = msmqMessage.Body.Buffer;
         ServerModeDecoder modeDecoder = new ServerModeDecoder();
         try
         {
             ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
         }
         catch (ProtocolException exception)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
         }
         if (modeDecoder.Mode != FramingMode.SingletonSized)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
         }
         ServerSingletonSizedDecoder decoder2 = new ServerSingletonSizedDecoder(0L, 0x800, 0x100);
         try
         {
             do
             {
                 if (size <= 0)
                 {
                     throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                 }
                 int num3 = decoder2.Decode(incoming, offset, size);
                 offset += num3;
                 size -= num3;
             }
             while (decoder2.CurrentState != ServerSingletonSizedDecoder.State.Start);
         }
         catch (ProtocolException exception2)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
         }
         if (size > listener.MaxReceivedMessageSize)
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
         }
         if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder2.ContentType))
         {
             receiver.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
         }
         byte[] dst = listener.BufferManager.TakeBuffer(size);
         Buffer.BlockCopy(incoming, offset, dst, 0, size);
         Message message = null;
         using (MsmqDiagnostics.BoundDecodeOperation())
         {
             try
             {
                 message = listener.MessageEncoderFactory.Encoder.ReadMessage(new ArraySegment<byte>(dst, 0, size), listener.BufferManager);
             }
             catch (XmlException exception3)
             {
                 receiver.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadXml"), exception3));
             }
             bool flag = true;
             try
             {
                 SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
                 if (property != null)
                 {
                     message.Properties.Security = property;
                 }
                 flag = false;
                 MsmqDiagnostics.TransferFromTransport(message);
                 message2 = message;
             }
             catch (Exception exception4)
             {
                 if (Fx.IsFatal(exception4))
                 {
                     throw;
                 }
                 receiver.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
             }
             finally
             {
                 if (flag)
                 {
                     message.Close();
                 }
             }
         }
     }
     return message2;
 }
 internal static IInputSessionChannel DecodeTransportSessiongram(MsmqInputSessionChannelListener listener, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty, MsmqReceiveContextLockManager receiveContextManager)
 {
     using (MsmqDiagnostics.BoundReceiveBytesOperation())
     {
         int num4;
         long num1 = msmqMessage.LookupId.Value;
         int size = msmqMessage.BodyLength.Value;
         int offset = 0;
         byte[] incoming = msmqMessage.Body.Buffer;
         MsmqReceiveHelper msmqReceiveHelper = listener.MsmqReceiveHelper;
         ServerModeDecoder modeDecoder = new ServerModeDecoder();
         try
         {
             ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
         }
         catch (ProtocolException exception)
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
         }
         if (modeDecoder.Mode != FramingMode.Simplex)
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
         }
         MsmqInputSessionChannel channel = null;
         ServerSessionDecoder decoder2 = new ServerSessionDecoder(0L, 0x800, 0x100);
         try
         {
             do
             {
                 if (size <= 0)
                 {
                     throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                 }
                 int num3 = decoder2.Decode(incoming, offset, size);
                 offset += num3;
                 size -= num3;
             }
             while (ServerSessionDecoder.State.EnvelopeStart != decoder2.CurrentState);
         }
         catch (ProtocolException exception2)
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
         }
         MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();
         if (!encoder.IsContentTypeSupported(decoder2.ContentType))
         {
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
         }
         ReceiveContext sessiongramReceiveContext = null;
         if (msmqReceiveHelper.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
         {
             sessiongramReceiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
         }
         channel = new MsmqInputSessionChannel(listener, Transaction.Current, sessiongramReceiveContext);
         Message item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
         SecurityMessageProperty property = null;
         try
         {
             property = listener.ValidateSecurity(msmqMessage);
         }
         catch (Exception exception3)
         {
             if (Fx.IsFatal(exception3))
             {
                 throw;
             }
             channel.FaultChannel();
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception3);
         }
         if (property != null)
         {
             item.Properties.Security = property;
         }
         item.Properties["MsmqMessageProperty"] = messageProperty;
         channel.EnqueueAndDispatch(item);
         listener.RaiseMessageReceived();
     Label_01F6:
         try
         {
             if (size <= 0)
             {
                 channel.FaultChannel();
                 msmqReceiveHelper.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
             }
             num4 = decoder2.Decode(incoming, offset, size);
         }
         catch (ProtocolException exception4)
         {
             channel.FaultChannel();
             msmqReceiveHelper.FinalDisposition(messageProperty);
             throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
         }
         offset += num4;
         size -= num4;
         if (ServerSessionDecoder.State.End != decoder2.CurrentState)
         {
             if (ServerSessionDecoder.State.EnvelopeStart == decoder2.CurrentState)
             {
                 item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
                 if (property != null)
                 {
                     item.Properties.Security = (SecurityMessageProperty) property.CreateCopy();
                 }
                 item.Properties["MsmqMessageProperty"] = messageProperty;
                 channel.EnqueueAndDispatch(item);
                 listener.RaiseMessageReceived();
             }
             goto Label_01F6;
         }
         channel.Shutdown();
         MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);
         return channel;
     }
 }
        //
        internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
        {
            msmqMessage = null;
            msmqProperty = null;

            if (null == result)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");

            if (this.receiveParameters.ExactlyOnce)
            {
                TryTransactedReceiveAsyncResult receiveResult = result as TryTransactedReceiveAsyncResult;
                if (null == receiveResult)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                return TryTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty);
            }
            else
            {
                TryNonTransactedReceiveAsyncResult receiveResult = result as TryNonTransactedReceiveAsyncResult;
                if (null == receiveResult)
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.InvalidAsyncResult));
                return TryNonTransactedReceiveAsyncResult.End(receiveResult, out msmqMessage, out msmqProperty);
            }
        }
        internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            Message message2;

            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long              num1        = msmqMessage.LookupId.Value;
                int               size        = msmqMessage.BodyLength.Value;
                int               offset      = 0;
                byte[]            incoming    = msmqMessage.Body.Buffer;
                ServerModeDecoder modeDecoder = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException exception)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
                }
                if (modeDecoder.Mode != FramingMode.SingletonSized)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
                }
                ServerSingletonSizedDecoder decoder2 = new ServerSingletonSizedDecoder(0L, 0x800, 0x100);
                try
                {
                    do
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                        }
                        int num3 = decoder2.Decode(incoming, offset, size);
                        offset += num3;
                        size   -= num3;
                    }while (decoder2.CurrentState != ServerSingletonSizedDecoder.State.Start);
                }
                catch (ProtocolException exception2)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
                }
                if (size > listener.MaxReceivedMessageSize)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                }
                if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder2.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
                }
                byte[] dst = listener.BufferManager.TakeBuffer(size);
                Buffer.BlockCopy(incoming, offset, dst, 0, size);
                Message message = null;
                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    try
                    {
                        message = listener.MessageEncoderFactory.Encoder.ReadMessage(new ArraySegment <byte>(dst, 0, size), listener.BufferManager);
                    }
                    catch (XmlException exception3)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadXml"), exception3));
                    }
                    bool flag = true;
                    try
                    {
                        SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
                        if (property != null)
                        {
                            message.Properties.Security = property;
                        }
                        flag = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                        message2 = message;
                    }
                    catch (Exception exception4)
                    {
                        if (Fx.IsFatal(exception4))
                        {
                            throw;
                        }
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
                    }
                    finally
                    {
                        if (flag)
                        {
                            message.Close();
                        }
                    }
                }
            }
            return(message2);
        }
Esempio n. 28
0
 internal void ReturnMessage(MsmqInputMessage message)
 {
     this.pool.ReturnMessage(message);
 }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     MsmqReceiveHelper.TryNonTransactedReceiveAsyncResult result2 = AsyncResult.End<MsmqReceiveHelper.TryNonTransactedReceiveAsyncResult>(result);
     msmqMessage = result2.msmqMessage;
     property = null;
     if (MsmqQueue.ReceiveResult.Timeout == result2.receiveResult)
     {
         return false;
     }
     if (MsmqQueue.ReceiveResult.OperationCancelled != result2.receiveResult)
     {
         property = new MsmqMessageProperty(msmqMessage);
     }
     return true;
 }
        internal static IInputSessionChannel DecodeTransportSessiongram(
            MsmqInputSessionChannelListener listener,
            MsmqInputMessage msmqMessage,
            MsmqMessageProperty messageProperty,
            MsmqReceiveContextLockManager receiveContextManager)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long lookupId = msmqMessage.LookupId.Value;

                int size = msmqMessage.BodyLength.Value;
                int offset = 0;
                byte[] incoming = msmqMessage.Body.Buffer;
                MsmqReceiveHelper receiver = listener.MsmqReceiveHelper;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.Simplex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                MsmqInputSessionChannel channel = null;
                ServerSessionDecoder sessionDecoder = new ServerSessionDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);

                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        int decoded = sessionDecoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size -= decoded;
                        if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                            break;
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();

                if (!encoder.IsContentTypeSupported(sessionDecoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                ReceiveContext receiveContext = null;

                // tack on the receive context property depending on the receive mode
                if (receiver.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
                {
                    receiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                }

                channel = new MsmqInputSessionChannel(listener, Transaction.Current, receiveContext);

                Message message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);

                SecurityMessageProperty securityProperty = null;
                try
                {
                    securityProperty = listener.ValidateSecurity(msmqMessage);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                        throw;
                    channel.FaultChannel();
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (null != securityProperty)
                    message.Properties.Security = securityProperty;

                message.Properties[MsmqMessageProperty.Name] = messageProperty;
                channel.EnqueueAndDispatch(message);
                listener.RaiseMessageReceived();

                for (;;)
                {
                    int decoded;
                    try
                    {
                        if (size <= 0)
                        {
                            channel.FaultChannel();
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        decoded = sessionDecoder.Decode(incoming, offset, size);
                    }
                    catch (ProtocolException ex)
                    {
                        channel.FaultChannel();
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    offset += decoded;
                    size -= decoded;
                    if (ServerSessionDecoder.State.End == sessionDecoder.CurrentState)
                        break;
                    if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                    {
                        message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);
                        if (null != securityProperty)
                        {
                            message.Properties.Security = (SecurityMessageProperty)securityProperty.CreateCopy();
                        }
                        message.Properties[MsmqMessageProperty.Name] = messageProperty;
                        channel.EnqueueAndDispatch(message);
                        listener.RaiseMessageReceived();
                    }
                }

                channel.Shutdown();
                MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);

                return channel;
            }
        }
Esempio n. 31
0
        protected override Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            MsmqInputChannelListener manager = base.Manager as MsmqInputChannelListener;

            return(MsmqDecodeHelper.DecodeTransportDatagram(manager, base.MsmqReceiveHelper, msmqMessage, messageProperty));
        }
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     TryNonTransactedReceiveAsyncResult asyncResult = AsyncResult.End<TryNonTransactedReceiveAsyncResult>(result);
     msmqMessage = asyncResult.msmqMessage;
     property = null;
     if (MsmqQueue.ReceiveResult.Timeout == asyncResult.receiveResult)
         return false;
     else if (MsmqQueue.ReceiveResult.OperationCancelled == asyncResult.receiveResult)
         return true;
     else
     {
         property = new MsmqMessageProperty(msmqMessage);
         return true;
     }
 }
 internal void ReturnMessage(MsmqInputMessage message)
 {
     this.pool.ReturnMessage(message);
 }
 internal TryNonTransactedReceiveAsyncResult(MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.receiver = receiver;
     this.msmqMessage = msmqMessage;
     receiver.Queue.BeginTryReceive(msmqMessage, timeout, onCompleteStatic, this);
 }
 protected override Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
 {
     MsmqInputChannelListener manager = base.Manager as MsmqInputChannelListener;
     return MsmqDecodeHelper.DecodeTransportDatagram(manager, base.MsmqReceiveHelper, msmqMessage, messageProperty);
 }
        unsafe int PeekLockCore(MsmqQueueHandle handle, MsmqInputMessage message, TimeSpan timeout)
        {
            int          retCode = 0;
            ITransaction internalTrans;

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            IntPtr nativePropertiesPointer = message.Pin();

            try
            {
                bool receivedMessage = false;

                while (!receivedMessage)
                {
                    retCode = UnsafeNativeMethods.MQBeginTransaction(out internalTrans);
                    if (retCode != 0)
                    {
                        return(retCode);
                    }

                    int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeoutHelper.RemainingTime());

                    // no timeout interval if timeout has been set to 0 otherwise a minimum of 100
                    int timeoutIntervalInMilliseconds = (timeoutInMilliseconds == 0) ? 0 : 100;

                    // receive until timeout but let go of receive lock for other contenders periodically
                    while (true)
                    {
                        lock (this.receiveLock)
                        {
                            retCode = UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutIntervalInMilliseconds,
                                                                           UnsafeNativeMethods.MQ_ACTION_RECEIVE, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, internalTrans);
                            if (retCode == UnsafeNativeMethods.MQ_ERROR_IO_TIMEOUT)
                            {
                                // keep trying until we timeout
                                timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeoutHelper.RemainingTime());
                                if (timeoutInMilliseconds == 0)
                                {
                                    return(retCode);
                                }
                            }
                            else if (retCode != 0)
                            {
                                BOID boid = new BOID();
                                internalTrans.Abort(
                                    ref boid, // pboidReason
                                    0,        // fRetaining
                                    0         // fAsync
                                    );

                                return(retCode);
                                // we don't need to release the ITransaction as MSMQ does not increment the ref counter
                                // in MQBeginTransaction
                            }
                            else
                            {
                                // we got a message within the specified time out
                                break;
                            }
                        }
                    }

                    TransactionLookupEntry entry;

                    lock (this.internalStateLock)
                    {
                        if (!this.lockMap.TryGetValue(message.LookupId.Value, out entry))
                        {
                            this.lockMap.Add(message.LookupId.Value, new TransactionLookupEntry(message.LookupId.Value, internalTrans));
                            receivedMessage = true;
                        }
                        else
                        {
                            // this was a message that was in the process of being handed off
                            // from some app trans to some internal MSMQ transaction
                            // and we grabbed it before the Abort() could finish
                            // need to be a good citizen and finish that Abort() job for it
                            entry.MsmqInternalTransaction = internalTrans;
                        }
                    }
                }
            }
            finally
            {
                message.Unpin();
            }

            return(retCode);
        }
        unsafe int PeekLockCore(MsmqQueueHandle handle, MsmqInputMessage message, TimeSpan timeout)
        {
            int retCode = 0;
            ITransaction internalTrans;

            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            IntPtr nativePropertiesPointer = message.Pin();
            try
            {
                bool receivedMessage = false;

                while (!receivedMessage)
                {
                    retCode = UnsafeNativeMethods.MQBeginTransaction(out internalTrans);
                    if (retCode != 0)
                    {
                        return retCode;
                    }

                    int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeoutHelper.RemainingTime());

                    // no timeout interval if timeout has been set to 0 otherwise a minimum of 100
                    int timeoutIntervalInMilliseconds = (timeoutInMilliseconds == 0) ? 0 : 100;

                    // receive until timeout but let go of receive lock for other contenders periodically
                    while (true)
                    {
                        lock (this.receiveLock)
                        {
                            retCode = UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutIntervalInMilliseconds,
                                        UnsafeNativeMethods.MQ_ACTION_RECEIVE, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, internalTrans);
                            if (retCode == UnsafeNativeMethods.MQ_ERROR_IO_TIMEOUT)
                            {
                                // keep trying until we timeout
                                timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeoutHelper.RemainingTime());
                                if (timeoutInMilliseconds == 0)
                                {
                                    return retCode;
                                }
                            }
                            else if (retCode != 0)
                            {
                                BOID boid = new BOID();
                                internalTrans.Abort(
                                    ref boid, // pboidReason
                                    0,  // fRetaining
                                    0   // fAsync
                                    );

                                return retCode;
                                // we don't need to release the ITransaction as MSMQ does not increment the ref counter
                                // in MQBeginTransaction
                            }
                            else
                            {
                                // we got a message within the specified time out
                                break;
                            }
                        }
                    }

                    TransactionLookupEntry entry;

                    lock (this.internalStateLock)
                    {
                        if (!this.lockMap.TryGetValue(message.LookupId.Value, out entry))
                        {
                            this.lockMap.Add(message.LookupId.Value, new TransactionLookupEntry(message.LookupId.Value, internalTrans));
                            receivedMessage = true;
                        }
                        else
                        {
                            // this was a message that was in the process of being handed off
                            // from some app trans to some internal MSMQ transaction
                            // and we grabbed it before the Abort() could finish
                            // need to be a good citizen and finish that Abort() job for it
                            entry.MsmqInternalTransaction = internalTrans;
                        }
                    }
                }
            }
            finally
            {
                message.Unpin();
            }

            return retCode;
        }
        internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long lookupId = msmqMessage.LookupId.Value;
                int size = msmqMessage.BodyLength.Value;
                int offset = 0;
                byte[] incoming = msmqMessage.Body.Buffer;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();

                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.SingletonSized)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                ServerSingletonSizedDecoder decoder = new ServerSingletonSizedDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);
                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder.CreatePrematureEOFException());
                        }

                        int decoded = decoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size -= decoded;
                        if (decoder.CurrentState == ServerSingletonSizedDecoder.State.Start)
                            break;
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (size > listener.MaxReceivedMessageSize)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                }

                if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size);
                Buffer.BlockCopy(incoming, offset, envelopeBuffer, 0, size);

                Message message = null;

                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    try
                    {
                        message = listener.MessageEncoderFactory.Encoder.ReadMessage(
                            new ArraySegment<byte>(envelopeBuffer, 0, size), listener.BufferManager);
                    }
                    catch (XmlException e)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e));
                    }

                    bool closeMessage = true;
                    try
                    {
                        SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                        if (null != securityProperty)
                            message.Properties.Security = securityProperty;

                        closeMessage = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                        return message;
                    }
                    catch (Exception ex)
                    {
                        if (Fx.IsFatal(ex))
                            throw;
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    finally
                    {
                        if (closeMessage)
                        {
                            message.Close();
                        }
                    }
                }
            }
        }
 abstract protected Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty property);
 private int PeekLockCore(MsmqQueueHandle handle, MsmqInputMessage message, TimeSpan timeout)
 {
     int num = 0;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     IntPtr properties = message.Pin();
     try
     {
         bool flag = false;
         while (!flag)
         {
             ITransaction transaction;
             bool flag2;
             object obj2;
             num = UnsafeNativeMethods.MQBeginTransaction(out transaction);
             if (num != 0)
             {
                 return num;
             }
             int num3 = (TimeoutHelper.ToMilliseconds(helper.RemainingTime()) == 0) ? 0 : 100;
         Label_0045:
             flag2 = false;
             try
             {
                 Monitor.Enter(obj2 = this.receiveLock, ref flag2);
                 num = UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), num3, 0, properties, null, IntPtr.Zero, IntPtr.Zero, transaction);
                 if (num == -1072824293)
                 {
                     if (TimeoutHelper.ToMilliseconds(helper.RemainingTime()) == 0)
                     {
                         return num;
                     }
                     goto Label_0045;
                 }
                 if (num != 0)
                 {
                     BOID pboidReason = new BOID();
                     transaction.Abort(ref pboidReason, 0, 0);
                     return num;
                 }
             }
             finally
             {
                 if (flag2)
                 {
                     Monitor.Exit(obj2);
                 }
             }
             lock (this.internalStateLock)
             {
                 TransactionLookupEntry entry;
                 if (!this.lockMap.TryGetValue(message.LookupId.Value, out entry))
                 {
                     this.lockMap.Add(message.LookupId.Value, new TransactionLookupEntry(message.LookupId.Value, transaction));
                     flag = true;
                 }
                 else
                 {
                     entry.MsmqInternalTransaction = transaction;
                 }
                 continue;
             }
         }
     }
     finally
     {
         message.Unpin();
     }
     return num;
 }
        public bool EndTryReceive(IAsyncResult result, out Message message)
        {
            message = null;

            if (null == result)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
            }

            DoneReceivingAsyncResult doneRecevingResult = result as DoneReceivingAsyncResult;

            if (doneRecevingResult != null)
            {
                return(DoneReceivingAsyncResult.End(doneRecevingResult));
            }

            MsmqInputMessage    msmqMessage = null;
            MsmqMessageProperty property    = null;

            try
            {
                bool retval = this.receiver.EndTryReceive(result, out msmqMessage, out property);
                if (retval)
                {
                    if (null != property)
                    {
                        message = DecodeMsmqMessage(msmqMessage, property);
                        message.Properties[MsmqMessageProperty.Name] = property;

                        if (this.receiveParameters.ReceiveContextSettings.Enabled)
                        {
                            message.Properties[MsmqReceiveContext.Name] = this.receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                        }

                        MsmqDiagnostics.DatagramReceived(msmqMessage.MessageId, message);
                        this.listener.RaiseMessageReceived();
                    }
                    else if (CommunicationState.Opened == this.State)
                    {
                        this.listener.FaultListener();
                        this.Fault();
                    }
                }
                return(retval);
            }
            catch (MsmqException ex)
            {
                if (ex.FaultReceiver)
                {
                    this.listener.FaultListener();
                    this.Fault();
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
            }
            finally
            {
                if (null != msmqMessage)
                {
                    this.receiver.ReturnMessage(msmqMessage);
                }
            }
        }
Esempio n. 42
0
        private int PeekLockCore(MsmqQueueHandle handle, MsmqInputMessage message, TimeSpan timeout)
        {
            int           num        = 0;
            TimeoutHelper helper     = new TimeoutHelper(timeout);
            IntPtr        properties = message.Pin();

            try
            {
                bool flag = false;
                while (!flag)
                {
                    ITransaction transaction;
                    bool         flag2;
                    object       obj2;
                    num = UnsafeNativeMethods.MQBeginTransaction(out transaction);
                    if (num != 0)
                    {
                        return(num);
                    }
                    int num3 = (TimeoutHelper.ToMilliseconds(helper.RemainingTime()) == 0) ? 0 : 100;
Label_0045:
                    flag2 = false;
                    try
                    {
                        Monitor.Enter(obj2 = this.receiveLock, ref flag2);
                        num = UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), num3, 0, properties, null, IntPtr.Zero, IntPtr.Zero, transaction);
                        if (num == -1072824293)
                        {
                            if (TimeoutHelper.ToMilliseconds(helper.RemainingTime()) == 0)
                            {
                                return(num);
                            }
                            goto Label_0045;
                        }
                        if (num != 0)
                        {
                            BOID pboidReason = new BOID();
                            transaction.Abort(ref pboidReason, 0, 0);
                            return(num);
                        }
                    }
                    finally
                    {
                        if (flag2)
                        {
                            Monitor.Exit(obj2);
                        }
                    }
                    lock (this.internalStateLock)
                    {
                        TransactionLookupEntry entry;
                        if (!this.lockMap.TryGetValue(message.LookupId.Value, out entry))
                        {
                            this.lockMap.Add(message.LookupId.Value, new TransactionLookupEntry(message.LookupId.Value, transaction));
                            flag = true;
                        }
                        else
                        {
                            entry.MsmqInternalTransaction = transaction;
                        }
                        continue;
                    }
                }
            }
            finally
            {
                message.Unpin();
            }
            return(num);
        }
 internal bool EndTryReceive(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty msmqProperty)
 {
     msmqMessage = null;
     msmqProperty = null;
     if (result == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("result");
     }
     if (this.receiveParameters.ExactlyOnce)
     {
         TryTransactedReceiveAsyncResult result2 = result as TryTransactedReceiveAsyncResult;
         if (result2 == null)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("InvalidAsyncResult"));
         }
         return TryTransactedReceiveAsyncResult.End(result2, out msmqMessage, out msmqProperty);
     }
     TryNonTransactedReceiveAsyncResult result3 = result as TryNonTransactedReceiveAsyncResult;
     if (result3 == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(System.ServiceModel.SR.GetString("InvalidAsyncResult"));
     }
     return TryNonTransactedReceiveAsyncResult.End(result3, out msmqMessage, out msmqProperty);
 }
Esempio n. 44
0
        internal static IInputSessionChannel DecodeTransportSessiongram(
            MsmqInputSessionChannelListener listener,
            MsmqInputMessage msmqMessage,
            MsmqMessageProperty messageProperty,
            MsmqReceiveContextLockManager receiveContextManager)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long lookupId = msmqMessage.LookupId.Value;

                int               size     = msmqMessage.BodyLength.Value;
                int               offset   = 0;
                byte[]            incoming = msmqMessage.Body.Buffer;
                MsmqReceiveHelper receiver = listener.MsmqReceiveHelper;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.Simplex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                MsmqInputSessionChannel channel        = null;
                ServerSessionDecoder    sessionDecoder = new ServerSessionDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);

                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        int decoded = sessionDecoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size   -= decoded;
                        if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                        {
                            break;
                        }
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();

                if (!encoder.IsContentTypeSupported(sessionDecoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                ReceiveContext receiveContext = null;

                // tack on the receive context property depending on the receive mode
                if (receiver.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
                {
                    receiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                }

                channel = new MsmqInputSessionChannel(listener, Transaction.Current, receiveContext);

                Message message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);

                SecurityMessageProperty securityProperty = null;
                try
                {
                    securityProperty = listener.ValidateSecurity(msmqMessage);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                    {
                        throw;
                    }
                    channel.FaultChannel();
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (null != securityProperty)
                {
                    message.Properties.Security = securityProperty;
                }

                message.Properties[MsmqMessageProperty.Name] = messageProperty;
                channel.EnqueueAndDispatch(message);
                listener.RaiseMessageReceived();

                for (;;)
                {
                    int decoded;
                    try
                    {
                        if (size <= 0)
                        {
                            channel.FaultChannel();
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException());
                        }

                        decoded = sessionDecoder.Decode(incoming, offset, size);
                    }
                    catch (ProtocolException ex)
                    {
                        channel.FaultChannel();
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    offset += decoded;
                    size   -= decoded;
                    if (ServerSessionDecoder.State.End == sessionDecoder.CurrentState)
                    {
                        break;
                    }
                    if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState)
                    {
                        message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize);
                        if (null != securityProperty)
                        {
                            message.Properties.Security = (SecurityMessageProperty)securityProperty.CreateCopy();
                        }
                        message.Properties[MsmqMessageProperty.Name] = messageProperty;
                        channel.EnqueueAndDispatch(message);
                        listener.RaiseMessageReceived();
                    }
                }

                channel.Shutdown();
                MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);

                return(channel);
            }
        }
        internal SecurityMessageProperty ValidateSecurity(MsmqInputMessage msmqMessage)
        {
            SecurityMessageProperty property        = null;
            X509Certificate2        certificate     = null;
            WindowsSidIdentity      primaryIdentity = null;

            try
            {
                if (MsmqAuthenticationMode.Certificate == base.ReceiveParameters.TransportSecurity.MsmqAuthenticationMode)
                {
                    try
                    {
                        certificate = new X509Certificate2(msmqMessage.SenderCertificate.GetBufferCopy(msmqMessage.SenderCertificateLength.Value));
                        X509SecurityToken token = new X509SecurityToken(certificate, false);
                        ReadOnlyCollection <IAuthorizationPolicy> tokenPolicies = this.x509SecurityTokenAuthenticator.ValidateToken(token);
                        property = new SecurityMessageProperty {
                            TransportToken         = new SecurityTokenSpecification(token, tokenPolicies),
                            ServiceSecurityContext = new ServiceSecurityContext(tokenPolicies)
                        };
                        goto Label_01C4;
                    }
                    catch (SecurityTokenValidationException exception)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadCertificate"), exception));
                    }
                    catch (CryptographicException exception2)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadCertificate"), exception2));
                    }
                }
                if (MsmqAuthenticationMode.WindowsDomain == base.ReceiveParameters.TransportSecurity.MsmqAuthenticationMode)
                {
                    byte[] bufferCopy = msmqMessage.SenderId.GetBufferCopy(msmqMessage.SenderIdLength.Value);
                    if (bufferCopy.Length == 0)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MsmqNoSid")));
                    }
                    SecurityIdentifier resource = new SecurityIdentifier(bufferCopy, 0);
                    List <Claim>       claims   = new List <Claim>(2)
                    {
                        new Claim(ClaimTypes.Sid, resource, Rights.Identity),
                        Claim.CreateWindowsSidClaim(resource)
                    };
                    ClaimSet issuance = new DefaultClaimSet(ClaimSet.System, claims);
                    List <IAuthorizationPolicy> list2 = new List <IAuthorizationPolicy>(1);
                    primaryIdentity = new WindowsSidIdentity(resource);
                    list2.Add(new UnconditionalPolicy(primaryIdentity, issuance));
                    ReadOnlyCollection <IAuthorizationPolicy> onlys2 = list2.AsReadOnly();
                    property = new SecurityMessageProperty {
                        TransportToken         = new SecurityTokenSpecification(null, onlys2),
                        ServiceSecurityContext = new ServiceSecurityContext(onlys2)
                    };
                }
            }
            catch (Exception exception3)
            {
                if (Fx.IsFatal(exception3))
                {
                    throw;
                }
                if (AuditLevel.Failure == (base.AuditBehavior.MessageAuthenticationAuditLevel & AuditLevel.Failure))
                {
                    this.WriteAuditEvent(AuditLevel.Failure, certificate, primaryIdentity, null);
                }
                throw;
            }
Label_01C4:
            if ((property != null) && (AuditLevel.Success == (base.AuditBehavior.MessageAuthenticationAuditLevel & AuditLevel.Success)))
            {
                this.WriteAuditEvent(AuditLevel.Success, certificate, primaryIdentity, null);
            }
            return(property);
        }
        internal bool TryReceive(MsmqInputMessage msmqMessage, TimeSpan timeout, MsmqTransactionMode transactionMode, out MsmqMessageProperty property)
        {
            property = null;

            MsmqQueue.ReceiveResult receiveResult = this.Queue.TryReceive(msmqMessage, timeout, transactionMode);
            if (MsmqQueue.ReceiveResult.OperationCancelled == receiveResult)
                return true;
            if (MsmqQueue.ReceiveResult.Timeout == receiveResult)
                return false;
            else
            {
                property = new MsmqMessageProperty(msmqMessage);
                if (this.Transactional)
                {
                    if (this.PoisonHandler.CheckAndHandlePoisonMessage(property))
                    {
                        long lookupId = property.LookupId;
                        property = null;
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new MsmqPoisonMessageException(lookupId));
                    }
                }
                return true;
            }
        }
 protected override Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty property)
 {
     MsmqIntegrationChannelListener listener = this.Manager as MsmqIntegrationChannelListener;
     return MsmqDecodeHelper.DecodeIntegrationDatagram(listener, this.MsmqReceiveHelper, msmqMessage as MsmqIntegrationInputMessage, property);
 }
        internal static IInputSessionChannel DecodeTransportSessiongram(MsmqInputSessionChannelListener listener, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty, MsmqReceiveContextLockManager receiveContextManager)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                int               num4;
                long              num1              = msmqMessage.LookupId.Value;
                int               size              = msmqMessage.BodyLength.Value;
                int               offset            = 0;
                byte[]            incoming          = msmqMessage.Body.Buffer;
                MsmqReceiveHelper msmqReceiveHelper = listener.MsmqReceiveHelper;
                ServerModeDecoder modeDecoder       = new ServerModeDecoder();
                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException exception)
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception);
                }
                if (modeDecoder.Mode != FramingMode.Simplex)
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadFrame")));
                }
                MsmqInputSessionChannel channel  = null;
                ServerSessionDecoder    decoder2 = new ServerSessionDecoder(0L, 0x800, 0x100);
                try
                {
                    do
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                        }
                        int num3 = decoder2.Decode(incoming, offset, size);
                        offset += num3;
                        size   -= num3;
                    }while (ServerSessionDecoder.State.EnvelopeStart != decoder2.CurrentState);
                }
                catch (ProtocolException exception2)
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception2);
                }
                MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder();
                if (!encoder.IsContentTypeSupported(decoder2.ContentType))
                {
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqBadContentType")));
                }
                ReceiveContext sessiongramReceiveContext = null;
                if (msmqReceiveHelper.MsmqReceiveParameters.ReceiveContextSettings.Enabled)
                {
                    sessiongramReceiveContext = receiveContextManager.CreateMsmqReceiveContext(msmqMessage.LookupId.Value);
                }
                channel = new MsmqInputSessionChannel(listener, Transaction.Current, sessiongramReceiveContext);
                Message item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
                SecurityMessageProperty property = null;
                try
                {
                    property = listener.ValidateSecurity(msmqMessage);
                }
                catch (Exception exception3)
                {
                    if (Fx.IsFatal(exception3))
                    {
                        throw;
                    }
                    channel.FaultChannel();
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception3);
                }
                if (property != null)
                {
                    item.Properties.Security = property;
                }
                item.Properties["MsmqMessageProperty"] = messageProperty;
                channel.EnqueueAndDispatch(item);
                listener.RaiseMessageReceived();
Label_01F6:
                try
                {
                    if (size <= 0)
                    {
                        channel.FaultChannel();
                        msmqReceiveHelper.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, decoder2.CreatePrematureEOFException());
                    }
                    num4 = decoder2.Decode(incoming, offset, size);
                }
                catch (ProtocolException exception4)
                {
                    channel.FaultChannel();
                    msmqReceiveHelper.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, exception4);
                }
                offset += num4;
                size   -= num4;
                if (ServerSessionDecoder.State.End != decoder2.CurrentState)
                {
                    if (ServerSessionDecoder.State.EnvelopeStart == decoder2.CurrentState)
                    {
                        item = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, decoder2.EnvelopeSize);
                        if (property != null)
                        {
                            item.Properties.Security = (SecurityMessageProperty)property.CreateCopy();
                        }
                        item.Properties["MsmqMessageProperty"] = messageProperty;
                        channel.EnqueueAndDispatch(item);
                        listener.RaiseMessageReceived();
                    }
                    goto Label_01F6;
                }
                channel.Shutdown();
                MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems);
                return(channel);
            }
        }
 internal TryTransactedReceiveAsyncResult(MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage,
     TimeSpan timeout, MsmqTransactionMode transactionMode, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.timeoutHelper = new TimeoutHelper(timeout);
     this.txCurrent = Transaction.Current;
     this.receiver = receiver;
     this.msmqMessage = msmqMessage;
     this.transactionMode = transactionMode;
     ActionItem.Schedule(onComplete, this);
 }
Esempio n. 50
0
        internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                long   lookupId = msmqMessage.LookupId.Value;
                int    size     = msmqMessage.BodyLength.Value;
                int    offset   = 0;
                byte[] incoming = msmqMessage.Body.Buffer;

                ServerModeDecoder modeDecoder = new ServerModeDecoder();

                try
                {
                    ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size);
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (modeDecoder.Mode != FramingMode.SingletonSized)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame)));
                }

                ServerSingletonSizedDecoder decoder = new ServerSingletonSizedDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize);
                try
                {
                    for (;;)
                    {
                        if (size <= 0)
                        {
                            throw listener.NormalizePoisonException(messageProperty.LookupId, decoder.CreatePrematureEOFException());
                        }

                        int decoded = decoder.Decode(incoming, offset, size);
                        offset += decoded;
                        size   -= decoded;
                        if (decoder.CurrentState == ServerSingletonSizedDecoder.State.Start)
                        {
                            break;
                        }
                    }
                }
                catch (ProtocolException ex)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                }

                if (size > listener.MaxReceivedMessageSize)
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                }

                if (!listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder.ContentType))
                {
                    receiver.FinalDisposition(messageProperty);
                    throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType)));
                }

                byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size);
                Buffer.BlockCopy(incoming, offset, envelopeBuffer, 0, size);

                Message message = null;

                using (MsmqDiagnostics.BoundDecodeOperation())
                {
                    try
                    {
                        message = listener.MessageEncoderFactory.Encoder.ReadMessage(
                            new ArraySegment <byte>(envelopeBuffer, 0, size), listener.BufferManager);
                    }
                    catch (XmlException e)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e));
                    }

                    bool closeMessage = true;
                    try
                    {
                        SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                        if (null != securityProperty)
                        {
                            message.Properties.Security = securityProperty;
                        }

                        closeMessage = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                        return(message);
                    }
                    catch (Exception ex)
                    {
                        if (Fx.IsFatal(ex))
                        {
                            throw;
                        }
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, ex);
                    }
                    finally
                    {
                        if (closeMessage)
                        {
                            message.Close();
                        }
                    }
                }
            }
        }
 abstract protected Message DecodeMsmqMessage(MsmqInputMessage msmqMessage, MsmqMessageProperty property);
 internal static bool End(IAsyncResult result, out MsmqInputMessage msmqMessage, out MsmqMessageProperty property)
 {
     TryTransactedReceiveAsyncResult receiveResult = AsyncResult.End<TryTransactedReceiveAsyncResult>(result);
     msmqMessage = receiveResult.msmqMessage;
     property = receiveResult.messageProperty;
     return !receiveResult.expired;
 }