RoutingTable.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 / SMSvcHost / System / ServiceModel / Activation / RoutingTable.cs / 1 / RoutingTable.cs

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

namespace System.ServiceModel.Activation 
{
    using System; 
    using System.Collections; 
    using System.Security.AccessControl;
    using System.Diagnostics; 
    using System.Threading;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Collections.Generic; 
    using System.ServiceModel.Diagnostics;
    using System.ServiceModel.Activation.Diagnostics; 
    using System.Globalization; 
    using System.Net;
 
    using StringTraceRecord = System.ServiceModel.Diagnostics.StringTraceRecord;

    static class RoutingTable
    { 
        class MessageQueueAndPath
        { 
            MessageQueue messageQueue; 
            Uri uri;
 
            internal MessageQueueAndPath(MessageQueue messageQueue, Uri uri)
            {
                this.messageQueue = messageQueue;
                this.uri = uri; 
            }
 
            internal MessageQueue MessageQueue { get { return messageQueue; } } 
            internal Uri Uri { get { return uri; } }
        } 

        static UriPrefixTable namedPipeMessageQueues = new UriPrefixTable();
        static UriPrefixTable tcpMessageQueues = new UriPrefixTable(true);
 
#if DEBUG
        internal static void DumpTables(TransportType transportType) 
        { 
            UriPrefixTable table = transportType == TransportType.Tcp ? tcpMessageQueues : namedPipeMessageQueues;
            lock (table) 
            {
                Debug.Print("RoutingTable dumping " + table.Count + " Route(s) for TransportType: " + transportType);
                int count = 0;
                foreach (KeyValuePair item in table.GetAll()) 
                {
                    bool activated = item.Value.MessageQueue.GetType().Equals(typeof(ActivatedMessageQueue)); 
                    Debug.Print("Registration #" + (++count).ToString(CultureInfo.CurrentUICulture)); 
                    Debug.Print("\tActivated:" + activated);
                    Debug.Print("\tCanDispatch:" + item.Value.MessageQueue.CanDispatch); 
                    Debug.Print("\tBaseAddress:" + item.Key.BaseAddress);
                    Debug.Print("\tHostNameComparisonMode:" + item.Key.HostNameComparisonMode);
                    List workers = item.Value.MessageQueue.SnapshotWorkers();
                    if (workers.Count == 0) 
                    {
                        Debug.Print("\tNo WorkerProcess Active."); 
                    } 
                    else
                    { 
                        Debug.Print("\t" + workers.Count + " WorkerProcess(es) Registered:");
                        foreach (WorkerProcess wp in workers)
                        {
                            Debug.Print("\t\tPid:" + wp.ProcessId); 
                            if (activated)
                            { 
                                Debug.Print("\t\tActive:" + wp.IsRegistered); 
                                Debug.Print("\t\tQueueId:" + wp.QueueId);
                            } 
                        }
                    }
                }
            } 
        }
#endif 
        internal static MessageQueue Lookup(Uri uri, IPAddress address, int port) 
        {
            Uri wildCardUri = uri; 
            UriPrefixTable table = namedPipeMessageQueues;
            if (address != null)
            {
                // Including port number to support TCP proxy (see MB56472). We only use it for wildcard matching below. 
                // NOTE: we don't need to call TcpChannelListener.FixIpv6Hostname to fix the host name because it's ignored anyway.
                UriBuilder uriBuilder = new UriBuilder(uri.Scheme, uri.Host, port, uri.PathAndQuery); 
                wildCardUri = uriBuilder.Uri; 
                table = tcpMessageQueues;
            } 

            MessageQueueAndPath found = null;
            bool success = table.TryLookupUri(wildCardUri, HostNameComparisonMode.StrongWildcard, out found);
            if (success && address != null) 
            {
                success = ValidateAddress(address, ref found); 
            } 
            if (!success)
            { 
                success = table.TryLookupUri(uri, HostNameComparisonMode.Exact, out found);
                if (success && address != null)
                {
                    success = ValidateAddress(address, ref found); 
                }
            } 
            if (!success) 
            {
                success = table.TryLookupUri(wildCardUri, HostNameComparisonMode.WeakWildcard, out found); 
                if (success && address != null)
                {
                    success = ValidateAddress(address, ref found);
                } 
            }
 
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTableLookup, new StringTraceRecord("Uri", uri.ToString()), null, null); 
            }

            Debug.Print("RoutingTable.Lookup(" + uri + ") matched: " + (found == null ? "" : found.Uri.ToString()));
            return found == null ? null : found.MessageQueue; 
        }
 
        static bool ValidateAddress(IPAddress address, ref MessageQueueAndPath found) 
        {
            if (found.Uri.HostNameType != UriHostNameType.IPv4 && found.Uri.HostNameType != UriHostNameType.IPv6) 
            {
                return true;
            }
 
            IPAddress foundAddress = IPAddress.Parse(found.Uri.DnsSafeHost);
            bool valid = (address.Equals(foundAddress) || 
                (foundAddress.Equals(IPAddress.Any) && foundAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) || 
                foundAddress.Equals(IPAddress.IPv6Any));
            found = valid ? found : null; 
            return valid;
        }

        internal static ListenerExceptionStatus NamedPipeStart(MessageQueue messageQueue, BaseUriWithWildcard path) 
        {
            int encodedSize = System.Text.Encoding.UTF8.GetByteCount(path.BaseAddress.AbsoluteUri); 
            if (encodedSize > ListenerConstants.MaxUriSize) 
            {
                if (DiagnosticUtility.ShouldTraceInformation) 
                {
                    ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTablePathTooLong, new StringTraceRecord("Path", path.ToString()), null, null);
                }
 
                return ListenerExceptionStatus.PathTooLong;
            } 
            lock (namedPipeMessageQueues) 
            {
                if (namedPipeMessageQueues.IsRegistered(path)) 
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTableNamespaceConflict, new StringTraceRecord("Path", path.ToString()), null, null); 
                    }
 
                    return ListenerExceptionStatus.ConflictingRegistration; 
                }
 
                TransportListener.Listen(path);
                namedPipeMessageQueues.RegisterUri(path.BaseAddress, path.HostNameComparisonMode, new MessageQueueAndPath(messageQueue, path.BaseAddress));
            }
 
            if (DiagnosticUtility.ShouldTraceInformation)
            { 
                ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTableRegisterSuccess, new StringTraceRecord("Path", path.ToString()), null, null); 
            }
 
            return ListenerExceptionStatus.Success;
        }

        internal static ListenerExceptionStatus Start(MessageQueue messageQueue, BaseUriWithWildcard path) 
        {
            if (messageQueue.TransportType == TransportType.Tcp) 
            { 
                return TcpStart(messageQueue, path);
            } 
            else
            {
                return NamedPipeStart(messageQueue, path);
            } 
        }
 
        static IPEndPoint GetEndPoint(Uri path) 
        {
            IPAddress address = IPAddress.Broadcast; 
            if (path.HostNameType == UriHostNameType.IPv4 || path.HostNameType == UriHostNameType.IPv6)
            {
                address = IPAddress.Parse(path.DnsSafeHost);
            } 
            return new IPEndPoint(address, path.Port);
        } 
 
        internal static void Stop(MessageQueue messageQueue, BaseUriWithWildcard path)
        { 
            if (messageQueue.TransportType == TransportType.Tcp)
            {
                IPEndPoint endPoint = GetEndPoint(path.BaseAddress);
                TransportListener.Stop(endPoint); 
                tcpMessageQueues.UnregisterUri(path.BaseAddress, path.HostNameComparisonMode);
            } 
            else 
            {
                TransportListener.Stop(path); 
                namedPipeMessageQueues.UnregisterUri(path.BaseAddress, path.HostNameComparisonMode);
            }
        }
 
        static ListenerExceptionStatus TcpStart(MessageQueue messageQueue, BaseUriWithWildcard path)
        { 
            int encodedSize = System.Text.Encoding.UTF8.GetByteCount(path.BaseAddress.AbsoluteUri); 
            if (encodedSize > ListenerConstants.MaxUriSize)
            { 
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTablePathTooLong, new StringTraceRecord("Path", path.ToString()), null, null);
                } 

                return ListenerExceptionStatus.PathTooLong; 
            } 
            IPEndPoint endPoint = GetEndPoint(path.BaseAddress);
            lock (tcpMessageQueues) 
            {
                if (tcpMessageQueues.IsRegistered(path))
                {
                    if (DiagnosticUtility.ShouldTraceInformation) 
                    {
                        ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTableNamespaceConflict, new StringTraceRecord("Path", path.ToString()), null, null); 
                    } 

                    return ListenerExceptionStatus.ConflictingRegistration; 
                }

                TransportListener.Listen(endPoint);
                tcpMessageQueues.RegisterUri(path.BaseAddress, path.HostNameComparisonMode, new MessageQueueAndPath(messageQueue, path.BaseAddress)); 
            }
 
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.RoutingTableRegisterSuccess, new StringTraceRecord("Path", path.ToString()), null, null); 
            }

            return ListenerExceptionStatus.Success;
        } 
    }
} 

// 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