예제 #1
0
            public async ValueTask <byte[]> Get(int freshness, int timeout)
            {
                bool queryOwner = false;

                try
                {
                    if (LastUpdate.AddSeconds(freshness) < DateTime.Now)
                    {
                        Logger.Log(LogSeverity.Info, LogType.Downloader, "Updating WebCache", "CachedDownloader");

                        if (WebQuery == null)
                        {
                            queryOwner = true;

                            Data = null;

                            WebQuery = new Task(() =>
                            {
                                using (var wc = new WebClient())
                                {
                                    Task.Run(async() =>
                                    {
                                        await Task.Delay(timeout);
                                        if (wc != null)
                                        {
                                            wc.CancelAsync();
                                        }
                                    });
                                    Data = wc.DownloadData(Location);
                                    Logger.Log(LogSeverity.Info, LogType.Downloader, "Download Complete", "CachedDownloader");
                                }
                                LastUpdate = DateTime.Now;
                            });
                            WebQuery.Start();
                        }

                        Logger.Log(LogSeverity.Info, LogType.Downloader, "Waiting for download", "CachedDownloader");
                        await WebQuery;
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Status != WebExceptionStatus.RequestCanceled)
                    {
                        Logger.Log(ex, "CachedDownloader");
                    }
                    else
                    {
                        Logger.Log(LogSeverity.Info, LogType.Downloader, "Download cancelled", "CachedDownloader");
                    }
                }
                finally
                {
                    if (queryOwner)
                    {
                        WebQuery = null;
                    }
                }

                return(Data);
            }