mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-22 00:54:26 +08:00
fix(launcher): extract startup subsystem and harden IPC detection
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
57
LanMountainDesktop.Launcher/Startup/PublicIpcConnection.cs
Normal file
57
LanMountainDesktop.Launcher/Startup/PublicIpcConnection.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using LanMountainDesktop.Launcher.Services;
|
||||
using LanMountainDesktop.Shared.IPC;
|
||||
|
||||
namespace LanMountainDesktop.Launcher.Startup;
|
||||
|
||||
internal static class PublicIpcConnection
|
||||
{
|
||||
public static async Task<bool> TryConnectAsync(
|
||||
LanMountainDesktopIpcClient ipcClient,
|
||||
TimeSpan timeout,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (ipcClient.IsConnected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var connectTask = ipcClient.ConnectAsync();
|
||||
var completedTask = await Task.WhenAny(connectTask, Task.Delay(timeout, cancellationToken)).ConfigureAwait(false);
|
||||
if (completedTask != connectTask)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
await connectTask.ConfigureAwait(false);
|
||||
return ipcClient.IsConnected;
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
Logger.Info($"Public IPC is not ready yet: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<bool> TryConnectWithBackoffAsync(
|
||||
LanMountainDesktopIpcClient ipcClient,
|
||||
IReadOnlyList<TimeSpan> attemptTimeouts,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (ipcClient.IsConnected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var timeout in attemptTimeouts)
|
||||
{
|
||||
if (await TryConnectAsync(ipcClient, timeout, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return ipcClient.IsConnected;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user