public void Download()
        {
            NetworkProtocolProviderFactory networkProtocolProviderFactory = new NetworkProtocolProviderFactory(new UrlProtocolParser());
            HttpProtocolProvider httpProtocolProvider = new HttpProtocolProvider(new WebRequestManager(new WebRequestFactory()));
            networkProtocolProviderFactory.RegisterProvider("http", httpProtocolProvider);
            networkProtocolProviderFactory.RegisterProvider("https", httpProtocolProvider);

            DownloadTask downloadTask = new DownloadTask(
                networkProtocolProviderFactory,
                new DownloadSegmentPositionsCalculator(),
                new MemoryDownloadStreamManager(),
                new InMemoryDownloadSettings(200, 5, 8096, 5, 5),
                new DownloadFileInfo(new Uri("https://androidnetworktester.googlecode.com/files/1mb.txt"), "1mb.txt", 5),
                new EventManager());

            downloadTask.EventManager.EventSubscriber.RegisterConsumer(new ActionEventConsumer<DownloadTaskFinishedEventMessage>(x =>
                {
                    string txt = Encoding.UTF8.GetString(((MemoryStream)x.DownloadStream).ToArray());
                    txt.ToString();
                }));

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            downloadTask.StartDownload();
            stopwatch.Stop();

            stopwatch.Elapsed.ToString();
        }
Example #2
0
 public static DownloadTask DownloadAsync(DownloadTask task, Priority priority = Priority.Normal)
 {
     task.TaskPriority = priority;
     Enqueue(task);
     DownloadNext();
     return task;
 }
Example #3
0
 void ThreadDownload(DownloadTask task)
 {
     task_ = task;
     for (int i = 0; i < 3; i++)
     {
         new Thread(new ThreadStart(Download)).Start();
     }
 }
Example #4
0
 static void Main(string[] args)
 {
     DownloadTask task = new DownloadTask();
     task.RemotePath = @"http://speed.myzone.cn/pc_elive_1.1.rar";
     task.LocalPath = @"d:\hehe.rar";
     task.BlockSize = 1024 * 1024 * 3;
     if (task.InitTask())
     {
         Console.WriteLine("begin download");
         Program p = new Program();
         p.ThreadDownload(task);
     }
     Console.ReadKey();
 }
Example #5
0
        public static DownloadTask GetCoreJarDownloadTask(DownloadSource downloadSource, Modules.Version version, LaunchHandler core)
        {
            string       to           = core.GetJarPath(version);
            string       from         = GetCoreJarDownloadURL(downloadSource, version);
            DownloadTask downloadTask = new DownloadTask("游戏版本核心Jar文件", from, to);

            if (!string.IsNullOrWhiteSpace(version.Downloads?.Client?.SHA1))
            {
                downloadTask.Checker = new SHA1Checker()
                {
                    CheckSum = version.Downloads.Client.SHA1, FilePath = to
                };
            }
            return(downloadTask);
        }
Example #6
0
        /// <summary>
        /// 获取Lib下载任务
        /// </summary>
        /// <param name="source">下载源</param>
        /// <param name="lib">lib实例</param>
        /// <param name="core">所使用的核心</param>
        /// <returns>下载任务</returns>
        public static DownloadTask GetLibDownloadTask(DownloadSource source, KeyValuePair <string, Library> lib)
        {
            string       from = GetLibDownloadURL(source, lib.Value);
            string       to   = lib.Key;
            DownloadTask task = new DownloadTask("版本依赖库文件" + lib.Value.Name, from, to);

            if (lib.Value.LibDownloadInfo != null)
            {
                task.Checker = new SHA1Checker()
                {
                    CheckSum = lib.Value.LibDownloadInfo.SHA1, FilePath = to
                };
            }
            return(task);
        }
Example #7
0
        static void Main(string[] args)
        {
            DownloadTask task = new DownloadTask();

            task.RemotePath = @"http://speed.myzone.cn/pc_elive_1.1.rar";
            task.LocalPath  = @"d:\hehe.rar";
            task.BlockSize  = 1024 * 1024 * 3;
            if (task.InitTask())
            {
                Console.WriteLine("begin download");
                Program p = new Program();
                p.ThreadDownload(task);
            }
            Console.ReadKey();
        }
 private void ThreadProc()
 {
     while (true)
     {
         DownloadTask task = null;
         if (m_taskQueue.TryDequeue(out task))
         {
             task.BeginDownload();
         }
         else
         {
             break;
         }
     }
 }
Example #9
0
        /// <summary>
        /// 获取Native下载任务
        /// </summary>
        /// <param name="source">下载源</param>
        /// <param name="native">native实例</param>
        /// <param name="core">所使用的核心</param>
        /// <returns>下载任务</returns>
        public static DownloadTask GetNativeDownloadTask(DownloadSource source, KeyValuePair <string, Native> native)
        {
            string       from = GetNativeDownloadURL(source, native.Value);
            string       to   = native.Key;
            DownloadTask task = new DownloadTask("版本系统依赖库文件" + native.Value.Name, from, to);

            if (native.Value.NativeDownloadInfo != null)
            {
                task.Checker = new SHA1Checker()
                {
                    CheckSum = native.Value.NativeDownloadInfo.SHA1, FilePath = to
                };
            }
            return(task);
        }
Example #10
0
        public async Task ResumingDownloadsWorks()
        {
            Stopwatch watch;
            ILogging  logger;

            StartTest(out watch, out logger);

            InitializeEnvironment(TestBasePath, false, false);

            var fileSystem = NPath.FileSystem;

            var baseUrl = new UriString($"http://localhost:{server.Port}/unity/git/windows");
            var package = Package.Load(Environment, baseUrl.ToString() + "/git-lfs.json");

            var downloadTask = new DownloadTask(TaskManager.Token, fileSystem, package.Uri, TestBasePath);

            StartTrackTime(watch, logger, package.Url);
            var task = await Task.WhenAny(downloadTask.Start().Task, Task.Delay(Timeout));

            StopTrackTimeAndLog(watch, logger);
            task.Should().BeEquivalentTo(downloadTask.Task);

            var downloadPath = await downloadTask.Task;

            Assert.NotNull(downloadPath);

            var downloadPathBytes = downloadPath.ReadAllBytes();

            Logger.Trace("File size {0} bytes", downloadPathBytes.Length);

            var cutDownloadPathBytes = downloadPathBytes.Take(downloadPathBytes.Length - 1000).ToArray();

            downloadPath.Delete();
            new NPath(downloadPath + ".partial").WriteAllBytes(cutDownloadPathBytes);

            downloadTask = new DownloadTask(TaskManager.Token, fileSystem, package.Uri, TestBasePath);

            StartTrackTime(watch, logger, package.Url);
            task = await Task.WhenAny(downloadTask.Start().Task, Task.Delay(Timeout));

            StopTrackTimeAndLog(watch, logger);
            task.Should().BeEquivalentTo(downloadTask.Task);
            downloadPath = await downloadTask.Task;

            var md5Sum = downloadPath.CalculateMD5();

            md5Sum.Should().BeEquivalentTo(package.Md5);
        }
 public void DownloadFileAsync(string pURL, Action <byte[]> pSuccessCallback, Action <string> pErrorCallback = null, Action <float> pProgressCallback = null)
 {
     if (m_pTaskCache.ContainsKey(pURL))
     {
         m_pTaskCache[pURL].successCallback(m_pTaskCache[pURL].bytes);
     }
     else
     {
         DownloadTask task = new DownloadTask();
         task.url              = pURL;
         task.successCallback  = pSuccessCallback;
         task.errorCallback    = pErrorCallback;
         task.progressCallback = pProgressCallback;
         m_pLoadingQueue.Enqueue(task);
     }
 }
Example #12
0
        private void DownloadFinished(DownloadTask task)
        {
            problemWebBrowser.Refresh();
            if (task.Error != null)
            {
                Logger.Add(task.Error.Message, "ProblemViewer | DownloadFinished()");
                return;
            }

            bool finish = false;
            long pnum   = (long)task.Token;

            if (current == null || current.pnum != pnum)
            {
                finish = true;
            }

            if (!finish) //if no error occured
            {
                string ext = Path.GetExtension(task.FileName);
                if (ext == ".pdf")
                {
                    System.Diagnostics.Process.Start(task.FileName);
                    finish = true;
                }
                else if (ext == ".html")
                {
                    string file = LocalDirectory.GetProblemHtml(pnum);
                    problemWebBrowser.Navigate(file);
                    int cnt = DownloadContents(pnum);
                    if (cnt == 0)
                    {
                        finish = true;
                    }
                }
                else
                {
                    finish = true;
                }
            }

            if (finish)
            {
                problemWebBrowser.Refresh();
                reloadButton.Enabled = true;
            }
        }
Example #13
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Updata);
            NewVerison.VersionCode        = Intent.GetCharSequenceExtra("VersionCode");
            NewVerison.VersionName        = Intent.GetCharSequenceExtra("VersionName");
            NewVerison.VersionDiscription = Intent.GetCharSequenceExtra("VersionDiscription");
            #region 设置ToolBar
            SetToolBar();
            #endregion
            TextView VerisonName = FindViewById <TextView>(Resource.Id.VerisonName);
            VerisonName.Text = NewVerison.VersionName;

            TextView VerisonDiscription = FindViewById <TextView>(Resource.Id.VerisonDiscription);
            VerisonDiscription.MovementMethod = ScrollingMovementMethod.Instance;
            VerisonDiscription.Text           = "";
            try
            {
                JObject jObject = (JObject)JsonConvert.DeserializeObject(NewVerison.VersionDiscription);
                JArray  jArray  = (JArray)jObject["VerisonDiscription"];
                foreach (var item in jArray)
                {
                    VerisonDiscription.Text += item.ToString() + "\r\n";
                }
            }
            catch
            {
                VerisonDiscription.Text = "获取更新描述失败";
            }
            #region  载更新按钮
            Button DownloadButton = FindViewById <Button>(Resource.Id.DownloadButton);
            DownloadButton.Click += (o, e) =>
            {
                DownloadTask downloadTask = new DownloadTask(this);
                downloadTask.Execute("https://github.com/UMI64/UinfoWork/raw/master/UinfoWork.UinfoWork.apk");
            };
            #endregion
            #region  载更新按钮
            TextView WebUpData = FindViewById <TextView>(Resource.Id.WebUpData);
            WebUpData.Click += (o, e) =>
            {
                Uri    uri    = Uri.Parse("https://github.com/UMI64/UinfoWork/raw/master/UinfoWork.UinfoWork.apk");
                Intent intent = new Intent(Intent.ActionView, uri);
                StartActivity(intent);
            };
            #endregion
        }
Example #14
0
    private void DownloadFinishWithMd5(DownloadTask task)
    {
        LoggerHelper.Debug("DownloadFinishWithMd51111");
        //验证MD5
#if UNITY_IPHONE
        //ios下如果封装该方法在一个函数中,调用该函数来产生文件的MD5的时候,就会抛JIT异常。
        //如果直接把这个产生MD5的方法体放在直接执行,就可以正常执行,这个原因还不清楚。
        string md5Compute = null;
        using (System.IO.FileStream fileStream = System.IO.File.OpenRead(task.FileName))
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] fileMD5Bytes = md5.ComputeHash(fileStream);
            md5Compute = System.BitConverter.ToString(fileMD5Bytes).Replace("-", "").ToLower();
        }
#else
        var md5Compute = Utils.BuildFileMd5(task.FileName);
#endif
        //md5验证失败
        if (md5Compute.Trim() != task.MD5.Trim())
        {
            //如果md5验证失败,删除原文件
            if (File.Exists(task.FileName))
            {
                File.Delete(task.FileName);
            }
            LoggerHelper.Error("断点MD5验证失败,从新下载:" + task.FileName + "--" + md5Compute + " vs " + task.MD5);
            task.bDownloadAgain = true;
            task.bFineshed      = false;
            CheckDownLoadList();
            return;
        }
        //所有通过验证就认为是下载完成
        LoggerHelper.Debug("断点下载验证全部通过,下载完成:" + task.FileName);
        if (FileDecompress != null)
        {
            FileDecompress(true);
        }
        task.bDownloadAgain = false;
        task.bFineshed      = true;
        task.Finished();
        if (FileDecompress != null)
        {
            FileDecompress(false);
        }
        LoggerHelper.Debug("断点下载完成后,再次核对下载列表");
        CheckDownLoadList();
    }
Example #15
0
        public void GivenValueInit_PropertyValueMustGiven()
        {
            var task = CreateTask(10, out var values);

            Assert.AreEqual(10, task.Max);
            Assert.IsFalse(task.IsDone);
            for (int i = 0; i < task.Tasks.Count; i++)
            {
                Assert.AreEqual(values[i], task.Tasks[i], i.ToString());
            }
            Assert.AreEqual(CancellationToken.None, task.CancellationToken);

            var tks = new CancellationTokenSource();

            task = new DownloadTask(values, tks.Token);
            Assert.AreEqual(tks.Token, task.CancellationToken);
        }
Example #16
0
    private void AsynDownloadApkNoExport(Action <int, int, string> taskProgress, string fileName, string url, string md5, Action <int, long, long> progress, Action finished, Action <Exception> error)
    {
        DownloadTask task = new DownloadTask {
            FileName      = fileName,
            Url           = url,
            Finished      = finished,
            Error         = error,
            TotalProgress = progress,
            MD5           = md5
        };

        LoggerHelper.Info("down load apk & md5: " + url + " " + md5, true);
        DownloadMgr.Instance.tasks = new List <DownloadTask> {
            task
        };
        DownloadMgr.Instance.TaskProgress = delegate(int total, int current, string filename) {
            if (taskProgress != null)
            {
                taskProgress(total, current, filename);
            }
        };
        DownloadMgr.Instance.AllDownloadFinished = delegate {
            DriverLib.Invoke(delegate {
                if (RuntimePlatform.IPhonePlayer == Application.platform)
                {
                    Action <bool> onClick = delegate(bool confirm) {
                        if (!confirm)
                        {
                            Application.Quit();
                        }
                    };
                    ForwardLoadingMsgBoxLib.Instance.ShowMsgBox(DefaultUI.dataMap[11].content, DefaultUI.dataMap[7].content, DefaultUI.dataMap[12].content, onClick);
                }
                else
                {
                    this.InstallApk(fileName);
                }
            });
            if (finished != null)
            {
                finished();
            }
            LoggerHelper.Debug("apk安装成功", true, 0);
        };
        DownloadMgr.Instance.CheckDownLoadList();
    }
Example #17
0
 void WC_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
 {
     if (e.Error != null)
     {
         ToErrorState();
         return;
     }
     if (UpdateState == UpdateStates.FetchingFiles)
     {
         DownloadTask DT = (DownloadTask)e.UserState;
         DownloadedSize  += DT.Size;
         PBProgress.Value = (int)(DownloadedSize * 100 / AllDownloadSize);
         CompletedDownloads.Add(DT);
         FileNum++;
         FetchFiles();
     }
 }
Example #18
0
 protected override void OnDispose(bool disposing)
 {
     base.OnDispose(disposing);
     if (!disposing)
     {
         return;
     }
     lock (this.m_myLock)
     {
         if (this.m_downloadTask == null)
         {
             return;
         }
         this.m_downloadTask.OnProgressChanged -= this.m_progressHandler;
         this.m_downloadTask = null;
     }
 }
Example #19
0
 public void UpdateStatusA(DownloadTask task, int attemp)
 {
     try
     {
         this.BeginInvoke(new MethodInvoker(delegate
         {
             progspin.Style  = MetroColorStyle.Red;
             Replaylb.Text   = "Chunk : " + task.Id.ToString() + " [Attempt " + attemp.ToString() + "]";
             specbtn.Visible = !Program.MainFormInstance.IsInGame;
             this.Refresh();
         }));
     }
     catch (Exception ex)
     {
         Logger.Instance.Log.Warn("FAILED to update status attempt", ex);
     }
 }
Example #20
0
    internal void Finished(string url, Exception e = null)
    {
        Action mycontinue = null;
        Action again      = null;
        Action finished   = null;

        LoggerHelper.Debug("Finished111111111111111", true, 0);
        DownloadTask task = this.GetTask(url);

        if (task != null)
        {
            if (e != null)
            {
                LoggerHelper.Error(url + "下载出错:" + e.Message, true);
                if (mycontinue == null)
                {
                    mycontinue = delegate {
                        task.bDownloadAgain = false;
                        task.bFineshed      = true;
                        this.CheckDownLoadList();
                    };
                }
                if (again == null)
                {
                    again = delegate {
                        task.bDownloadAgain = true;
                        task.bFineshed      = false;
                        this.CheckDownLoadList();
                    };
                }
                if (finished == null)
                {
                    finished = () => this.DownloadFinishWithMd5(task);
                }
                HandleNetworkError(e, mycontinue, again, finished);
            }
            else
            {
                this.DownloadFinishWithMd5(task);
            }
        }
        else
        {
            LoggerHelper.Debug("Finished Task Null", true, 0);
        }
    }
Example #21
0
        /// <summary>
        /// 获取Forge下载
        /// </summary>
        /// <param name="downloadSource">下载源</param>
        /// <param name="mcversion">Mc版本</param>
        /// <param name="forgeversion">Forge版本</param>
        /// <returns></returns>
        public static DownloadTask GetForgeDownloadURL(DownloadSource downloadSource, string mcversion, string forgeversion)
        {
            string local     = App.Handler.GameRootPath + "\\forge-" + mcversion + "-" + forgeversion + "-installer.jar";
            string forgePath = string.Format("maven/net/minecraftforge/forge/{0}-{1}/forge-{0}-{1}-installer.jar", mcversion, forgeversion);
            string Source    = ForgeUrl;

            switch (downloadSource)
            {
            case DownloadSource.Mojang:
                Source = ForgeUrl;
                break;

            case DownloadSource.BMCLAPI:
                Source = BMCLUrl;
                break;

            case DownloadSource.MCBBS:
                Source = MCBBSUrl;
                break;
            }

            Source += forgePath;

            DownloadTask dt = new DownloadTask(App.GetResourceString("String.NewDownloadTaskWindow.Core.Forge"),
                                               Source, local);

            dt.Todo = new Func <ProgressCallback, CancellationToken, Exception>((callback, cancelToken) =>
            {
                try
                {
                    IInstaller installer = new ForgeInstaller(local, new CommonInstallOptions()
                    {
                        GameRootPath   = App.Handler.GameRootPath,
                        IsClient       = true,
                        DownloadSource = App.Config.MainConfig.Download.DownloadSource,
                        Java           = App.Handler.Java
                    });
                    installer.BeginInstall(callback, cancelToken).Wait();
                    return(null);
                }
                catch (Exception ex)
                { return(ex); }
            });
            return(dt);
        }
Example #22
0
        void DownloadMedia(DownloadTask task, TeasePage page, TeaseMedia teaseMedia)
        {
            string mediaName = teaseMedia.Id;

            if (teaseMedia.Id.Contains("*"))
            {
                mediaName = teaseMedia.Id.Replace("*", String.Format("[{0}]", Guid.NewGuid()));
            }
            if (teaseMedia.Id.StartsWith("http://") || teaseMedia.Id.StartsWith("https://"))
            {
                mediaName = new Uri(teaseMedia.Id).Segments.Last();
            }
            string fileName = Path.Combine(task.MediaDirectory.FullName, mediaName);

            if (!File.Exists(fileName))
            {
                string url = String.Format("{0}/media/get.php?folder={1}/{2}&name={3}", MilovanaRootUrl, task.AuthorId, task.TeaseId, teaseMedia.Id);
                if (teaseMedia.Id.StartsWith("http://") || teaseMedia.Id.StartsWith("https://"))
                {
                    url = teaseMedia.Id;
                }
                try
                {
                    using (var client = new WebClient())
                    {
                        backgroundWorker.ReportProgress(0, "Downloading " + url);

                        // Be nice to the Milovana webserver and wait a bit before the next request...
                        Thread.Sleep(800);

                        client.DownloadFile(url, fileName);
                        backgroundWorker.ReportProgress(0, "Ok");
                    }
                }
                catch (Exception err)
                {
                    backgroundWorker.ReportProgress(1, String.Format("Error: [{0}] {1}", err.GetType(), err.Message));
                    page.Errors = String.Format("Error while downloading file '{0}'. {1}.", url, page.Errors);
                }
            }
            if (teaseMedia.Id.StartsWith("http://") || teaseMedia.Id.StartsWith("https://"))
            {
                teaseMedia.Id = mediaName;
            }
        }
Example #23
0
 public DownloadingTaskViewModel(string downloadAddress, string localPath, string name)
 {
     downloadTask = new DownloadTask(downloadAddress, localPath, name);
     DownloadTasksLogger.AddRecord(downloadTask);
     downloadTask.DownloadFileProgressChanged += (sender, e) =>
     {
         OnPropertyChanged("Completed");
         OnPropertyChanged("Total");
         OnPropertyChanged("Progress");
     };
     downloadTask.DownloadFileCompleted += (sender, e) =>
     {
         if (e.State == FileDownloader.CompletedState.Succeeded)
         {
             DownloadCompleted?.Invoke(this, new EventArgs());
         }
     };
 }
Example #24
0
 void FetchFiles()
 {
     if (DownloadTasks.Count > 0)
     {
         DownloadTask DT = DownloadTasks.Dequeue();
         TBProgress.Text = string.Format(UpdaterResources.DownloadingFile, FileNum, FileCount, DT.Name);
         WC.DownloadFileAsync(
             new Uri(DT.Source, UriKind.Absolute),
             DT.TempFile, DT);
     }
     else
     {
         UpdateState                = UpdateStates.InstallingUpdates;
         TBProgress.Text            = UpdaterResources.InstallingUpdate;
         PBProgress.IsIndeterminate = true;
         new Thread(InstallUpdate).Start();
     }
 }
Example #25
0
    internal void Progress(string url, int progress)
    {
        Action action = delegate {
            if (this.ContainKey(url))
            {
                DownloadTask task = this.GetTask(url);
                task.OnTotalProgress(progress, 0L, 0L);
                if (this.TaskProgress != null)
                {
                    int    num = Enumerable.Count <DownloadTask>(this.tasks, (Func <DownloadTask, bool>)(ta => ta.bFineshed));
                    string str = task.FileName.Substring(task.FileName.LastIndexOf("/") + 1);
                    this.TaskProgress(this.tasks.Count, num, str);
                }
            }
        };

        action();
    }
Example #26
0
 private void TaskFinish(DownloadTask task, Exception e = null)
 {
     if (task == null)
     {
         return;
     }
     if (e != null)
     {
         Debug.Log("下载出错" + e.Message);
         Debug.LogException(e);
         task.OnError(e);
         task.Finished(false);
     }
     else
     {
         DownloadFinishWithMd5(task);
     }
 }
Example #27
0
    private void Finished(string url, Exception e = null)
    {
        Debug.Log("下载完成!");
        DownloadTask task = this.m_listTasks.FirstOrDefault(t => t.Url == url);

        if (task != null)
        {
            if (e != null)
            {
                Debug.LogWarning("下载出错!" + e.Message);
            }
            else
            {
                //验证MD5码
                DownloadFinishedWithMd5(task);
            }
        }
    }
        private void DownloadDoneCallbcak(DownloadTask downloadTask, ulong size)
        {
            UpdateFileInfo updateFileInfo = newFileInfoTable[downloadTask.FileName];

            lock (lockObj)
            {
                if (VerificationFile(updateFileInfo, downloadTask.DownloadPath))
                {
                    alreadyDownloadSuccessCount++;
                    AppendHasUpdateFile(updateFileInfo);
                    RefreshDownloadState();
                }
                else
                {
                    DownloadErrorCallbcak(downloadTask, String.Empty);
                }
            }
        }
Example #29
0
    void Finished(WWW www, string error)
    {
        DownloadTask currentTask = _task;

        if (www == null)
        {
            isLoading = false;
            NetworkErrorHandle.ShowMsgBoxForWWWError(() =>
            {
                //跳过当前这个下载下一个
                currentTask.state = DownloadTaskState.Retry;
                _mgr.CheckDownLoadList();
            }, error);
            return;
        }
        Exception ex = CheckMd5(www, currentTask);

        if (ex != null)
        {
            isLoading = false;
            NetworkErrorHandle.Handle(currentTask.itemPath, ex,
                                      () =>
            {
                //跳过当前这个下载下一个
                currentTask.state = DownloadTaskState.Failed;
                _mgr.CheckDownLoadList();
            },
                                      () =>
            {
                //从新下载这个
                currentTask.state = DownloadTaskState.Retry;
                _mgr.CheckDownLoadList();
            });
            www.Dispose();
            return;
        }
        //Logger.Assert(currentTask.state == DownloadTaskState.Success, " task state must be Success " + currentTask.state);
        isLoading       = false;
        currentTask.www = www;
        GameStart.Invoke(() =>
        {
            _mgr.CheckDownLoadList();
        });
    }
Example #30
0
        /// <summary>
        /// Construct a TorrentDownloader.
        /// </summary>
        /// <param name="task">Download task, must with HttpRangedTarget.</param>
        /// <param name="engine">Client engine of MonoTorrent which provides torrent and magnet downloading.</param>
        /// <param name="folderProvider">Folder provider must not be null.</param>
        /// <param name="cacheProvider">Cache provider must not be null.</param>
        /// <param name="checkPoint">Set the downloader to start at given checkPoint.</param>
        /// <param name="maximumConnections">
        /// The maximum number of concurrent open connections for this torrent.
        /// Defaults to 60.</param>
        /// <param name="maximumDownloadSpeed">
        /// The maximum download speed, in bytes per second, for this torrent.
        /// A value of 0 means unlimited. Defaults to 0.</param>
        /// <param name="maximumUploadSpeed">
        /// The maximum upload speed, in bytes per second, for this torrent.
        /// A value of 0 means unlimited. defaults to 0.</param>
        /// <param name="uploadSlots">
        /// The number of peers which can be uploaded to concurrently for this torrent.
        /// A value of 0 means unlimited. defaults to 8.</param>
        /// <param name="customAnnounceUrls">Custom announce URLs.</param>
        public TorrentDownloader(
            DownloadTask task,
            ClientEngine engine,
            IFolderProvider folderProvider,
            ICacheStorageProvider cacheProvider,
            byte[] checkPoint                 = null,
            int maximumConnections            = 60,
            int maximumDownloadSpeed          = 0,
            int maximumUploadSpeed            = 0,
            int uploadSlots                   = 8,
            IEnumerable <string> announceUrls = null
            ) : base(task)
        {
            Ensure.That(task.Target is TorrentTarget).IsTrue();
            Ensure.That(cacheProvider, nameof(cacheFolder)).IsNotNull();
            Ensure.That(folderProvider, nameof(folderProvider)).IsNotNull();
            Ensure.That(engine, nameof(engine)).IsNotNull();
            Ensure.That(maximumConnections).IsGt(0);
            Ensure.That(maximumDownloadSpeed).IsGte(0);
            Ensure.That(maximumUploadSpeed).IsGte(0);
            Ensure.That(uploadSlots).IsGt(0);

            this.engine               = engine;
            this.cacheProvider        = cacheProvider;
            this.folderProvider       = folderProvider;
            this.maximumConnections   = maximumConnections;
            this.maximumDownloadSpeed = maximumDownloadSpeed;
            this.maximumUploadSpeed   = maximumUploadSpeed;
            this.uploadSlots          = uploadSlots;
            this.announceUrls         = announceUrls?.ToList();

            TorrentTarget realTarget = (TorrentTarget)task.Target;

            Progress = new BaseMeasurableProgress(
                realTarget.Torrent.Files.Sum(
                    file => realTarget.IsFileSelected(file) ? file.Length : 0));
            Speed = SharedSpeedCalculatorFactory.NewSpeedCalculator();
            Progress.ProgressChanged += (sender, arg) => Speed.CurrentValue = Progress.DownloadedSize;

            if (checkPoint != null)
            {
                ApplyCheckPoint(checkPoint);
            }
        }
        /// <summary>
        /// Asynchronously submits a task to the downloader.
        /// </summary>
        /// <param name="task">The DownloadTask instance.</param>
        private void SubmitTaskAsync(DownloadTask task)
        {
            IDownloader downloader = GetDownloader(task);


            if (FeedSource.EnclosureCacheSize != FeedSource.DefaultEnclosureCacheSize)
            {
                //BUGBUG: If we don't know the file size then we don't error because we treat it is -1 to
                //err on the side of allowing instead of disallowing.
                long limitInBytes = FeedSource.EnclosureCacheSize * 1024 * 1024;
                long filesize     = GetFileSize(task);

                var  targetDir = new DirectoryInfo(FeedSource.EnclosureFolder);
                long spaceUsed = FileHelper.GetSize(targetDir);

                if (!String.Equals(FeedSource.EnclosureFolder, FeedSource.PodcastFolder, StringComparison.OrdinalIgnoreCase))
                {
                    DirectoryInfo podcastDir = new DirectoryInfo(FeedSource.PodcastFolder);
                    spaceUsed += FileHelper.GetSize(podcastDir);
                }

                if ((filesize + spaceUsed) > limitInBytes)
                {
                    DownloadRegistryManager.Current.UnRegisterTask(task);

                    string fileName = task.DownloadItem.File.LocalName;
                    int    limit    = FeedSource.EnclosureCacheSize;
                    throw new DownloaderException(String.Format(ComponentsText.ExceptionEnclosureCacheLimitReached,
                                                                fileName, limit));
                }
            }


            // If there's no files to download
            // Consider the download as done
            if ((downloader == null) || (task.DownloadItem.File == null))
            {
                OnDownloadCompleted(null, new TaskEventArgs(task));
            }
            else
            {
                downloader.BeginDownload(task);
            }
        }
Example #32
0
    /// <summary>
    /// 抛给下载器进行下载
    /// </summary>
    private void PushDownloadTask()
    {
        DownloadTask firstTask = this.m_needDownloadTasks.Peek();

        this.m_currTask = firstTask;
        firstTask.Url   = this.m_remoteUrl + firstTask.FileName;

        //设置保存本地的目录
        firstTask.FileName = this.m_localPath + firstTask.FileName;
        string path = Path.GetDirectoryName(firstTask.FileName);

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        DownloadMgr.Inst.InitDownloadCallBacks(null, this.OneTaskProgressChanged, this.OneTaskFinished);
        DownloadMgr.Inst.AsynDownLoadFile(firstTask);
    }
Example #33
0
        public static string AddImport(string importurl, string baseurl, DownloadManager manager, Guid OwnerObjectId)
        {
            string fullimporturl = UrlHelper.Combine(baseurl, importurl);

            string OriginalImportUrl = string.IsNullOrEmpty(manager.OriginalImportUrl) ? baseurl : manager.OriginalImportUrl;

            bool issamehost = UrlHelper.isSameHost(fullimporturl, OriginalImportUrl);

            string relativeimporturl = UrlHelper.RelativePath(fullimporturl, issamehost);

            DownloadTask newdownload = new DownloadTask();

            newdownload.AbsoluteUrl   = fullimporturl;
            newdownload.ConstType     = ConstObjectType.Style;
            newdownload.RelativeUrl   = relativeimporturl;
            newdownload.OwnerObjectId = OwnerObjectId;
            manager.AddTask(newdownload);
            return(relativeimporturl);
        }
        /// <summary>
        /// Cancels a pending task.
        /// </summary>
        /// <param name="task">The DownloadTask instance.</param>
        /// <returns></returns>
        public bool EndTask(DownloadTask task)
        {
            IDownloader downloader = GetDownloader(task);
            bool        success    = true;

            if (downloader != null)
            {
                try
                {
                    success = downloader.CancelDownload(task);
                }
                finally
                {
                    Release(downloader);
                }
            } //if(downloader != null)

            return(success);
        }
Example #35
0
        private static void __DownloadCategoryFileCompleted(DownloadTask task)
        {
            string msg = "Failed to downloaded category list.";
            if (task.Status == ProgressStatus.Completed)
            {
                LocalDatabase.LoadCategoryData((string)task.Token);
                Interactivity.CategoryDataUpdated();

                msg = "Downloaded Category file: " + (string)task.Token;
                Logger.Add(msg, "__DownloadCategoryFileCompleted");
            }
            else if (task.Error != null)
            {
                Logger.Add(task.Error.Message, "__DownloadCategoryFileCompleted");
            }

            Interactivity.SetStatus(msg);
            Interactivity.SetProgress(0);
        }
Example #36
0
        private static void __DownloadCategoryIndexProgress(DownloadTask task)
        {
            string msg = msg = "Downloading category index... [{0}/{1} completed]";
            msg = string.Format(msg, Functions.FormatMemory(task.Received), Functions.FormatMemory(task.Total));
            Interactivity.SetStatus(msg);

            int percent = task.ProgressPercentage;
            Interactivity.SetProgress(task.ProgressPercentage);
        }
Example #37
0
    private void DownDownloadPackageList(Action<bool> fileDecompress, string packageUrl, List<KeyValuePair<String, String>> downloadList, Action<int, int, string> taskProgress, Action<int, long, long> progress, Action finished, Action<Exception> error)
    {
        var allTask = new List<DownloadTask>();
        //收集downloadlist,生成所有下载任务
        for (int i = 0; i < downloadList.Count; i++)
        {
            LoggerHelper.Debug("收集数据包,生成任务:" + i);
            KeyValuePair<String, String> kvp = downloadList[i];
            LoggerHelper.Debug("下载文件名:" + kvp.Value);
            String localFile = String.Concat(SystemConfig.ResourceFolder, kvp.Value);

            Action OnDownloadFinished = () =>
            {
                LoggerHelper.Debug("下载完成,进入完成回调");
                LoggerHelper.Debug("本地文件:" + File.Exists(localFile));
                if (File.Exists(localFile))
                    FileAccessManager.DecompressFile(localFile);
                LoggerHelper.Debug("解压完成,保存版本信息到version.xml");
                if (File.Exists(localFile))
                    File.Delete(localFile);
                LocalVersion.ResouceVersionInfo = new VersionCodeInfo(kvp.Key);
                SaveVersion(LocalVersion);
                //if (taskProgress != null)
                //    taskProgress(downloadList.Count, i + 1);
            };
            String fileUrl = String.Concat(packageUrl, kvp.Value);
            var task = new DownloadTask
            {
                FileName = localFile,
                Url = fileUrl,
                Finished = OnDownloadFinished,
                Error = error,
                TotalProgress = progress
            };
            //task.Error = erroract;
            string fileNoEXtension = kvp.Value;// task.FileName.LastIndexOf('/') != 0 ? task.FileName.Substring(task.FileName.LastIndexOf('/') + 1) : task.FileName;
            LoggerHelper.Debug(task.FileName + "::fileNoEXtension::" + fileNoEXtension);
            if (ServerVersion.PackageMD5Dic.ContainsKey(fileNoEXtension))
            {
                task.MD5 = ServerVersion.PackageMD5Dic[fileNoEXtension];
                allTask.Add(task);
            }
            else
            {
                error(new Exception("down pkg not exit :" + fileNoEXtension));
                return;
                //LoggerHelper.Error("down pkg not exit :" + fileNoEXtension);
            }
        }
        //全部下载完成的回调
        Action AllFinished = () =>
        {
            LoggerHelper.Debug("更新包全部下载完成");
            finished();
        };
        //添加taskProgress的回调
        Action<int, int, string> TaskProgress = (total, current, filename) =>
        {
            if (taskProgress != null)
                taskProgress(total, current, filename);
        };
        //添加文件解压的回调函数
        Action<bool> filedecompress = (decompress) =>
       {
           if (fileDecompress != null)
               fileDecompress(decompress);
       };
        //所有任务收集好了,开启下载
        LoggerHelper.Debug("所有任务收集好了,开启下载");
        DownloadMgr.Instance.tasks = allTask;
        DownloadMgr.Instance.AllDownloadFinished = AllFinished;
        DownloadMgr.Instance.TaskProgress = TaskProgress;
        DownloadMgr.Instance.FileDecompress = filedecompress;
        DownloadMgr.Instance.CheckDownLoadList();
    }
        /// <summary>
        /// 刷新下载状态
        /// </summary>
        private void RefreshList()
        {
            TotalProgressChanged();

            //根据numOnce及正在下载的情况生成下载
            int downloadingCount = webs.Count;
            for (int j = 0; j < NumOnce - downloadingCount; j++)
            {
                if (numLeft > 0)
                {
                    string url = downloadItems[downloadItems.Count - numLeft].Url;

                    //string dest = Uri.UnescapeDataString(url.Substring(url.LastIndexOf('/') + 1));
                    string dest = downloadItems[downloadItems.Count - numLeft].FileName;
                    dest = ReplaceInvalidPathChars(dest, ' ');
                    if (IsSepSave && dest.IndexOf(' ') > 0)
                    {
                        string sepPath = saveLocation + "\\" + dest.Substring(0, dest.IndexOf(' '));
                        if (!System.IO.Directory.Exists(sepPath))
                            System.IO.Directory.CreateDirectory(sepPath);
                        dest = sepPath + "\\" + dest;
                    }
                    else
                    {
                        dest = saveLocation + "\\" + dest;
                    }

                    if (System.IO.File.Exists(dest))
                    {
                        downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.Failed;
                        downloadItems[downloadItems.Count - numLeft].Size = "文件已存在";
                        WriteErrText("moe_error.txt", url + ": 文件已存在");
                        j--;
                    }
                    else if (dest.Length > 259)
                    {
                        downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.Failed;
                        downloadItems[downloadItems.Count - numLeft].Size = "路径过长";
                        WriteErrText("moe_error.txt", url + ": 路径过长");
                        j--;
                    }
                    else
                    {
                        downloadItems[downloadItems.Count - numLeft].StatusE = DLStatus.DLing;

                        DownloadTask task = new DownloadTask(url, dest, MainWindow.IsNeedReferer(url));
                        webs.Add(url, task);

                        //异步下载开始
                        System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Download));
                        thread.Start(task);
                    }

                    numLeft--;
                }
                else break;
            }
            RefreshStatus();
        }
Example #39
0
        /// <summary>
        /// Adds a new download task to the queue.
        /// </summary>
        public void AddToQueue(DownloadTask task)
        {
            task.DownloadingFinished += delegate(DownloadTask t)
            {
                mCurrent.Remove(t);
                if (mRunning) StartQueue();
            };

            mQueue.Enqueue(task);
            if (mRunning) StartQueue();
        }
Example #40
0
        private static void __DownloadProblemDatabaseProgress(DownloadTask task)
        {
            string msg = "Downloading problem list... [{0}/{1} completed]";
            msg = string.Format(msg, Functions.FormatMemory(task.Received), Functions.FormatMemory(task.Total));
            Interactivity.SetStatus(msg);

            int percent = task.ProgressPercentage;
            Interactivity.SetProgress(task.ProgressPercentage);
        }
Example #41
0
 public static DownloadTask DownloadStringAsync(
     string url,
     object token = null,
     Priority priority = Priority.High,
     DownloadTaskHandler progress = null,
     DownloadTaskHandler completed = null,
     int retry = 0)
 {
     DownloadTask task = new DownloadTask(url, null, token, retry);
     task.ProgressChangedEvent += progress;
     task.DownloadCompletedEvent += completed;
     return DownloadAsync(task, priority);
 }
Example #42
0
        private static void __DownloadUserInfoCompleted(DownloadTask task)
        {
            string msg = "Failed to downloaded user submissions.";
            if (task.Status == ProgressStatus.Completed)
            {
                LocalDatabase.LoadDefaultUser();
                msg = "Downloaded user submissions.";
            }
            else if (task.Error != null)
            {
                Logger.Add(task.Error.Message, "__DownloadUserInfoCompleted");
            }

            Interactivity.SetStatus(msg);
            Interactivity.SetProgress(0);
        }
Example #43
0
        private static void __DownloadUseridCompleted(DownloadTask task)
        {
            string uid = (string)task.Result;
            string name = (string)task.Token;

            task.Status = ProgressStatus.Failed;
            if (string.IsNullOrEmpty(uid))
                task.Error = new Exception("Connection error. Retry please.");
            else if (uid.Trim() == "0")
                task.Error = new Exception(name + " doesn't exist.");
            else if (task.Error == null)
                task.Status = ProgressStatus.Completed;

            if (task.Status == ProgressStatus.Completed)
            {
                RegistryAccess.AddUserid(name, uid);
                string msg = "Username downloaded : {0} = {1}";
                Interactivity.SetStatus(string.Format(msg, name, uid));
            }
        }
        private void saveButton_Click(object sender, EventArgs e)
        {
            saveDialog.InitialDirectory = TeasesFolder.FullName;
            saveDialog.FileName = teaseTitleTextBox.Text.Remove(Path.GetInvalidFileNameChars());
            if (DialogResult.OK == saveDialog.ShowDialog())
            {
                loadButton.Enabled = false;
                cancelButton.Enabled = true;
                saveButton.Enabled = false;

                teaseFile = new FileInfo(saveDialog.FileName);
                var mediaDirectory = new DirectoryInfo(Path.Combine(teaseFile.DirectoryName, Path.GetFileNameWithoutExtension(teaseFile.Name)));
                if (!mediaDirectory.Exists)
                {
                    mediaDirectory.Create();
                }

                var downloadTask = new DownloadTask
                {
                    TeaseId = teaseId,
                    TeaseTitle = teaseTitleTextBox.Text,
                    TeaseUrl = teaseUrl,
                    AuthorId = authorId,
                    AuthorName = authorNameTextBox.Text,
                    IsFlashTease = isFlashTease,
                    TeaseFile = teaseFile,
                    MediaDirectory = mediaDirectory
                };

                if (!backgroundWorker.IsBusy)
                {
                    backgroundWorker.RunWorkerAsync(downloadTask);
                }
            }
        }
        void DownloadMedia(DownloadTask task, TeasePage page, TeaseMedia teaseMedia)
        {
            string mediaName = teaseMedia.Id;
            if (teaseMedia.Id.Contains("*"))
            {
                mediaName = teaseMedia.Id.Replace("*", String.Format("[{0}]", Guid.NewGuid()));
            }
            if (teaseMedia.Id.StartsWith("http://") || teaseMedia.Id.StartsWith("https://"))
            {
                mediaName = new Uri(teaseMedia.Id).Segments.Last();
            }
            string fileName = Path.Combine(task.MediaDirectory.FullName, mediaName);
            if (!File.Exists(fileName))
            {
                string url = String.Format("{0}/media/get.php?folder={1}/{2}&name={3}", MilovanaRootUrl, task.AuthorId, task.TeaseId, teaseMedia.Id);
                if (teaseMedia.Id.StartsWith("http://") || teaseMedia.Id.StartsWith("https://"))
                {
                    url = teaseMedia.Id;
                }
                try
                {
                    using (var client = new WebClient())
                    {
                        backgroundWorker.ReportProgress(0, "Downloading " + url);

                        // Be nice to the Milovana webserver and wait a bit before the next request...
                        Thread.Sleep(800);

                        client.DownloadFile(url, fileName);
                        backgroundWorker.ReportProgress(0, "Ok");
                    }
                }
                catch (Exception err)
                {
                    backgroundWorker.ReportProgress(1, String.Format("Error: [{0}] {1}", err.GetType(), err.Message));
                    page.Errors = String.Format("Error while downloading file '{0}'. {1}.", url, page.Errors);
                }
            }
            if (teaseMedia.Id.StartsWith("http://") || teaseMedia.Id.StartsWith("https://"))
            {
                teaseMedia.Id = mediaName;
            }
        }
Example #46
0
        /// <summary>
        /// Renders the layer
        /// </summary>
        /// <param name="graphics">Graphics object reference</param>
        /// <param name="map">Map which is rendered</param>
        public override void Render(Graphics graphics, Map map)
        {
            var bbox = map.Envelope;
            var extent = new Extent(bbox.MinX, bbox.MinY, bbox.MaxX, bbox.MaxY);
            var level = BruTile.Utilities.GetNearestLevel(_source.Schema.Resolutions, map.PixelSize);
            var tiles = _source.Schema.GetTileInfos(extent, level);
            
            //Abort previous running Threads
            Cancel();

            using (var ia = new ImageAttributes())
            {
                if (!_transparentColor.IsEmpty)
                    ia.SetColorKey(_transparentColor, _transparentColor);
#if !PocketPC
                ia.SetWrapMode(WrapMode.TileFlipXY);
#endif
                var tileWidth = _source.Schema.GetTileWidth(level);
                var tileHeight = _source.Schema.GetTileHeight(level);

                foreach (TileInfo info in tiles)
                {
                    if (_bitmaps.Find(info.Index) != null)
                    {
                        //draws directly the bitmap
                        var bb = new Envelope(new Coordinate(info.Extent.MinX, info.Extent.MinY),
                                              new Coordinate(info.Extent.MaxX, info.Extent.MaxY));
                        HandleMapNewTileAvaliable(map, graphics, bb, _bitmaps.Find(info.Index), 
                                                  tileWidth, tileHeight, ia);
                    }
                    else if (_fileCache != null && _fileCache.Exists(info.Index))
                    {

                        Bitmap img = GetImageFromFileCache(info) as Bitmap;
                        _bitmaps.Add(info.Index, img);

                        //draws directly the bitmap
                        var btExtent = info.Extent;
                        var bb = new Envelope(new Coordinate(btExtent.MinX, btExtent.MinY),
                                              new Coordinate(btExtent.MaxX, btExtent.MaxY));
                        HandleMapNewTileAvaliable(map, graphics, bb, _bitmaps.Find(info.Index),
                                                  tileWidth, tileHeight, ia);
                    }
                    else
                    {
                        var cancelToken = new CancellationTokenSource();
                        var token = cancelToken.Token;
                        var l_info = info;
                        if (Logger.IsDebugEnabled)
                            Logger.DebugFormat("Starting new Task to download tile {0},{1},{2}", info.Index.Level, info.Index.Col, info.Index.Row);
                        var t = new System.Threading.Tasks.Task(delegate
                        {
                            if (token.IsCancellationRequested)
                                token.ThrowIfCancellationRequested();

                            if (Logger.IsDebugEnabled)
                                Logger.DebugFormat("Task started for download of tile {0},{1},{2}", info.Index.Level, info.Index.Col, info.Index.Row);

                            var res = GetTileOnThread(token, _source, l_info, _bitmaps, true);
                            if (res)
                            {
                                Interlocked.Decrement(ref _numPendingDownloads);
                                var e = DownloadProgressChanged;
                                if (e != null)
                                    e(_numPendingDownloads);
                            }

                        }, token);
                        var dt = new DownloadTask() { CancellationToken = cancelToken, Task = t };
                        lock (_currentTasks)
                        {
                            _currentTasks.Add(dt);
                            _numPendingDownloads++;
                        }
                        t.Start();
                    }
                }
            }

        }
Example #47
0
        //
        // Download Queue
        //
        public static void Enqueue(DownloadTask task)
        {
            if (task == null) return;

            task.Status = ProgressStatus.Waiting;
            switch (task.TaskPriority)
            {
                case Priority.Low:
                    DownloadQueue.Add(task);
                    break;
                case Priority.Normal:
                    DownloadQueue.Insert(_normalend, task);
                    ++_normalend;
                    break;
                case Priority.High:
                    DownloadQueue.Insert(_highend, task);
                    ++_normalend;
                    ++_highend;
                    break;
            }
        }
Example #48
0
        private static void __DownloadProblemDatabaseCompleted(DownloadTask task)
        {
            _DownloadingProblemDatabase = false;

            string msg = "Failed to update problem list.";
            if (task.Status == ProgressStatus.Completed)
            {
                LocalDatabase.LoadDatabase();
                msg = "Problem list is successfully updated.";
                Logger.Add("Downloaded problem database file", "__DownloadProblemDatabaseCompleted");
            }
            else if (task.Error != null)
            {
                if (task.Token == null)
                {
                    LocalDatabase.LoadDatabase();
                    Logger.Add(task.Error.Message, "__DownloadProblemDatabaseCompleted");
                }
                else
                {
                    DownloadFileAsync((string)task.Token, task.FileName, null, Priority.High,
                        __DownloadProblemDatabaseProgress, __DownloadProblemDatabaseCompleted, 0);
                }
            }

            Interactivity.SetStatus(msg);
            Interactivity.SetProgress(0);
        }
Example #49
0
        public static void DownloadUserid(string name, Internet.DownloadTaskHandler complete)
        {
            string url = "http://uhunt.felix-halim.net/api/uname2uid/" + name;
            DownloadTask dt = new DownloadTask(url, null, name, 2);

            if (LocalDatabase.ContainsUser(name))
            {
                dt.Result = LocalDatabase.GetUserid(name);
                dt.Status = ProgressStatus.Completed;
                if (complete != null) complete(dt);
            }
            else
            {
                dt.DownloadCompletedEvent += __DownloadUseridCompleted;
                if (complete != null) dt.DownloadCompletedEvent += complete;
                DownloadAsync(dt, Priority.High);
            }
        }
Example #50
0
        public static void DownloadNext()
        {
            if (!__ContinueDownload || IsBusy()) return;

            //get next task
            Dequeue();
            if (DownloadQueue.Count == 0) return;
            CurrentTask = DownloadQueue[0];

            //if already canceled, then skip it
            if (CurrentTask.Status == ProgressStatus.Cancelled)
            {
                CurrentTask.ReportComplete();
                DownloadNext();
                return;
            }

            //run task
            CurrentTask.Download();
        }
Example #51
0
    private void AsynDownloadApk(Action<int, int, string> taskProgress, string fileName, string url, Action<int, long, long> progress, Action finished, Action<Exception> error)
    {
        var task = new DownloadTask
        {
            FileName = fileName,
            Url = url,
            Finished = finished,
            Error = error,
            TotalProgress = progress,
            MD5 = ServerVersion.ApkMd5
        };
        LoggerHelper.Info("down load apk & md5: " + url + " " + ServerVersion.ApkMd5);
        //LoggerHelper.Debug("APK保存的MD5值: " + ServerVersion.ApkMd5);
        var tasks = new List<DownloadTask> { task };
        DownloadMgr.Instance.tasks = tasks;
        //添加taskProgress的回调
        Action<int, int, string> TaskProgress = (total, current, filename) =>
        {
            if (taskProgress != null)
                taskProgress(total, current, filename);
        };
        DownloadMgr.Instance.TaskProgress = TaskProgress;
        StreamingAssetManager go = null;
        ResourceIndexInfo.Instance.Init(Application.streamingAssetsPath + "/ResourceIndexInfo.txt", () =>
            {
                LoggerHelper.Info("资源索引信息:" + ResourceIndexInfo.Instance.GetLeftFilePathes().Length);
                if (ResourceIndexInfo.Instance.GetLeftFilePathes().Length == 0)
                {
                    go = new StreamingAssetManager();
                    go.UpdateProgress = false;
                    go.ApkFinished = true;
                }
                else
                {
                    //下载apk时导出资源
                    go = new StreamingAssetManager { UpdateProgress = false };
                    go.AllFinished = () =>
                    {
                        LoggerHelper.Debug("打开资源导出完成的标识11ApkFinished");
                        go.ApkFinished = true;
                    };
                }
            });
        DownloadMgr.Instance.AllDownloadFinished = () =>
        {
            LoggerHelper.Info("APK download finish, wait for export finish:" + fileName);
            if (go != null)
            {
                go.UpdateProgress = true;
                LoggerHelper.Debug("打开导出进度显示:" + go.ApkFinished);
                //先判断资源导出是否完成,再安装apk,没完成则等待完成
                while (!go.ApkFinished)
                {
                    System.Threading.Thread.Sleep(500);
                }
                LoggerHelper.Info("APK and export download finish.");
                go = null;
            }
            DriverLib.Invoke(() =>
            {
#if UNITY_IPHONE
            Action<bool> InstallIpa = (confirm) =>
            {
                if (confirm)
                {
                    //IOSUtils.UpdateLoader(fileName);
                }
                else
                    Application.Quit();
            };
				ForwardLoadingMsgBoxLib.Instance.ShowMsgBox(DefaultUI.dataMap[11].content,
				                                         DefaultUI.dataMap[7].content,
				                                         DefaultUI.dataMap[12].content,
				                                         InstallIpa);
#else
                InstallApk(fileName);
#endif
            });
            if (finished != null)
                finished();
            LoggerHelper.Debug("apk安装成功");
        };
        var stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();
        while (go == null)
        {
            System.Threading.Thread.Sleep(50);
            if (stopwatch.ElapsedMilliseconds > 3000)
                break;
            //LoggerHelper.Debug("阻塞直到StreamingAssetManager不为null或超时3秒");
        }
        stopwatch.Stop();
        //开始下载apk同时导出资源文件
        if (go != null && !go.ApkFinished)
        {
            LoggerHelper.Debug("apk下载同时导出资源");
            go.UpdateApkExport();
        }
        DownloadMgr.Instance.CheckDownLoadList();

    }
Example #52
0
 private void dt_progressChanged(DownloadTask task)
 {
     string status = string.Format("Downloading submission data on problem... [{0} of {1} received]",
        Functions.FormatMemory(task.Received), Functions.FormatMemory(task.Total));
     Interactivity.SetStatus(status);
     Interactivity.SetProgress(task.ProgressPercentage);
 }
Example #53
0
    private void DownloadFinishWithMd5(DownloadTask task)
    {
        LoggerHelper.Debug("DownloadFinishWithMd51111");
        //验证MD5
#if UNITY_IPHONE
    //ios下如果封装该方法在一个函数中,调用该函数来产生文件的MD5的时候,就会抛JIT异常。
    //如果直接把这个产生MD5的方法体放在直接执行,就可以正常执行,这个原因还不清楚。
                string md5Compute = null;
				using(System.IO.FileStream fileStream = System.IO.File.OpenRead(task.FileName))
				{
					System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
					byte[] fileMD5Bytes = md5.ComputeHash(fileStream);
                    md5Compute = System.BitConverter.ToString(fileMD5Bytes).Replace("-", "").ToLower();
				}
#else
        var md5Compute = Utils.BuildFileMd5(task.FileName);
#endif
        //md5验证失败
        if (md5Compute.Trim() != task.MD5.Trim())
        {
            //如果md5验证失败,删除原文件
            if (File.Exists(task.FileName))
                File.Delete(task.FileName);
            LoggerHelper.Error("断点MD5验证失败,从新下载:" + task.FileName + "--" + md5Compute + " vs " + task.MD5);
            task.bDownloadAgain = true;
            task.bFineshed = false;
            CheckDownLoadList();
            return;
        }
        //所有通过验证就认为是下载完成
        LoggerHelper.Debug("断点下载验证全部通过,下载完成:" + task.FileName);
        if (FileDecompress != null)
            FileDecompress(true);
        task.bDownloadAgain = false;
        task.bFineshed = true;
        task.Finished();
        if (FileDecompress != null)
            FileDecompress(false);
        LoggerHelper.Debug("断点下载完成后,再次核对下载列表");
        CheckDownLoadList();
    }
Example #54
0
        private static void __DownloadUserInfoProgress(DownloadTask task)
        {
            string msg = "Downloading {0}'s submissions... [{1}/{2} completed]";
            msg = string.Format(msg, task.Token,
                Functions.FormatMemory(task.Received), Functions.FormatMemory(task.Total));
            Interactivity.SetStatus(msg);

            int percent = task.ProgressPercentage;
            Interactivity.SetProgress(task.ProgressPercentage);
        }
Example #55
0
        private void DownloadFinished(DownloadTask task)
        {
            if (task.Error != null)
            {
                Logger.Add(task.Error.Message, "ProblemViewer | DownloadFinished()");
                return;
            }

            this.BeginInvoke((MethodInvoker)delegate
            {
                problemWebBrowser.Refresh();
            });

            bool finish = false;
            long pnum = (long)task.Token;
            if (current == null || current.pnum != pnum)
                finish = true;

            if (!finish) //if no error occured
            {
                string ext = Path.GetExtension(task.FileName);
                if (ext == ".pdf")
                {
                    if (LocalDirectory.GetFileSize(task.FileName) > 100)
                    {
                        setPdfFile(task.FileName);
                        finish = true;
                    }
                }
                else if (ext == ".html")
                {
                    if (LocalDirectory.GetFileSize(task.FileName) > 100)
                    {
                        problemWebBrowser.Navigate(task.FileName);
                        int cnt = DownloadContents(pnum);
                        if (cnt == 0) finish = true;
                    };

                }
                else
                {
                    finish = true;
                }
            }

            if (finish)
            {
                reloadButton.Enabled = true;
            }
        }
Example #56
0
 public ThreadDownloadBreakPoint(DownloadMgr mgr, DownloadTask task)
 {
     Mgr = mgr;
     Task = task;
 }
Example #57
0
        private void dt_taskCompleted(DownloadTask task)
        {
            //check validity of result
            if (task.Status != ProgressStatus.Completed)
            {
                if (task.Error != null)
                {
                    Interactivity.SetStatus("Failed to download submission data on problem.");
                    Logger.Add(task.Error.Message, "Problem Viewer | dt_taskCompleted(DownloadTask task)");
                }
                return;
            }

            try
            {
                string user = (string)task.Token;

                //set result to listview
                if (_curSubType == SubViewType.UsersSub)
                {
                    task.Result = task.Result.Remove(0, task.Result.IndexOf(":") + 1);
                    task.Result = task.Result.Remove(task.Result.Length - 1);
                    UserInfo uinfo = JsonConvert.DeserializeObject<UserInfo>(task.Result);
                    uinfo.Process();
                    submissionStatus.SetObjects(uinfo.submissions);
                }
                else if (_curSubType == SubViewType.Comapre)
                {
                    List<UserSubmission> allsubs = new List<UserSubmission>();

                    string data = task.Result.Substring(1, task.Result.Length - 2);
                    do
                    {
                        int i = data.IndexOf("{");
                        if (i < 0) break;
                        int j = data.IndexOf("}", i);
                        if (j < 0) break;

                        string tmp = data.Substring(i, j - i + 1);
                        UserInfo uinfo = JsonConvert.DeserializeObject<UserInfo>(tmp);
                        uinfo.Process();
                        allsubs.AddRange(uinfo.submissions.ToArray());

                        data = data.Substring(j + 1);
                    }
                    while (data.Length > 0);
                    submissionStatus.SetObjects(allsubs);
                }
                else
                {
                    List<SubmissionMessage> lsm =
                        JsonConvert.DeserializeObject<List<SubmissionMessage>>(task.Result);
                    if (lsm == null) return;
                    submissionStatus.SetObjects(lsm);
                }

                //sort listview and set other flags
                submissionStatus.ShowGroups = false;
                switch (_curSubType)
                {
                    case SubViewType.LastSubmission:
                        submissionStatus.Sort(sidSUB, SortOrder.Descending);
                        subListLabel.Text = "Last submissions on this problem from " +
                            dateTimePicker1.Value.ToString();
                        break;
                    case SubViewType.Ranklist:
                        submissionStatus.Sort(rankSUB, SortOrder.Ascending);
                        subListLabel.Text = "Rank-list of this problem displaying first " +
                            numericUpDown1.Value.ToString() + "' users.";
                        break;
                    case SubViewType.UsersRank:
                        submissionStatus.Sort(rankSUB, SortOrder.Ascending);
                        subListLabel.Text = user + "'s nearby users on this problem";
                        break;
                    case SubViewType.UsersSub:
                        submissionStatus.Sort(sidSUB, SortOrder.Descending);
                        subListLabel.Text = user + "'s submissions on this problem";
                        break;
                    case SubViewType.Comapre:
                        submissionStatus.BuildGroups(unameSUB, SortOrder.Ascending);
                        subListLabel.Text = "Comparison between all users in this problem";
                        break;
                }
            }
            catch (Exception ex)
            {
                Interactivity.SetStatus("Failed to download submission data on problem.");
                Logger.Add(ex.Message, "Problem Viewer | dt_taskCompleted(DownloadTask task)");
            }
        }
Example #58
0
 public bool AsynDownLoadText(DownloadTask task)
 {
     if (!ContainKey(task.Url))
     {
         _webClient.DownloadStringAsync(new Uri(task.Url), task.Url);
         return true;
     }
     return false;
 }
Example #59
0
        private static void __DownloadProblemCategoryCompleted(DownloadTask task)
        {
            string msg = "Failed to downloaded category list.";
            if (task.Status == ProgressStatus.Completed)
            {
                DownloadCategoryIndexFiles();
                msg = "Downloaded category list.";
                Logger.Add("Downloaded problem's categories.", "__DownloadProblemCategoryCompleted");
            }
            else if (task.Error != null)
            {
                Logger.Add(task.Error.Message, "__DownloadProblemCategoryCompleted");
            }

            Interactivity.SetStatus(msg);
            Interactivity.SetProgress(0);
        }
Example #60
0
        public TestDownloadClient()
        {
            SandSoft.Downloader.DownloaderClient client = new SandSoft.Downloader.DownloaderClient();
            Console.WriteLine("Max concurrent download " + client.MaxConcurrentDownload);

            string[] url = {
                               "https://uva.onlinejudge.org/index.php",
                               "http://www.stackoverlow.com",
                               "http://codeforces.com/api/contest.list",
                               "http://uhunt.felix-halim.net/api/p",
                               "http://codeforces.com/api/contest.list?gym=true",
                               "http://www.google.com",
                               "http://www.facebook.com",
                               "https://icpcarchive.ecs.baylor.edu/",
                               "http://acm.hust.edu.cn/vjudge/toIndex.action"
                           };
            ThreadPriority[] tp = {
                                      ThreadPriority.Normal,
                                      ThreadPriority.Normal,
                                      ThreadPriority.AboveNormal,
                                      ThreadPriority.Highest,
                                      ThreadPriority.Normal,
                                      ThreadPriority.Normal,
                                      ThreadPriority.Normal,
                                      ThreadPriority.Lowest,
                                      ThreadPriority.BelowNormal
                                  };

            for (int i = 0; i < url.Length; ++i)
            {
                var t = new DownloadTask(url[i], tp[i]);
                t.DownloadingFinished += delegate(DownloadTask task)
                {
                    Console.WriteLine("### Finished: " + task.Url + " : " + task.Recieved + " : " + task.TaskPriority);
                    ++finishCount;
                    if (finishCount == totalCount)
                    {
                        Console.WriteLine("\nFinished all.");
                        Console.WriteLine("Time: " + (new TimeSpan((DateTime.Now - startTime).Ticks)).TotalSeconds + " secs");
                    }
                };
                t.DownloadProgressChanged += delegate(string message, DownloadTask task)
                {
                    // Console.WriteLine(message + task.Url + " : " + task.Recieved + " : " + task.Total + " : " + task.TaskPriority);
                };
                client.AddToQueue(t);
            }

            finishCount = 0;
            totalCount = url.Length;
            startTime = DateTime.Now;

            //while (client.Downloading > 0 || client.Remaining > 0)
            {
                // Console.WriteLine("Downloading... Downloading:" + client.Downloading + "; Remaining:" + client.Remaining);
                //System.Threading.Thread.Sleep(200);
            }
        }