-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathServer.cs
293 lines (259 loc) · 11.3 KB
/
Server.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
namespace EasyPipes
{
/// <summary>
/// <see cref="NamedPipeClientStream"/> based IPC server
/// </summary>
public class Server
{
/// <summary>
/// Name of the pipe
/// </summary>
public string PipeName { get; private set; }
/// <summary>
/// Token to cancel server operations
/// </summary>
public CancellationTokenSource CancellationToken { get; private set; }
/// <summary>
/// List of types registered with serializer
/// <seealso cref="System.Runtime.Serialization.DataContractSerializer.KnownTypes"/>
/// </summary>
public List<Type> KnownTypes { get; private set; }
const int NumberOfThreads = 2;
public const int ReadTimeOut = 30000;
readonly Dictionary<string, object> services = new Dictionary<string, object>();
readonly Dictionary<string, Type> types = new Dictionary<string, Type>();
protected List<Task> serverTask;
protected Func<string, int, NamedPipeServerStream> pipeOpeningFunc;
/// <summary>
/// Construct server
/// </summary>
/// <param name="pipe">Name of the pipe to use</param>
/// <param name="pipeOpening">Optional delegate to open PipeStream (for example through NamedPipeServerStreamAcl)</param>
public Server(string pipe, Func<string, int, NamedPipeServerStream> pipeOpening = null)
{
PipeName = pipe;
CancellationToken = new CancellationTokenSource();
KnownTypes = new List<Type>();
if (pipeOpening == null)
pipeOpeningFunc = OpenPipe;
else
pipeOpeningFunc = pipeOpening;
}
/// <summary>
/// Register a service interface on the server
/// This is a one instance for all clients service
/// </summary>
/// <typeparam name="T">The interface of the service</typeparam>
/// <param name="instance">Instance of a class implementing the service</param>
public void RegisterService<T>(T instance)
{
if(!typeof(T).IsInterface)
throw new InvalidOperationException("Service Type is not an interface");
if (!(instance is T))
throw new InvalidOperationException("Instance must implement service interface");
services[typeof(T).Name] = instance;
types[typeof(T).Name] = typeof(T);
IpcStream.ScanInterfaceForTypes(typeof(T), KnownTypes);
}
/// <summary>
/// Register a service interface on the server that keeps its state for a connection
/// This a one instance for one client/connection service
/// </summary>
/// <typeparam name="T">The interface of the service</typeparam>
/// <param name="t">Type of the class implementing the service, should have a default constructor
/// which will be called on connection of a new client</param>
public void RegisterStatefulService<T>(Type t)
{
if (!typeof(T).IsInterface)
throw new InvalidOperationException("Service Type is not an interface");
// check if service implements interface
if (t.GetInterface(typeof(T).Name) == null)
throw new InvalidOperationException("Instance must implement service interface");
// check for default constructor
if (t.GetConstructor(Type.EmptyTypes) == null || t.IsAbstract)
throw new InvalidOperationException("Stateful service requires default constructor");
services[typeof(T).Name] = new StatefulProxy(t);
types[typeof(T).Name] = typeof(T);
IpcStream.ScanInterfaceForTypes(typeof(T), KnownTypes);
}
/// <summary>
/// Deregister a service interface from the server
/// </summary>
/// <typeparam name="T">The interface of the service</typeparam>
public void DeregisterService<T>()
{
// TODO deregister StatefulProxy
services.Remove(typeof(T).Name);
types.Remove(typeof(T).Name);
}
/// <summary>
/// Start running the server tasks
/// Note, this will spawn asynchronous servers and return immediatly
/// </summary>
public void Start()
{
if (serverTask != null)
throw new InvalidOperationException("Already running");
// asynchonously start the receiving threads, store task for later await
serverTask = new List<Task>();
DoStart();
}
/// <summary>
/// Spawn server tasks
/// </summary>
protected virtual void DoStart()
{
// Start 1 receive actions
for(short i=0; i<NumberOfThreads; i)
{
Task t = Task.Factory.StartNew(ReceiveAction);
serverTask.Add(t);
}
}
/// <summary>
/// Stop the server
/// </summary>
public virtual void Stop()
{
// send cancel token and wait for threads to end
CancellationToken.Cancel();
Task.WaitAll(serverTask.ToArray(), 500);
serverTask = null;
}
/// <summary>
/// Default creation of PipeStream
/// </summary>
/// <param name="name">Name of the pipe</param>
/// <param name="number">Max number of instances</param>
/// <returns>Opened Pipestream</returns>
protected virtual NamedPipeServerStream OpenPipe(string name, int number)
{
return new NamedPipeServerStream(
name,
PipeDirection.InOut,
number,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous);
}
/// <summary>
/// Wait on connection and received messages
/// </summary>
protected virtual void ReceiveAction()
{
try
{
using (NamedPipeServerStream serverStream = pipeOpeningFunc(PipeName, NumberOfThreads))
{
Task t = serverStream.WaitForConnectionAsync(CancellationToken.Token);
t.GetAwaiter().GetResult();
Guid id = Guid.NewGuid();
IpcStream stream = new IpcStream(serverStream, KnownTypes);
while (ProcessMessage(stream, id))
{ }
StatefulProxy.NotifyDisconnect(id);
}
// Todo: make sure there's a new listener, even when this isn't reached
serverTask.Add(Task.Factory.StartNew(ReceiveAction));
}
catch (OperationCanceledException) { }
}
/// <summary>
/// Process a received message by calling the corresponding method on the service instance and
/// returning the return value over the network.
/// </summary>
/// <param name="stream">The message stream</param>
/// <param name="streamId">A GUID identifying this connection</param>
public bool ProcessMessage(IpcStream stream, Guid streamId)
{
IpcMessage msg = stream.ReadMessage();
// this was a close-connection notification
if (msg.StatusMsg == StatusMessage.CloseConnection)
return false;
// or this is just a keepalive ping
else if (msg.StatusMsg == StatusMessage.Ping)
return true;
bool processedOk = false;
string error = "";
object rv = null;
IpcMessage returnMsg = new IpcMessage();
// find the service
if (services.TryGetValue(msg.Service, out object instance) && instance != null)
{
// double check method existence against type-list for security
// typelist will contain interfaces instead of instances
if (types[msg.Service].GetMethod(msg.Method) != null )
{
// separate handling for stateful service
if (instance is StatefulProxy)
{
try
{
// invoke method
System.Reflection.MethodInfo method =
(instance as StatefulProxy).Type.GetMethod(msg.Method);
if (method == null)
throw new InvalidOperationException("Method not found in stateful proxy");
rv = (instance as StatefulProxy).Invoke(streamId, method, msg.Parameters);
processedOk = true;
// check if encryption is required
if(Attribute.IsDefined(method, typeof(EncryptIfTrueAttribute))
&& (bool)rv == true)
{
returnMsg.StatusMsg = StatusMessage.Encrypt;
}
}
catch (Exception e) { error = e.ToString(); }
}
else
{
// get the method
System.Reflection.MethodInfo method = instance.GetType().GetMethod(msg.Method);
if (method != null)
{
try
{
// invoke method
rv = method.Invoke(instance, msg.Parameters);
processedOk = true;
// check if encryption is required
if (Attribute.IsDefined(method, typeof(EncryptIfTrueAttribute))
&& (bool)rv == true)
returnMsg.StatusMsg = StatusMessage.Encrypt;
}
catch (Exception e) { error = e.ToString(); }
}
else
error = "Could not find method";
}
}
else
error = "Could not find method in type";
}
else
error = "Could not find service";
// return either return value or error message
if (processedOk)
returnMsg.Return = rv;
else
{
returnMsg.Error = error;
returnMsg.StatusMsg = StatusMessage.None;
}
stream.WriteMessage(returnMsg);
// if there's more to come, keep reading a next message
if (msg.StatusMsg == StatusMessage.KeepAlive)
return true;
else // otherwise close the connection
return false;
}
}
}