From 2932aec58d0f1c8a9fb735fbc0dacbefe6d556ba Mon Sep 17 00:00:00 2001 From: aiqinxuancai Date: Fri, 2 Aug 2024 18:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0aria2=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria2Fast/Service/Aria2ApiManager.cs | 56 ++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/Aria2Fast/Service/Aria2ApiManager.cs b/Aria2Fast/Service/Aria2ApiManager.cs index 4611e33..8f6f418 100644 --- a/Aria2Fast/Service/Aria2ApiManager.cs +++ b/Aria2Fast/Service/Aria2ApiManager.cs @@ -18,6 +18,8 @@ using Aria2Fast.Utils; using Newtonsoft.Json.Linq; using System.Threading.Channels; +using System.Text; +using System.Collections.Concurrent; namespace Aria2Fast.Service { @@ -53,6 +55,8 @@ public class Aria2ApiManager private Timer _debounceTimer; private readonly object _locker = new object(); + private static ConcurrentQueue _outputQueue = new ConcurrentQueue(); + private Process? _aria2Process = null; @@ -101,7 +105,7 @@ public void Init() /// /// 启动本地Aria2并管理,退出时跟随退出 /// - private void StartupLocalAria2() + private async void StartupLocalAria2() { //key=ARIA2FAST //port=6809 @@ -170,9 +174,35 @@ private void StartupLocalAria2() WorkingDirectory = aria2Path, CreateNoWindow = true, UseShellExecute = false, - + RedirectStandardOutput = true, + RedirectStandardError = true, + StandardOutputEncoding = Encoding.UTF8, // 设置标准输出编码 + StandardErrorEncoding = Encoding.UTF8 // 设置标准错误编码 + }; + + _aria2Process = new Process + { + StartInfo = startInfo, + EnableRaisingEvents = true }; - _aria2Process = Process.Start(startInfo); + + _aria2Process.OutputDataReceived += (sender, e) => { if (e.Data != null) AddToQueue(e.Data); }; + _aria2Process.ErrorDataReceived += (sender, e) => { if (e.Data != null) AddToQueue(e.Data); }; + + _aria2Process.Start(); + _aria2Process.BeginOutputReadLine(); + _aria2Process.BeginErrorReadLine(); + + // 等待 5 秒(异步) + + + + Task.Run(async () => + { + await Task.Delay(5000); + RecordOutput(); + }); + EasyLogManager.Logger.Info($"Aria2已启动"); } else @@ -188,6 +218,26 @@ private void StartupLocalAria2() } } + private static void AddToQueue(string line) + { + _outputQueue.Enqueue(line); + while (_outputQueue.Count > 1000) + { + _outputQueue.TryDequeue(out _); + } + } + + private static void RecordOutput() + { + StringBuilder sb = new StringBuilder(); + foreach (var line in _outputQueue) + { + sb.AppendLine(line); + } + EasyLogManager.Logger.Info(@$"Aria2启动日志:{sb.ToString()}"); + } + + public static bool ExistLocalAria2() { var aria2Path = Path.Combine(Directory.GetCurrentDirectory(), "Aria2");