예제 #1
0
        /// <summary>
        /// 招到下一个下载请求
        /// </summary>
        protected virtual DownloadRequest GetNextDownloadRequest()
        {
            DownloadRequest bestRequest  = null;
            float           highestScore = float.MinValue;

            lock (m_requests.SyncRoot)
            {
                for (int i = m_requests.Count - 1; i >= 0; i--)
                {
                    DownloadRequest request = (DownloadRequest)m_requests[i];
                    if (request.IsDownloading)
                    {
                        continue;
                    }

                    float score = request.CalculateScore();
                    if (float.IsNegativeInfinity(score))
                    {
                        // Request is of no interest anymore, remove it
                        m_requests.RemoveAt(i);
                        request.Dispose();
                        continue;
                    }

                    if (score > highestScore)
                    {
                        highestScore = score;
                        bestRequest  = request;
                    }
                }
            }

            return(bestRequest);
        }
예제 #2
0
        /// <summary>
        /// 添加一个下载请求到当前下载队列
        /// </summary>
        public virtual void Add(DownloadRequest newRequest)
        {
            if (newRequest == null)
            {
                throw new NullReferenceException();
            }

            lock (m_requests.SyncRoot)
            {
                foreach (DownloadRequest request in m_requests)
                {
                    if (request.Key == newRequest.Key)
                    {
                        newRequest.Dispose();
                        return;
                    }
                }

                m_requests.Add(newRequest);

                if (m_requests.Count > MaxQueueLength)
                {
                    // Remove lowest scoring queued request
                    DownloadRequest leastImportantRequest = null;
                    float           lowestScore           = float.MinValue;

                    for (int i = m_requests.Count - 1; i >= 0; i--)
                    {
                        DownloadRequest request = (DownloadRequest)m_requests[i];
                        if (request.IsDownloading)
                        {
                            continue;
                        }

                        float score = request.CalculateScore();
                        if (score == float.MinValue)
                        {
                            // Request is of no interest anymore, remove it
                            m_requests.Remove(request);
                            request.Dispose();
                            return;
                        }

                        if (score < lowestScore)
                        {
                            lowestScore           = score;
                            leastImportantRequest = request;
                        }
                    }

                    if (leastImportantRequest != null)
                    {
                        m_requests.Remove(leastImportantRequest);
                        leastImportantRequest.Dispose();
                    }
                }
            }

            ServiceDownloadQueue();
        }
예제 #3
0
        /// <summary>
        /// 在下载完成时的回调函数
        /// </summary>
        internal void OnComplete(DownloadRequest request)
        {
            lock (m_requests.SyncRoot)
            {
                // Remove the finished item from queue
                m_requests.Remove(request);
                request.Dispose();
            }

            // Start next download
            ServiceDownloadQueue();
        }
예제 #4
0
 /// <summary>
 /// 清空所有下载请求
 /// </summary>
 /// <param name="owner"></param>
 public virtual void Clear(object owner)
 {
     lock (m_requests.SyncRoot)
     {
         for (int i = m_requests.Count - 1; i >= 0; i--)
         {
             DownloadRequest request = (DownloadRequest)m_requests[i];
             if (request.Owner == owner)
             {
                 m_requests.RemoveAt(i);
                 request.Dispose();
             }
         }
     }
     ServiceDownloadQueue();
 }
예제 #5
0
        /// <summary>
        /// 从下载队列中移除一个下载请求
        /// </summary>
        public virtual void Remove(DownloadRequest request)
        {
            lock (m_requests.SyncRoot)
            {
                for (int i = m_activeDownloads.Count - 1; i >= 0; i--)
                {
                    if (request == m_activeDownloads[i])
                    {
                        // Already downloading, let it finish
                        return;
                    }
                }

                m_requests.Remove(request);
            }
            request.Dispose();

            ServiceDownloadQueue();
        }