private void RequestCompleted(Artwork aArtwork) { iMutexRequest.WaitOne(); if (iPendingList.Count > 0) { iManualResetEvent.Set(); } iMutexRequest.ReleaseMutex(); iSemaphore.Release(); }
internal void Download(Artwork aArtwork) { iMutexRequest.WaitOne(); iPendingList.Remove(aArtwork); if (!aArtwork.Error && aArtwork.Image == null) { iPendingList.Insert(0, aArtwork); iManualResetEvent.Set(); } iMutexRequest.ReleaseMutex(); }
private void ProcessRequest(Artwork aArtwork) { Trace.WriteLine(Trace.kKinsky, "ArtworkCache.ProcessRequest: Downloading " + aArtwork.UriDownload); try { HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(aArtwork.UriDownload); wreq.Proxy = new WebProxy(); wreq.KeepAlive = false; wreq.Pipelined = false; wreq.AllowWriteStreamBuffering = true; WebRequestPool.QueueJob(new JobGetResponse(GetResponseCallback, new WebRequestState(wreq, aArtwork))); } catch (Exception e) { DownloadError(aArtwork, e.Message); } }
public IArtwork Artwork(Uri aUri) { Assert.Check(aUri != null); string query = aUri.Query; System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(string.Format("(?'before'([?&]{0}=)[0-9][0-9]*)", kSizeQuery)); if (r.IsMatch(query)) { query = r.Replace(query, string.Format("?{0}={1}", kSizeQuery, iMaxImageSize)); } Uri uri = aUri; if (!string.IsNullOrEmpty(aUri.Query)) { uri = new Uri(aUri.AbsoluteUri.Replace(aUri.Query, query)); } iMutex.WaitOne(); Artwork value; if (iArtworkCache.TryGetValue(uri.AbsoluteUri, out value)) { iArtworkCacheUsage.Remove(uri.AbsoluteUri); iArtworkCacheUsage.Add(uri.AbsoluteUri); iMutex.ReleaseMutex(); return value; } value = new Artwork(aUri, uri); iArtworkCache.Add(value.UriDownload.AbsoluteUri, value); iArtworkCacheUsage.Add(value.UriDownload.AbsoluteUri); RemoveStaleCacheItems(); iMutex.ReleaseMutex(); Download(value); return value; }
private void GetResponseCallback(object aResult) { WebRequestState wreq = aResult as WebRequestState; Artwork artwork = wreq.Artwork; try { HttpWebResponse wresp = (HttpWebResponse)wreq.Request.GetResponse(); try { Stream stream = wresp.GetResponseStream(); if (stream != null) { try { Bitmap image = new Bitmap(stream); try { Image scaledImage = ScaleImage(image); Image reflectedImage = ReflectImage(scaledImage); artwork.Image = scaledImage; artwork.ImageReflected = reflectedImage; artwork.Error = false; } catch (Exception e) { DownloadError(artwork, e.Message); return; } image.Dispose(); if (EventUpdated != null) { EventUpdated(this, new EventArgsArtwork(artwork)); } RequestCompleted(artwork); return; } catch (Exception e) { DownloadError(artwork, e.Message); return; } finally { stream.Close(); } } } catch (Exception e) { DownloadError(artwork, e.Message); return; } finally { if (wresp != null) { wresp.Close(); } } } catch (WebException e) { DownloadError(artwork, e.Message); return; } DownloadError(artwork, "Unknown error"); }
public WebRequestState(HttpWebRequest aRequest, Artwork aArtwork) { Request = aRequest; Artwork = aArtwork; }