Exemple #1
0
        protected unsafe int ReceiveByLookupIdCoreDtcTransacted(MsmqQueueHandle handle, long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            IDtcTransaction dtcTransaction = GetNativeTransaction(transactionMode);

            IntPtr nativePropertiesPointer = message.Pin();

            try
            {
                if (dtcTransaction != null)
                {
                    try
                    {
                        return(UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, nativePropertiesPointer, null, IntPtr.Zero, dtcTransaction));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(dtcTransaction);
                    }
                }
                else
                {
                    return(UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, nativePropertiesPointer, null, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode)));
                }
            }
            finally
            {
                message.Unpin();
            }
        }
        private int ReceiveCoreDtcTransacted(MsmqQueueHandle handle, NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
        {
            int             num2;
            IDtcTransaction nativeTransaction = this.GetNativeTransaction(transactionMode);
            int             num        = TimeoutHelper.ToMilliseconds(timeout);
            IntPtr          properties = message.Pin();

            try
            {
                if (nativeTransaction != null)
                {
                    try
                    {
                        return(UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), num, action, properties, null, IntPtr.Zero, IntPtr.Zero, nativeTransaction));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(nativeTransaction);
                    }
                }
                num2 = UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), num, action, properties, null, IntPtr.Zero, IntPtr.Zero, (IntPtr)this.GetTransactionConstant(transactionMode));
            }
            finally
            {
                message.Unpin();
            }
            return(num2);
        }
        protected int ReceiveByLookupIdCoreDtcTransacted(MsmqQueueHandle handle, long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            int             num;
            IDtcTransaction nativeTransaction = this.GetNativeTransaction(transactionMode);
            IntPtr          properties        = message.Pin();

            try
            {
                if (nativeTransaction != null)
                {
                    try
                    {
                        return(UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, properties, null, IntPtr.Zero, nativeTransaction));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(nativeTransaction);
                    }
                }
                num = UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, properties, null, IntPtr.Zero, (IntPtr)this.GetTransactionConstant(transactionMode));
            }
            finally
            {
                message.Unpin();
            }
            return(num);
        }
Exemple #4
0
        unsafe int ReceiveCoreDtcTransacted(MsmqQueueHandle handle, NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
        {
            IDtcTransaction dtcTransaction        = GetNativeTransaction(transactionMode);
            int             timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout);

            IntPtr nativePropertiesPointer = message.Pin();

            try
            {
                if (dtcTransaction != null)
                {
                    try
                    {
                        return(UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutInMilliseconds,
                                                                    action, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, dtcTransaction));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(dtcTransaction);
                    }
                }
                else
                {
                    return(UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutInMilliseconds,
                                                                action, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode)));
                }
            }
            finally
            {
                message.Unpin();
            }
        }
        public void Send(NativeMsmqMessage message, MsmqTransactionMode transactionMode)
        {
            int error = 0;

            if (this.RequiresDtcTransaction(transactionMode))
            {
                error = this.SendDtcTransacted(message, transactionMode);
            }
            else
            {
                MsmqQueueHandle handle     = this.GetHandle();
                IntPtr          properties = message.Pin();
                try
                {
                    error = UnsafeNativeMethods.MQSendMessage(handle, properties, (IntPtr)this.GetTransactionConstant(transactionMode));
                }
                finally
                {
                    message.Unpin();
                }
            }
            if (error != 0)
            {
                if (IsErrorDueToStaleHandle(error))
                {
                    this.HandleIsStale(this.handle);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqSendError", new object[] { MsmqError.GetErrorString(error) }), error));
            }
        }
Exemple #6
0
        int SendDtcTransacted(NativeMsmqMessage message, MsmqTransactionMode transactionMode)
        {
            IDtcTransaction dtcTransaction = GetNativeTransaction(transactionMode);

            MsmqQueueHandle handle = GetHandle();
            IntPtr          nativePropertiesPointer = message.Pin();

            try
            {
                if (dtcTransaction != null)
                {
                    try
                    {
                        return(UnsafeNativeMethods.MQSendMessage(handle, nativePropertiesPointer,
                                                                 dtcTransaction));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(dtcTransaction);
                    }
                }
                else
                {
                    return(UnsafeNativeMethods.MQSendMessage(handle, nativePropertiesPointer,
                                                             (IntPtr)GetTransactionConstant(transactionMode)));
                }
            }
            finally
            {
                message.Unpin();
            }
        }
        private int SendDtcTransacted(NativeMsmqMessage message, MsmqTransactionMode transactionMode)
        {
            int             num;
            IDtcTransaction nativeTransaction = this.GetNativeTransaction(transactionMode);
            MsmqQueueHandle handle            = this.GetHandle();
            IntPtr          properties        = message.Pin();

            try
            {
                if (nativeTransaction != null)
                {
                    try
                    {
                        return(UnsafeNativeMethods.MQSendMessage(handle, properties, nativeTransaction));
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(nativeTransaction);
                    }
                }
                num = UnsafeNativeMethods.MQSendMessage(handle, properties, (IntPtr)this.GetTransactionConstant(transactionMode));
            }
            finally
            {
                message.Unpin();
            }
            return(num);
        }
        private ReceiveResult TryReceiveInternal(NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
        {
            TimeoutHelper   helper = new TimeoutHelper(timeout);
            MsmqQueueHandle handle = this.GetHandle();

            while (true)
            {
                int error = this.ReceiveCore(handle, message, helper.RemainingTime(), transactionMode, action);
                if (error == 0)
                {
                    return(ReceiveResult.MessageReceived);
                }
                if (!IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    if (error == -1072824293)
                    {
                        return(ReceiveResult.Timeout);
                    }
                    if (error == -1072824312)
                    {
                        return(ReceiveResult.OperationCancelled);
                    }
                    if (error == -1072824313)
                    {
                        return(ReceiveResult.OperationCancelled);
                    }
                    if (IsErrorDueToStaleHandle(error))
                    {
                        this.HandleIsStale(handle);
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqReceiveError", new object[] { MsmqError.GetErrorString(error) }), error));
                }
                message.GrowBuffers();
            }
        }
Exemple #9
0
        public void Send(NativeMsmqMessage message, MsmqTransactionMode transactionMode)
        {
            int error = 0;

            if (RequiresDtcTransaction(transactionMode))
            {
                error = SendDtcTransacted(message, transactionMode);
            }
            else
            {
                MsmqQueueHandle handle = GetHandle();
                IntPtr          nativePropertiesPointer = message.Pin();
                try
                {
                    error = UnsafeNativeMethods.MQSendMessage(handle, nativePropertiesPointer,
                                                              (IntPtr)GetTransactionConstant(transactionMode));
                }
                finally
                {
                    message.Unpin();
                }
            }

            if (error != 0)
            {
                if (IsErrorDueToStaleHandle(error))
                {
                    HandleIsStale(handle);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqSendError, MsmqError.GetErrorString(error)), error));
            }
        }
Exemple #10
0
            unsafe void StartReceive(bool synchronously)
            {
                bool useCompletionPort;

                try
                {
                    this.handle = this.msmqQueue.GetHandleForAsync(out useCompletionPort);
                }
                catch (MsmqException ex)
                {
                    OnCompletion(ex.ErrorCode, synchronously);
                    return;
                }
                if (null != nativeOverlapped)
                {
                    Fx.Assert("---- in StartReceive");
                }

                IntPtr nativePropertiesPointer = this.message.Pin();

                nativeOverlapped = new Overlapped(0, 0, IntPtr.Zero, this).UnsafePack(onPortedCompletion, this.message.GetBuffersForAsync());

                int error;

                try
                {
                    if (useCompletionPort)
                    {
                        error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(),
                                                           this.action, nativeOverlapped, null);
                    }
                    else
                    {
                        if (onNonPortedCompletion == null)
                        {
                            onNonPortedCompletion = new UnsafeNativeMethods.MQReceiveCallback(OnNonPortedCompletion);
                        }
                        error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(),
                                                           this.action, nativeOverlapped, onNonPortedCompletion);
                    }
                }
                catch (ObjectDisposedException ex)
                {
                    // if Close ----s with the async Receive, it is possible that SafeHandle will throw ObjectDisposedException
                    // the behavior should be same as if operation was just cancelled (the channel will return no message)
                    MsmqDiagnostics.ExpectedException(ex);
                    error = UnsafeNativeMethods.MQ_ERROR_OPERATION_CANCELLED;
                }
                if (error != 0)
                {
                    if (error != UnsafeNativeMethods.MQ_INFORMATION_OPERATION_PENDING)
                    {
                        Overlapped.Free(nativeOverlapped);
                        nativeOverlapped = null;
                        GC.SuppressFinalize(this);
                        OnCompletion(error, synchronously);
                    }
                }
            }
        unsafe int TryRelockMessage(long lookupId)
        {
            int          retCode = 0;
            ITransaction internalTrans;

            using (MsmqEmptyMessage message = new MsmqEmptyMessage())
            {
                IntPtr nativePropertiesPointer = message.Pin();
                try
                {
                    // don't want other threads receiving the message we want to relock
                    lock (this.receiveLock)
                    {
                        MsmqQueueHandle handle = GetHandle();

                        TransactionLookupEntry entry;

                        lock (this.internalStateLock)
                        {
                            if (!this.lockMap.TryGetValue(lookupId, out entry))
                            {
                                // should never get here
                                return(retCode);
                            }

                            if (entry.MsmqInternalTransaction == null)
                            {
                                retCode = UnsafeNativeMethods.MQBeginTransaction(out internalTrans);
                                if (retCode != 0)
                                {
                                    return(retCode);
                                }

                                retCode = UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, UnsafeNativeMethods.MQ_LOOKUP_RECEIVE_CURRENT,
                                                                                         nativePropertiesPointer, null, IntPtr.Zero, internalTrans);
                                if (retCode != 0)
                                {
                                    BOID boid = new BOID();
                                    internalTrans.Abort(
                                        ref boid, // pboidReason
                                        0,        // fRetaining
                                        0         // fAsync
                                        );

                                    return(retCode);
                                }
                                entry.MsmqInternalTransaction = internalTrans;
                            }
                        }
                    }
                }
                finally
                {
                    message.Unpin();
                }
            }

            return(retCode);
        }
Exemple #12
0
        static bool IsCompletionPortSupported(MsmqQueueHandle handle)
        {
            // if it's a kernel handle, then it supports completion ports
            int flags;

#pragma warning suppress 56523
            return(UnsafeNativeMethods.GetHandleInformation(handle, out flags) != 0);
        }
 protected void HandleIsStale(MsmqQueueHandle handle)
 {
     lock (this.ThisLock)
     {
         if (this.handle == handle)
         {
             this.CloseQueue();
         }
     }
 }
Exemple #14
0
        unsafe int ReceiveCoreAsync(MsmqQueueHandle handle, IntPtr nativePropertiesPointer, TimeSpan timeout,
                                    int action, NativeOverlapped *nativeOverlapped,
                                    UnsafeNativeMethods.MQReceiveCallback receiveCallback)
        {
            int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout);

            return(UnsafeNativeMethods.MQReceiveMessage(handle, timeoutInMilliseconds, action,
                                                        nativePropertiesPointer, nativeOverlapped, receiveCallback,
                                                        IntPtr.Zero, (IntPtr)UnsafeNativeMethods.MQ_NO_TRANSACTION));
        }
 protected MsmqQueueHandle GetHandle()
 {
     lock (this.ThisLock)
     {
         if (this.handle == null)
         {
             this.handle = this.OpenQueue();
         }
         return(this.handle);
     }
 }
 public virtual void CloseQueue()
 {
     if (this.handle != null)
     {
         this.CloseQueue(this.handle);
         this.handle = null;
         this.isBoundToCompletionPort = false;
         this.isAsyncEnabled          = false;
         MsmqDiagnostics.QueueClosed(this.formatName);
     }
 }
Exemple #17
0
        private int TryRelockMessage(long lookupId)
        {
            int num = 0;

            using (MsmqEmptyMessage message = new MsmqEmptyMessage())
            {
                IntPtr properties = message.Pin();
                try
                {
                    lock (this.receiveLock)
                    {
                        MsmqQueueHandle handle = base.GetHandle();
                        lock (this.internalStateLock)
                        {
                            TransactionLookupEntry entry;
                            if (this.lockMap.TryGetValue(lookupId, out entry))
                            {
                                ITransaction transaction;
                                if (entry.MsmqInternalTransaction != null)
                                {
                                    return(num);
                                }
                                num = UnsafeNativeMethods.MQBeginTransaction(out transaction);
                                if (num != 0)
                                {
                                    return(num);
                                }
                                num = UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, 0x40000020, properties, null, IntPtr.Zero, transaction);
                                if (num != 0)
                                {
                                    BOID pboidReason = new BOID();
                                    transaction.Abort(ref pboidReason, 0, 0);
                                    return(num);
                                }
                                entry.MsmqInternalTransaction = transaction;
                            }
                            return(num);
                        }
                        return(num);
                    }
                    return(num);
                }
                finally
                {
                    message.Unpin();
                }
            }
            return(num);
        }
            private unsafe void StartReceive(bool synchronously)
            {
                bool flag;
                int  num;

                try
                {
                    this.handle = this.msmqQueue.GetHandleForAsync(out flag);
                }
                catch (MsmqException exception)
                {
                    this.OnCompletion(exception.ErrorCode, synchronously);
                    return;
                }
                NativeOverlapped *nativeOverlapped        = this.nativeOverlapped;
                IntPtr            nativePropertiesPointer = this.message.Pin();

                this.nativeOverlapped = new Overlapped(0, 0, IntPtr.Zero, this).UnsafePack(onPortedCompletion, this.message.GetBuffersForAsync());
                try
                {
                    if (flag)
                    {
                        num = this.msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), this.action, this.nativeOverlapped, null);
                    }
                    else
                    {
                        if (onNonPortedCompletion == null)
                        {
                            onNonPortedCompletion = new UnsafeNativeMethods.MQReceiveCallback(MsmqQueue.TryReceiveAsyncResult.OnNonPortedCompletion);
                        }
                        num = this.msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), this.action, this.nativeOverlapped, onNonPortedCompletion);
                    }
                }
                catch (ObjectDisposedException exception2)
                {
                    MsmqDiagnostics.ExpectedException(exception2);
                    num = -1072824312;
                }
                if ((num != 0) && (num != 0x400e0006))
                {
                    Overlapped.Free(this.nativeOverlapped);
                    this.nativeOverlapped = null;
                    GC.SuppressFinalize(this);
                    this.OnCompletion(num, synchronously);
                }
            }
        private int TryMoveMessageDtcTransacted(long lookupId, MsmqQueueHandle sourceQueueHandle, MsmqQueueHandle destinationQueueHandle, MsmqTransactionMode transactionMode)
        {
            IDtcTransaction nativeTransaction = this.GetNativeTransaction(transactionMode);

            if (nativeTransaction != null)
            {
                try
                {
                    return(UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, destinationQueueHandle, lookupId, nativeTransaction));
                }
                finally
                {
                    Marshal.ReleaseComObject(nativeTransaction);
                }
            }
            return(UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, destinationQueueHandle, lookupId, (IntPtr)this.GetTransactionConstant(transactionMode)));
        }
Exemple #20
0
        public MoveReceiveResult TryMoveMessage(long lookupId, MsmqQueue destinationQueue, MsmqTransactionMode transactionMode)
        {
            MsmqQueueHandle sourceQueueHandle      = GetHandle();
            MsmqQueueHandle destinationQueueHandle = destinationQueue.GetHandle();
            int             error;

            try
            {
                if (RequiresDtcTransaction(transactionMode))
                {
                    error = TryMoveMessageDtcTransacted(lookupId, sourceQueueHandle, destinationQueueHandle, transactionMode);
                }
                else
                {
                    error = UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, destinationQueueHandle,
                                                              lookupId, (IntPtr)GetTransactionConstant(transactionMode));
                }
            }
            catch (ObjectDisposedException ex)
            {
                MsmqDiagnostics.ExpectedException(ex);
                return(MoveReceiveResult.Succeeded);
            }
            if (error != 0)
            {
                if (error == UnsafeNativeMethods.MQ_ERROR_MESSAGE_NOT_FOUND)
                {
                    return(MoveReceiveResult.MessageNotFound);
                }
                else if (error == UnsafeNativeMethods.MQ_ERROR_MESSAGE_LOCKED_UNDER_TRANSACTION)
                {
                    return(MoveReceiveResult.MessageLockedUnderTransaction);
                }

                else if (IsErrorDueToStaleHandle(error))
                {
                    HandleIsStale(sourceQueueHandle);
                    destinationQueue.HandleIsStale(destinationQueueHandle);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqSendError,
                                                                                                         MsmqError.GetErrorString(error)), error));
            }

            return(MoveReceiveResult.Succeeded);
        }
Exemple #21
0
        public MoveReceiveResult TryReceiveByLookupId(long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            MsmqQueueHandle handle = GetHandle();
            int             error  = 0;

            while (true)
            {
                try
                {
                    error = ReceiveByLookupIdCore(handle, lookupId, message, transactionMode, action);
                }
                catch (ObjectDisposedException ex)
                {
                    // ---- with Close
                    MsmqDiagnostics.ExpectedException(ex);
                    return(MoveReceiveResult.Succeeded);
                }

                if (0 == error)
                {
                    return(MoveReceiveResult.Succeeded);
                }

                if (IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    message.GrowBuffers();
                    continue;
                }
                else if (UnsafeNativeMethods.MQ_ERROR_MESSAGE_NOT_FOUND == error)
                {
                    return(MoveReceiveResult.MessageNotFound);
                }
                else if (UnsafeNativeMethods.MQ_ERROR_MESSAGE_LOCKED_UNDER_TRANSACTION == error)
                {
                    return(MoveReceiveResult.MessageLockedUnderTransaction);
                }
                else if (IsErrorDueToStaleHandle(error))
                {
                    HandleIsStale(handle);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqReceiveError, MsmqError.GetErrorString(error)), error));
            }
        }
Exemple #22
0
 unsafe int ReceiveByLookupIdCore(MsmqQueueHandle handle, long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
 {
     if (RequiresDtcTransaction(transactionMode))
     {
         return(ReceiveByLookupIdCoreDtcTransacted(handle, lookupId, message, transactionMode, action));
     }
     else
     {
         IntPtr nativePropertiesPointer = message.Pin();
         try
         {
             return(UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, nativePropertiesPointer, null, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode)));
         }
         finally
         {
             message.Unpin();
         }
     }
 }
        private int ReceiveByLookupIdCore(MsmqQueueHandle handle, long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            int num;

            if (this.RequiresDtcTransaction(transactionMode))
            {
                return(this.ReceiveByLookupIdCoreDtcTransacted(handle, lookupId, message, transactionMode, action));
            }
            IntPtr properties = message.Pin();

            try
            {
                num = UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, properties, null, IntPtr.Zero, (IntPtr)this.GetTransactionConstant(transactionMode));
            }
            finally
            {
                message.Unpin();
            }
            return(num);
        }
        public override ReceiveResult TryReceive(NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode)
        {
            // ignore the transactionMode
            TimeoutHelper   timeoutHelper = new TimeoutHelper(timeout);
            MsmqQueueHandle handle        = GetHandle();

            while (true)
            {
                int error = PeekLockCore(handle, (MsmqInputMessage)message, timeoutHelper.RemainingTime());

                if (error == 0)
                {
                    return(ReceiveResult.MessageReceived);
                }

                if (IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    message.GrowBuffers();
                    continue;
                }
                else if (error == UnsafeNativeMethods.MQ_ERROR_IO_TIMEOUT)
                {
                    return(ReceiveResult.Timeout);
                }
                else if (error == UnsafeNativeMethods.MQ_ERROR_OPERATION_CANCELLED)
                {
                    return(ReceiveResult.OperationCancelled);
                }
                else if (error == UnsafeNativeMethods.MQ_ERROR_INVALID_HANDLE)
                {
                    // should happen only if racing with Close
                    return(ReceiveResult.OperationCancelled);
                }
                else if (IsErrorDueToStaleHandle(error))
                {
                    HandleIsStale(handle);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqReceiveError, MsmqError.GetErrorString(error)), error));
            }
        }
Exemple #25
0
 unsafe int ReceiveCore(MsmqQueueHandle handle, NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
 {
     if (RequiresDtcTransaction(transactionMode))
     {
         return(ReceiveCoreDtcTransacted(handle, message, timeout, transactionMode, action));
     }
     else
     {
         int    timeoutInMilliseconds   = TimeoutHelper.ToMilliseconds(timeout);
         IntPtr nativePropertiesPointer = message.Pin();
         try
         {
             return(UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutInMilliseconds,
                                                         action, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode)));
         }
         finally
         {
             message.Unpin();
         }
     }
 }
 MsmqQueueHandle GetHandleForAsync(out bool useCompletionPort)
 {
     lock (this.ThisLock)
     {
         if (this.handle == null)
         {
             this.handle = OpenQueue();
         }
         if (!this.isAsyncEnabled)
         {
             if (IsCompletionPortSupported(this.handle))
             {
                 ThreadPool.BindHandle(this.handle);
                 this.isBoundToCompletionPort = true;
             }
             this.isAsyncEnabled = true;
         }
         useCompletionPort = this.isBoundToCompletionPort;
         return this.handle;
     }
 }
 private MsmqQueueHandle GetHandleForAsync(out bool useCompletionPort)
 {
     lock (this.ThisLock)
     {
         if (this.handle == null)
         {
             this.handle = this.OpenQueue();
         }
         if (!this.isAsyncEnabled)
         {
             if (IsCompletionPortSupported(this.handle))
             {
                 ThreadPool.BindHandle(this.handle);
                 this.isBoundToCompletionPort = true;
             }
             this.isAsyncEnabled = true;
         }
         useCompletionPort = this.isBoundToCompletionPort;
         return(this.handle);
     }
 }
        private int ReceiveCore(MsmqQueueHandle handle, NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
        {
            int num2;

            if (this.RequiresDtcTransaction(transactionMode))
            {
                return(this.ReceiveCoreDtcTransacted(handle, message, timeout, transactionMode, action));
            }
            int    num        = TimeoutHelper.ToMilliseconds(timeout);
            IntPtr properties = message.Pin();

            try
            {
                num2 = UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), num, action, properties, null, IntPtr.Zero, IntPtr.Zero, (IntPtr)this.GetTransactionConstant(transactionMode));
            }
            finally
            {
                message.Unpin();
            }
            return(num2);
        }
        public MoveReceiveResult TryMoveMessage(long lookupId, MsmqQueue destinationQueue, MsmqTransactionMode transactionMode)
        {
            int             num;
            MsmqQueueHandle sourceQueueHandle = this.GetHandle();
            MsmqQueueHandle handle            = destinationQueue.GetHandle();

            try
            {
                if (this.RequiresDtcTransaction(transactionMode))
                {
                    num = this.TryMoveMessageDtcTransacted(lookupId, sourceQueueHandle, handle, transactionMode);
                }
                else
                {
                    num = UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, handle, lookupId, (IntPtr)this.GetTransactionConstant(transactionMode));
                }
            }
            catch (ObjectDisposedException exception)
            {
                MsmqDiagnostics.ExpectedException(exception);
                return(MoveReceiveResult.Succeeded);
            }
            switch (num)
            {
            case 0:
                return(MoveReceiveResult.Succeeded);

            case -1072824184:
                return(MoveReceiveResult.MessageNotFound);

            case -1072824164:
                return(MoveReceiveResult.MessageLockedUnderTransaction);
            }
            if (IsErrorDueToStaleHandle(num))
            {
                this.HandleIsStale(sourceQueueHandle);
                destinationQueue.HandleIsStale(handle);
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqSendError", new object[] { MsmqError.GetErrorString(num) }), num));
        }
        public void MarkMessageRejected(long lookupId)
        {
            MsmqQueueHandle handle = this.GetHandle();
            int             error  = 0;

            try
            {
                error = UnsafeNativeMethods.MQMarkMessageRejected(handle, lookupId);
            }
            catch (ObjectDisposedException exception)
            {
                MsmqDiagnostics.ExpectedException(exception);
            }
            if (error != 0)
            {
                if (IsErrorDueToStaleHandle(error))
                {
                    this.HandleIsStale(handle);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqSendError", new object[] { MsmqError.GetErrorString(error) }), error));
            }
        }
        public MoveReceiveResult TryReceiveByLookupId(long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            MsmqQueueHandle handle = this.GetHandle();
            int             error  = 0;

            while (true)
            {
                try
                {
                    error = this.ReceiveByLookupIdCore(handle, lookupId, message, transactionMode, action);
                }
                catch (ObjectDisposedException exception)
                {
                    MsmqDiagnostics.ExpectedException(exception);
                    return(MoveReceiveResult.Succeeded);
                }
                if (error == 0)
                {
                    return(MoveReceiveResult.Succeeded);
                }
                if (!IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    if (-1072824184 == error)
                    {
                        return(MoveReceiveResult.MessageNotFound);
                    }
                    if (-1072824164 == error)
                    {
                        return(MoveReceiveResult.MessageLockedUnderTransaction);
                    }
                    if (IsErrorDueToStaleHandle(error))
                    {
                        this.HandleIsStale(handle);
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqReceiveError", new object[] { MsmqError.GetErrorString(error) }), error));
                }
                message.GrowBuffers();
            }
        }
        protected unsafe int ReceiveByLookupIdCoreDtcTransacted(MsmqQueueHandle handle, long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            IDtcTransaction dtcTransaction = GetNativeTransaction(transactionMode);

            IntPtr nativePropertiesPointer = message.Pin();
            try
            {
                if (dtcTransaction != null)
                {
                    try
                    {
                        return UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, nativePropertiesPointer, null, IntPtr.Zero, dtcTransaction);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(dtcTransaction);
                    }
                }
                else
                {
                    return UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, nativePropertiesPointer, null, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode));
                }
            }
            finally
            {
                message.Unpin();
            }

        }
 public unsafe static extern int MQMarkMessageRejected(MsmqQueueHandle handle, long lookupId);
        unsafe int ReceiveCoreDtcTransacted(MsmqQueueHandle handle, NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
        {
            IDtcTransaction dtcTransaction = GetNativeTransaction(transactionMode);
            int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout);

            IntPtr nativePropertiesPointer = message.Pin();
            try
            {
                if (dtcTransaction != null)
                {
                    try
                    {
                        return UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutInMilliseconds,
                                                                    action, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, dtcTransaction);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(dtcTransaction);
                    }
                }
                else
                {
                    return UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutInMilliseconds,
                                                                action, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode));
                }
            }
            finally
            {
                message.Unpin();
            }
        }
 unsafe int ReceiveByLookupIdCore(MsmqQueueHandle handle, long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
 {
     if (RequiresDtcTransaction(transactionMode))
     {
         return ReceiveByLookupIdCoreDtcTransacted(handle, lookupId, message, transactionMode, action);
     }
     else
     {
         IntPtr nativePropertiesPointer = message.Pin();
         try
         {
             return UnsafeNativeMethods.MQReceiveMessageByLookupId(handle, lookupId, action, nativePropertiesPointer, null, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode));
         }
         finally
         {
             message.Unpin();
         }
     }
 }
 public static extern int MQMoveMessage(MsmqQueueHandle sourceQueueHandle,
                                        MsmqQueueHandle destinationQueueHandle,
                                        long lookupId,
                                        IDtcTransaction transaction);
 unsafe int ReceiveCore(MsmqQueueHandle handle, NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode, int action)
 {
     if (RequiresDtcTransaction(transactionMode))
     {
         return ReceiveCoreDtcTransacted(handle, message, timeout, transactionMode, action);
     }
     else
     {
         int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout);
         IntPtr nativePropertiesPointer = message.Pin();
         try
         {
             return UnsafeNativeMethods.MQReceiveMessage(handle.DangerousGetHandle(), timeoutInMilliseconds,
                                                         action, nativePropertiesPointer, null, IntPtr.Zero, IntPtr.Zero, (IntPtr)GetTransactionConstant(transactionMode));
         }
         finally
         {
             message.Unpin();
         }
     }
 }
 public virtual void CloseQueue()
 {
     if (this.handle != null)
     {
         CloseQueue(this.handle);
         this.handle = null;
         this.isBoundToCompletionPort = false;
         this.isAsyncEnabled = false;
         MsmqDiagnostics.QueueClosed(this.formatName);
     }
 }
 public static extern int MQOpenQueue(string formatName, int access, int shareMode, out MsmqQueueHandle handle);
 protected MsmqQueueHandle GetHandle()
 {
     lock (this.ThisLock)
     {
         if (this.handle == null)
         {
             this.handle = OpenQueue();
         }
         return this.handle;
     }
 }
 protected void HandleIsStale(MsmqQueueHandle handle)
 {
     lock (this.ThisLock)
     {
         if (this.handle == handle)
         {
             CloseQueue();
         }
     }
 }
 public unsafe static extern int MQReceiveMessageByLookupId(MsmqQueueHandle handle, long lookupId, int action,
     IntPtr properties, NativeOverlapped* nativeOverlapped, IntPtr receiveCallback, IDtcTransaction transaction);
 public unsafe static extern int MQReceiveMessage(MsmqQueueHandle handle, int timeout, int action, IntPtr properties,
     NativeOverlapped* nativeOverlapped, MQReceiveCallback receiveCallback, IntPtr cursorHandle, IntPtr transaction);
 public static extern int MQSendMessage(MsmqQueueHandle handle, IntPtr properties, IDtcTransaction transaction);
 int TryMoveMessageDtcTransacted(long lookupId, MsmqQueueHandle sourceQueueHandle, MsmqQueueHandle destinationQueueHandle, MsmqTransactionMode transactionMode)
 {
     IDtcTransaction dtcTransaction = GetNativeTransaction(transactionMode);
     if (dtcTransaction != null)
     {
         try
         {
             return UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, destinationQueueHandle,
                                                      lookupId, dtcTransaction);
         }
         finally
         {
             Marshal.ReleaseComObject(dtcTransaction);
         }
     }
     else
     {
         return UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, destinationQueueHandle,
                                                  lookupId, (IntPtr)GetTransactionConstant(transactionMode));
     }
 }
        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 void StartReceive(bool synchronously)
            {
                bool useCompletionPort;
                try
                {
                    this.handle = this.msmqQueue.GetHandleForAsync(out useCompletionPort);
                }
                catch (MsmqException ex)
                {
                    OnCompletion(ex.ErrorCode, synchronously);
                    return;
                }
                if (null != nativeOverlapped)
                {
                    Fx.Assert("---- in StartReceive");
                }

                IntPtr nativePropertiesPointer = this.message.Pin();
                nativeOverlapped = new Overlapped(0, 0, IntPtr.Zero, this).UnsafePack(onPortedCompletion, this.message.GetBuffersForAsync());

                int error;
                try
                {
                    if (useCompletionPort)
                    {
                        error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), 
                                                           this.action, nativeOverlapped, null);
                    }
                    else
                    {
                        if (onNonPortedCompletion == null)
                        {
                            onNonPortedCompletion = new UnsafeNativeMethods.MQReceiveCallback(OnNonPortedCompletion);
                        }
                        error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), 
                                                           this.action, nativeOverlapped, onNonPortedCompletion);
                    }
                }
                catch (ObjectDisposedException ex)
                {
                    // if Close ----s with the async Receive, it is possible that SafeHandle will throw ObjectDisposedException
                    // the behavior should be same as if operation was just cancelled (the channel will return no message)
                    MsmqDiagnostics.ExpectedException(ex);
                    error = UnsafeNativeMethods.MQ_ERROR_OPERATION_CANCELLED;
                }
                if (error != 0)
                {
                    if (error != UnsafeNativeMethods.MQ_INFORMATION_OPERATION_PENDING)
                    {
                        Overlapped.Free(nativeOverlapped);
                        nativeOverlapped = null;
                        GC.SuppressFinalize(this);
                        OnCompletion(error, synchronously);
                    }
                }
            }
 public unsafe static extern int GetHandleInformation(MsmqQueueHandle handle, out int flags);
 void CloseQueue(MsmqQueueHandle handle)
 {
     handle.Dispose();
 }
        unsafe int ReceiveCoreAsync(MsmqQueueHandle handle, IntPtr nativePropertiesPointer, TimeSpan timeout,
                                    int action, NativeOverlapped* nativeOverlapped,
                                    UnsafeNativeMethods.MQReceiveCallback receiveCallback)
        {
            int timeoutInMilliseconds = TimeoutHelper.ToMilliseconds(timeout);

            return UnsafeNativeMethods.MQReceiveMessage(handle, timeoutInMilliseconds, action,
                                                        nativePropertiesPointer, nativeOverlapped, receiveCallback,
                                                        IntPtr.Zero, (IntPtr)UnsafeNativeMethods.MQ_NO_TRANSACTION);
        }
 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;
 }
        static bool IsCompletionPortSupported(MsmqQueueHandle handle)
        {
            // if it's a kernel handle, then it supports completion ports
            int flags;
#pragma warning suppress 56523
            return UnsafeNativeMethods.GetHandleInformation(handle, out flags) != 0;
        }