private static void Process() { bool maxDownloadersExceed = false; lock (startedDownloadersSynchronizer) { } if (QueueCountEmpty() || maxDownloadersExceed) { Thread.Sleep(50); return; } TileDownloader downloader = GetDownloader(); if (downloader != null) { lock (startedDownloadersSynchronizer) { if (!startedDownloaders.ContainsKey(downloader)) { startedDownloaders.Add(downloader, true); } } ThreadPool.QueueUserWorkItem(new WaitCallback(StartDownloadQueue), downloader); } }
internal static void RemoveDownloader(TileDownloader downloader) { lock (startedDownloadersSynchronizer) { startedDownloaders.Remove(downloader); } }
internal static void EnqueueDownload(TileDownloader downloader, bool active) { lock (startedDownloadersSynchronizer) { startedDownloaders.Remove(downloader); } if (active) { lock (activeQueueSynchronizer) { activeQueue.Enqueue(downloader); } } else { lock (problematicTilesSynchronizer) { downloadingComplete = false; problematicTilesQueue.Enqueue(downloader); Thread.Sleep(100); } } processRequest.Set(); }
private static TileDownloader GetDownloader() { TileDownloader downloader = null; lock (activeQueueSynchronizer) { if (activeQueue.Count > 0) { downloader = activeQueue.Dequeue(); } } if (downloader == null) { lock (problematicTilesSynchronizer) { if (problematicTilesQueue.Count > 0) { downloader = problematicTilesQueue.Dequeue(); } } } return(downloader); }
private void AssyncImageReader(TileDownloader tileDownloader) { if (tileDownloader.Uri != null) { this.CacheTile(tileDownloader.TileId.Level, tileDownloader.TileId.X, tileDownloader.TileId.Y, tileDownloader.Uri, tileDownloader.Expires, tileDownloader.TileBody); } this.AssyncImageReaderComplete(tileDownloader); }
private void AssyncImageReaderComplete(TileDownloader downloader) { TileDownloadState e = new TileDownloadState() { TileId = downloader.TileId, TileBody = downloader.TileBody }; downloader.Dispose(); this.UpdateTile(e); }
internal void DownloadTile(TileId tileId) { IList <object> objects = null; using (ManualResetEvent complete = new ManualResetEvent(false)) { this.Dispatcher.BeginInvoke(new Action(() => { objects = this.GetTileLayers(tileId.Level, tileId.X, tileId.Y); complete.Set(); })); complete.WaitOne(); } if (objects.Count > 0) { Uri uri = objects[0] as Uri; Stream stream = objects[0] as Stream; if (uri != null) { TileDownloadState e = new TileDownloadState() { TileId = tileId, Uri = uri, Callback = this.AssyncImageCacheReader }; this.GetCachedTileAsync(tileId.Level, tileId.X, tileId.Y, e.RaiseEvent); } else if (stream != null) { this.Dispatcher.BeginInvoke(new Action(() => { TileDownloader tile = new TileDownloader(tileId, stream, this.AssyncImageReader); tile.StartDownload(); })); } else { TileDownloadState e = new TileDownloadState() { TileId = tileId }; this.UpdateTile(e); } } }
private void AssyncImageCacheReader(TileDownloadState e) { TilesDownloadManager manager = this.downloadManager; if (manager != null) { if (e.TileBody == null) { TileDownloader downloader = new TileDownloader(e.TileId, e.Uri, this.AssyncImageReader, manager.ValidateTile); downloader.StartDownload(this.RequestCacheLevel, this.RequestCredentials, this.HttpUnauthorizedHandler); } else { this.UpdateTile(e); } } }
private static void ReadTileCallback(IAsyncResult asyncResult) { TileDownloader tile = (TileDownloader)asyncResult.AsyncState; int loaded = tile.stream.EndRead(asyncResult); if (loaded > 0) { tile.memoryStream.Write(tile.tileBodyPart, 0, loaded); tile.stream.BeginRead(tile.tileBodyPart, 0, TileBodyPartLength, ReadTileCallback, tile); return; } tile.tileBody = tile.memoryStream.ToArray(); tile.memoryStream.Dispose(); tile.memoryStream = null; tile.stream.Dispose(); tile.stream = null; tile.tileBodyPart = null; tile.readerCallback(tile); }