The Labs \ Source Viewer \ SSCLI \ System.Runtime.Remoting.Lifetime \ ClientSponsor

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //+----------------------------------------------------------------------------
  16. //
  17. // File: ClientSponsor.cs
  18. //
  19. // Contents: Agent for keeping Server Object's lifetime in sync with a client's lifetime
  20. //
  21. // History: 8/9/00 Created
  22. //
  23. //+----------------------------------------------------------------------------
  24. namespace System.Runtime.Remoting.Lifetime
  25. {
  26.     using System;
  27.     using System.Collections;
  28.     using System.Security.Permissions;
  29.    
  30.     [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  31.     [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
  32.     [System.Runtime.InteropServices.ComVisible(true)]
  33.     public class ClientSponsor : MarshalByRefObject, ISponsor
  34.     {
  35.         private Hashtable sponsorTable = new Hashtable(10);
  36.         private TimeSpan m_renewalTime = TimeSpan.FromMinutes(2);
  37.        
  38.         public ClientSponsor()
  39.         {
  40.         }
  41.        
  42.         public ClientSponsor(TimeSpan renewalTime)
  43.         {
  44.             this.m_renewalTime = renewalTime;
  45.         }
  46.        
  47.         public TimeSpan RenewalTime {
  48.             get { return m_renewalTime; }
  49.             set { m_renewalTime = value; }
  50.         }
  51.        
  52.         public bool Register(MarshalByRefObject obj)
  53.         {
  54.             BCLDebug.Trace("REMOTE", "ClientSponsor Register " + obj);
  55.             ILease lease = (ILease)obj.GetLifetimeService();
  56.             if (lease == null)
  57.                 return false;
  58.            
  59.             lease.Register(this);
  60.             lock (sponsorTable) {
  61.                 sponsorTable[obj] = lease;
  62.             }
  63.             return true;
  64.         }
  65.        
  66.         public void Unregister(MarshalByRefObject obj)
  67.         {
  68.             BCLDebug.Trace("REMOTE", "ClientSponsor Unregister " + obj);
  69.            
  70.             ILease lease = null;
  71.             lock (sponsorTable) {
  72.                 lease = (ILease)sponsorTable[obj];
  73.             }
  74.             if (lease != null)
  75.                 lease.Unregister(this);
  76.         }
  77.        
  78.         // ISponsor method
  79.         public TimeSpan Renewal(ILease lease)
  80.         {
  81.             BCLDebug.Trace("REMOTE", "ClientSponsor Renewal " + m_renewalTime);
  82.             return m_renewalTime;
  83.         }
  84.        
  85.         public void Close()
  86.         {
  87.             BCLDebug.Trace("REMOTE", "ClientSponsor Close");
  88.             lock (sponsorTable) {
  89.                 IDictionaryEnumerator e = sponsorTable.GetEnumerator();
  90.                 while (e.MoveNext())
  91.                     ((ILease)e.Value).Unregister(this);
  92.                 sponsorTable.Clear();
  93.             }
  94.         }
  95.        
  96.         // Don't create a lease on the sponsor
  97.         public override object InitializeLifetimeService()
  98.         {
  99.             return null;
  100.         }
  101.        
  102.         ~ClientSponsor()
  103.         {
  104.             BCLDebug.Trace("REMOTE", "ClientSponsor Finalize");
  105.         }
  106.     }
  107. }

Developer Fusion