/// <summary> /// 下载磁链接文件 /// </summary> /// <param name="metalink">磁链接文件</param> /// <param name="fileName">输出文件名</param> /// <param name="dir">下载目录</param> /// <param name="taskOptions">下载任务选项</param> /// <returns>成功返回任务标识符数组,失败返回空数组</returns> public string[] AddMetalink(string metalink, string fileName = "", string dir = "", List <Dictionary <string, string> > taskOptions = null) { try { string autoSaveFileID = ""; string[] gids = Aria2cWarpper.AddMetalink(ref autoSaveFileID, metalink, fileName, dir, taskOptions); foreach (var gid in gids) { if (!string.IsNullOrWhiteSpace(gid)) { Aria2cTask task = Aria2cWarpper.TellStatus(gid); OnStart?.Invoke(this, new Aria2cTaskEvent(task)); AddDownTask(task); if (autoSaveFileID != "") { gidAndTorrentOrMeta4Map.Add(gid, autoSaveFileID + ".meta4"); } } } return(gids); } catch (Exception ex) { Console.WriteLine(ex); return(new string[] { "" }); } }
/// <summary> /// 获取下载状态 /// </summary> /// <param name="gid">任务标识符</param> /// <param name="task">获取的任务信息</param> /// <param name="keys">属性过滤字段</param> /// <returns>成功返回任务信息,失败返回空</returns> public static Aria2cTask TellStatus(string gid, params string[] keys) { XmlRpcStruct xmlstruct = aria2c.TellStatus(Aria2cRpcSecret, gid, keys); Aria2cTask task = new Aria2cTask(xmlstruct); return(task); }
/// <summary> /// 更新任务 /// </summary> /// <param name="task"></param> private void UpdateDownTask(Aria2cTask task) { lock (objlock) { downLoadDict[task.Gid] = task; } }
/// <summary> /// 下载种子链接文件 /// </summary> /// <param name="torrent">种子文件</param> /// <param name="fileName">输出文件名</param> /// <param name="dir">下载目录</param> /// <param name="taskOptions">下载任务选项</param> /// <returns>成功返回任务标识符,失败返回空</returns> public string AddTorrent(string torrent, string fileName = "", string dir = "", List <Dictionary <string, string> > taskOptions = null) { try { string autoSaveFileID = ""; string gid = Aria2cWarpper.AddTorrent(ref autoSaveFileID, torrent, fileName, dir, taskOptions); if (!string.IsNullOrWhiteSpace(gid)) { Aria2cTask task = Aria2cWarpper.TellStatus(gid); OnStart?.Invoke(this, new Aria2cTaskEvent(task)); AddDownTask(task); if (autoSaveFileID != "") { gidAndTorrentOrMeta4Map.Add(gid, autoSaveFileID + ".torrent"); } } return(gid); } catch (Exception ex) { Console.WriteLine(ex); return(string.Empty); } }
/// <summary> /// 添加任务 /// </summary> /// <param name="task"></param> private void AddDownTask(Aria2cTask task) { lock (objlock) { downLoadDict[task.Gid] = task; IsPauseAll = false; } }
/// <summary> /// 将RPC信息转化为任务状态 /// </summary> /// <param name="rpcStruct"></param> /// <returns></returns> public static List <Aria2cTask> ConvertToAria2cTasks(XmlRpcStruct[] rpcStruct) { List <Aria2cTask> taskList = new List <Aria2cTask>(); foreach (XmlRpcStruct rpc in rpcStruct) { Aria2cTask task = new Aria2cTask(rpc); taskList.Add(task); } return(taskList); }
/// <summary> /// 获取任务状态信息 /// </summary> private void GetTaskStatus() { try { //复制key List <string> keyList = new List <string>(); foreach (var key in downLoadDict.Keys) { keyList.Add(key); } //遍历查询状态 foreach (var gid in keyList) { try { Aria2cTask task = TellStatus(gid); if (task == null) { //当一个任务出问题,则本次循环就放弃掉.例如被杀掉进程后,要是还循环获取状态,是始终拿不到状态的. break; } if (downLoadDict[gid].Status != task.Status) { OnStatusChanged(task); } if (task.Status == Aria2cTaskStatus.Active) { OnLoading?.Invoke(this, new Aria2cTaskEvent(task)); } UpdateDownTask(task); } catch (Exception ex) { Console.WriteLine(ex); } } } catch (Exception ex) { Console.WriteLine(ex); } }
/// <summary> /// 取消暂停任务 /// </summary> /// <param name="gid">任务标识符</param> /// <returns>成功返回true, 失败返回fale</returns> public bool UnPause(string gid) { try { bool result = Aria2cWarpper.UnPause(gid); if (result) { Aria2cTask task = Aria2cWarpper.TellStatus(gid); OnUnPause?.Invoke(this, new Aria2cTaskEvent(task)); } return(result); } catch (Exception ex) { Console.WriteLine(ex); return(false); } }
/// <summary> /// 强制暂停任务 /// </summary> /// <param name="gid">任务标识符</param> /// <returns>成功返回true, 失败返回fale</returns> public bool ForcePause(string gid) { try { bool pause = Aria2cWarpper.ForcePause(gid); if (pause) { Aria2cTask task = Aria2cWarpper.TellStatus(gid); OnPaused?.Invoke(this, new Aria2cTaskEvent(task)); } return(pause); } catch (Exception ex) { Console.WriteLine(ex); return(false); } }
/// <summary> /// 下载磁链接文件 /// </summary> /// <param name="metalink">磁链接文件</param> /// <param name="dir">下载目录</param> /// <returns>成功返回任务标识符,失败返回空</returns> public string AddMetalink(string metalink, string fileName = "", string dir = "") { try { string gid = Aria2cWarpper.AddMetalink(metalink, fileName, dir); if (!string.IsNullOrWhiteSpace(gid)) { Aria2cTask task = Aria2cWarpper.TellStatus(gid); OnStart?.Invoke(this, new Aria2cTaskEvent(task)); AddDownTask(task); } return(gid); } catch (Exception ex) { Console.WriteLine(ex); return(string.Empty); } }
/// <summary> /// 添加下载任务 /// </summary> /// <param name="uri">下载地址</param> /// <param name="fileName">输出文件名</param> /// <param name="dir">下载文件夹</param> /// <param name="taskOptions">下载任务选项</param> /// <returns>成功返回任务标识符,失败返回空</returns> public string AddUri(string uri, string fileName = "", string dir = "", List <Dictionary <string, string> > taskOptions = null) { try { string gid = Aria2cWarpper.AddUri(uri, fileName, dir, taskOptions); if (!string.IsNullOrWhiteSpace(gid)) { Aria2cTask task = Aria2cWarpper.TellStatus(gid); OnStart?.Invoke(this, new Aria2cTaskEvent(task)); AddDownTask(task); } return(gid); } catch (Exception ex) { Console.WriteLine(ex); return(string.Empty); } }
/// <summary> /// 任务状态变换 /// </summary> /// <param name="task"></param> private void OnStatusChanged(Aria2cTask task) { switch (task.Status) { case Aria2cTaskStatus.Active: OnUnPause?.Invoke(this, new Aria2cTaskEvent(task)); break; case Aria2cTaskStatus.Complete: { OnFinish?.Invoke(this, new Aria2cTaskEvent(task)); RemoveDownTask(task.Gid); if (CountDownTask() == 0) { OnAllFinish?.Invoke(this, new EventArgs()); } } break; case Aria2cTaskStatus.Error: OnError?.Invoke(this, new Aria2cTaskEvent(task)); break; case Aria2cTaskStatus.Paused: OnPaused?.Invoke(this, new Aria2cTaskEvent(task)); break; case Aria2cTaskStatus.Removed: OnRemoved?.Invoke(this, new Aria2cEvent(task.Gid)); downLoadDict.Remove(task.Gid); break; case Aria2cTaskStatus.Waiting: OnWaiting?.Invoke(this, new Aria2cTaskEvent(task)); break; default: break; } }
public Aria2cTaskEvent(Aria2cTask task) { Gid = task.Gid; Task = task; }
/// <summary> /// 强制删除所有任务 /// </summary> /// <returns>返回删除成功的任务数量</returns> public int ForceRemoveAll() { int iRemovedCount = 0; try { //复制key,避免downLoadDict循环时直接删除key导致报错 List <string> keyList = new List <string>(); foreach (var key in downLoadDict.Keys) { keyList.Add(key); } foreach (var gid in keyList) { Aria2cTask task = TellStatus(gid); if (task == null) { continue; } if (task.Status == Aria2cTaskStatus.Active || task.Status == Aria2cTaskStatus.Waiting || task.Status == Aria2cTaskStatus.Paused) { bool result = ForceRemove(gid); if (result) { iRemovedCount++; } } else if (task.Status == Aria2cTaskStatus.Complete || task.Status == Aria2cTaskStatus.Error || task.Status == Aria2cTaskStatus.Removed)//清除下载列表中其他的任务 { try { RemoveDownTask(gid); } catch { } } } //还有一种情况是,启动aria2c之后session自动加载的任务 var activelist = this.TellActive(); var waitList = this.TellWaiting(-1, int.MaxValue);//最后一个任务往前数N个 //var stopList = this.TellStopped(-1, int.MaxValue);//取消剩余,已停止的在aria2c保留 var all_tasks = new List <Aria2cTask>(); all_tasks.AddRange(activelist); all_tasks.AddRange(waitList); foreach (var task in all_tasks) { bool result = ForceRemove(task.Gid); if (result) { iRemovedCount++; } } return(iRemovedCount); } catch (Exception ex) { Console.WriteLine(ex); return(iRemovedCount); } }
/// <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); } }
/// <summary> /// 强制删除所有任务 /// </summary> /// <param name="lst">移除任务的下载路径集合</param> /// <returns>返回删除成功的任务数量</returns> public int ForceRemoveAll(ref List <string> lst) { int iRemovedCount = 0; try { if (!IsPauseAll) { ForcePauseAll(); } //复制key,避免downLoadDict循环时直接删除key导致报错 List <string> keyList = new List <string>(); //foreach (var key in downLoadDict.Keys) //{ // keyList.Add(key); //} keyList.AddRange(downLoadDict.Keys); foreach (var gid in keyList) { Aria2cTask task = TellStatus(gid); if (task == null) { continue; } if (task.Status == Aria2cTaskStatus.Active || task.Status == Aria2cTaskStatus.Waiting || task.Status == Aria2cTaskStatus.Paused) { bool result = ForceRemove(gid); if (result) { if (gidAndTorrentOrMeta4Map.ContainsKey(gid)) { lst.AddRange(new string[] { task.Dir, Path.Combine(task.Dir, gidAndTorrentOrMeta4Map[gid]) }); gidAndTorrentOrMeta4Map.Remove(gid); } if (!string.IsNullOrWhiteSpace(task.InfoHash)) { lst.AddRange(new string[] { task.Dir, Path.Combine(task.Dir, task.InfoHash.ToLower() + ".torrent") }); } if (task.Bittorrent != null) { lst.AddRange(new string[] { Path.Combine(task.Dir, task.Bittorrent.Info), task.Files[0].Path }); } else { lst.AddRange(new string[] { task.Dir, task.Files[0].Path }); } iRemovedCount++; } } else if (task.Status == Aria2cTaskStatus.Complete || task.Status == Aria2cTaskStatus.Error || task.Status == Aria2cTaskStatus.Removed)//清除下载列表中其他的任务 { try { RemoveDownTask(gid); } catch { } } } //还有一种情况是,启动aria2c之后session自动加载的任务 var activelist = this.TellActive(); var waitList = this.TellWaiting(-1, int.MaxValue);//最后一个任务往前数N个 //var stopList = this.TellStopped(-1, int.MaxValue);//已停止的下载任务 var all_tasks = new List <Aria2cTask>(); all_tasks.AddRange(activelist); all_tasks.AddRange(waitList); //all_tasks.AddRange(stopList); foreach (var task in all_tasks) { bool result = ForceRemove(task.Gid); if (result) { if (gidAndTorrentOrMeta4Map.ContainsKey(task.Gid)) { lst.AddRange(new string[] { task.Dir, Path.Combine(task.Dir, gidAndTorrentOrMeta4Map[task.Gid]) }); gidAndTorrentOrMeta4Map.Remove(task.Gid); } if (!string.IsNullOrWhiteSpace(task.InfoHash)) { lst.AddRange(new string[] { task.Dir, Path.Combine(task.Dir, task.InfoHash.ToLower() + ".torrent") }); } if (task.Bittorrent != null) { lst.AddRange(new string[] { Path.Combine(task.Dir, task.Bittorrent.Info), task.Files[0].Path }); } else { lst.AddRange(new string[] { task.Dir, task.Files[0].Path }); } iRemovedCount++; } } return(iRemovedCount); } catch (Exception ex) { Console.WriteLine(ex); return(iRemovedCount); } }