-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPSNamedPipes.psm1
218 lines (174 loc) · 7.19 KB
/
PSNamedPipes.psm1
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
$ErrorActionPreference = "Stop"
if (-not ("CallbackEventBridge" -as [type])) {
Add-Type @"
using System;
public sealed class CallbackEventBridge
{
public event AsyncCallback CallbackComplete = delegate {};
private void CallbackInternal(IAsyncResult result)
{
CallbackComplete(result);
}
public AsyncCallback Callback
{
get { return new AsyncCallback(CallbackInternal); }
}
}
"@}
class NamedPipeServer {
hidden $_pipeName = "";
hidden [System.IO.Pipes.NamedPipeServerStream]$_serverPipe = $null;
hidden $_readBuffer = $null;
hidden $_OnConnectedCallbackBridge = $null; # this is our internal async event bridge
hidden [System.Management.Automation.ScriptBlock]$_OnConnectedScriptBlock = {}; # this is the scriptblock the user wants to call
hidden $_OnDataAvailableCallbackBridge = $null; # this is our internal async event bridge
hidden [System.Management.Automation.ScriptBlock]$_OnDataAvailableScriptBlock = {}; # this is the scriptblock the user wants to call
hidden $_OnDataWrittenCallbackBridge = $null; # this is our internal async event bridge
# Constructor
NamedPipeServer ([string]$PipeName){
$this._pipeName = $PipeName
$this._readBuffer = New-Object byte[] 1024
$this._serverPipe = New-Object System.IO.Pipes.NamedPipeServerStream $PipeName, InOut, 1, Byte, Asynchronous, 1024, 1024
# Create a bridge for OnConnected async events
$this._OnConnectedCallbackBridge = New-Object CallbackEventBridge
Register-ObjectEvent -InputObject $this._OnConnectedCallbackBridge -EventName CallbackComplete `
-Action {
param($asyncResult)
$asyncResult.AsyncState.OnClientConnected.Invoke($asyncResult);
} > $null
# Create a bridge for OnDataAvailable async events
$this._OnDataAvailableCallbackBridge = New-Object CallbackEventBridge
Register-ObjectEvent -InputObject $this._OnDataAvailableCallbackBridge -EventName CallbackComplete `
-Action {
param($asyncResult)
$asyncResult.AsyncState.OnDataAvailable.Invoke($asyncResult);
} > $null
# Create a bridge for OnDataWritten async events
$this._OnDataWrittenCallbackBridge = New-Object CallbackEventBridge
Register-ObjectEvent -InputObject $this._OnDataWrittenCallbackBridge -EventName CallbackComplete `
-Action {
param($asyncResult)
$asyncResult.AsyncState.OnDataWritten.Invoke($asyncResult);
} > $null
}
[void]WaitForConnection(){
$this._serverPipe.WaitForConnection();
}
[void]BeginWaitForConnection([System.Management.Automation.ScriptBlock]$Callback = {}){
$this._OnConnectedScriptBlock = $Callback; # Save the script the user wants to call on connect
$this._serverPipe.BeginWaitForConnection($this._OnConnectedCallbackBridge.Callback, $this);
}
hidden [void]OnClientConnected($asyncResult){
$this._serverPipe.EndWaitForConnection($asyncResult)
$this._OnConnectedScriptBlock.Invoke($this);
}
[void]BeginRead([System.Management.Automation.ScriptBlock]$Callback){
$this._OnDataAvailableScriptBlock = $Callback; # Save the script the user wants to call when data is available
$this._serverPipe.BeginRead($this._readBuffer, 0, $this._readBuffer.Length, $this._OnDataAvailableCallbackBridge.Callback, $this)
}
hidden [void]OnDataAvailable($asyncResult){
$MessageLength = $this._serverPipe.EndRead($asyncResult)
if ($MessageLength -gt 0){
$PipeText = [System.Text.Encoding]::UTF8.GetString($this._readBuffer, 0, $MessageLength)
$this._OnDataAvailableScriptBlock.Invoke($PipeText, $this);
$this.BeginRead($this._OnDataAvailableScriptBlock);
} else {
# Pipe was closed
#Write-Verbose "Pipe closed" -ForegroundColor Red
}
}
[void]BeginWrite([string]$Message){
$_writeBufferLocal = [System.Text.Encoding]::UTF8.GetBytes($Message)
$this._serverPipe.BeginWrite($_writeBufferLocal, 0, $_writeBufferLocal.Length, $this._OnDataWrittenCallbackBridge.Callback, $this)
}
hidden [void]OnDataWritten($asyncResult){
$asyncResult._serverPipe.EndWrite($asyncResult)
}
[Boolean] IsConnected(){
return $this._serverPipe.IsConnected
}
[string] GetPipeName(){
return $this._pipeName
}
Close (){
$this._serverPipe.Close();
}
}
class NamedPipeClient{
hidden $_pipeName = "";
hidden [System.IO.Pipes.NamedPipeClientStream]$_clientPipe = $null;
hidden $_readBuffer = $null;
hidden $_OnDataAvailableCallbackBridge = $null; # this is our internal async event bridge
hidden [System.Management.Automation.ScriptBlock]$_OnDataAvailableScriptBlock = {}; # this is the scriptblock the user wants to call
hidden $_OnDataWrittenCallbackBridge = $null; # this is our internal async event bridge
NamedPipeClient ([string]$PipeName){
$this._pipeName = $PipeName
[string]$PipeServer = "."
$this._readBuffer = New-Object byte[] 1024
$this._clientPipe = New-Object System.IO.Pipes.NamedPipeClientStream $PipeServer, $PipeName, InOut, Asynchronous
# Create a bridge for OnDataAvailable async events
$this._OnDataAvailableCallbackBridge = New-Object CallbackEventBridge
Register-ObjectEvent -InputObject $this._OnDataAvailableCallbackBridge -EventName CallbackComplete `
-Action {
param($asyncResult)
$asyncResult.AsyncState.OnDataAvailable.Invoke($asyncResult);
} > $null
# Create a bridge for OnDataWritten async events
$this._OnDataWrittenCallbackBridge = New-Object CallbackEventBridge
Register-ObjectEvent -InputObject $this._OnDataWrittenCallbackBridge -EventName CallbackComplete `
-Action {
param($asyncResult)
$asyncResult.AsyncState.OnDataWritten.Invoke($asyncResult);
} > $null
}
[void]BeginRead([System.Management.Automation.ScriptBlock]$Callback){
$this._OnDataAvailableScriptBlock = $Callback; # Save the script the user wants to call when data is available
$this._clientPipe.BeginRead($this._readBuffer, 0, $this._readBuffer.Length, $this._OnDataAvailableCallbackBridge.Callback, $this)
}
hidden [void]OnDataAvailable($asyncResult){
$MessageLength = $this._clientPipe.EndRead($asyncResult)
if ($MessageLength -gt 0){
$PipeText = [System.Text.Encoding]::UTF8.GetString($this._readBuffer, 0, $MessageLength)
$this._OnDataAvailableScriptBlock.Invoke($PipeText, $this);
$this.BeginRead($this._OnDataAvailableScriptBlock);
} else {
# Pipe was closed
#Write-Verbose "Pipe closed" -ForegroundColor Red
}
}
[void]BeginWrite([string]$Message){
$_writeBufferLocal = [System.Text.Encoding]::UTF8.GetBytes($Message)
$this._clientPipe.BeginWrite($_writeBufferLocal, 0, $_writeBufferLocal.Length, $this._OnDataWrittenCallbackBridge.Callback, $this)
}
hidden [void]OnDataWritten($asyncResult){
$asyncResult._clientPipe.EndWrite($asyncResult)
}
Connect([int]$Timeout = 100){
$this._clientPipe.Connect($Timeout);
}
Connect(){
$this._clientPipe.Connect(100);
}
[string] GetPipeName(){
return $this._pipeName
}
Close (){
$this._clientPipe.Close();
}
}
# We need this function because our classes are not visible outside the module
function New-NamedPipeServer{
Param(
[string]$PipeName
)
return [NamedPipeServer]::new($PipeName)
}
Export-ModuleMember New-NamedPipeServer
# We need this function because our classes are not visible outside the module
function New-NamedPipeClient{
Param(
[string]$PipeName
)
return [NamedPipeClient]::new($PipeName)
}
Export-ModuleMember New-NamedPipeClient