1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
//----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime;
class NamedPipeConnectionPoolRegistry : ConnectionPoolRegistry
{
public NamedPipeConnectionPoolRegistry()
: base()
{
}
protected override ConnectionPool CreatePool(IConnectionOrientedTransportChannelFactorySettings settings)
{
Fx.Assert(settings is IPipeTransportFactorySettings, "NamedPipeConnectionPool requires an IPipeTransportFactorySettings.");
return new NamedPipeConnectionPool((IPipeTransportFactorySettings)settings);
}
class NamedPipeConnectionPool : ConnectionPool
{
PipeNameCache pipeNameCache;
IPipeTransportFactorySettings transportFactorySettings;
public NamedPipeConnectionPool(IPipeTransportFactorySettings settings)
: base(settings, TimeSpan.MaxValue)
{
this.pipeNameCache = new PipeNameCache();
this.transportFactorySettings = settings;
}
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.transportFactorySettings);
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<Uri, string> forwardTable = new Dictionary<Uri, string>();
Dictionary<string, ICollection<Uri>> reverseTable = new Dictionary<string, ICollection<Uri>>();
public void Add(Uri uri, string pipeName)
{
this.forwardTable.Add(uri, pipeName);
ICollection<Uri> uris;
if (!this.reverseTable.TryGetValue(pipeName, out uris))
{
uris = new Collection<Uri>();
this.reverseTable.Add(pipeName, uris);
}
uris.Add(uri);
}
public void Clear()
{
this.forwardTable.Clear();
this.reverseTable.Clear();
}
public void Purge(string pipeName)
{
ICollection<Uri> 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);
}
}
}
}
|