/// <summary>
        /// 全局状态
        /// </summary>
        private void GetGlobalStatus()
        {
            try
            {
                Aria2cGlobalStat status = this.GetGlobalStat();
                if (status == null)
                {
                    return;
                }

                if (globalStat.DownloadSpeed != status.DownloadSpeed ||
                    globalStat.UploadSpeed != status.UploadSpeed ||
                    globalStat.NumActive != status.NumActive ||
                    globalStat.NumWaiting != status.NumWaiting ||
                    globalStat.NumStopped != status.NumStopped ||
                    globalStat.NumStoppedTotal != status.NumStoppedTotal)
                {
                    OnGlobalStatusChanged?.Invoke(this, new Aria2cGlobalStatEvent(status));
                    globalStat = status;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        /// <summary>
        /// 获取全局下载状态
        /// </summary>
        /// <param name="globalStat">设置信息</param>
        /// <returns>成功返回全局下载状态, 失败返回空</returns>
        public static Aria2cGlobalStat GetGlobalStat()
        {
            XmlRpcStruct     xmlstruct  = aria2c.GetGlobalStat(Aria2cRpcSecret);
            Aria2cGlobalStat globalStat = new Aria2cGlobalStat(xmlstruct);

            return(globalStat);
        }
        public Aria2c()
        {
            objlock      = new object();
            globalStat   = new Aria2cGlobalStat();
            downLoadDict = new Dictionary <string, Aria2cTask>();

            StartTimer();
        }
Esempio n. 4
0
        public Aria2c()
        {
            objlock       = new object();
            globalStat    = new Aria2cGlobalStat();
            downLoadDict  = new Dictionary <string, Aria2cTask>();
            pauseAll      = false;
            stoppedStatus = new List <Aria2cTaskStatus>();
            stoppedStatus.AddRange(new Aria2cTaskStatus[] { Aria2cTaskStatus.Complete, /*Aria2cTaskStatus.Error,可能需要恢复下载,不移除*/ Aria2cTaskStatus.Removed });

            StartTimer();
        }
Esempio n. 5
0
        public Aria2c(double dCycleInterval)
        {
            objlock       = new object();
            globalStat    = new Aria2cGlobalStat();
            downLoadDict  = new Dictionary <string, Aria2cTask>();
            pauseAll      = false;
            stoppedStatus = new List <Aria2cTaskStatus>();
            stoppedStatus.AddRange(new Aria2cTaskStatus[] { Aria2cTaskStatus.Complete, /*Aria2cTaskStatus.Error,可能需要恢复下载,不移除*/ Aria2cTaskStatus.Removed });

            dInterval = dCycleInterval;
            StartTimer();
            isCycle = true;

            gidAndTorrentOrMeta4Map = new Dictionary <string, string>();
        }
Esempio n. 6
0
        /// <summary>
        /// 全局状态
        /// </summary>
        private async void GetGlobalStatus(Aria2cGlobalStat status)
        {
            try
            {
                if (status == null && isCycle)
                {
                    status = this.GetGlobalStat();
                }
                if (status == null)
                {
                    return;
                }

                if (globalStat.DownloadSpeed != status.DownloadSpeed || globalStat.UploadSpeed != status.UploadSpeed || globalStat.NumActive != status.NumActive ||
                    globalStat.NumWaiting != status.NumWaiting || globalStat.NumStopped != status.NumStopped || globalStat.NumStoppedTotal != status.NumStoppedTotal)
                {
                    OnGlobalStatusChanged?.Invoke(this, new Aria2cGlobalStatEvent(status));
                    globalStat = status;
                }

                if (status.NumActive == 0L && status.NumWaiting == 0L && status.DownloadSpeed == 0L && isCycle)
                {
                    await Task.Delay(Convert.ToInt32(this.dInterval * 3)); //等待3秒后,再做判断,避免刚添加完任务的瞬间,误判定为全部结束

                    status = this.GetGlobalStat();                         //获取最新status
                    //timerTMP == null是一个flag,表明OnAllFinish没有触发过
                    if (status.NumActive == 0L && status.NumWaiting == 0L && status.DownloadSpeed == 0L && !IsLoading && timerTMP == null)
                    {
                        OnAllFinish?.Invoke(this, new Aria2cGlobalStatEvent(status));

                        StartTmpTimer();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 全局状态
        /// </summary>
        private void GetGlobalStatus(Aria2cGlobalStat status)
        {
            try
            {
                if (status == null && !IsPauseAll)
                {
                    status = this.GetGlobalStat();
                }
                if (status == null)
                {
                    return;
                }

                if (globalStat.DownloadSpeed != status.DownloadSpeed || globalStat.UploadSpeed != status.UploadSpeed || globalStat.NumActive != status.NumActive ||
                    globalStat.NumWaiting != status.NumWaiting || globalStat.NumStopped != status.NumStopped || globalStat.NumStoppedTotal != status.NumStoppedTotal)
                {
                    OnGlobalStatusChanged?.Invoke(this, new Aria2cGlobalStatEvent(status));
                    globalStat = status;
                }

                if (status.NumActive == 0 && status.NumWaiting == 0 && status.DownloadSpeed == 0 && !IsPauseAll)
                {
                    OnAllFinish?.Invoke(this, new Aria2cGlobalStatEvent(status));

                    if (timerTMP == null)
                    {
                        timerTMP           = new Timer(10000);//10s后执行(等待其他如GlobalStatus),改变“暂停”变量,减少计时器轮询操作
                        timerTMP.Elapsed  += new ElapsedEventHandler(SetPauseAll);
                        timerTMP.AutoReset = false;
                        timerTMP.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
 public Aria2cGlobalStatEvent(Aria2cGlobalStat stat)
 {
     Stat = stat;
 }
Esempio n. 9
0
        /// <summary>
        /// 获取任务状态信息
        /// </summary>
        private Aria2cGlobalStat GetTaskStatus()
        {
            try
            {
                if (IsPauseAll || CountDownTask() == 0)
                {
                    return(null);
                }

                //复制key
                List <string> keyList = new List <string>();
                foreach (var key in downLoadDict.Keys)
                {
                    keyList.Add(key);
                }

                int iDownloadingCount = 0, iTaskCount = 0;
                //int iMaxCount = maxConcurrentDowns.Value; //不再限定最大轮询数量,可能造成任务状态更新丢失
                //if (iMaxCount <= 5) { iMaxCount *= 5; }
                //else if (iMaxCount <= 20) { iMaxCount *= 3; }
                //else if (iMaxCount <= 40) { iMaxCount *= 2; }
                //else { iMaxCount += 16; }
                Aria2cGlobalStat status = null;
                //遍历查询状态
                for (int i = 0; i < keyList.Count; i++)
                {
                    iTaskCount++;
                    try
                    {
                        Aria2cTask task = TellStatus(keyList[i]);
                        if (task == null)//当一个任务出问题,则本次循环就放弃掉.例如被杀掉进程后,要是还循环获取状态,是始终拿不到状态的.
                        {
                            break;
                        }

                        /*优化逻辑,注释掉
                         * if (task.Status == Aria2cTaskStatus.Paused)
                         *  //当一个任务出现暂停,说明全部任务都是暂停状态,停止轮询(×  web ui可以单任务暂停)
                         *  break;
                         */

                        if (task.Status == Aria2cTaskStatus.Active)
                        {
                            OnLoading?.Invoke(this, new Aria2cTaskEvent(task));
                            iDownloadingCount++;
                            if (iDownloadingCount == maxConcurrentDowns.Value)//下载数统计达到最大下载数,后续任务都是waiting,已完成的已遍历过,跳过等待任务
                            {
                                status = this.GetGlobalStat();
                                if (status != null)
                                {
                                    i += (int)status.NumWaiting;
                                }
                            }
                        }
                        else if (task.Status == Aria2cTaskStatus.Waiting)
                        {
                            continue;
                        }

                        if (downLoadDict[task.Gid].Status != task.Status)
                        {
                            OnStatusChanged(task);
                        }

                        if (stoppedStatus.Contains(task.Status))
                        {
                            if (downLoadDict.ContainsKey(task.Gid))
                            {
                                RemoveDownTask(task.Gid);
                            }
                        }
                        else
                        {
                            UpdateDownTask(task);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                    //if (iTaskCount >= iMaxCount) //不再限定最大轮询数量,可能造成任务状态更新丢失
                    //    break;
                }

                return(status);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(null);
            }
        }