NamedPipeConnectionPool.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 / Channels / NamedPipeConnectionPool.cs / 1 / NamedPipeConnectionPool.cs

                            //---------------------------------------------------------------------------- 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//---------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{ 
    using System.Collections.Generic;
    using System.Collections.ObjectModel; 
    using System.Net; 
    using System.Net.Sockets;
    using System.IO; 

    class NamedPipeConnectionPoolRegistry : ConnectionPoolRegistry
    {
        public NamedPipeConnectionPoolRegistry() 
            : base()
        { 
        } 

        protected override ConnectionPool CreatePool(IConnectionOrientedTransportChannelFactorySettings settings) 
        {
            return new NamedPipeConnectionPool(settings);
        }
 
        class NamedPipeConnectionPool : ConnectionPool
        { 
            PipeNameCache pipeNameCache; 

            public NamedPipeConnectionPool(IConnectionOrientedTransportChannelFactorySettings settings) 
                : base(settings, TimeSpan.MaxValue)
            {
                this.pipeNameCache = new PipeNameCache();
            } 

            protected override EndpointConnectionPool CreateEndpointConnectionPool(string key) 
            { 
                return new NamedPipeEndpointConnectionPool(this, key);
            } 

            protected override string GetPoolKey(EndpointAddress address, Uri via)
            {
                string result; 
                lock (base.ThisLock)
                { 
                    if (!this.pipeNameCache.TryGetValue(via, out result)) 
                    {
                        result = PipeConnectionInitiator.GetPipeName(via); 
                        this.pipeNameCache.Add(via, result);
                    }
                }
                return result; 
            }
 
            protected override void OnClosed() 
            {
                base.OnClosed(); 
                this.pipeNameCache.Clear();
            }

            void OnConnectionAborted(string pipeName) 
            {
                // the underlying pipe name may have changed; purge the old one from the cache 
                lock (base.ThisLock) 
                {
                    this.pipeNameCache.Purge(pipeName); 
                }
            }

            protected class NamedPipeEndpointConnectionPool : IdleTimeoutEndpointConnectionPool 
            {
                NamedPipeConnectionPool parent; 
 
                public NamedPipeEndpointConnectionPool(NamedPipeConnectionPool parent, string key)
                    : base(parent, key) 
                {
                    this.parent = parent;
                }
 
                protected override void OnConnectionAborted()
                { 
                    parent.OnConnectionAborted(this.Key); 
                }
            } 
        }

        // not thread-safe
        class PipeNameCache 
        {
            Dictionary forwardTable = new Dictionary(); 
            Dictionary> reverseTable = new Dictionary>(); 

            public void Add(Uri uri, string pipeName) 
            {
                this.forwardTable.Add(uri, pipeName);

                ICollection uris; 
                if (!this.reverseTable.TryGetValue(pipeName, out uris))
                { 
                    uris = new Collection(); 
                    this.reverseTable.Add(pipeName, uris);
                } 
                uris.Add(uri);
            }

            public void Clear() 
            {
                this.forwardTable.Clear(); 
                this.reverseTable.Clear(); 
            }
 
            public void Purge(string pipeName)
            {
                ICollection uris;
                if (this.reverseTable.TryGetValue(pipeName, out uris)) 
                {
                    this.reverseTable.Remove(pipeName); 
                    foreach (Uri uri in uris) 
                    {
                        this.forwardTable.Remove(uri); 
                    }
                }
            }
 
            public bool TryGetValue(Uri uri, out string pipeName)
            { 
                return this.forwardTable.TryGetValue(uri, out pipeName); 
            }
        } 
    }
}

 

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