From ba246e99e8abaaf0f666e17836e484fca4a4289b Mon Sep 17 00:00:00 2001 From: Minghao Hu Date: Sun, 24 Dec 2023 16:21:21 +0900 Subject: [PATCH 1/2] Fix mixed sync/async ops on Process streams. --- v2rayN/v2rayN/Handler/CoreHandler.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index 377b0758..95729e63 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -147,7 +147,7 @@ namespace v2rayN.Handler } } - private string CoreFindexe(CoreInfo coreInfo) + private string CoreFindExe(CoreInfo coreInfo) { string fileName = string.Empty; foreach (string name in coreInfo.coreExes) @@ -225,7 +225,7 @@ namespace v2rayN.Handler try { var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.Xray); - string fileName = CoreFindexe(coreInfo); + string fileName = CoreFindExe(coreInfo); if (fileName == "") return -1; Process p = new() @@ -269,6 +269,7 @@ namespace v2rayN.Handler if (p.WaitForExit(1000)) { + p.CancelErrorRead(); throw new Exception(p.StandardError.ReadToEnd()); } @@ -295,7 +296,7 @@ namespace v2rayN.Handler { try { - string fileName = CoreFindexe(coreInfo); + string fileName = CoreFindExe(coreInfo); if (Utils.IsNullOrEmpty(fileName)) { return null; @@ -343,6 +344,7 @@ namespace v2rayN.Handler if (proc.WaitForExit(1000)) { + proc.CancelErrorRead(); throw new Exception(displayLog ? proc.StandardError.ReadToEnd() : "启动进程失败并退出 (Failed to start the process and exited)"); } From 44cfa2d8dc152bcd541ee2ca9660cc081fa21e8e Mon Sep 17 00:00:00 2001 From: Minghao Hu Date: Mon, 12 Feb 2024 22:12:57 +0900 Subject: [PATCH 2/2] Add a start up error receiver. --- v2rayN/v2rayN/Handler/CoreHandler.cs | 66 ++++++++-------------------- 1 file changed, 19 insertions(+), 47 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreHandler.cs b/v2rayN/v2rayN/Handler/CoreHandler.cs index f352def5..cf6fca78 100644 --- a/v2rayN/v2rayN/Handler/CoreHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreHandler.cs @@ -154,7 +154,9 @@ namespace v2rayN.Handler } } - private string CoreFindexe(CoreInfo coreInfo) + #region Private + + private string CoreFindExe(CoreInfo coreInfo) { string fileName = string.Empty; foreach (string name in coreInfo.coreExes) @@ -233,52 +235,11 @@ namespace v2rayN.Handler ShowMsg(false, configPath); try { - var coreInfo = LazyConfig.Instance.GetCoreInfo(ECoreType.Xray); - string fileName = CoreFindexe(coreInfo); - if (fileName == "") return -1; - - Process p = new() + var coreInfo = LazyConfig.Instance.GetCoreInfo(coreType); + var proc = RunProcess(new(), coreInfo, $" -c {Global.CoreSpeedtestConfigFileName}", true, ShowMsg); + if (proc is null) { - StartInfo = new ProcessStartInfo - { - FileName = fileName, - Arguments = "-config stdin:", - WorkingDirectory = Utils.GetConfigPath(), - UseShellExecute = false, - RedirectStandardInput = true, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true, - StandardOutputEncoding = Encoding.UTF8, - StandardErrorEncoding = Encoding.UTF8 - } - }; - p.OutputDataReceived += (sender, e) => - { - if (!String.IsNullOrEmpty(e.Data)) - { - string msg = e.Data + Environment.NewLine; - ShowMsg(false, msg); - } - }; - p.ErrorDataReceived += (sender, e) => - { - if (!string.IsNullOrEmpty(e.Data)) - { - string msg = e.Data + Environment.NewLine; - ShowMsg(false, msg); - } - }; - p.Start(); - p.BeginOutputReadLine(); - p.BeginErrorReadLine(); - - p.StandardInput.Write(configStr); - p.StandardInput.Close(); - - if (p.WaitForExit(1000)) - { - throw new Exception(p.StandardError.ReadToEnd()); + return -1; } return proc.Id; @@ -325,6 +286,8 @@ namespace v2rayN.Handler StandardErrorEncoding = displayLog ? Encoding.UTF8 : null, } }; + var startUpErrorMessage = new StringBuilder(); + var startUpSuccessful = false; if (displayLog) { proc.OutputDataReceived += (sender, e) => @@ -341,6 +304,11 @@ namespace v2rayN.Handler { string msg = e.Data + Environment.NewLine; update(false, msg); + + if (!startUpSuccessful) + { + startUpErrorMessage.Append(msg); + } } }; } @@ -354,7 +322,11 @@ namespace v2rayN.Handler if (proc.WaitForExit(1000)) { proc.CancelErrorRead(); - throw new Exception(displayLog ? proc.StandardError.ReadToEnd() : "启动进程失败并退出 (Failed to start the process and exited)"); + throw new Exception(displayLog ? startUpErrorMessage.ToString() : "启动进程失败并退出 (Failed to start the process and exited)"); + } + else + { + startUpSuccessful = true; } LazyConfig.Instance.AddProcess(proc.Handle);