-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
618d0c2
commit da1f36a
Showing
10 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using ControlzEx.Standard; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.IO.MemoryMappedFiles; | ||
using System.IO.Pipes; | ||
using System.Linq; | ||
using System.Security.AccessControl; | ||
using System.Security.Principal; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OngekiFumenEditor.Utils | ||
{ | ||
internal static class IPCHelper | ||
{ | ||
const int FileSize = 10240; | ||
private static MemoryMappedFile mmf; | ||
private static bool enableMultiProc; | ||
|
||
internal class ArgsWrapper | ||
{ | ||
public string[] Args { get; set; } | ||
} | ||
|
||
static IPCHelper() | ||
{ | ||
enableMultiProc = Properties.ProgramSetting.Default.EnableMultiInstances; | ||
} | ||
|
||
public static void Init() | ||
{ | ||
if (enableMultiProc) | ||
return; | ||
|
||
mmf = MemoryMappedFile.CreateOrOpen("OngekiFumenEditor_MMF", FileSize, MemoryMappedFileAccess.ReadWrite); | ||
using var accessor = mmf.CreateViewAccessor(0, FileSize); | ||
|
||
var pid = accessor.ReadInt32(0); | ||
if (pid != 0) | ||
{ | ||
var process = Process.GetProcessById(pid); | ||
if (process is not null) | ||
{ | ||
//send to host | ||
var r = "CMD:" + JsonSerializer.Serialize(new ArgsWrapper() { Args = Environment.GetCommandLineArgs().Skip(1).ToArray() }); | ||
|
||
var buffer = Encoding.UTF8.GetBytes(r); | ||
accessor.WriteArray(sizeof(int) * 2, buffer, 0, Math.Min(buffer.Length, FileSize - sizeof(int) * 2)); | ||
accessor.Write(sizeof(int), buffer.Length); | ||
accessor.Flush(); | ||
|
||
Environment.Exit(0); | ||
return; | ||
} | ||
} | ||
|
||
accessor.Write(0, Process.GetCurrentProcess().Id); | ||
} | ||
|
||
public static string ReadLineAsync(CancellationToken cancellation) | ||
{ | ||
if (enableMultiProc) | ||
return string.Empty; | ||
|
||
using var accessor = mmf.CreateViewAccessor(0, FileSize); | ||
|
||
while (!cancellation.IsCancellationRequested) | ||
{ | ||
var size = accessor.ReadInt32(sizeof(int)); | ||
if (size > 0) | ||
{ | ||
var bytes = new byte[size]; | ||
accessor.ReadArray(sizeof(int) * 2, bytes, 0, size); | ||
accessor.Write(sizeof(int), 0); | ||
|
||
return Encoding.UTF8.GetString(bytes); | ||
} | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} | ||
} |