Exemplo n.º 1
0
        /// <summary>
        /// Releases expired host structures.
        /// </summary>
        public void TimerCallback()
        {
            int             now             = GenuineUtility.TickCount;
            HostInformation hostInformation = null;

            // the released host
            ArrayList hostsToDelete = new ArrayList();

            // the entries being deleted
            ArrayList urisToDelete = new ArrayList();

            lock (this.SyncRoot)
            {
                // through all registered hosts
                foreach (DictionaryEntry entry in this._hashtable)
                {
                    ArrayList hosts = (ArrayList)entry.Value;
                    for (int i = 0; i < hosts.Count;)
                    {
                        hostInformation = (HostInformation)hosts[i];

                        // if the time has run out
                        if (GenuineUtility.IsTimeoutExpired(hostInformation.ExpireTime, now) || hostInformation.IsDisposed)
                        {
                            // exclude the host
                            hosts.RemoveAt(i);
                            hostsToDelete.Add(hostInformation);

                            // check on entry excluding
                            if (hosts.Count <= 0)
                            {
                                urisToDelete.Add(entry.Key);
                            }
                            continue;
                        }

                        i++;
                    }
                }

                // it is very important to remove all references to the host before disposing it
                foreach (string key in urisToDelete)
                {
                    this._hashtable.Remove(key);
                }

                // dispose all hosts
                foreach (HostInformation hostInformationExcluded in hostsToDelete)
                {
                    // LOG:
                    BinaryLogWriter binaryLogWriter = GenuineLoggingServices.BinaryLogWriter;
                    if (binaryLogWriter != null && binaryLogWriter[LogCategory.HostInformation] > 0)
                    {
                        binaryLogWriter.WriteEvent(LogCategory.HostInformation, "KnownHosts.this[string]",
                                                   LogMessageType.HostInformationReferencesDisassociated, GenuineExceptions.Get_Debugging_GeneralWarning("The association between HostInformation and its URL or URI has been broken."),
                                                   null, hostInformationExcluded, null,
                                                   GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                   null, null, -1, 0, 0, 0, hostInformationExcluded.Uri, hostInformationExcluded.Url, null, null,
                                                   "The current HostInformation does not refer to \"{0}\" and \"{1}\" any longer.",
                                                   hostInformationExcluded.Uri == null ? string.Empty : hostInformationExcluded.Uri,
                                                   hostInformationExcluded.Url == null ? string.Empty : hostInformationExcluded.Url);
                    }

                    GenuineThreadPool.QueueUserWorkItem(new WaitCallback(this.ReleaseHostResources), new HostInformationAndReason(hostInformationExcluded, GenuineExceptions.Get_Channel_ClientDidNotReconnectWithinTimeOut(hostInformationExcluded.ToString())), true);
                }
            }
        }