diff --git a/Marsey/IPC/Client.cs b/Marsey/IPC/Client.cs index 9166216..0995909 100644 --- a/Marsey/IPC/Client.cs +++ b/Marsey/IPC/Client.cs @@ -12,7 +12,7 @@ public string ConnRecv(string name) try { - pipeClient.Connect(150); // Pipe should connect immediately + pipeClient.Connect(2000); } catch(TimeoutException) { diff --git a/Marsey/IPC/Server.cs b/Marsey/IPC/Server.cs index 870161f..736d420 100644 --- a/Marsey/IPC/Server.cs +++ b/Marsey/IPC/Server.cs @@ -1,4 +1,4 @@ -using System.IO.Pipes; +using System.IO.Pipes; using System.Text; using Marsey.Misc; @@ -17,20 +17,15 @@ public async Task ReadySend(string name, string data) { MarseyLogger.Log(MarseyLogger.LogType.INFO, "IPC-SERVER", $"Opening {name} (attempt {attempt}/{maxAttempts})"); - await using NamedPipeServerStream pipeServer = new NamedPipeServerStream( + var pipeServer = new NamedPipeServerStream( name, PipeDirection.Out, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); - await pipeServer.WaitForConnectionAsync(); - - byte[] buffer = Encoding.UTF8.GetBytes(data); - await pipeServer.WriteAsync(buffer); - - MarseyLogger.Log(MarseyLogger.LogType.INFO, "IPC-SERVER", $"Closing {name}"); - pipeServer.Close(); + var wait = pipeServer.WaitForConnectionAsync(); + _ = SendWhenConnected(pipeServer, wait, name, data); return; } catch (IOException ex) when (attempt < maxAttempts) @@ -42,4 +37,23 @@ public async Task ReadySend(string name, string data) MarseyLogger.Log(MarseyLogger.LogType.ERRO, "IPC-SERVER", $"Failed to open pipe {name}: all attempts exhausted."); } + + private static async Task SendWhenConnected(NamedPipeServerStream pipeServer, Task wait, string name, string data) + { + try + { + await wait; + byte[] buffer = Encoding.UTF8.GetBytes(data); + await pipeServer.WriteAsync(buffer); + MarseyLogger.Log(MarseyLogger.LogType.INFO, "IPC-SERVER", $"Closing {name}"); + } + catch (Exception ex) + { + MarseyLogger.Log(MarseyLogger.LogType.ERRO, "IPC-SERVER", $"Failed to send {name}: {ex.Message}"); + } + finally + { + await pipeServer.DisposeAsync(); + } + } } diff --git a/SS14.Launcher/Models/Connector.cs b/SS14.Launcher/Models/Connector.cs index 98928b1..6ba261e 100644 --- a/SS14.Launcher/Models/Connector.cs +++ b/SS14.Launcher/Models/Connector.cs @@ -650,16 +650,14 @@ private async Task InstallContentBundleAsync( return null; } - // Prepare IPC pipes for Marsey without blocking process start. - // Pipes must be opened before/around launch, but awaiting here deadlocks - // because the loader is the one that connects to them. - _ = Marsify().ContinueWith(t => + try { - if (t.Exception != null) - { - Log.Error(t.Exception, "Marsey IPC preparation failed"); - } - }, TaskScheduler.Default); + await Marsify(); + } + catch (Exception e) + { + Log.Error(e, "Marsey IPC preparation failed"); + } ConfigureEnvironmentVariables(startInfo, launchInfo, engineVersion); ConfigureLogging(startInfo);