/// <summary>Check the queue for pending work</summary>
        private void EnqueuePending()
        {
            lock (queue)
            {
                if (queue.Count > 0)
                {
                    int nr = 0;
                    lock (activeDownloads)
                    {
                        nr = activeDownloads.Count;
                    }

                    Logger.DebugLog(nr.ToString() + " active downloads. Queued textures: " + queue.Count.ToString());

                    for (int i = nr; i < ParallelDownloads && queue.Count > 0; i++)
                    {
                        DownloadRequest item = queue.Dequeue();
                        lock (activeDownloads)
                        {
                            string addr = item.Address.ToString();
                            if (activeDownloads.ContainsKey(addr))
                            {
                                activeDownloads[addr].CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownloads[addr].ProgresHadlers.Add(item.DownloadProgressCallback);
                                }
                            }
                            else
                            {
                                ActiveDownload activeDownload = new ActiveDownload();
                                activeDownload.CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownload.ProgresHadlers.Add(item.DownloadProgressCallback);
                                }

                                Logger.DebugLog("Requesting " + item.Address.ToString());
                                activeDownload.Request = SetupRequest(item.Address, item.ContentType);
                                CapsBase.DownloadDataAsync(
                                    activeDownload.Request,
                                    item.MillisecondsTimeout,
                                    (HttpWebRequest request, HttpWebResponse response, int bytesReceived, int totalBytesToReceive) =>
                                {
                                    foreach (CapsBase.DownloadProgressEventHandler handler in activeDownload.ProgresHadlers)
                                    {
                                        handler(request, response, bytesReceived, totalBytesToReceive);
                                    }
                                },
                                    (HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error) =>
                                {
                                    lock (activeDownloads) activeDownloads.Remove(addr);
                                    if (error == null || item.Attempt >= item.Retries || (error != null && error.Message.Contains("404")))
                                    {
                                        foreach (CapsBase.RequestCompletedEventHandler handler in activeDownload.CompletedHandlers)
                                        {
                                            handler(request, response, responseData, error);
                                        }
                                    }
                                    else
                                    {
                                        item.Attempt++;
                                        Logger.Log(string.Format("Texture {0} HTTP download failed, trying again retry {1}/{2}",
                                                                 item.Address, item.Attempt, item.Retries), Helpers.LogLevel.Warning);
                                        lock (queue) queue.Enqueue(item);
                                    }

                                    EnqueuePending();
                                }
                                    );

                                activeDownloads[addr] = activeDownload;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>Check the queue for pending work</summary>
        private void EnqueuePending()
        {
            lock (queue)
            {
                if (queue.Count > 0)
                {
                    int nr = 0;
                    lock (activeDownloads)
                    {
                        nr = activeDownloads.Count;
                    }

                    for (int i = nr; i < ParallelDownloads && queue.Count > 0; i++)
                    {
                        DownloadRequest item = queue.Dequeue();
                        lock (activeDownloads)
                        {
                            string addr = item.Address.ToString();
                            if (activeDownloads.ContainsKey(addr))
                            {
                                activeDownloads[addr].CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownloads[addr].ProgresHadlers.Add(item.DownloadProgressCallback);
                                }
                            }
                            else
                            {
                                ActiveDownload activeDownload = new ActiveDownload();
                                activeDownload.CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownload.ProgresHadlers.Add(item.DownloadProgressCallback);
                                }

                                Logger.DebugLog("Requesting " + item.Address.ToString());
                                activeDownload.Request = SetupRequest(item.Address, item.ContentType);
                                CapsBase.DownloadDataAsync(
                                    activeDownload.Request,
                                    item.MillisecondsTimeout,
                                    (HttpWebRequest request, HttpWebResponse response, int bytesReceived, int totalBytesToReceive) =>
                                    {
                                        foreach (CapsBase.DownloadProgressEventHandler handler in activeDownload.ProgresHadlers)
                                        {
                                            handler(request, response, bytesReceived, totalBytesToReceive);
                                        }
                                    },
                                    (HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error) =>
                                    {
                                        lock (activeDownloads) activeDownloads.Remove(addr);
                                        foreach (CapsBase.RequestCompletedEventHandler handler in activeDownload.CompletedHandlers)
                                        {
                                            handler(request, response, responseData, error);
                                        }
                                        EnqueuePending();
                                    }
                                );

                                activeDownloads[addr] = activeDownload;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>Check the queue for pending work</summary>
        private void EnqueuePending()
        {
            lock (queue)
            {
                if (queue.Count > 0)
                {
                    int nr = 0;
                    lock (activeDownloads)
                    {
                        nr = activeDownloads.Count;
                    }

                    // Logger.DebugLog(nr.ToString() + " active downloads. Queued textures: " + queue.Count.ToString());

                    for (int i = nr; i < ParallelDownloads && queue.Count > 0; i++)
                    {
                        DownloadRequest item = queue.Dequeue();
                        lock (activeDownloads)
                        {
                            string addr = item.Address.ToString();
                            if (activeDownloads.ContainsKey(addr))
                            {
                                activeDownloads[addr].CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownloads[addr].ProgresHadlers.Add(item.DownloadProgressCallback);
                                }
                            }
                            else
                            {
                                ActiveDownload activeDownload = new ActiveDownload();
                                activeDownload.CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownload.ProgresHadlers.Add(item.DownloadProgressCallback);
                                }

                                Logger.DebugLog("Requesting " + item.Address.ToString());
                                activeDownload.Request = SetupRequest(item.Address, item.ContentType);
                                CapsBase.DownloadDataAsync(
                                    activeDownload.Request,
                                    item.MillisecondsTimeout,
                                    (HttpWebRequest request, HttpWebResponse response, int bytesReceived, int totalBytesToReceive) =>
                                    {
                                        foreach (CapsBase.DownloadProgressEventHandler handler in activeDownload.ProgresHadlers)
                                        {
                                            handler(request, response, bytesReceived, totalBytesToReceive);
                                        }
                                    },
                                    (HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error) =>
                                    {
                                        lock (activeDownloads) activeDownloads.Remove(addr);
                                        if (error == null || item.Attempt >= item.Retries || (error != null && error.Message.Contains("404")))
                                        {
                                            foreach (CapsBase.RequestCompletedEventHandler handler in activeDownload.CompletedHandlers)
                                            {
                                                handler(request, response, responseData, error);
                                            }
                                        }
                                        else
                                        {
                                            item.Attempt++;
                                            Logger.Log(string.Format("Texture {0} HTTP download failed, trying again retry {1}/{2}",
                                                item.Address, item.Attempt, item.Retries), Helpers.LogLevel.Warning);
                                            lock (queue) queue.Enqueue(item);
                                        }

                                        EnqueuePending();
                                    }
                                );

                                activeDownloads[addr] = activeDownload;
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>Check the queue for pending work</summary>
        private void EnqueuePending()
        {
            lock (queue)
            {
                if (queue.Count > 0)
                {
                    int nr = 0;
                    lock (activeDownloads)
                    {
                        nr = activeDownloads.Count;
                    }

                    for (int i = nr; i < ParallelDownloads && queue.Count > 0; i++)
                    {
                        DownloadRequest item = queue.Dequeue();
                        lock (activeDownloads)
                        {
                            string addr = item.Address.ToString();
                            if (activeDownloads.ContainsKey(addr))
                            {
                                activeDownloads[addr].CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownloads[addr].ProgresHadlers.Add(item.DownloadProgressCallback);
                                }
                            }
                            else
                            {
                                ActiveDownload activeDownload = new ActiveDownload();
                                activeDownload.CompletedHandlers.Add(item.CompletedCallback);
                                if (item.DownloadProgressCallback != null)
                                {
                                    activeDownload.ProgresHadlers.Add(item.DownloadProgressCallback);
                                }

                                Logger.DebugLog("Requesting " + item.Address.ToString());
                                activeDownload.Request = SetupRequest(item.Address, item.ContentType);
                                CapsBase.DownloadDataAsync(
                                    activeDownload.Request,
                                    item.MillisecondsTimeout,
                                    (HttpWebRequest request, HttpWebResponse response, int bytesReceived, int totalBytesToReceive) =>
                                {
                                    foreach (CapsBase.DownloadProgressEventHandler handler in activeDownload.ProgresHadlers)
                                    {
                                        handler(request, response, bytesReceived, totalBytesToReceive);
                                    }
                                },
                                    (HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error) =>
                                {
                                    lock (activeDownloads) activeDownloads.Remove(addr);
                                    foreach (CapsBase.RequestCompletedEventHandler handler in activeDownload.CompletedHandlers)
                                    {
                                        handler(request, response, responseData, error);
                                    }
                                    EnqueuePending();
                                }
                                    );

                                activeDownloads[addr] = activeDownload;
                            }
                        }
                    }
                }
            }
        }