Exemplo n.º 1
0
        void ReadCallback(IAsyncResult result)
        {
            GetTileData data = (GetTileData)result.AsyncState;

            try
            {
                int read = data.ResponseStream.EndRead(result);
                if (read > 0)
                {
                    data.MemoryStream.Write(data.Buffer, 0, read);
                    data.ResponseStream.BeginRead(data.Buffer, 0, data.Buffer.Length, new AsyncCallback(ReadCallback), data);
                }
                else
                {
                    using (data)
                    {
                        string tilePath = GetTilePathForKey(data.Key);
                        using (FileStream file = new FileStream(tilePath, FileMode.Create, FileAccess.Write))
                        {
                            file.Write(data.MemoryStream.GetBuffer(), 0, (int)data.MemoryStream.Length);
                        }

                        data.MemoryStream.Seek(0, SeekOrigin.Begin);
                        IMapDrawable pbitmap = data.Renderer.GetBitmapFromStream(this, data.MemoryStream);
                        if (pbitmap == null)
                        {
                            throw new Exception();
                        }
                        TileCache[data.Key] = new TileData(pbitmap);
                        data.Callback(data.State);
                    }
                }
            }
            catch (Exception e)
            {
                CleanupTileData(data);
            }
        }
Exemplo n.º 2
0
        void GetTile(object o)
        {
            using (GetTileData data = (GetTileData)o)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(data.Uri);
                    request.Timeout   = 15000;
                    request.Method    = "GET";
                    request.UserAgent = "Windows-RSS-Platform/1.0 (MSIE 7.0; Windows NT 5.1)";

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new Exception("Error while downloading tile.");
                        }

                        if (TileCache != null)
                        {
                            using (Stream s = response.GetResponseStream())
                            {
                                using (MemoryStream mem = new MemoryStream())
                                {
                                    int    read   = 0;
                                    byte[] buffer = new byte[10000];
                                    do
                                    {
                                        read = s.Read(buffer, 0, buffer.Length);
                                        mem.Write(buffer, 0, read);
                                    }while (read != 0);
                                    mem.Seek(0, SeekOrigin.Begin);

                                    string tilePath = GetTilePathForKey(data.Key);
                                    using (FileStream file = new FileStream(tilePath, FileMode.Create, FileAccess.Write))
                                    {
                                        file.Write(mem.GetBuffer(), 0, (int)mem.Length);
                                    }

                                    IMapDrawable pbitmap = data.Renderer.GetBitmapFromStream(this, mem);
                                    if (pbitmap == null)
                                    {
                                        throw new Exception();
                                    }
                                    TileCache[data.Key] = new TileData(pbitmap);
                                }
                            }
                            if (data.Callback != null)
                            {
                                data.Callback(data.State);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (TileCache != null)
                    {
                        //TileCache.Remove(data.Key);
                        TileCache[data.Key] = InvalidTile;
                    }
                }
            }
        }