1 using System.Diagnostics;
4 using System.Threading;
8 static partial class ChildServerRelay
10 public static Process Process;
11 public static bool IsProcessAlive => Process is { HasExited:
false };
13 private static bool localHandlesDisposed;
14 private static AnonymousPipeServerStream writePipe;
15 private static AnonymousPipeServerStream readPipe;
17 public static void Start(ProcessStartInfo processInfo)
20 CrashReportFilePath =
null;
22 writePipe =
new AnonymousPipeServerStream(PipeDirection.Out, System.IO.HandleInheritability.Inheritable);
23 readPipe =
new AnonymousPipeServerStream(PipeDirection.In, System.IO.HandleInheritability.Inheritable);
25 writeStream = writePipe; readStream = readPipe;
29 processInfo.ArgumentList.Add(
"-pipes");
30 processInfo.ArgumentList.Add(writePipe.GetClientHandleAsString());
31 processInfo.ArgumentList.Add(readPipe.GetClientHandleAsString());
34 Process = Process.Start(processInfo);
38 DebugConsole.ThrowError($
"Failed to start ChildServerRelay Process. File: {processInfo.FileName}, arguments: {processInfo.Arguments}");
43 localHandlesDisposed =
false;
46 public static void DisposeLocalHandles()
48 if (localHandlesDisposed) {
return; }
49 writePipe.DisposeLocalCopyOfClientHandle(); readPipe.DisposeLocalCopyOfClientHandle();
50 localHandlesDisposed =
true;
53 public static void AttemptGracefulShutDown(
int maxAttempts = 20)
55 status = StatusEnum.RequestedShutDown;
56 writeManualResetEvent?.Set();
58 while (Process is { HasExited:
false })
60 if (checks >= maxAttempts)
62 DebugConsole.AddWarning(
"Server could not be shut down gracefully");
71 public static void ForceShutDown()
73 Process?.Kill(); Process =
null;
77 public static string CrashString {
get;
private set; }
78 public static string CrashReportFilePath {
get;
private set; }
80 public static LocalizedString CrashMessage
81 =>
string.IsNullOrEmpty(CrashReportFilePath)
82 ? TextManager.Get(
"ServerProcessClosed")
83 : TextManager.GetWithVariable(
"ServerProcessCrashed",
"[reportfilepath]", CrashReportFilePath);
85 static partial
void HandleCrashString(
string str)
87 DebugConsole.ThrowError($
"The server has crashed: {str}");
88 CrashReportFilePath = str.Split(
"||").FirstOrDefault() ??
"servercrashreport.log";