-
Notifications
You must be signed in to change notification settings - Fork 632
/
DicomServer.cs
569 lines (475 loc) · 20.7 KB
/
DicomServer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright (c) 2012-2019 fo-dicom contributors.
// Licensed under the Microsoft Public License (MS-PL).
#if !NET35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Dicom.Log;
namespace Dicom.Network
{
/// <summary>
/// Representation of a DICOM server.
/// </summary>
/// <typeparam name="T">DICOM service that the server should manage.</typeparam>
public class DicomServer<T> : IDicomServer<T> where T : DicomService, IDicomServiceProvider
{
#region FIELDS
private readonly List<Task> _services;
private readonly CancellationTokenSource _cancellationSource;
private string _ipAddress;
private int _port;
private Logger _logger;
private object _userState;
private string _certificateName;
private Encoding _fallbackEncoding;
private bool _isIpAddressSet;
private bool _isPortSet;
private bool _wasStarted;
private bool _disposed;
private readonly AsyncManualResetEvent _hasServicesFlag;
private readonly AsyncManualResetEvent _hasNonMaxServicesFlag;
#endregion
#region CONSTRUCTORS
/// <summary>
/// Initializes an instance of the <see cref="DicomServer{T}"/> class.
/// </summary>
public DicomServer()
{
_cancellationSource = new CancellationTokenSource();
_services = new List<Task>();
IsListening = false;
Exception = null;
_isIpAddressSet = false;
_isPortSet = false;
_wasStarted = false;
_disposed = false;
_hasServicesFlag = new AsyncManualResetEvent(false);
_hasNonMaxServicesFlag = new AsyncManualResetEvent(true);
}
#endregion
#region PROPERTIES
/// <inheritdoc />
public virtual string IPAddress
{
get { return _ipAddress; }
protected set
{
if (_isIpAddressSet && !string.Equals(_ipAddress, value, StringComparison.OrdinalIgnoreCase))
throw new DicomNetworkException("IP Address cannot be set twice. Current value: {0}", _ipAddress);
_ipAddress = value;
_isIpAddressSet = true;
}
}
/// <inheritdoc />
public virtual int Port
{
get { return _port; }
protected set
{
if (_isPortSet && _port != value)
throw new DicomNetworkException("Port cannot be set twice. Current value: {0}", _port);
_port = value;
_isPortSet = true;
}
}
/// <inheritdoc />
public bool IsListening { get; protected set; }
/// <inheritdoc />
public Exception Exception { get; protected set; }
public DicomServiceOptions Options { get; protected set; }
/// <inheritdoc />
public Logger Logger
{
get { return _logger ?? (_logger = LogManager.GetLogger("Dicom.Network")); }
set { _logger = value; }
}
/// <summary>
/// Gets the number of clients currently connected to the server.
/// </summary>
/// <remarks>Included for testing purposes only.</remarks>
internal int CompletedServicesCount => _services.Count(service => service.IsCompleted);
/// <summary>
/// Gets whether the list of services contains the maximum number of services or not.
/// </summary>
private bool IsServicesAtMax
{
get
{
var maxClientsAllowed = Options?.MaxClientsAllowed ?? DicomServiceOptions.Default.MaxClientsAllowed;
return maxClientsAllowed > 0 && _services.Count >= maxClientsAllowed;
}
}
#endregion
#region METHODS
/// <inheritdoc />
public virtual Task StartAsync(string ipAddress, int port, string certificateName, Encoding fallbackEncoding,
DicomServiceOptions options, object userState)
{
if (_wasStarted)
{
throw new DicomNetworkException("Server has already been started once, cannot be started again.");
}
_wasStarted = true;
IPAddress = string.IsNullOrEmpty(ipAddress?.Trim()) ? NetworkManager.IPv4Any : ipAddress;
Port = port;
Options = options;
_userState = userState;
_certificateName = certificateName;
_fallbackEncoding = fallbackEncoding;
return Task.WhenAll(ListenForConnectionsAsync(), RemoveUnusedServicesAsync());
}
/// <inheritdoc />
public virtual void Stop()
{
if (!_cancellationSource.IsCancellationRequested)
{
_cancellationSource.Cancel();
}
}
/// <inheritdoc />
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Execute the disposal.
/// </summary>
/// <param name="disposing">True if called from <see cref="Dispose()"/>, false otherwise.</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
Stop();
_cancellationSource.Dispose();
}
ClearServices();
var removed = DicomServer.Unregister(this);
if (!removed)
{
Logger.Warn(
"Could not unregister DICOM server on port {0}, either because never registered or because has already been unregistered once.",
Port);
}
_disposed = true;
}
/// <summary>
/// Create an instance of the DICOM service class.
/// </summary>
/// <param name="stream">Network stream.</param>
/// <returns>An instance of the DICOM service class.</returns>
protected virtual T CreateScp(INetworkStream stream)
{
var instance = (T)Activator.CreateInstance(typeof(T), stream, _fallbackEncoding, Logger);
instance.UserState = _userState;
return instance;
}
/// <summary>
/// Listen indefinitely for network connections on the specified port.
/// </summary>
private async Task ListenForConnectionsAsync()
{
INetworkListener listener = null;
try
{
var noDelay = Options?.TcpNoDelay ?? DicomServiceOptions.Default.TcpNoDelay;
listener = NetworkManager.CreateNetworkListener(IPAddress, Port);
await listener.StartAsync().ConfigureAwait(false);
IsListening = true;
while (!_cancellationSource.IsCancellationRequested)
{
await _hasNonMaxServicesFlag.WaitAsync().ConfigureAwait(false);
var networkStream = await listener
.AcceptNetworkStreamAsync(_certificateName, noDelay, _cancellationSource.Token)
.ConfigureAwait(false);
if (networkStream != null)
{
var scp = CreateScp(networkStream);
if (Options != null)
{
scp.Options = Options;
}
_services.Add(scp.RunAsync());
_hasServicesFlag.Set();
if (IsServicesAtMax) _hasNonMaxServicesFlag.Reset();
}
}
}
catch (OperationCanceledException)
{
}
catch (Exception e)
{
Logger.Error("Exception listening for DICOM services, {@error}", e);
Stop();
Exception = e;
}
finally
{
listener?.Stop();
IsListening = false;
}
}
/// <summary>
/// Remove no longer used client connections.
/// </summary>
private async Task RemoveUnusedServicesAsync()
{
while (!_cancellationSource.IsCancellationRequested)
{
try
{
await _hasServicesFlag.WaitAsync().ConfigureAwait(false);
await Task.WhenAny(_services).ConfigureAwait(false);
_services.RemoveAll(service => service.IsCompleted);
if (_services.Count == 0) _hasServicesFlag.Reset();
if (!IsServicesAtMax) _hasNonMaxServicesFlag.Set();
}
catch (OperationCanceledException)
{
Logger.Info("Disconnected client cleanup manually terminated.");
ClearServices();
}
catch (Exception e)
{
Logger.Warn("Exception removing disconnected clients, {@error}", e);
}
}
}
private void ClearServices()
{
_services.Clear();
_hasServicesFlag.Reset();
_hasNonMaxServicesFlag.Set();
}
#endregion
#region INNER TYPES
#endregion
}
/// <summary>
/// Support class for managing multiple DICOM server instances.
/// </summary>
/// <remarks>Controls that only one DICOM server per <see cref="IDicomServer.Port"/> is initialized. Current implementation
/// only allows one server per port. It is not possible to initialize multiple servers listening to different network interfaces
/// (for example IPv4 vs. IPv6) via these methods if the port is the same.</remarks>
public static class DicomServer
{
#region FIELDS
private static readonly IDictionary<IDicomServer, Task> _servers = new Dictionary<IDicomServer, Task>();
private static readonly object _lock = new object();
#endregion
#region METHODS
/// <summary>
/// Creates a DICOM server object.
/// </summary>
/// <typeparam name="T">DICOM service that the server should manage.</typeparam>
/// <param name="port">Port to listen to.</param>
/// <param name="certificateName">Certificate name for authenticated connections.</param>
/// <param name="options">Service options.</param>
/// <param name="fallbackEncoding">Fallback encoding.</param>
/// <param name="logger">Logger, if null default logger will be applied.</param>
/// <returns>An instance of <see cref="DicomServer{T}"/>, that starts listening for connections in the background.</returns>
public static IDicomServer Create<T>(
int port,
string certificateName = null,
DicomServiceOptions options = null,
Encoding fallbackEncoding = null,
Logger logger = null) where T : DicomService, IDicomServiceProvider
{
return Create<T, DicomServer<T>>(NetworkManager.IPv4Any, port, null, certificateName, options,
fallbackEncoding, logger);
}
/// <summary>
/// Creates a DICOM server object.
/// </summary>
/// <typeparam name="T">DICOM service that the server should manage.</typeparam>
/// <param name="port">Port to listen to.</param>
/// <param name="userState">Optional optional parameters.</param>
/// <param name="certificateName">Certificate name for authenticated connections.</param>
/// <param name="options">Service options.</param>
/// <param name="fallbackEncoding">Fallback encoding.</param>
/// <param name="logger">Logger, if null default logger will be applied.</param>
/// <returns>An instance of <see cref="DicomServer{T}"/>, that starts listening for connections in the background.</returns>
[Obsolete("Use suitable DicomServer.Create overload instead.")]
public static IDicomServer Create<T>(
int port,
object userState,
string certificateName = null,
DicomServiceOptions options = null,
Encoding fallbackEncoding = null,
Logger logger = null) where T : DicomService, IDicomServiceProvider
{
return Create<T, DicomServer<T>>(NetworkManager.IPv4Any, port, userState, certificateName, options,
fallbackEncoding, logger);
}
/// <summary>
/// Creates a DICOM server object.
/// </summary>
/// <typeparam name="T">DICOM service that the server should manage.</typeparam>
/// <param name="ipAddress">IP address(es) to listen to.</param>
/// <param name="port">Port to listen to.</param>
/// <param name="userState">Optional optional parameters.</param>
/// <param name="certificateName">Certificate name for authenticated connections.</param>
/// <param name="options">Service options.</param>
/// <param name="fallbackEncoding">Fallback encoding.</param>
/// <param name="logger">Logger, if null default logger will be applied.</param>
/// <returns>An instance of <see cref="DicomServer{T}"/>, that starts listening for connections in the background.</returns>
public static IDicomServer Create<T>(
string ipAddress,
int port,
object userState = null,
string certificateName = null,
DicomServiceOptions options = null,
Encoding fallbackEncoding = null,
Logger logger = null) where T : DicomService, IDicomServiceProvider
{
return Create<T, DicomServer<T>>(ipAddress, port, userState, certificateName, options, fallbackEncoding,
logger);
}
/// <summary>
/// Creates a DICOM server object.
/// </summary>
/// <typeparam name="T">DICOM service that the server should manage.</typeparam>
/// <typeparam name="TServer">Concrete DICOM server type to be returned.</typeparam>
/// <param name="ipAddress">IP address(es) to listen to. Value <code>null</code> applies default, IPv4Any.</param>
/// <param name="port">Port to listen to.</param>
/// <param name="userState">Optional optional parameters.</param>
/// <param name="certificateName">Certificate name for authenticated connections.</param>
/// <param name="options">Service options.</param>
/// <param name="fallbackEncoding">Fallback encoding.</param>
/// <param name="logger">Logger, if null default logger will be applied.</param>
/// <returns>An instance of <typeparamref name="TServer"/>, that starts listening for connections in the background.</returns>
public static IDicomServer Create<T, TServer>(
string ipAddress,
int port,
object userState = null,
string certificateName = null,
DicomServiceOptions options = null,
Encoding fallbackEncoding = null,
Logger logger = null) where T : DicomService, IDicomServiceProvider where TServer : IDicomServer<T>, new()
{
bool portInUse;
lock (_lock)
{
portInUse = _servers.Any(IsMatching(port, ipAddress));
}
if (portInUse)
{
throw new DicomNetworkException("There is already a DICOM server registered on port: {0}", port);
}
var server = new TServer();
if (logger != null) server.Logger = logger;
var runner = server.StartAsync(ipAddress, port, certificateName, fallbackEncoding, options, userState);
lock (_lock)
{
if (_servers.Any(IsMatching(port, ipAddress)))
{
throw new DicomNetworkException(
"Could not register DICOM server on port {0}, probably because another server just registered to the same port.",
port);
}
_servers.Add(server, runner);
}
return server;
}
/// <summary>
/// Gets DICOM server instance registered to <paramref name="port"/>.
/// </summary>
/// <param name="port">Port number for which DICOM server is requested.</param>
/// <param name="ipAddress">IP Address for which the service listener is requested.</param>
/// <returns>Registered DICOM server for <paramref name="port"/>.</returns>
public static IDicomServer GetInstance(int port, string ipAddress)
{
IDicomServer server;
lock (_lock)
{
server = _servers.SingleOrDefault(IsMatching(port, ipAddress)).Key;
}
return server;
}
/// <summary>
/// Gets DICOM server instance registered to <paramref name="port"/>.
/// </summary>
/// <param name="port">Port number for which DICOM server is requested.</param>
/// <returns>Registered DICOM server for <paramref name="port"/>.</returns>
public static IDicomServer GetInstance(int port)
{
return GetInstance(port, NetworkManager.IPv4Any);
}
/// <summary>
/// Gets service listener for the DICOM server instance registered to <paramref name="port"/>.
/// </summary>
/// <param name="port">Port number for which the service listener is requested.</param>
/// <param name="ipAddress">IP Address for which the service listener is requested.</param>
/// <returns>Service listener for the <paramref name="port"/> DICOM server.</returns>
public static Task GetListener(int port, string ipAddress)
{
Task listener;
lock (_lock)
{
listener = _servers.SingleOrDefault(IsMatching(port, ipAddress)).Value;
}
return listener;
}
/// <summary>
/// Gets service listener for the DICOM server instance registered to <paramref name="port"/>.
/// </summary>
/// <param name="port">Port number for which the service listener is requested.</param>
/// <returns>Service listener for the <paramref name="port"/> DICOM server.</returns>
public static Task GetListener(int port)
{
return GetListener(port, NetworkManager.IPv4Any);
}
/// <summary>
/// Gets an indicator of whether a DICOM server is registered and listening on the specified <paramref name="port"/>.
/// </summary>
/// <param name="port">Port number for which listening status is requested.</param>
/// <returns>True if DICOM server on <paramref name="port"/> is registered and listening, false otherwise.</returns>
public static bool IsListening(int port)
{
return IsListening(port, NetworkManager.IPv4Any);
}
/// <summary>
/// Gets an indicator of whether a DICOM server is registered and listening on the specified <paramref name="port"/>.
/// </summary>
/// <param name="port">Port number for which listening status is requested.</param>
/// <param name="ipAddress">IP Address for which listening status is requested.</param>
/// <returns>True if DICOM server on <paramref name="port"/> is registered and listening, false otherwise.</returns>
public static bool IsListening(int port, string ipAddress)
{
return GetInstance(port, ipAddress)?.IsListening ?? false;
}
/// <summary>
/// Removes a DICOM server from the list of registered servers.
/// </summary>
/// <param name="server">Server to remove.</param>
/// <returns>True if <paramref name="server"/> could be removed, false otherwise.</returns>
internal static bool Unregister(IDicomServer server)
{
bool removed;
lock (_lock)
{
removed = _servers.Remove(server);
}
return removed;
}
/// <summary>
/// Gets the function to be used in LINQ queries when searching for server matches.
/// </summary>
/// <param name="port">Matching port.</param>
/// <param name="ipAddress">Matching IP Address</param>
/// <returns>Function to be used in LINQ queries when searching for server matches.</returns>
private static Func<KeyValuePair<IDicomServer, Task>, bool> IsMatching(int port, string ipAddress)
{
return (s => s.Key.Port == port && s.Key.IPAddress == ipAddress);
}
#endregion
}
}
#endif