ByteStreamMessageEncoder.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.ServiceModel.Channels / System / ServiceModel / Channels / ByteStreamMessageEncoder.cs / 1305376 / ByteStreamMessageEncoder.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------

namespace System.ServiceModel.Channels 
{
    using System; 
    using System.Xml; 
    using System.ServiceModel.Diagnostics;
 
    class ByteStreamMessageEncoder : MessageEncoder
    {
        string maxReceivedMessageSizeExceededResourceString;
        string maxSentMessageSizeExceededResourceString; 
        XmlDictionaryReaderQuotas quotas;
 
        public ByteStreamMessageEncoder(XmlDictionaryReaderQuotas quotas) 
        {
            this.quotas = new XmlDictionaryReaderQuotas(); 
            quotas.CopyTo(this.quotas);

            this.maxSentMessageSizeExceededResourceString = SR.MaxSentMessageSizeExceeded("{0}");
            this.maxReceivedMessageSizeExceededResourceString = SR.MaxReceivedMessageSizeExceeded("{0}"); 
        }
 
        public override string ContentType 
        {
            get { return null; } 
        }

        public override string MediaType
        { 
            get { return null; }
        } 
 
        public override MessageVersion MessageVersion
        { 
            get { return MessageVersion.None; }
        }

        public override bool IsContentTypeSupported(string contentType) 
        {
            return true; 
        } 

        public override Message ReadMessage(System.IO.Stream stream, int maxSizeOfHeaders, string contentType) 
        {
            if (stream == null)
            {
                throw FxTrace.Exception.ArgumentNull("stream"); 
            }
 
            Message message = new ByteStreamMessage(stream, this.quotas); 
            message.Properties.Encoder = this;
 
            if (MessageLogger.LogMessagesAtTransportLevel)
            {
                MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportReceive);
            } 

            return message; 
        } 

        public override Message ReadMessage(ArraySegment buffer, BufferManager bufferManager, string contentType) 
        {
            if (buffer == null)
            {
                throw FxTrace.Exception.ArgumentNull("buffer"); 
            }
            if (bufferManager == null) 
            { 
                throw FxTrace.Exception.ArgumentNull("bufferManager");
            } 

            ByteStreamBufferedMessageData messageData = new ByteStreamBufferedMessageData(bufferManager, buffer.Count);
            Buffer.BlockCopy(buffer.Array, buffer.Offset, messageData.Buffer, 0, buffer.Count);
 
            Message message = new ByteStreamMessage(messageData, this.quotas);
            message.Properties.Encoder = this; 
 
            if (MessageLogger.LogMessagesAtTransportLevel)
            { 
                MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportReceive);
            }

            return message; 
        }
 
        public override void WriteMessage(Message message, System.IO.Stream stream) 
        {
            if (message == null) 
            {
                throw FxTrace.Exception.ArgumentNull("message");
            }
            if (stream == null) 
            {
                throw FxTrace.Exception.ArgumentNull("stream"); 
            } 

            ThrowIfMismatchedMessageVersion(message); 
            message.Properties.Encoder = this;

            if (MessageLogger.LogMessagesAtTransportLevel)
            { 
                MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend);
            } 
 
            using (XmlWriter writer = new XmlByteStreamWriter(stream, false))
            { 
                message.WriteMessage(writer);
                writer.Flush();
            }
        } 

        public override ArraySegment WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) 
        { 
            if (message == null)
            { 
                throw FxTrace.Exception.ArgumentNull("message");
            }
            if (bufferManager == null)
            { 
                throw FxTrace.Exception.ArgumentNull("bufferManager");
            } 
            if (maxMessageSize < 0) 
            {
                throw FxTrace.Exception.ArgumentOutOfRange("maxMessageSize", maxMessageSize, SR.ArgumentOutOfMinRange(0)); 
            }
            if (messageOffset < 0)
            {
                throw FxTrace.Exception.ArgumentOutOfRange("messageOffset", messageOffset, SR.ArgumentOutOfMinRange(0)); 
            }
 
            ThrowIfMismatchedMessageVersion(message); 
            message.Properties.Encoder = this;
 
            byte[] messageBuffer;
            int size;

            using (BufferManagerOutputStream stream = new BufferManagerOutputStream(maxSentMessageSizeExceededResourceString, 0, maxMessageSize, bufferManager)) 
            {
                stream.Skip(messageOffset); 
                using (XmlWriter writer = new XmlByteStreamWriter(stream, true)) 
                {
                    message.WriteMessage(writer); 
                    writer.Flush();
                    messageBuffer = stream.ToArray(out size);
                }
            } 

            if (MessageLogger.LogMessagesAtTransportLevel) 
            { 
                ByteStreamBufferedMessageData messageData = new ByteStreamBufferedMessageData(bufferManager, size - messageOffset);
                using (XmlReader reader = new XmlBufferedByteStreamReader(messageData, this.quotas)) 
                {
                    MessageLogger.LogMessage(ref message, reader, MessageLoggingSource.TransportSend);
                }
            } 

            return new ArraySegment(messageBuffer, messageOffset, size - messageOffset); 
        } 

        public override string ToString() 
        {
            return ByteStreamMessageUtility.EncoderName;
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK