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();
                }
            }
        }
 private void OnCloseCore(bool isAborting, TimeSpan timeout)
 {
     if (!isAborting && (this.buffers.Count > 1))
     {
         lock (base.ThisLock)
         {
             this.VerifyTransaction();
             this.buffers.Add(this.EncodeEndMarker());
         }
         int bodySize = this.CalcSessionGramSize();
         using (MsmqOutputMessage <IOutputSessionChannel> message = new MsmqOutputMessage <IOutputSessionChannel>(this.Factory, bodySize, this.RemoteAddress))
         {
             message.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
             message.Body.EnsureBufferLength(bodySize);
             message.Body.BufferLength = bodySize;
             this.CopySessionGramToBuffer(message.Body.Buffer);
             bool lockHeld = false;
             try
             {
                 Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                 this.msmqQueue.Send(message, MsmqTransactionMode.CurrentOrSingle);
                 MsmqDiagnostics.SessiongramSent(this.Session.Id, message.MessageId, this.buffers.Count);
             }
             catch (MsmqException exception)
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
             }
             finally
             {
                 if (lockHeld)
                 {
                     Msmq.LeaveXPSendLock();
                 }
                 this.ReturnSessionGramBuffers();
             }
         }
     }
     if (this.msmqQueue != null)
     {
         this.msmqQueue.Dispose();
     }
     this.msmqQueue = null;
     if (this.certificateTokenProvider != null)
     {
         if (isAborting)
         {
             this.certificateTokenProvider.Abort();
         }
         else
         {
             this.certificateTokenProvider.Close(timeout);
         }
     }
 }
Exemplo n.º 3
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();
                }
            }
        }
Exemplo n.º 4
0
        void OnCloseCore(bool isAborting, TimeSpan timeout)
        {
            // Dump the messages into the queue as a big bag.
            // no MSMQ send if aborting
            // no MSMQ send if the channel has only a preamble (no actual messages sent)
            if (!isAborting && this.buffers.Count > 1)
            {
                lock (ThisLock)
                {
                    VerifyTransaction();

                    buffers.Add(EncodeEndMarker());
                }

                int size = CalcSessionGramSize();

                using (MsmqOutputMessage <IOutputSessionChannel> msmqMessage = new MsmqOutputMessage <IOutputSessionChannel>(this.Factory, size, this.RemoteAddress))
                {
                    msmqMessage.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
                    msmqMessage.Body.EnsureBufferLength(size);
                    msmqMessage.Body.BufferLength = size;
                    CopySessionGramToBuffer(msmqMessage.Body.Buffer);

                    bool lockHeld = false;
                    try
                    {
                        Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                        this.msmqQueue.Send(msmqMessage, MsmqTransactionMode.CurrentOrSingle);
                        MsmqDiagnostics.SessiongramSent(this.Session.Id, msmqMessage.MessageId, this.buffers.Count);
                    }
                    catch (MsmqException ex)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
                    }
                    finally
                    {
                        if (lockHeld)
                        {
                            Msmq.LeaveXPSendLock();
                        }
                        ReturnSessionGramBuffers();
                    }
                }
            }

            if (null != this.msmqQueue)
            {
                this.msmqQueue.Dispose();
            }
            this.msmqQueue = null;

            if (certificateTokenProvider != null)
            {
                if (isAborting)
                {
                    certificateTokenProvider.Abort();
                }
                else
                {
                    certificateTokenProvider.Close(timeout);
                }
            }
        }