-
Notifications
You must be signed in to change notification settings - Fork 44
/
stdpipe.ps1
70 lines (58 loc) · 2.05 KB
/
stdpipe.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
param (
[string]$ServerName = ".",
[Parameter(Mandatory=$true)][string]$PipeName
)
# Use named pipe as stdin/out
$Source = @"
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading.Tasks;
public static class StdPipe
{
public static void RouteToPipe(string pipeServer, string pipeName)
{
var pipeClient = new NamedPipeClientStream(pipeServer, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
pipeClient.Connect();
var pipeReader = new StreamReader(pipeClient, Console.OutputEncoding);
var pipeWriter = new StreamWriter(pipeClient, Console.InputEncoding);
var pipeBuffer = new char[512];
var stdBuffer = new char[512];
var tasks = new Task<int>[2]
{
pipeReader.ReadAsync(pipeBuffer, 0, pipeBuffer.Length),
ReadFromStdinAsync(stdBuffer, 0, stdBuffer.Length)
};
while (true)
{
var doneIdx = Task.WaitAny(tasks);
var bytesRead = tasks[doneIdx].Result;
if (doneIdx == 0)
{//Input from pipe
if (bytesRead == 0)
{// If the pipe has closed out on us, punk out
break;
}
Console.Out.Write(pipeBuffer, 0, bytesRead);
Console.Out.Flush();
tasks[doneIdx] = pipeReader.ReadAsync(pipeBuffer, 0, pipeBuffer.Length);
}
else
{//Input from stdin
pipeWriter.Write(stdBuffer, 0, bytesRead);
pipeWriter.Flush();
tasks[doneIdx] = ReadFromStdinAsync(stdBuffer, 0, stdBuffer.Length);
}
}
}
private static Task<int> ReadFromStdinAsync(char[] buffer, int offset, int len)
{
return Task.Run(() =>
{
return Console.In.Read(buffer, offset, len);
});
}
}
"@
Add-Type -TypeDefinition $Source -Language CSharp
[StdPipe]::RouteToPipe($ServerName, $PipeName)