We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Runtime.Remoting.Lifetime
- {
-
- using System;
- using System.Threading;
- using System.Security;
- using System.Security.Permissions;
- using System.Runtime.Remoting.Contexts;
- using System.Runtime.Remoting.Messaging;
- using System.Globalization;
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
- [System.Runtime.InteropServices.ComVisible(true)]
- public sealed class LifetimeServices
- {
-
- private static bool isLeaseTime = false;
- private static bool isRenewOnCallTime = false;
- private static bool isSponsorshipTimeout = false;
-
-
- private static TimeSpan m_leaseTime = TimeSpan.FromMinutes(5);
- private static TimeSpan m_renewOnCallTime = TimeSpan.FromMinutes(2);
- private static TimeSpan m_sponsorshipTimeout = TimeSpan.FromMinutes(2);
- private static TimeSpan m_pollTime = TimeSpan.FromMilliseconds(10000);
-
-
-
-
-
-
-
- private static object s_LifetimeSyncObject = null;
-
- private static object LifetimeSyncObject {
- get {
- if (s_LifetimeSyncObject == null) {
- object o = new object();
- Interlocked.CompareExchange(ref s_LifetimeSyncObject, o, null);
- }
- return s_LifetimeSyncObject;
- }
- }
-
-
- public static TimeSpan LeaseTime {
- get { return m_leaseTime; }
-
- [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
- set {
- lock (LifetimeSyncObject) {
- if (isLeaseTime)
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Lifetime_SetOnce"), "LeaseTime"));
-
-
- m_leaseTime = value;
- isLeaseTime = true;
- }
- }
- }
-
-
-
- public static TimeSpan RenewOnCallTime {
-
- get { return m_renewOnCallTime; }
- [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
- set {
- lock (LifetimeSyncObject) {
- if (isRenewOnCallTime)
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Lifetime_SetOnce"), "RenewOnCallTime"));
-
-
- m_renewOnCallTime = value;
- isRenewOnCallTime = true;
- }
- }
- }
-
-
-
-
- public static TimeSpan SponsorshipTimeout {
-
- get { return m_sponsorshipTimeout; }
- [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
- set {
- lock (LifetimeSyncObject) {
- if (isSponsorshipTimeout)
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Lifetime_SetOnce"), "SponsorshipTimeout"));
- m_sponsorshipTimeout = value;
- isSponsorshipTimeout = true;
- }
- }
- }
-
-
-
-
- public static TimeSpan LeaseManagerPollTime {
-
- get { return m_pollTime; }
- [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
- set {
- lock (LifetimeSyncObject) {
- m_pollTime = value;
- if (LeaseManager.IsInitialized())
- LeaseManager.GetLeaseManager().ChangePollTime(m_pollTime);
- }
- }
- }
-
-
-
-
-
- static internal ILease GetLeaseInitial(MarshalByRefObject obj)
- {
- ILease lease = null;
- LeaseManager leaseManager = LeaseManager.GetLeaseManager(LeaseManagerPollTime);
- lease = (ILease)leaseManager.GetLease(obj);
- if (lease == null)
- lease = CreateLease(obj);
- return lease;
-
- }
-
-
-
- static internal ILease GetLease(MarshalByRefObject obj)
- {
- ILease lease = null;
- LeaseManager leaseManager = LeaseManager.GetLeaseManager(LeaseManagerPollTime);
- lease = (ILease)leaseManager.GetLease(obj);
- return lease;
-
- }
-
-
-
-
-
-
-
- static internal ILease CreateLease(MarshalByRefObject obj)
- {
- return CreateLease(LeaseTime, RenewOnCallTime, SponsorshipTimeout, obj);
-
- }
-
-
-
- static internal ILease CreateLease(TimeSpan leaseTime, TimeSpan renewOnCallTime, TimeSpan sponsorshipTimeout, MarshalByRefObject obj)
- {
-
- LeaseManager.GetLeaseManager(LeaseManagerPollTime);
- return (ILease)(new Lease(leaseTime, renewOnCallTime, sponsorshipTimeout, obj));
-
- }
-
- }
-
-
- [Serializable()]
- internal class LeaseLifeTimeServiceProperty : IContextProperty, IContributeObjectSink
- {
-
-
- public string Name {
-
- get { return "LeaseLifeTimeServiceProperty"; }
- }
-
-
-
-
- public bool IsNewContextOK(Context newCtx)
- {
- return true;
-
- }
-
-
-
- public void Freeze(Context newContext)
- {
-
- }
-
-
-
-
-
-
-
- public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)
- {
- bool fServer;
- ServerIdentity identity = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);
- BCLDebug.Assert(identity != null, "[LifetimeServices.GetObjectSink] identity != null");
-
-
-
-
-
- if (identity.IsSingleCall()) {
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, no lease SingleCall", obj, ", NextSink " + nextSink);
- return nextSink;
- }
-
-
-
-
-
- object leaseObj = obj.InitializeLifetimeService();
-
-
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, return from InitializeLifetimeService obj ", obj, ", lease ", leaseObj);
-
-
-
-
-
-
-
-
- if (leaseObj == null) {
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, no lease ", obj, ", NextSink " + nextSink);
- return nextSink;
- }
-
- if (!(leaseObj is System.Runtime.Remoting.Lifetime.ILease))
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Lifetime_ILeaseReturn"), leaseObj));
-
- ILease ilease = (ILease)leaseObj;
-
- if (ilease.InitialLeaseTime.CompareTo(TimeSpan.Zero) <= 0) {
-
- {
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, no lease because InitialLeaseTime is Zero ", obj);
- if (ilease is System.Runtime.Remoting.Lifetime.Lease) {
- ((Lease)ilease).Remove();
- }
- return nextSink;
- }
- }
-
-
- Lease lease = null;
- lock (identity) {
- if (identity.Lease != null) {
-
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, Lease already exists for object ", obj);
- lease = (Lease)identity.Lease;
- lease.Renew(lease.InitialLeaseTime);
-
- }
- else {
-
- if (!(ilease is System.Runtime.Remoting.Lifetime.Lease)) {
-
-
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, New Lease, lease not of type Lease ", obj);
- lease = (Lease)LifetimeServices.GetLeaseInitial(obj);
- if (lease.CurrentState == LeaseState.Initial) {
- lease.InitialLeaseTime = ilease.InitialLeaseTime;
- lease.RenewOnCallTime = ilease.RenewOnCallTime;
- lease.SponsorshipTimeout = ilease.SponsorshipTimeout;
- }
- }
- else {
-
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, New Lease, lease is type Lease ", obj);
- lease = (Lease)ilease;
- }
-
-
-
- identity.Lease = lease;
-
-
-
- if (identity.ObjectRef != null) {
- lease.ActivateLease();
- }
- }
- }
-
-
- if (lease.RenewOnCallTime > TimeSpan.Zero) {
-
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, lease created ", obj);
- return new LeaseSink(lease, nextSink);
- }
- else {
-
- BCLDebug.Trace("REMOTE", "LeaseLifeTimeServiceProperty.GetObjectSink, No RenewOnCall so no sink created ", obj);
- return nextSink;
- }
-
- }
-
- }
-
- }