✨ IPC 子进程监听主进程退出时关闭

This commit is contained in:
Aigio Liu
2023-05-25 16:42:33 +08:00
parent 34082e56d6
commit 2f197337e0
4 changed files with 33 additions and 10 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ static unsafe partial class Program
{
public const string dotnet_version_major = "7";
public const string dotnet_version_minor = "0";
public const string dotnet_version_build = "4";
public const string dotnet_version_build = "5";
public const string dotnet_version = $"{dotnet_version_major}.{dotnet_version_minor}.{dotnet_version_build}";
const string dotnet_runtime = "Microsoft.NETCore.App";
@@ -26,8 +26,8 @@ public interface IPCSubProcessService : IDisposable
const int ExitCode_EmptyArrayArgs = 4001;
const int ExitCode_EmptyPipeName = 4002;
const int ExitCode_EmptyProcessId = 4003;
const int ExitCode_NotFoundProcessId = 4004;
const int ExitCode_EmptyMainProcessId = 4003;
const int ExitCode_NotFoundMainProcessId = 4004;
/// <summary>
/// 子进程 IPC 程序启动通用函数
@@ -49,15 +49,16 @@ public interface IPCSubProcessService : IDisposable
if (string.IsNullOrWhiteSpace(pipeName))
return ExitCode_EmptyPipeName;
if (!int.TryParse(args[1], out var pid))
return ExitCode_EmptyProcessId;
var process = Process.GetProcessById(pid);
if (process == null)
return ExitCode_NotFoundProcessId;
return ExitCode_EmptyMainProcessId;
var mainProcess = Process.GetProcessById(pid);
if (mainProcess == null)
return ExitCode_NotFoundMainProcessId;
TaskCompletionSource tcs = new();
process.Exited += (_, _) =>
mainProcess.EnableRaisingEvents = true;
mainProcess.Exited += (_, _) =>
{
tcs.TrySetResult();
tcs.TrySetResult(); // 监听主进程退出时关闭当前子进程
};
IPCSubProcessServiceImpl? ipc = null;
@@ -79,8 +79,10 @@ public sealed partial class IPCMainProcessServiceImpl : IPCMainProcessService
public void Run()
{
var tickCount64 = Environment.TickCount64;
var pid = Environment.ProcessId;
ipcProvider = new IpcProvider(
$"wtts_ipc_{_()}_{Environment.TickCount64}_{Environment.ProcessId}",
$"ipc_{_()}{tickCount64}{pid / 3}{pid % 3}",
new IpcConfiguration
{
IpcLoggerProvider = _ => new IpcLogger_(loggerFactory, nameof(IPCMainProcessServiceImpl)),
@@ -295,6 +295,26 @@ partial class Startup // 自定义控制台命令参数
}),
};
rootCommand.AddCommand(shutdown);
// -clt sudo
var sudo = new Command("sudo", "使用管理员权限启动 IPC 服务进程")
{
Handler = CommandHandler.Create(() =>
{
// TODO
}),
};
rootCommand.AddCommand(sudo);
// -clt plugins {插件名} {插件需要解析的参数}
var plugins = new Command("plugins", "插件使用的 IPC 服务进程")
{
Handler = CommandHandler.Create(() =>
{
// TODO
}),
};
rootCommand.AddCommand(plugins);
}
}
#endif