mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-08-18 18:24:56 +02:00
.config
.run
.workdir
Agent
Common
Docker
Server
Utils
Phantom.Utils.Collections
Phantom.Utils.Collections.Tests
Phantom.Utils.Cryptography
Phantom.Utils.Events
Phantom.Utils.IO
Phantom.Utils.Rpc
Phantom.Utils.Runtime
AssemblyAttributes.cs
CancellableBackgroundTask.cs
CancellableSemaphore.cs
EnvironmentVariables.cs
OneShotProcess.cs
Phantom.Utils.Runtime.csproj
PosixSignals.cs
Process.cs
ProcessConfigurator.cs
StopProcedureException.cs
TaskManager.cs
Tasks.cs
WaitHandleExtensions.cs
Phantom.Utils.Runtime.Tests
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json
44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
namespace Phantom.Utils.Runtime;
|
|
|
|
public static class PosixSignals {
|
|
public static void RegisterCancellation(CancellationTokenSource cancellationTokenSource, Action? callback = null) {
|
|
var cancellationCallback = new CancellationCallback(cancellationTokenSource, callback);
|
|
var handlePosixSignal = cancellationCallback.HandlePosixSignal;
|
|
PosixSignalRegistration.Create(PosixSignal.SIGINT, handlePosixSignal);
|
|
PosixSignalRegistration.Create(PosixSignal.SIGTERM, handlePosixSignal);
|
|
PosixSignalRegistration.Create(PosixSignal.SIGQUIT, handlePosixSignal);
|
|
Console.CancelKeyPress += cancellationCallback.HandleConsoleCancel;
|
|
}
|
|
|
|
private sealed class CancellationCallback {
|
|
private readonly CancellationTokenSource cancellationTokenSource;
|
|
private readonly Action? callback;
|
|
|
|
public CancellationCallback(CancellationTokenSource cancellationTokenSource, Action? callback) {
|
|
this.cancellationTokenSource = cancellationTokenSource;
|
|
this.callback = callback;
|
|
}
|
|
|
|
public void HandlePosixSignal(PosixSignalContext context) {
|
|
context.Cancel = true;
|
|
Run();
|
|
}
|
|
|
|
public void HandleConsoleCancel(object? sender, ConsoleCancelEventArgs e) {
|
|
e.Cancel = true;
|
|
Run();
|
|
}
|
|
|
|
private void Run() {
|
|
lock (this) {
|
|
if (!cancellationTokenSource.IsCancellationRequested) {
|
|
cancellationTokenSource.Cancel();
|
|
callback?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|