protected override void OnSend(Message message, TimeSpan timeout)
        {
            // serialize the indigo message to byte array and copy the .NET framing preamble
            ArraySegment <byte> messageData = this.factory.MessageEncoderFactory.Encoder.WriteMessage(
                message, int.MaxValue, this.factory.BufferManager, preamble.Length);

            Buffer.BlockCopy(preamble, 0, messageData.Array, messageData.Offset - preamble.Length, preamble.Length);

            byte[] buffer = messageData.Array;
            int    offset = messageData.Offset - preamble.Length;
            int    size   = messageData.Count + preamble.Length;

            MsmqOutputMessage <IOutputChannel> msmqMessage = this.outputMessages.Take();

            if (msmqMessage == null)
            {
                msmqMessage = new MsmqOutputMessage <IOutputChannel>(this.factory, size, this.RemoteAddress);
                MsmqDiagnostics.PoolFull(this.factory.MaxPoolSize);
            }
            try
            {
                msmqMessage.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
                msmqMessage.Body.EnsureBufferLength(size);
                msmqMessage.Body.BufferLength = size;
                Buffer.BlockCopy(buffer, offset, msmqMessage.Body.Buffer, 0, size);
                this.factory.BufferManager.ReturnBuffer(buffer);

                bool lockHeld = false;
                try
                {
                    Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                    this.msmqQueue.Send(msmqMessage, this.transactionMode);
                    MsmqDiagnostics.DatagramSent(msmqMessage.MessageId, message);
                }
                catch (MsmqException ex)
                {
                    if (ex.FaultSender)
                    {
                        this.Fault();
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
                }
                finally
                {
                    if (lockHeld)
                    {
                        Msmq.LeaveXPSendLock();
                    }
                }
            }
            finally
            {
                if (!this.outputMessages.Return(msmqMessage))
                {
                    msmqMessage.Dispose();
                }
            }
        }
Example #2
0
        protected override void OnSend(Message message, TimeSpan timeout)
        {
            ArraySegment <byte> segment = this.factory.MessageEncoderFactory.Encoder.WriteMessage(message, 0x7fffffff, this.factory.BufferManager, this.preamble.Length);

            Buffer.BlockCopy(this.preamble, 0, segment.Array, segment.Offset - this.preamble.Length, this.preamble.Length);
            byte[] array     = segment.Array;
            int    srcOffset = segment.Offset - this.preamble.Length;
            int    bodySize  = segment.Count + this.preamble.Length;
            MsmqOutputMessage <IOutputChannel> message2 = this.outputMessages.Take();

            if (message2 == null)
            {
                message2 = new MsmqOutputMessage <IOutputChannel>(this.factory, bodySize, this.RemoteAddress);
                MsmqDiagnostics.PoolFull(this.factory.MaxPoolSize);
            }
            try
            {
                message2.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
                message2.Body.EnsureBufferLength(bodySize);
                message2.Body.BufferLength = bodySize;
                Buffer.BlockCopy(array, srcOffset, message2.Body.Buffer, 0, bodySize);
                this.factory.BufferManager.ReturnBuffer(array);
                bool lockHeld = false;
                try
                {
                    Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                    this.msmqQueue.Send(message2, this.transactionMode);
                    MsmqDiagnostics.DatagramSent(message2.MessageId, message);
                }
                catch (MsmqException exception)
                {
                    if (exception.FaultSender)
                    {
                        base.Fault();
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
                }
                finally
                {
                    if (lockHeld)
                    {
                        Msmq.LeaveXPSendLock();
                    }
                }
            }
            finally
            {
                if (!this.outputMessages.Return(message2))
                {
                    message2.Dispose();
                }
            }
        }