| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.ServiceModel
- {
- using System.Collections.Generic;
- using System.Runtime;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Diagnostics.Application;
- sealed class ChannelFactoryRef<TChannel>
- where TChannel : class
- {
- ChannelFactory<TChannel> channelFactory;
- int refCount = 1;
- public ChannelFactoryRef(ChannelFactory<TChannel> channelFactory)
- {
- this.channelFactory = channelFactory;
- }
- public void AddRef()
- {
- this.refCount++;
- }
- // The caller should call Close/Abort when the return value is true.
- public bool Release()
- {
- --this.refCount;
- Fx.Assert(this.refCount >= 0, "RefCount should not be less than zero.");
- if (this.refCount == 0)
- {
- return true;
- }
- return false;
- }
- public void Close(TimeSpan timeout)
- {
- this.channelFactory.Close(timeout);
- }
- public void Abort()
- {
- this.channelFactory.Abort();
- }
- public ChannelFactory<TChannel> ChannelFactory
- {
- get
- {
- return this.channelFactory;
- }
- }
- }
- class ChannelFactoryRefCache<TChannel> : MruCache<EndpointTrait<TChannel>, ChannelFactoryRef<TChannel>>
- where TChannel : class
- {
- static EndpointTraitComparer DefaultEndpointTraitComparer = new EndpointTraitComparer();
- readonly int watermark;
- class EndpointTraitComparer : IEqualityComparer<EndpointTrait<TChannel>>
- {
- public bool Equals(EndpointTrait<TChannel> x, EndpointTrait<TChannel> y)
- {
- if (x != null)
- {
- if (y != null)
- return x.Equals(y);
- return false;
- }
- if (y != null)
- return false;
- return true;
- }
- public int GetHashCode(EndpointTrait<TChannel> obj)
- {
- if (obj == null)
- return 0;
- return obj.GetHashCode();
- }
- }
- public ChannelFactoryRefCache(int watermark)
- : base(watermark * 4 / 5, watermark, DefaultEndpointTraitComparer)
- {
- this.watermark = watermark;
- }
- protected override void OnSingleItemRemoved(ChannelFactoryRef<TChannel> item)
- {
- // Remove from cache.
- if (item.Release())
- {
- item.Abort();
- }
- if (TD.ClientBaseCachedChannelFactoryCountIsEnabled())
- {
- TD.ClientBaseCachedChannelFactoryCount(this.Count, this.watermark, this);
- }
- }
- protected override void OnItemAgedOutOfCache(ChannelFactoryRef<TChannel> item)
- {
- if (TD.ClientBaseChannelFactoryAgedOutofCacheIsEnabled())
- {
- TD.ClientBaseChannelFactoryAgedOutofCache(this.watermark, this);
- }
- }
- }
- }
|