1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- namespace Vinno.IUS.Common.Network.Channels
- {
- internal class ChannelProxy<T> : IChannelProxy where T : Channel
- {
- protected readonly T Channel;
- protected readonly ChannelFactory<T> _factory;
- private bool _disposed;
- public ChannelProxy(T channel, ChannelFactory<T> factory)
- {
- Channel = channel;
- _factory = factory;
- }
- ~ChannelProxy()
- {
- DoDispose();
- }
- protected virtual void DoDispose()
- {
- if (!_disposed)
- {
- try
- {
- //Just put itself back to the factory.
- _factory.Put(Channel);
- }
- catch (ObjectDisposedException)
- {
- //Pool is closed
- Channel.Close();
- }
- _disposed = true;
- }
- }
- /// <summary>
- /// The dispose method only put the channel back to the pool, will not
- /// destroy the channel.
- /// </summary>
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|