Skip to content

Instantly share code, notes, and snippets.

@TheBuzzSaw
Created May 7, 2019 04:56
Show Gist options
  • Select an option

  • Save TheBuzzSaw/1387605d4b6fcbfee1433a0b3ff02604 to your computer and use it in GitHub Desktop.

Select an option

Save TheBuzzSaw/1387605d4b6fcbfee1433a0b3ff02604 to your computer and use it in GitHub Desktop.
These pipes work...
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace PipeBurst
{
class Program
{
static string GetDotNet()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return @"C:\Program Files\dotnet\dotnet.exe";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return "/usr/bin/dotnet";
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return "/usr/bin/dotnet";
throw new PlatformNotSupportedException("What OS are you running, dawg?");
}
static async Task RunServer()
{
Console.WriteLine("Starting server...");
using (var inPipe = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable))
using (var outPipe = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable))
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = GetDotNet(),
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
var argList = process.StartInfo.ArgumentList;
argList.Add(Environment.CommandLine);
argList.Add(outPipe.GetClientHandleAsString());
argList.Add(inPipe.GetClientHandleAsString());
process.Start();
outPipe.DisposeLocalCopyOfClientHandle();
inPipe.DisposeLocalCopyOfClientHandle();
var buffer = new byte[1 << 20];
int used = 0;
while (true)
{
int n = await inPipe.ReadAsync(buffer, used, buffer.Length - used);
if (n < 1)
break;
used += n;
}
Console.WriteLine("[server] detected client hangup");
for (int i = 0; i < used; ++i)
buffer[i] += 13;
await outPipe.WriteAsync(buffer, 0, used);
Console.WriteLine("[server] sent entire buffer");
}
}
static async Task RunClient(string inputHandle, string outputHandle)
{
Console.WriteLine("Starting client...");
using (var inPipe = new AnonymousPipeClientStream(PipeDirection.In, inputHandle))
{
using (var outPipe = new AnonymousPipeClientStream(PipeDirection.Out, outputHandle))
{
const int Size = 1 << 19;
var outBuffer = new byte[64];
var random = new Random();
for (int i = 0; i < Size; i += outBuffer.Length)
{
for (int j = 0; j < outBuffer.Length; ++j)
outBuffer[j] = (byte)('a' + random.Next(13));
await outPipe.WriteAsync(outBuffer, 0, outBuffer.Length);
}
}
using (var fileStream = File.Create("client.txt"))
{
var inBuffer = new byte[1024];
int used = 0;
while (true)
{
int n = await inPipe.ReadAsync(inBuffer, used, inBuffer.Length - used);
if (n < 1)
break;
await fileStream.WriteAsync(inBuffer, 0, n);
await Task.Delay(50);
}
}
}
Console.WriteLine("Ending client...");
}
static async Task Main(string[] args)
{
try
{
if (args.Length > 0)
{
await RunClient(args[0], args[1]);
}
else
{
await RunServer();
}
}
catch (Exception ex)
{
for (var e = ex; e != null; e = e.InnerException)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment