mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-20 23:54:26 +08:00
feat.airapp与融合桌面
This commit is contained in:
@@ -6,6 +6,7 @@ using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Threading;
|
||||
using LanMountainDesktop.Launcher.Models;
|
||||
using LanMountainDesktop.Launcher.Services;
|
||||
using LanMountainDesktop.Launcher.Services.AirApp;
|
||||
using LanMountainDesktop.Launcher.Services.Ipc;
|
||||
using LanMountainDesktop.Launcher.Views;
|
||||
using LanMountainDesktop.Shared.Contracts.Launcher;
|
||||
@@ -61,6 +62,13 @@ public partial class App : Application
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.IsAirAppBrokerCommand)
|
||||
{
|
||||
_ = RunAirAppBrokerAsync(desktop, context);
|
||||
base.OnFrameworkInitializationCompleted();
|
||||
return;
|
||||
}
|
||||
|
||||
// 调试模式:只显示 DevDebugWindow,不走正常启动流程
|
||||
// 避免启动主程序后 Launcher 自动退出,导致开发者无法预览 UI
|
||||
if (context.IsDebugMode && !context.IsPreviewCommand &&
|
||||
@@ -90,6 +98,45 @@ public partial class App : Application
|
||||
base.OnFrameworkInitializationCompleted();
|
||||
}
|
||||
|
||||
private static async Task RunAirAppBrokerAsync(
|
||||
IClassicDesktopStyleApplicationLifetime desktop,
|
||||
CommandContext context)
|
||||
{
|
||||
var appRoot = Commands.ResolveAppRoot(context);
|
||||
var requesterPid = context.GetIntOption("requester-pid", 0);
|
||||
Logger.Info($"Air APP broker starting. AppRoot='{appRoot}'; RequesterPid={requesterPid}.");
|
||||
|
||||
using var airAppIpcHost = new LauncherAirAppLifecycleIpcHost(
|
||||
new LauncherAirAppLifecycleService(
|
||||
new AirAppProcessStarter(
|
||||
new AirAppHostLocator(),
|
||||
() => appRoot,
|
||||
() => null)));
|
||||
airAppIpcHost.Start();
|
||||
|
||||
await WaitForAirAppBrokerExitAsync(requesterPid, airAppIpcHost.LifecycleService).ConfigureAwait(false);
|
||||
|
||||
Logger.Info("Air APP broker exiting.");
|
||||
await Dispatcher.UIThread.InvokeAsync(() => desktop.Shutdown(0), DispatcherPriority.Background);
|
||||
}
|
||||
|
||||
internal static async Task WaitForAirAppBrokerExitAsync(
|
||||
int requesterPid,
|
||||
LauncherAirAppLifecycleService airAppLifecycleService)
|
||||
{
|
||||
while (ShouldKeepAirAppBrokerAlive(requesterPid, airAppLifecycleService))
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool ShouldKeepAirAppBrokerAlive(
|
||||
int requesterPid,
|
||||
LauncherAirAppLifecycleService airAppLifecycleService)
|
||||
{
|
||||
return TryGetLiveProcess(requesterPid) || airAppLifecycleService.HasLiveAirApps();
|
||||
}
|
||||
|
||||
private bool HandlePreviewCommand(CommandContext context, IClassicDesktopStyleApplicationLifetime desktop)
|
||||
{
|
||||
switch (context.Command.ToLowerInvariant())
|
||||
@@ -236,7 +283,6 @@ public partial class App : Application
|
||||
var startupAttemptRegistry = new StartupAttemptRegistry();
|
||||
var coordinatorPipeName = LauncherCoordinatorIpcServer.CreatePipeName();
|
||||
var successPolicy = LauncherFlowCoordinator.ResolveSuccessPolicyKey(context);
|
||||
|
||||
if (!startupAttemptRegistry.TryReserveCoordinator(
|
||||
context.LaunchSource,
|
||||
successPolicy,
|
||||
@@ -257,6 +303,14 @@ public partial class App : Application
|
||||
return;
|
||||
}
|
||||
|
||||
using var airAppIpcHost = new LauncherAirAppLifecycleIpcHost(
|
||||
new LauncherAirAppLifecycleService(
|
||||
new AirAppProcessStarter(
|
||||
new AirAppHostLocator(),
|
||||
() => appRoot,
|
||||
() => null)));
|
||||
airAppIpcHost.Start();
|
||||
|
||||
using var coordinatorServer = new LauncherCoordinatorIpcServer(
|
||||
coordinatorPipeName,
|
||||
BuildCoordinatorStatusFromAttempt(reservedAttempt),
|
||||
@@ -334,9 +388,45 @@ public partial class App : Application
|
||||
await WriteLauncherResultAsync(context, result).ConfigureAwait(false);
|
||||
|
||||
Environment.ExitCode = result.Success ? 0 : 1;
|
||||
if (result.Success)
|
||||
{
|
||||
var hostPid = ResolveManagedHostPid(result, startupAttemptRegistry.GetOwnedAttempt()?.HostPid ?? 0);
|
||||
await WaitForManagedProcessesToExitAsync(hostPid, airAppIpcHost.LifecycleService).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() => desktop.Shutdown(Environment.ExitCode), DispatcherPriority.Background);
|
||||
}
|
||||
|
||||
private static int ResolveManagedHostPid(LauncherResult result, int fallbackHostPid)
|
||||
{
|
||||
if (result.Details.TryGetValue("hostPid", out var hostPidText) &&
|
||||
int.TryParse(hostPidText, out var hostPid))
|
||||
{
|
||||
return hostPid;
|
||||
}
|
||||
|
||||
if (result.Details.TryGetValue("existingHostPid", out var existingHostPidText) &&
|
||||
int.TryParse(existingHostPidText, out var existingHostPid))
|
||||
{
|
||||
return existingHostPid;
|
||||
}
|
||||
|
||||
return fallbackHostPid;
|
||||
}
|
||||
|
||||
private static async Task WaitForManagedProcessesToExitAsync(
|
||||
int hostPid,
|
||||
LauncherAirAppLifecycleService airAppLifecycleService)
|
||||
{
|
||||
Logger.Info($"Launcher entering managed background lifetime. HostPid={hostPid}.");
|
||||
while (TryGetLiveProcess(hostPid) || airAppLifecycleService.HasLiveAirApps())
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
Logger.Info("Launcher managed background lifetime completed; no host or Air APP process remains.");
|
||||
}
|
||||
|
||||
private static async Task<LauncherResult> AttachToExistingCoordinatorAsync(
|
||||
CommandContext context,
|
||||
SplashWindow? splashWindow,
|
||||
|
||||
@@ -4,11 +4,14 @@ namespace LanMountainDesktop.Launcher;
|
||||
|
||||
internal sealed class CommandContext
|
||||
{
|
||||
public const string AirAppBrokerCommand = "air-app-broker";
|
||||
|
||||
private const string LaunchSourceOptionName = "launch-source";
|
||||
|
||||
private static readonly string[] GuiCommands =
|
||||
[
|
||||
"launch",
|
||||
AirAppBrokerCommand,
|
||||
"apply-update",
|
||||
"preview-splash",
|
||||
"preview-error",
|
||||
@@ -60,6 +63,9 @@ internal sealed class CommandContext
|
||||
public bool IsPreviewCommand =>
|
||||
Command.StartsWith("preview-", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public bool IsAirAppBrokerCommand =>
|
||||
string.Equals(Command, AirAppBrokerCommand, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public bool IsGuiCommand =>
|
||||
GuiCommands.Contains(Command, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
namespace LanMountainDesktop.Launcher.Services.AirApp;
|
||||
|
||||
internal sealed class AirAppHostLocator
|
||||
{
|
||||
private const string WindowsExecutableName = "LanMountainDesktop.AirAppHost.exe";
|
||||
private const string DllName = "LanMountainDesktop.AirAppHost.dll";
|
||||
|
||||
public string Resolve(string? packageRoot, string? hostPath = null)
|
||||
{
|
||||
foreach (var candidate in EnumerateCandidates(packageRoot, hostPath))
|
||||
{
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FileNotFoundException("Unable to find LanMountainDesktop.AirAppHost output.");
|
||||
}
|
||||
|
||||
private static IEnumerable<string> EnumerateCandidates(string? packageRoot, string? hostPath)
|
||||
{
|
||||
foreach (var root in EnumerateRoots(packageRoot, hostPath))
|
||||
{
|
||||
yield return Path.Combine(root, "AirAppHost", WindowsExecutableName);
|
||||
yield return Path.Combine(root, "AirAppHost", DllName);
|
||||
yield return Path.Combine(root, WindowsExecutableName);
|
||||
yield return Path.Combine(root, DllName);
|
||||
|
||||
if (Directory.Exists(root))
|
||||
{
|
||||
foreach (var deploymentDirectory in Directory.GetDirectories(root, "app-*", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
yield return Path.Combine(deploymentDirectory, "AirAppHost", WindowsExecutableName);
|
||||
yield return Path.Combine(deploymentDirectory, "AirAppHost", DllName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var current = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
for (var depth = 0; depth < 8 && current is not null; depth++, current = current.Parent)
|
||||
{
|
||||
yield return Path.Combine(
|
||||
current.FullName,
|
||||
"LanMountainDesktop.AirAppHost",
|
||||
"bin",
|
||||
#if DEBUG
|
||||
"Debug",
|
||||
#else
|
||||
"Release",
|
||||
#endif
|
||||
"net10.0",
|
||||
WindowsExecutableName);
|
||||
|
||||
yield return Path.Combine(
|
||||
current.FullName,
|
||||
"LanMountainDesktop.AirAppHost",
|
||||
"bin",
|
||||
#if DEBUG
|
||||
"Debug",
|
||||
#else
|
||||
"Release",
|
||||
#endif
|
||||
"net10.0",
|
||||
DllName);
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<string> EnumerateRoots(string? packageRoot, string? hostPath)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(packageRoot))
|
||||
{
|
||||
yield return Path.GetFullPath(packageRoot);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(hostPath))
|
||||
{
|
||||
var hostDirectory = Path.GetDirectoryName(Path.GetFullPath(hostPath));
|
||||
if (!string.IsNullOrWhiteSpace(hostDirectory))
|
||||
{
|
||||
yield return hostDirectory;
|
||||
}
|
||||
}
|
||||
|
||||
yield return AppContext.BaseDirectory;
|
||||
yield return Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, ".."));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace LanMountainDesktop.Launcher.Services.AirApp;
|
||||
|
||||
internal static class AirAppInstanceKey
|
||||
{
|
||||
public static string Build(string appId, string? sourceComponentId, string? sourcePlacementId)
|
||||
{
|
||||
var normalizedAppId = Normalize(appId, "unknown");
|
||||
var normalizedComponentId = Normalize(sourceComponentId, "none");
|
||||
var normalizedPlacementId = Normalize(sourcePlacementId, "none");
|
||||
return $"{normalizedAppId}:{normalizedComponentId}:{normalizedPlacementId}";
|
||||
}
|
||||
|
||||
private static string Normalize(string? value, string fallback)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value)
|
||||
? fallback
|
||||
: value.Trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace LanMountainDesktop.Launcher.Services.AirApp;
|
||||
|
||||
internal interface IAirAppProcessStarter
|
||||
{
|
||||
Process? Start(string appId, string sessionId, string instanceKey, string? sourceComponentId, string? sourcePlacementId);
|
||||
}
|
||||
|
||||
internal sealed class AirAppProcessStarter : IAirAppProcessStarter
|
||||
{
|
||||
private readonly AirAppHostLocator _locator;
|
||||
private readonly Func<string?> _packageRootProvider;
|
||||
private readonly Func<string?> _hostPathProvider;
|
||||
|
||||
public AirAppProcessStarter(
|
||||
AirAppHostLocator locator,
|
||||
Func<string?> packageRootProvider,
|
||||
Func<string?> hostPathProvider)
|
||||
{
|
||||
_locator = locator;
|
||||
_packageRootProvider = packageRootProvider;
|
||||
_hostPathProvider = hostPathProvider;
|
||||
}
|
||||
|
||||
public Process? Start(
|
||||
string appId,
|
||||
string sessionId,
|
||||
string instanceKey,
|
||||
string? sourceComponentId,
|
||||
string? sourcePlacementId)
|
||||
{
|
||||
var hostPath = _locator.Resolve(_packageRootProvider(), _hostPathProvider());
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
UseShellExecute = false,
|
||||
WorkingDirectory = Path.GetDirectoryName(hostPath) ?? AppContext.BaseDirectory
|
||||
};
|
||||
|
||||
if (OperatingSystem.IsWindows() &&
|
||||
string.Equals(Path.GetExtension(hostPath), ".exe", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
startInfo.FileName = hostPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
startInfo.FileName = "dotnet";
|
||||
startInfo.ArgumentList.Add(hostPath);
|
||||
}
|
||||
|
||||
AddArgument(startInfo, "--app-id", appId);
|
||||
AddArgument(startInfo, "--session-id", sessionId);
|
||||
AddArgument(startInfo, "--instance-key", instanceKey);
|
||||
AddArgument(startInfo, "--launcher-pipe", LanMountainDesktop.Shared.IPC.IpcConstants.AirAppLifecyclePipeName);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(sourceComponentId))
|
||||
{
|
||||
AddArgument(startInfo, "--source-component-id", sourceComponentId.Trim());
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(sourcePlacementId))
|
||||
{
|
||||
AddArgument(startInfo, "--source-placement-id", sourcePlacementId.Trim());
|
||||
}
|
||||
|
||||
return Process.Start(startInfo);
|
||||
}
|
||||
|
||||
private static void AddArgument(ProcessStartInfo startInfo, string name, string value)
|
||||
{
|
||||
startInfo.ArgumentList.Add(name);
|
||||
startInfo.ArgumentList.Add(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using LanMountainDesktop.Shared.IPC;
|
||||
using LanMountainDesktop.Shared.IPC.Abstractions.Services;
|
||||
|
||||
namespace LanMountainDesktop.Launcher.Services.AirApp;
|
||||
|
||||
internal sealed class LauncherAirAppLifecycleIpcHost : IDisposable
|
||||
{
|
||||
private readonly PublicIpcHostService _host;
|
||||
|
||||
public LauncherAirAppLifecycleIpcHost(LauncherAirAppLifecycleService lifecycleService)
|
||||
{
|
||||
LifecycleService = lifecycleService;
|
||||
_host = new PublicIpcHostService(IpcConstants.AirAppLifecyclePipeName);
|
||||
_host.RegisterPublicService<IAirAppLifecycleService>(lifecycleService);
|
||||
}
|
||||
|
||||
public LauncherAirAppLifecycleService LifecycleService { get; }
|
||||
|
||||
public void Start()
|
||||
{
|
||||
_host.Start();
|
||||
Logger.Info($"Air APP lifecycle IPC started. Pipe='{IpcConstants.AirAppLifecyclePipeName}'.");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_host.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using LanMountainDesktop.Shared.IPC.Abstractions.Services;
|
||||
|
||||
namespace LanMountainDesktop.Launcher.Services.AirApp;
|
||||
|
||||
internal sealed class LauncherAirAppLifecycleService : IAirAppLifecycleService
|
||||
{
|
||||
private readonly object _gate = new();
|
||||
private readonly IAirAppProcessStarter _processStarter;
|
||||
private readonly Dictionary<string, ManagedAirAppInstance> _instances = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public LauncherAirAppLifecycleService(IAirAppProcessStarter processStarter)
|
||||
{
|
||||
_processStarter = processStarter;
|
||||
}
|
||||
|
||||
public Task<AirAppOperationResult> OpenAsync(AirAppOpenRequest request)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
var appId = Normalize(request.AppId, "unknown");
|
||||
var instanceKey = AirAppInstanceKey.Build(appId, request.SourceComponentId, request.SourcePlacementId);
|
||||
Logger.Info(
|
||||
$"Air APP open requested. AppId='{appId}'; InstanceKey='{instanceKey}'; RequesterProcessId={request.RequesterProcessId}.");
|
||||
|
||||
lock (_gate)
|
||||
{
|
||||
CleanupExitedInstances();
|
||||
|
||||
if (_instances.TryGetValue(instanceKey, out var existing) && IsProcessAlive(existing.ProcessId))
|
||||
{
|
||||
TryActivateProcess(existing.ProcessId);
|
||||
existing.Touch();
|
||||
return Task.FromResult(BuildResult(true, "activated_existing", "Activated existing Air APP instance.", existing));
|
||||
}
|
||||
|
||||
var sessionId = Guid.NewGuid().ToString("N");
|
||||
try
|
||||
{
|
||||
var process = _processStarter.Start(
|
||||
appId,
|
||||
sessionId,
|
||||
instanceKey,
|
||||
request.SourceComponentId,
|
||||
request.SourcePlacementId);
|
||||
if (process is null)
|
||||
{
|
||||
return Task.FromResult(BuildResult(false, "start_failed", "AirAppHost process was not created.", null));
|
||||
}
|
||||
|
||||
var instance = new ManagedAirAppInstance(
|
||||
instanceKey,
|
||||
appId,
|
||||
sessionId,
|
||||
process.Id,
|
||||
$"{appId} - Air APP",
|
||||
request.SourceComponentId,
|
||||
request.SourcePlacementId);
|
||||
_instances[instanceKey] = instance;
|
||||
Logger.Info($"Started Air APP. AppId='{appId}'; InstanceKey='{instanceKey}'; ProcessId={process.Id}.");
|
||||
return Task.FromResult(BuildResult(true, "started", "Started Air APP instance.", instance));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Warn($"Failed to start Air APP '{appId}': {ex.Message}");
|
||||
return Task.FromResult(BuildResult(false, "start_failed", ex.Message, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task<AirAppOperationResult> ActivateAsync(string instanceKey)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
CleanupExitedInstances();
|
||||
if (!_instances.TryGetValue(instanceKey, out var instance))
|
||||
{
|
||||
return Task.FromResult(BuildResult(false, "not_found", "Air APP instance was not found.", null));
|
||||
}
|
||||
|
||||
var accepted = TryActivateProcess(instance.ProcessId);
|
||||
instance.Touch();
|
||||
return Task.FromResult(BuildResult(
|
||||
accepted,
|
||||
accepted ? "activated" : "activation_failed",
|
||||
accepted ? "Air APP instance activated." : "Failed to activate Air APP instance.",
|
||||
instance));
|
||||
}
|
||||
}
|
||||
|
||||
public Task<AirAppOperationResult> CloseAsync(string instanceKey)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
CleanupExitedInstances();
|
||||
if (!_instances.TryGetValue(instanceKey, out var instance))
|
||||
{
|
||||
return Task.FromResult(BuildResult(false, "not_found", "Air APP instance was not found.", null));
|
||||
}
|
||||
|
||||
var accepted = TryCloseProcess(instance.ProcessId);
|
||||
instance.Touch();
|
||||
return Task.FromResult(BuildResult(
|
||||
accepted,
|
||||
accepted ? "close_requested" : "close_failed",
|
||||
accepted ? "Air APP close requested." : "Failed to request Air APP close.",
|
||||
instance));
|
||||
}
|
||||
}
|
||||
|
||||
public Task<AirAppInstanceInfo[]> GetInstancesAsync()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
CleanupExitedInstances();
|
||||
return Task.FromResult(_instances.Values.Select(static instance => instance.ToInfo()).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public Task<AirAppOperationResult> RegisterAsync(AirAppRegistrationRequest request)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
lock (_gate)
|
||||
{
|
||||
var instanceKey = string.IsNullOrWhiteSpace(request.InstanceKey)
|
||||
? AirAppInstanceKey.Build(request.AppId, request.SourceComponentId, request.SourcePlacementId)
|
||||
: request.InstanceKey.Trim();
|
||||
var instance = new ManagedAirAppInstance(
|
||||
instanceKey,
|
||||
Normalize(request.AppId, "unknown"),
|
||||
Normalize(request.SessionId, Guid.NewGuid().ToString("N")),
|
||||
request.ProcessId,
|
||||
Normalize(request.WindowTitle, $"{request.AppId} - Air APP"),
|
||||
request.SourceComponentId,
|
||||
request.SourcePlacementId);
|
||||
_instances[instanceKey] = instance;
|
||||
Logger.Info($"Registered Air APP. AppId='{instance.AppId}'; InstanceKey='{instanceKey}'; ProcessId={instance.ProcessId}.");
|
||||
return Task.FromResult(BuildResult(true, "registered", "Air APP instance registered.", instance));
|
||||
}
|
||||
}
|
||||
|
||||
public Task<AirAppOperationResult> UnregisterAsync(string instanceKey, int processId)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
if (_instances.TryGetValue(instanceKey, out var instance) &&
|
||||
(processId <= 0 || instance.ProcessId == processId))
|
||||
{
|
||||
_instances.Remove(instanceKey);
|
||||
Logger.Info($"Unregistered Air APP. InstanceKey='{instanceKey}'; ProcessId={processId}.");
|
||||
return Task.FromResult(BuildResult(true, "unregistered", "Air APP instance unregistered.", instance));
|
||||
}
|
||||
|
||||
return Task.FromResult(BuildResult(false, "not_found", "Air APP instance was not found.", null));
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasLiveAirApps()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
CleanupExitedInstances();
|
||||
return _instances.Values.Any(static instance => IsProcessAlive(instance.ProcessId));
|
||||
}
|
||||
}
|
||||
|
||||
private void CleanupExitedInstances()
|
||||
{
|
||||
var exitedKeys = _instances
|
||||
.Where(static pair => !IsProcessAlive(pair.Value.ProcessId))
|
||||
.Select(static pair => pair.Key)
|
||||
.ToList();
|
||||
|
||||
foreach (var key in exitedKeys)
|
||||
{
|
||||
_instances.Remove(key);
|
||||
Logger.Info($"Pruned exited Air APP instance. InstanceKey='{key}'.");
|
||||
}
|
||||
}
|
||||
|
||||
private static AirAppOperationResult BuildResult(
|
||||
bool accepted,
|
||||
string code,
|
||||
string message,
|
||||
ManagedAirAppInstance? instance)
|
||||
{
|
||||
return new AirAppOperationResult(accepted, code, message, instance?.ToInfo());
|
||||
}
|
||||
|
||||
private static bool TryActivateProcess(int processId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var process = Process.GetProcessById(processId);
|
||||
if (process.HasExited)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
process.Refresh();
|
||||
var handle = process.MainWindowHandle;
|
||||
if (handle == IntPtr.Zero)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
_ = ShowWindow(handle, SW_SHOWNORMAL);
|
||||
_ = SetForegroundWindow(handle);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryCloseProcess(int processId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var process = Process.GetProcessById(processId);
|
||||
if (process.HasExited)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return process.CloseMainWindow();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsProcessAlive(int processId)
|
||||
{
|
||||
if (processId <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using var process = Process.GetProcessById(processId);
|
||||
return !process.HasExited;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string Normalize(string? value, string fallback)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value)
|
||||
? fallback
|
||||
: value.Trim();
|
||||
}
|
||||
|
||||
private const int SW_SHOWNORMAL = 1;
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||
|
||||
private sealed class ManagedAirAppInstance
|
||||
{
|
||||
private readonly DateTimeOffset _startedAtUtc = DateTimeOffset.UtcNow;
|
||||
|
||||
public ManagedAirAppInstance(
|
||||
string instanceKey,
|
||||
string appId,
|
||||
string sessionId,
|
||||
int processId,
|
||||
string windowTitle,
|
||||
string? sourceComponentId,
|
||||
string? sourcePlacementId)
|
||||
{
|
||||
InstanceKey = instanceKey;
|
||||
AppId = appId;
|
||||
SessionId = sessionId;
|
||||
ProcessId = processId;
|
||||
WindowTitle = windowTitle;
|
||||
SourceComponentId = sourceComponentId;
|
||||
SourcePlacementId = sourcePlacementId;
|
||||
UpdatedAtUtc = _startedAtUtc;
|
||||
}
|
||||
|
||||
public string InstanceKey { get; }
|
||||
|
||||
public string AppId { get; }
|
||||
|
||||
public string SessionId { get; }
|
||||
|
||||
public int ProcessId { get; }
|
||||
|
||||
public string WindowTitle { get; }
|
||||
|
||||
public string? SourceComponentId { get; }
|
||||
|
||||
public string? SourcePlacementId { get; }
|
||||
|
||||
public DateTimeOffset UpdatedAtUtc { get; private set; }
|
||||
|
||||
public void Touch()
|
||||
{
|
||||
UpdatedAtUtc = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public AirAppInstanceInfo ToInfo()
|
||||
{
|
||||
return new AirAppInstanceInfo(
|
||||
InstanceKey,
|
||||
AppId,
|
||||
SessionId,
|
||||
ProcessId,
|
||||
WindowTitle,
|
||||
SourceComponentId,
|
||||
SourcePlacementId,
|
||||
IsProcessAlive(ProcessId),
|
||||
_startedAtUtc,
|
||||
UpdatedAtUtc);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user