public void Start()
 {
     if (_worker != null)
     {
         _worker.Start();
     }
     TryDoFails();
     _source.EnumerateTileRange(_mapConfig.LastTile,
                                (zoom) =>
     {
         _tilePathBuilder.BuildZoomFold(zoom, _mapConfig.OffsetZoom);
     },
                                (tile) =>
     {
         _worker.TryQueue(new TileCoordWrap
         {
             Tile      = tile,
             OnSuccess = null,
             OnFailed  = (tile1, ex) =>
             {
                 _failsProvider.Insert(tile1);
                 Console.WriteLine(ex.Message);
             },
             OnFinally = (tile2) =>
             {
                 _mapConfig.LastTile = tile2;
                 if (_currTileIndex == TotalTile)
                 {
                     _mapConfig.Save();
                     OnFinished();
                 }
             }
         });
     });
 }
Example #2
0
        public void Start()
        {
            if (!_isStop)
            {
                _blockingQueue = new BlockingCollection <TileCoord>(_threadCount);
                _tokenSource   = new CancellationTokenSource();
                var token       = _tokenSource.Token;
                var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning);

                for (int i = 0; i < _threadCount; i++)
                {
                    Task workThread = taskFactory.StartNew(() =>
                    {
                        while (!_isStop)
                        {
                            token.ThrowIfCancellationRequested();

                            TileCoord tileCoord = null;
                            if (_blockingQueue.TryTake(out tileCoord, 100))
                            //tileCoord = _blockingQueue.Take();
                            //if (tileCoord != null)
                            {
                                using (Stream stream = _source.GetTile(tileCoord))
                                {
                                    string filePath = _tilePathBuilder.BuildTilePath(tileCoord);
                                    using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                                    {
                                        int ret       = 0;
                                        byte[] buffer = new byte[8192];//8K
                                        ret           = stream.Read(buffer, 0, buffer.Length);
                                        while (ret > 0)
                                        {
                                            fs.Write(buffer, 0, ret);
                                            ret = stream.Read(buffer, 0, buffer.Length);
                                        }
                                        OnTileLoaded(tileCoord);
                                    }
                                }
                            }
                            //else
                            //{
                            //    token.WaitHandle.WaitOne(100);
                            //}
                        }
                    }, token);
                }

                List <Extent> fullTileRange = _source.TileGrid.TileRanges;
                for (int z = 0; z < fullTileRange.Count; z++)
                {
                    _tilePathBuilder.BuildZoomFold(z, _offsetZoom);

                    for (double x = fullTileRange[z].MinX; x <= fullTileRange[z].MaxX; ++x)
                    {
                        for (double y = fullTileRange[z].MinY; y <= fullTileRange[z].MaxY; ++y)
                        {
                            var tile = new TileCoord(z, x, y);
                            //_blockingQueue.TryAdd(tile, 500);
                            _blockingQueue.Add(tile);
                        }
                    }
                }
            }
        }