-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNamedPipe.cs
206 lines (180 loc) · 5.88 KB
/
NamedPipe.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace Qemu_GUI
{
public enum ConnectionMode
{
MODE_CLIENT = 0x00000000,
MODE_SERVER = 0x00000001,
MODE_AUTO = 0x00000002
}
public class NamedPipe : Pipe
{
public const int PIPE_SIZE = 1024;
private PipeStream ioStream; /* stream for io operations */
private String wCommand; /* buffer of a single command line */
private List<String> cmdList; /*list of commands pending to be written */
public event PipeReceiveEventHandler PipeReceiveEvent;
public event PipeErrorEventHandler PipeErrorEvent;
private static ManualResetEvent newWriteData = new ManualResetEvent(false);
public NamedPipe()
{
cmdList = new List<string>();
}
public bool CreateServerPipe(string name)
{
/* create a pipe and wait for a client */
NamedPipeServerStream sStream = new NamedPipeServerStream(name, PipeDirection.InOut, 1,
PipeTransmissionMode.Byte, PipeOptions.Asynchronous, PIPE_SIZE, PIPE_SIZE);
sStream.WaitForConnection();
if (sStream.IsConnected)
{
ioStream = sStream;
return true;
}
else
{
return false;
}
}
public bool CreateClientPipe(string name)
{
/* try to connect as a client */
/* (QEMU -serial pipe or VMware in pipe server mode) */
try
{
NamedPipeClientStream cStream = new NamedPipeClientStream(".", name, PipeDirection.InOut, PipeOptions.Asynchronous);
cStream.Connect(100);
if (cStream.IsConnected)
{
ioStream = cStream;
return true;
}
else
{
return false;
}
}
catch (Exception)
{
return false;
}
}
public bool CreatePipe(string name, ConnectionMode mode)
{
if (name == "" || name == null)
{
return false;
}
switch (mode)
{
case ConnectionMode.MODE_AUTO:
//check if pipe exists, if not create server pipe, wait certain time, check if pipe...
//TODO: server-client lookup should time out
while (true)
{
if (CreateClientPipe(name))
{
break;
}
if (CreateServerPipe(name))
{
break;
}
}
return true;
case ConnectionMode.MODE_CLIENT:
while (!CreateClientPipe(name)) ;
/* pipe open, everything fine */
return true;
case ConnectionMode.MODE_SERVER:
if (CreateServerPipe(name))
{
/* wait for a client*/
return true;
}
break;
}
return false;
}
public void Close()
{
if (ioStream != null)
ioStream.Close();
}
public void WriteLoop()
{
try
{
while (true)
{
if (cmdList.Count > 0)
{
byte[] wBuf = new byte[cmdList[0].Length];
UTF8Encoding.UTF8.GetBytes(cmdList[0], 0, cmdList[0].Length, wBuf, 0);
ioStream.Write(wBuf, 0, cmdList[0].Length);
/* remove written data from commandlist */
cmdList.RemoveAt(0);
}
else if (cmdList.Count == 0)
{
/* wait until new data is signaled */
newWriteData.Reset();
newWriteData.WaitOne();
}
}
}
catch (Exception e)
{
if (PipeErrorEvent != null)
PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
}
}
public void ReadLoop()
{
byte[] buf = new byte[PIPE_SIZE];
int read = 0;
try
{
while (true)
{
read = ioStream.Read(buf, 0, PIPE_SIZE);
if (read > 0)
{
if (PipeReceiveEvent != null)
PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(UTF8Encoding.UTF8.GetString(buf, 0, read)));
}
else
{
/* connecion closed */
break;
}
}
}
catch (Exception e)
{
if (PipeErrorEvent != null)
PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
}
}
public bool Write(string str)
{
/* only forward a complete line */
wCommand += str;
if (str[str.Length - 1] == '\r')
{
cmdList.Add(wCommand);
wCommand = null;
/* wake up the write thread */
newWriteData.Set();
}
return true;
}
}
}