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));
            }
        }
        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));
            }
        }
        internal virtual MsmqQueueHandle OpenQueue()
        {
            MsmqQueueHandle handle;
            int             error = UnsafeNativeMethods.MQOpenQueue(this.formatName, this.accessMode, this.shareMode, out handle);

            if (error != 0)
            {
                Utility.CloseInvalidOutSafeHandle(handle);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqOpenError", new object[] { MsmqError.GetErrorString(error) }), error));
            }
            MsmqDiagnostics.QueueOpened(this.formatName);
            return(handle);
        }
        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));
            }
        }
        public static void GetMsmqInformation(ref Version version, ref bool activeDirectoryEnabled)
        {
            PrivateComputerProperties properties = new PrivateComputerProperties();

            using (properties)
            {
                IntPtr ptr = properties.Pin();
                try
                {
                    int error = UnsafeNativeMethods.MQGetPrivateComputerInformation(null, ptr);
                    if (error != 0)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqGetPrivateComputerInformationError", new object[] { MsmqError.GetErrorString(error) }), error));
                    }
                    int num2 = properties.Version.Value;
                    version = new Version(num2 >> 0x18, (num2 & 0xff0000) >> 0x10, num2 & 0xffff);
                    activeDirectoryEnabled = properties.ActiveDirectory.Value;
                }
                finally
                {
                    properties.Unpin();
                }
            }
        }
        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();
            }
        }
Exemplo n.º 7
0
        private bool TryOpenLockQueueForCollection(string subqueueName, out MsmqQueue lockQueue)
        {
            lockQueue = null;
            string formatName = base.formatName + ";" + subqueueName;
            int    accessMode = 1;
            int    shareMode  = 1;

            try
            {
                int error = 0;
                if (MsmqQueue.IsQueueOpenable(formatName, accessMode, shareMode, out error))
                {
                    lockQueue = new MsmqQueue(formatName, accessMode, shareMode);
                    lockQueue.EnsureOpen();
                }
                else
                {
                    if ((error != -1072824311) && (error != -1072824317))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqOpenError", new object[] { MsmqError.GetErrorString(error) }), error));
                    }
                    return(false);
                }
            }
            catch (MsmqException)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 8
0
            void OnCompletion(int error, bool completedSynchronously)
            {
                Exception completionException = null;

                this.receiveResult = ReceiveResult.MessageReceived;

                try
                {
                    if (error != 0)
                    {
                        if (error == UnsafeNativeMethods.MQ_ERROR_IO_TIMEOUT)
                        {
                            this.receiveResult = ReceiveResult.Timeout;
                        }
                        else if (error == UnsafeNativeMethods.MQ_ERROR_OPERATION_CANCELLED)
                        {
                            this.receiveResult = ReceiveResult.OperationCancelled;
                        }
                        else
                        {
                            if (IsReceiveErrorDueToInsufficientBuffer(error))
                            {
                                this.message.Unpin();
                                message.GrowBuffers();
                                StartReceive(completedSynchronously);
                                return;
                            }
                            else if (IsErrorDueToStaleHandle(error))
                            {
                                this.msmqQueue.HandleIsStale(this.handle);
                            }

                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqReceiveError, MsmqError.GetErrorString(error)), error));
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e is NullReferenceException || e is SEHException)
                    {
                        throw;
                    }
                    completionException = e;
                }

                this.message.Unpin();
                Complete(completedSynchronously, completionException);
            }
Exemplo n.º 9
0
        private bool TryOpenLockQueueForCollection(string subqueueName, out MsmqQueue lockQueue)
        {
            lockQueue = null;
            string formatName = this.formatName + ";" + subqueueName;
            int    accessMode = UnsafeNativeMethods.MQ_RECEIVE_ACCESS;
            int    shareMode  = UnsafeNativeMethods.MQ_DENY_RECEIVE_SHARE;

            try
            {
                int error = 0;
                if (MsmqQueue.IsQueueOpenable(formatName, accessMode, shareMode, out error))
                {
                    lockQueue = new MsmqQueue(formatName, accessMode, shareMode);
                    lockQueue.EnsureOpen();
                }
                else
                {
                    // The lock subqueue is either being actively used by a channel or is not available.
                    // So, we do not have to collect this lock queue.
                    if (error == UnsafeNativeMethods.MQ_ERROR_SHARING_VIOLATION ||
                        error == UnsafeNativeMethods.MQ_ERROR_QUEUE_NOT_FOUND)
                    {
                        return(false);
                    }
                    else
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqOpenError, MsmqError.GetErrorString(error)), error));
                    }
                }
            }
            catch (MsmqException)
            {
                // The error has already been logged. Since this function is to decide whether to collect
                // the lock queue, we return false.
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
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));
            }
        }
Exemplo n.º 11
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));
            }
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
0
        public static void GetMsmqInformation(ref Version version, ref bool activeDirectoryEnabled)
        {
            PrivateComputerProperties properties = new PrivateComputerProperties();

            using (properties)
            {
                IntPtr nativePropertiesPointer = properties.Pin();
                try
                {
                    int error = UnsafeNativeMethods.MQGetPrivateComputerInformation(null,
                                                                                    nativePropertiesPointer);
                    if (error != 0)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(
                                                                                                        SR.MsmqGetPrivateComputerInformationError, MsmqError.GetErrorString(error)), error));
                    }
                    int packedVersion = properties.Version.Value;
                    version = new Version(
                        packedVersion >> 24,
                        (packedVersion & 0x00FF0000) >> 16,
                        packedVersion & 0xFFFF);
                    activeDirectoryEnabled = properties.ActiveDirectory.Value;
                }
                finally
                {
                    properties.Unpin();
                }
            }
        }
Exemplo n.º 14
0
        public static string FromQueuePath(string queuePath)
        {
            int           len    = 256;
            StringBuilder buffer = new StringBuilder(len);
            int           error  = UnsafeNativeMethods.MQPathNameToFormatName(queuePath, buffer, ref len);

            if (UnsafeNativeMethods.MQ_ERROR_FORMATNAME_BUFFER_TOO_SMALL == error)
            {
                buffer = new StringBuilder(len);
                error  = UnsafeNativeMethods.MQPathNameToFormatName(queuePath, buffer, ref len);
            }

            if (0 != error)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new MsmqException(SR.GetString(SR.MsmqPathLookupError, queuePath, MsmqError.GetErrorString(error)), error));
            }

            return(buffer.ToString());
        }
        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));
        }
Exemplo n.º 16
0
        internal override MsmqQueueHandle OpenQueue()
        {
            if (!this.validHostName)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqOpenError,
                                                                                                         MsmqError.GetErrorString(UnsafeNativeMethods.MQ_ERROR_UNSUPPORTED_FORMATNAME_OPERATION)),
                                                                                            UnsafeNativeMethods.MQ_ERROR_UNSUPPORTED_FORMATNAME_OPERATION));
            }

            this.EnsureLockQueuesOpen();
            this.mainQueueForMove.EnsureOpen();
            // first time collection
            this.OnCollectionTimer(null);
            return(base.OpenQueue());
        }
        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();
            }
        }
Exemplo n.º 18
0
        public static string FromQueuePath(string queuePath)
        {
            int           capacity   = 0x100;
            StringBuilder formatName = new StringBuilder(capacity);
            int           error      = UnsafeNativeMethods.MQPathNameToFormatName(queuePath, formatName, ref capacity);

            if (-1072824289 == error)
            {
                formatName = new StringBuilder(capacity);
                error      = UnsafeNativeMethods.MQPathNameToFormatName(queuePath, formatName, ref capacity);
            }
            if (error != 0)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqPathLookupError", new object[] { queuePath, MsmqError.GetErrorString(error) }), error));
            }
            return(formatName.ToString());
        }
            private void OnCompletion(int error, bool completedSynchronously)
            {
                Exception exception = null;

                this.receiveResult = MsmqQueue.ReceiveResult.MessageReceived;
                try
                {
                    if (error != 0)
                    {
                        if (error != -1072824293)
                        {
                            if (error != -1072824312)
                            {
                                if (!MsmqQueue.IsReceiveErrorDueToInsufficientBuffer(error))
                                {
                                    if (MsmqQueue.IsErrorDueToStaleHandle(error))
                                    {
                                        this.msmqQueue.HandleIsStale(this.handle);
                                    }
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqReceiveError", new object[] { MsmqError.GetErrorString(error) }), error));
                                }
                                this.message.Unpin();
                                this.message.GrowBuffers();
                                this.StartReceive(completedSynchronously);
                                return;
                            }
                            this.receiveResult = MsmqQueue.ReceiveResult.OperationCancelled;
                        }
                        else
                        {
                            this.receiveResult = MsmqQueue.ReceiveResult.Timeout;
                        }
                    }
                }
                catch (Exception exception2)
                {
                    if ((exception2 is NullReferenceException) || (exception2 is SEHException))
                    {
                        throw;
                    }
                    exception = exception2;
                }
                this.message.Unpin();
                base.Complete(completedSynchronously, exception);
            }
Exemplo n.º 20
0
 internal override MsmqQueueHandle OpenQueue()
 {
     if (!this.validHostName)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqOpenError", new object[] { MsmqError.GetErrorString(-1072824288) }), -1072824288));
     }
     this.EnsureLockQueuesOpen();
     this.mainQueueForMove.EnsureOpen();
     this.OnCollectionTimer(null);
     return(base.OpenQueue());
 }