EndpointAddressMessageFilter.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Dispatcher / EndpointAddressMessageFilter.cs / 1 / EndpointAddressMessageFilter.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{ 
    using System;
    using System.ServiceModel.Channels; 
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.IO; 
    using System.ServiceModel.Security;
    using System.Text;
    using System.Xml;
 
    using QName = System.ServiceModel.Dispatcher.EndpointAddressProcessor.QName;
    using HeaderBit = System.ServiceModel.Dispatcher.EndpointAddressProcessor.HeaderBit; 
 
    public class EndpointAddressMessageFilter : MessageFilter
    { 
        EndpointAddress address;
        bool includeHostNameInComparison;
        EndpointAddressMessageFilterHelper helper;
        EqualityComparer comparer; 

        public EndpointAddressMessageFilter(EndpointAddress address) 
            : this(address, false) 
        {
        } 

        public EndpointAddressMessageFilter(EndpointAddress address, bool includeHostNameInComparison)
        {
            if (address == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("address"); 
            } 

            this.address = address; 
            this.includeHostNameInComparison = includeHostNameInComparison;
            this.helper = new EndpointAddressMessageFilterHelper(this.address);

            if (includeHostNameInComparison) 
            {
                this.comparer = HostUriComparer.Value; 
            } 
            else
            { 
                this.comparer = NoHostUriComparer.Value;
            }
        }
 
        public EndpointAddress Address
        { 
            get 
            {
                return this.address; 
            }
        }

        public bool IncludeHostNameInComparison 
        {
            get { return this.includeHostNameInComparison; } 
        } 

        protected internal override IMessageFilterTable CreateFilterTable() 
        {
            return new EndpointAddressMessageFilterTable();
        }
 
        public override bool Match(MessageBuffer messageBuffer)
        { 
            if (messageBuffer == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageBuffer"); 
            }

            Message msg = messageBuffer.CreateMessage();
            try 
            {
                return Match(msg); 
            } 
            finally
            { 
                msg.Close();
            }
        }
 
        public override bool Match(Message message)
        { 
            if (message == null) 
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message"); 
            }

            // To
#pragma warning suppress 56506 // [....], Message.Headers can never be null 
            Uri to = message.Headers.To;
            Uri actingAs = this.address.Uri; 
 
            if (to == null || !this.comparer.Equals(actingAs, to))
            { 
                return false;
            }

            return this.helper.Match(message); 
        }
 
        internal Dictionary HeaderLookup 
        {
            get { return this.helper.HeaderLookup; } 
        }

        internal abstract class UriComparer : EqualityComparer
        { 
            protected UriComparer()
            { 
            } 

            protected abstract bool CompareHost { get; } 

            public override bool Equals(Uri u1, Uri u2)
            {
                return EndpointAddress.UriEquals(u1, u2, true /* ignoreCase */, CompareHost); 
            }
 
            public override int GetHashCode(Uri uri) 
            {
                return EndpointAddress.UriGetHashCode(uri, CompareHost); 
            }
        }

        internal sealed class HostUriComparer : UriComparer 
        {
            internal static readonly EqualityComparer Value = new HostUriComparer(); 
 
            HostUriComparer()
            { 
            }

            protected override bool CompareHost
            { 
                get
                { 
                    return true; 
                }
            } 
        }

        internal sealed class NoHostUriComparer : UriComparer
        { 
            internal static readonly EqualityComparer Value = new NoHostUriComparer();
 
            NoHostUriComparer() 
            {
            } 

            protected override bool CompareHost
            {
                get 
                {
                    return false; 
                } 
            }
        } 
    }

    internal class EndpointAddressMessageFilterHelper
    { 
        EndpointAddress address;
        WeakReference processorPool; 
 
        int size;
        byte[] mask; 
        Dictionary qnameLookup;
        Dictionary headerLookup;

        internal EndpointAddressMessageFilterHelper(EndpointAddress address) 
        {
            this.address = address; 
 
            if (this.address.Headers.Count > 0)
            { 
                CreateMask();
                this.processorPool = new WeakReference(null);
            }
            else 
            {
                this.qnameLookup = null; 
                this.headerLookup = null; 
                this.size = 0;
                this.mask = null; 
            }
        }

        void CreateMask() 
        {
            int nextBit = 0; 
            string key; 
            HeaderBit[] bits;
            QName qname; 
            this.qnameLookup = new Dictionary(EndpointAddressProcessor.QNameComparer);
            this.headerLookup = new Dictionary();
            StringBuilder builder = null;
 
            for (int i = 0; i < this.address.Headers.Count; ++i)
            { 
                if (builder == null) 
                    builder = new StringBuilder();
                else 
                    builder.Remove(0, builder.Length);


                key = this.address.Headers[i].GetComparableForm(builder); 
                if (this.headerLookup.TryGetValue(key, out bits))
                { 
                    Array.Resize(ref bits, bits.Length + 1); 
                    bits[bits.Length - 1] = new HeaderBit(nextBit++);
                    this.headerLookup[key] = bits; 
                }
                else
                {
 
                    this.headerLookup.Add(key, new HeaderBit[] { new HeaderBit(nextBit++) });
                    AddressHeader parameter = this.address.Headers[i]; 
 
                    qname.name = parameter.Name;
                    qname.ns = parameter.Namespace; 
                    this.qnameLookup[qname] = 1;
                }
            }
 
            if (nextBit == 0)
            { 
                this.size = 0; 
            }
            else 
            {
                this.size = (nextBit - 1) / 8 + 1;
            }
 
            if (this.size > 0)
            { 
                this.mask = new byte[size]; 
                for (int i = 0; i < this.size - 1; ++i)
                { 
                    this.mask[i] = 0xff;
                }

                if ((nextBit % 8) == 0) 
                {
                    this.mask[this.size - 1] = 0xff; 
                } 
                else
                { 
                    this.mask[this.size - 1] = (byte)((1 << (nextBit % 8)) - 1);
                }
            }
        } 

        internal Dictionary HeaderLookup 
        { 
            get
            { 
                if (this.headerLookup == null)
                    this.headerLookup = new Dictionary();
                return this.headerLookup;
            } 
        }
 
        EndpointAddressProcessor CreateProcessor(int length) 
        {
            if (this.processorPool.Target != null) 
            {
                lock (this.processorPool)
                {
                    object o = this.processorPool.Target; 
                    if (o != null)
                    { 
                        EndpointAddressProcessor p = (EndpointAddressProcessor)o; 
                        this.processorPool.Target = p.Next;
                        p.Next = null; 
                        p.Clear(length);
                        return p;
                    }
                } 
            }
 
            return new EndpointAddressProcessor(length); 
        }
 
        internal bool Match(Message message)
        {
            if (this.size == 0)
            { 
                return true;
            } 
 
            EndpointAddressProcessor context = CreateProcessor(this.size);
            context.ProcessHeaders(message, this.qnameLookup, this.headerLookup); 
            bool result = context.TestExact(this.mask);
            ReleaseProcessor(context);
            return result;
        } 

        void ReleaseProcessor(EndpointAddressProcessor context) 
        { 
            lock (this.processorPool)
            { 
                context.Next = this.processorPool.Target as EndpointAddressProcessor;
                this.processorPool.Target = context;
            }
        } 

    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

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