ChannelProxy.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. namespace Vinno.IUS.Common.Network.Channels
  3. {
  4. internal class ChannelProxy<T> : IChannelProxy where T : Channel
  5. {
  6. protected readonly T Channel;
  7. protected readonly ChannelFactory<T> _factory;
  8. private bool _disposed;
  9. public ChannelProxy(T channel, ChannelFactory<T> factory)
  10. {
  11. Channel = channel;
  12. _factory = factory;
  13. }
  14. ~ChannelProxy()
  15. {
  16. DoDispose();
  17. }
  18. protected virtual void DoDispose()
  19. {
  20. if (!_disposed)
  21. {
  22. try
  23. {
  24. //Just put itself back to the factory.
  25. _factory.Put(Channel);
  26. }
  27. catch (ObjectDisposedException)
  28. {
  29. //Pool is closed
  30. Channel.Close();
  31. }
  32. _disposed = true;
  33. }
  34. }
  35. /// <summary>
  36. /// The dispose method only put the channel back to the pool, will not
  37. /// destroy the channel.
  38. /// </summary>
  39. public void Dispose()
  40. {
  41. DoDispose();
  42. GC.SuppressFinalize(this);
  43. }
  44. }
  45. }