Ejemplo n.º 1
0
        // On another thread
        internal void SponsorCallback(IAsyncResult iar)
        {
            BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback IAsyncResult ", iar, " state ", ((Enum)state).ToString());
            if (state == LeaseState.Expired)
            {
                return;
            }

            int thisThread = Thread.CurrentThread.GetHashCode();

            if (thisThread == sponsorCallThread)
            {
                WaitCallback threadFunc = new WaitCallback(this.SponsorCallback);
                ThreadPool.QueueUserWorkItem(threadFunc, iar);
                return;
            }

            AsyncResult      asyncResult      = (AsyncResult)iar;
            AsyncRenewal     ar               = (AsyncRenewal)asyncResult.AsyncDelegate;
            ISponsor         sponsor          = (ISponsor)ar.Target;
            SponsorStateInfo sponsorStateInfo = null;

            if (iar.IsCompleted)
            {
                // Sponsor came back with renewal
                BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback sponsor completed");
                bool     exceptionOccurred = false;
                TimeSpan renewalTime       = TimeSpan.Zero;
                try
                {
                    renewalTime = (TimeSpan)ar.EndInvoke(iar);
                }catch (Exception)
                {
                    // Sponsor not avaiable
                    exceptionOccurred = true;
                }
                if (exceptionOccurred)
                {
                    BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback Sponsor Exception ");
                    Unregister(sponsor);
                    ProcessNextSponsor();
                }
                else
                {
                    Object sponsorId = GetSponsorId(sponsor);
                    lock (sponsorTable)
                    {
                        if (sponsorTable.ContainsKey(sponsorId))
                        {
                            sponsorStateInfo = (SponsorStateInfo)sponsorTable[sponsorId];
                            sponsorStateInfo.sponsorState = SponsorState.Completed;
                            sponsorStateInfo.renewalTime  = renewalTime;
                        }
                        else
                        {
                            // Sponsor was deleted, possibly from a sponsor time out
                        }
                    }

                    if (sponsorStateInfo == null)
                    {
                        // Sponsor was deleted
                        ProcessNextSponsor();
                    }
                    else if (sponsorStateInfo.renewalTime == TimeSpan.Zero)
                    {
                        BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback sponsor did not renew ");
                        Unregister(sponsor);
                        ProcessNextSponsor();
                    }
                    else
                    {
                        Renew(sponsorStateInfo.renewalTime);
                    }
                }
            }
            else
            {
                // Sponsor timed out
                // Note time outs should be handled by the LeaseManager
                BCLDebug.Trace("REMOTE", "Lease ", id, " SponsorCallback sponsor did not complete, timed out");
                Unregister(sponsor);
                ProcessNextSponsor();
            }
        }
Ejemplo n.º 2
0
 internal void SponsorCallback(IAsyncResult iar)
 {
     if (this.state != LeaseState.Expired)
     {
         if (Thread.CurrentThread.GetHashCode() == this.sponsorCallThread)
         {
             WaitCallback callBack = new WaitCallback(this.SponsorCallback);
             ThreadPool.QueueUserWorkItem(callBack, iar);
         }
         else
         {
             AsyncResult      result        = (AsyncResult)iar;
             AsyncRenewal     asyncDelegate = (AsyncRenewal)result.AsyncDelegate;
             ISponsor         target        = (ISponsor)asyncDelegate.Target;
             SponsorStateInfo info          = null;
             if (iar.IsCompleted)
             {
                 bool     flag = false;
                 TimeSpan zero = TimeSpan.Zero;
                 try
                 {
                     zero = asyncDelegate.EndInvoke(iar);
                 }
                 catch (Exception)
                 {
                     flag = true;
                 }
                 if (flag)
                 {
                     this.Unregister(target);
                     this.ProcessNextSponsor();
                 }
                 else
                 {
                     object sponsorId = this.GetSponsorId(target);
                     lock (this.sponsorTable)
                     {
                         if (this.sponsorTable.ContainsKey(sponsorId))
                         {
                             info = (SponsorStateInfo)this.sponsorTable[sponsorId];
                             info.sponsorState = SponsorState.Completed;
                             info.renewalTime  = zero;
                         }
                     }
                     if (info == null)
                     {
                         this.ProcessNextSponsor();
                     }
                     else if (info.renewalTime == TimeSpan.Zero)
                     {
                         this.Unregister(target);
                         this.ProcessNextSponsor();
                     }
                     else
                     {
                         this.RenewInternal(info.renewalTime);
                     }
                 }
             }
             else
             {
                 this.Unregister(target);
                 this.ProcessNextSponsor();
             }
         }
     }
 }