Last active
March 9, 2020 02:35
-
-
Save TheBuzzSaw/a43133a3509d56e36c1d778f76926cbc to your computer and use it in GitHub Desktop.
Working out exactly how pipe inheritance works
This file contains hidden or 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
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.IO.Pipes; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace PipeDream2020 | |
{ | |
class Program | |
{ | |
static async Task DumpAllAsync(Stream stream) | |
{ | |
var buffer = new byte[256]; | |
while (true) | |
{ | |
int n = await stream.ReadAsync(buffer, 0, buffer.Length); | |
if (n < 1) | |
break; | |
var text = Encoding.UTF8.GetString(buffer, 0, n); | |
Console.Write(text); | |
} | |
Console.WriteLine("DumpAllSync ended."); | |
} | |
static async Task RunServerAsync() | |
{ | |
await using var stream = new AnonymousPipeServerStream( | |
PipeDirection.In, | |
HandleInheritability.Inheritable); | |
var task = DumpAllAsync(stream); | |
var thisProcess = Process.GetCurrentProcess(); | |
var thisFile = thisProcess.MainModule.FileName; | |
var processStartInfo = new ProcessStartInfo | |
{ | |
FileName = thisFile, | |
CreateNoWindow = true, | |
UseShellExecute = false, | |
WorkingDirectory = Environment.CurrentDirectory | |
}; | |
var arguments = processStartInfo.ArgumentList; | |
if (Path.GetFileName(thisFile) == "dotnet") | |
arguments.Add(typeof(Program).Assembly.Location); | |
arguments.Add(stream.GetClientHandleAsString()); | |
Console.WriteLine("Starting process 1..."); | |
using (var process = Process.Start(processStartInfo)) | |
process.WaitForExit(); | |
Console.WriteLine("Starting process 2..."); | |
using (var process = Process.Start(processStartInfo)) | |
process.WaitForExit(); | |
Console.WriteLine("Starting dual processes..."); | |
using (var process1 = Process.Start(processStartInfo)) | |
using (var process2 = Process.Start(processStartInfo)) | |
{ | |
process1.WaitForExit(); | |
process2.WaitForExit(); | |
} | |
Console.WriteLine("Starting process 3..."); | |
using (var process = Process.Start(processStartInfo)) | |
{ | |
stream.DisposeLocalCopyOfClientHandle(); | |
process.WaitForExit(); | |
} | |
Console.WriteLine("Starting process 4..."); | |
using (var process = Process.Start(processStartInfo)) | |
process.WaitForExit(); | |
Console.WriteLine("Awaiting stream task..."); | |
await task; | |
Console.WriteLine("Completed."); | |
} | |
static async Task RunClientAsync(string handle) | |
{ | |
await using var stream = new AnonymousPipeClientStream( | |
PipeDirection.Out, | |
handle); | |
var thisProcess = Process.GetCurrentProcess(); | |
for (int i = 0; i < 4; ++i) | |
{ | |
var message = $"Greetings ({i}) from process {thisProcess.Id}.\n"; | |
var bytes = Encoding.UTF8.GetBytes(message); | |
await stream.WriteAsync(bytes, 0, bytes.Length); | |
await Task.Delay(500); | |
} | |
} | |
static async Task Main(string[] args) | |
{ | |
try | |
{ | |
// var thisProcess = Process.GetCurrentProcess(); | |
// var thisFile = thisProcess.MainModule.FileName; | |
// Console.WriteLine(thisFile); | |
// Console.WriteLine(typeof(Program).Assembly.Location); | |
if (args.Length > 0) | |
{ | |
await RunClientAsync(args[0]); | |
} | |
else | |
{ | |
await RunServerAsync(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment