-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCommunicationLayer.cs
More file actions
637 lines (571 loc) · 18.1 KB
/
CommunicationLayer.cs
File metadata and controls
637 lines (571 loc) · 18.1 KB
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Networking.Sniffers;
using Waher.Runtime.Collections;
namespace Waher.Networking
{
/// <summary>
/// Delegate for text sniffer events.
/// </summary>
/// <param name="Text">Text</param>
/// <return>Text, possibly modified.</return>
public delegate string TextSnifferEvent(string Text);
/// <summary>
/// Simple base class for classes implementing communication protocols.
/// </summary>
public class CommunicationLayer : ICommunicationLayer
{
private readonly ChunkedList<ISniffer> sniffers;
private readonly bool decoupledEvents;
private ISniffer[] staticList;
private bool hasSniffers;
/// <summary>
/// Simple base class for classes implementing communication protocols.
/// </summary>
/// <param name="DecoupledEvents">If events raised from the communication
/// layer are decoupled, i.e. executed in parallel with the source that raised
/// them.</param>
/// <param name="Sniffers">Sniffers.</param>
public CommunicationLayer(bool DecoupledEvents, params ISniffer[] Sniffers)
{
this.decoupledEvents = DecoupledEvents;
this.sniffers = new ChunkedList<ISniffer>();
this.sniffers.AddRange(Sniffers);
this.staticList = this.sniffers.ToArray();
this.hasSniffers = this.sniffers.Count > 0;
}
/// <summary>
/// If events raised from the communication layer are decoupled, i.e. executed
/// in parallel with the source that raised them.
/// </summary>
public bool DecoupledEvents => this.decoupledEvents;
/// <summary>
/// <see cref="ICommunicationLayer.Add"/>
/// </summary>
public virtual void Add(ISniffer Sniffer)
{
if (!(Sniffer is null))
{
lock (this.sniffers)
{
if (!this.sniffers.Contains(Sniffer))
{
this.sniffers.Add(Sniffer);
this.staticList = this.sniffers.ToArray();
this.hasSniffers = true;
}
}
}
}
/// <summary>
/// <see cref="ICommunicationLayer.AddRange"/>
/// </summary>
public virtual void AddRange(IEnumerable<ISniffer> Sniffers)
{
if (!(Sniffers is null))
{
lock (this.sniffers)
{
foreach (ISniffer Sniffer in Sniffers)
{
if (!(Sniffer is null) && !this.sniffers.Contains(Sniffer))
this.sniffers.Add(Sniffer);
}
this.staticList = this.sniffers.ToArray();
this.hasSniffers = this.staticList.Length > 0;
}
}
}
/// <summary>
/// <see cref="ICommunicationLayer.Remove"/>
/// </summary>
public virtual bool Remove(ISniffer Sniffer)
{
lock (this.sniffers)
{
if (this.sniffers.Remove(Sniffer))
{
this.staticList = this.sniffers.ToArray();
this.hasSniffers = this.staticList.Length > 0;
return true;
}
}
return false;
}
/// <summary>
/// Removes a set of sniffers, if registered.
/// </summary>
/// <param name="Sniffers">Sniffers to remove.</param>
public Task RemoveRange(IEnumerable<ISniffer> Sniffers)
{
return this.RemoveRange(Sniffers, false);
}
/// <summary>
/// Removes a set of sniffers, if registered.
/// </summary>
/// <param name="Sniffers">Sniffers to remove.</param>
/// <param name="DisposeSniffers">If the sniffers should be disposed.</param>
public async Task RemoveRange(IEnumerable<ISniffer> Sniffers, bool DisposeSniffers)
{
foreach (ISniffer Sniffer in Sniffers)
{
this.Remove(Sniffer);
if (DisposeSniffers)
{
if (Sniffer is IDisposableAsync DisposableAsync)
await DisposableAsync.DisposeAsync();
else if (Sniffer is IDisposable Disposable)
Disposable.Dispose();
}
}
}
/// <summary>
/// Registered sniffers.
/// </summary>
public ISniffer[] Sniffers => (ISniffer[])this.staticList?.Clone();
/// <summary>
/// If there are sniffers registered on the object.
/// </summary>
public bool HasSniffers => this.hasSniffers;
/// <summary>
/// Gets a typed enumerator.
/// </summary>
public IEnumerator<ISniffer> GetEnumerator()
{
return new SnifferEnumerator(this.staticList);
}
/// <summary>
/// Gets an untyped enumerator.
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
return this.staticList.GetEnumerator();
}
/// <summary>
/// Called when binary data has been received.
/// </summary>
/// <param name="Count">Number of bytes received.</param>
public void ReceiveBinary(int Count)
{
this.ReceiveBinary(DateTime.UtcNow, Count);
}
/// <summary>
/// Called when binary data has been received.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Count">Number of bytes received.</param>
public void ReceiveBinary(DateTime Timestamp, int Count)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.ReceiveBinary(Timestamp, Count);
}
}
/// <summary>
/// Called when binary data has been received.
/// </summary>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
public void ReceiveBinary(bool ConstantBuffer, byte[] Data)
{
this.ReceiveBinary(DateTime.UtcNow, ConstantBuffer, Data);
}
/// <summary>
/// Called when binary data has been received.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
public void ReceiveBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.ReceiveBinary(Timestamp, ConstantBuffer, Data);
}
}
/// <summary>
/// Called when binary data has been received.
/// </summary>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
/// <param name="Offset">Offset into buffer where received data begins.</param>
/// <param name="Count">Number of bytes received.</param>
public void ReceiveBinary(bool ConstantBuffer, byte[] Data, int Offset, int Count)
{
this.ReceiveBinary(DateTime.UtcNow, ConstantBuffer, Data, Offset, Count);
}
/// <summary>
/// Called when binary data has been received.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
/// <param name="Offset">Offset into buffer where received data begins.</param>
/// <param name="Count">Number of bytes received.</param>
public void ReceiveBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data, int Offset, int Count)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.ReceiveBinary(Timestamp, ConstantBuffer, Data, Offset, Count);
}
}
/// <summary>
/// Called when binary data has been transmitted.
/// </summary>
/// <param name="Count">Number of bytes transmitted.</param>
public void TransmitBinary(int Count)
{
this.TransmitBinary(DateTime.UtcNow, Count);
}
/// <summary>
/// Called when binary data has been transmitted.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Count">Number of bytes transmitted.</param>
public void TransmitBinary(DateTime Timestamp, int Count)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.TransmitBinary(Timestamp, Count);
}
}
/// <summary>
/// Called when binary data has been transmitted.
/// </summary>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
public void TransmitBinary(bool ConstantBuffer, byte[] Data)
{
this.TransmitBinary(DateTime.UtcNow, ConstantBuffer, Data);
}
/// <summary>
/// Called when binary data has been transmitted.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
public void TransmitBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.TransmitBinary(Timestamp, ConstantBuffer, Data);
}
}
/// <summary>
/// Called when binary data has been transmitted.
/// </summary>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
/// <param name="Offset">Offset into buffer where transmitted data begins.</param>
/// <param name="Count">Number of bytes transmitted.</param>
public void TransmitBinary(bool ConstantBuffer, byte[] Data, int Offset, int Count)
{
this.TransmitBinary(DateTime.UtcNow, ConstantBuffer, Data, Offset, Count);
}
/// <summary>
/// Called when binary data has been transmitted.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="ConstantBuffer">If the contents of the buffer remains constant (true),
/// or if the contents in the buffer may change after the call (false).</param>
/// <param name="Data">Binary Data.</param>
/// <param name="Offset">Offset into buffer where transmitted data begins.</param>
/// <param name="Count">Number of bytes transmitted.</param>
public void TransmitBinary(DateTime Timestamp, bool ConstantBuffer, byte[] Data, int Offset, int Count)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.TransmitBinary(Timestamp, ConstantBuffer, Data, Offset, Count);
}
}
/// <summary>
/// Called when text has been received.
/// </summary>
/// <param name="Text">Text</param>
public void ReceiveText(string Text)
{
this.ReceiveText(DateTime.UtcNow, Text);
}
/// <summary>
/// Called when text has been received.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Text">Text</param>
public void ReceiveText(DateTime Timestamp, string Text)
{
if (this.hasSniffers)
{
Text = this.Transform(this.OnReceiveText, Text);
if (!string.IsNullOrEmpty(Text))
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.ReceiveText(Timestamp, Text);
}
}
}
private string Transform(TextSnifferEvent Callback, string s)
{
if (!(Callback is null))
{
try
{
return Callback(s);
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
return s;
}
/// <summary>
/// Event received when a block of text has been received. Can be used to modify output.
/// </summary>
public event TextSnifferEvent OnReceiveText = null;
/// <summary>
/// Called when text has been transmitted.
/// </summary>
/// <param name="Text">Text</param>
public void TransmitText(string Text)
{
this.TransmitText(DateTime.UtcNow, Text);
}
/// <summary>
/// Called when text has been transmitted.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Text">Text</param>
public void TransmitText(DateTime Timestamp, string Text)
{
if (this.hasSniffers)
{
Text = this.Transform(this.OnTransmitText, Text);
if (!string.IsNullOrEmpty(Text))
{
if (Text == " ")
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.Information(Timestamp, "Heart beat");
}
else
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.TransmitText(Timestamp, Text);
}
}
}
}
/// <summary>
/// Event received when a block of text has been sent. Can be used to modify output.
/// </summary>
public event TextSnifferEvent OnTransmitText = null;
/// <summary>
/// Called to inform the viewer of something.
/// </summary>
/// <param name="Comment">Comment.</param>
public void Information(string Comment)
{
this.Information(DateTime.UtcNow, Comment);
}
/// <summary>
/// Called to inform the viewer of something.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Comment">Comment.</param>
public void Information(DateTime Timestamp, string Comment)
{
if (this.hasSniffers)
{
Comment = this.Transform(this.OnInformation, Comment);
if (!string.IsNullOrEmpty(Comment))
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.Information(Timestamp, Comment);
}
}
}
/// <summary>
/// Event received when information is logged.
/// </summary>
public event TextSnifferEvent OnInformation = null;
/// <summary>
/// Called to inform the viewer of a warning state.
/// </summary>
/// <param name="Warning">Warning.</param>
public void Warning(string Warning)
{
this.Warning(DateTime.UtcNow, Warning);
}
/// <summary>
/// Called to inform the viewer of a warning state.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Warning">Warning.</param>
public void Warning(DateTime Timestamp, string Warning)
{
if (this.hasSniffers)
{
Warning = this.Transform(this.OnWarning, Warning);
if (!string.IsNullOrEmpty(Warning))
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.Warning(Timestamp, Warning);
}
}
}
/// <summary>
/// Event received when a warning is logged.
/// </summary>
public event TextSnifferEvent OnWarning = null;
/// <summary>
/// Called to inform the viewer of an error state.
/// </summary>
/// <param name="Error">Error.</param>
public void Error(string Error)
{
this.Error(DateTime.UtcNow, Error);
}
/// <summary>
/// Called to inform the viewer of an error state.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Error">Error.</param>
public void Error(DateTime Timestamp, string Error)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.Error(Timestamp, Error);
}
}
/// <summary>
/// Called to inform the viewer of an exception state.
/// </summary>
/// <param name="Exception">Exception.</param>
public void Exception(Exception Exception)
{
this.Exception(DateTime.UtcNow, Exception);
}
/// <summary>
/// Called to inform the viewer of an exception state.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Exception">Exception.</param>
public virtual void Exception(DateTime Timestamp, Exception Exception)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.Exception(Timestamp, Exception);
}
}
/// <summary>
/// Called to inform the viewer of an exception state.
/// </summary>
/// <param name="Exception">Exception.</param>
public void Exception(string Exception)
{
this.Exception(DateTime.UtcNow, Exception);
}
/// <summary>
/// Called to inform the viewer of an exception state.
/// </summary>
/// <param name="Timestamp">Timestamp of event.</param>
/// <param name="Exception">Exception.</param>
public virtual void Exception(DateTime Timestamp, string Exception)
{
if (this.hasSniffers)
{
foreach (ISniffer Sniffer in this.staticList)
Sniffer.Exception(Timestamp, Exception);
}
}
/// <summary>
/// Checks if the string contains control characters.
/// </summary>
/// <param name="Text">String</param>
/// <returns>If the string contains control characters</returns>
public static bool ContainsControlCharacters(string Text)
{
return Text.IndexOfAny(controlCharacters) >= 0;
}
/// <summary>
/// Encodes control characters in a string.
/// </summary>
/// <param name="Text">String</param>
/// <returns>String, with control characters encoded.</returns>
public static string EncodeControlCharacters(string Text)
{
int i = Text.IndexOfAny(controlCharacters);
if (i < 0)
return Text;
StringBuilder sb = new StringBuilder();
int j = 0;
while (i >= 0)
{
if (i > j)
sb.Append(Text.Substring(j, i - j));
sb.Append(' ');
sb.Append(controlCharacterNames[Array.IndexOf(controlCharacters, Text[i])]);
sb.Append(' ');
j = i + 1;
i = Text.IndexOfAny(controlCharacters, j);
}
if (j < Text.Length)
sb.Append(Text.Substring(j));
return sb.ToString();
}
private static readonly char[] controlCharacters = new char[]
{
(char)00, (char)01, (char)02, (char)03, (char)04, (char)05, (char)06, (char)07, (char)08,
(char)11, (char)12, (char)14, (char)15, (char)16, (char)17, (char)18, (char)19,
(char)20, (char)21, (char)22, (char)23, (char)24, (char)25, (char)26, (char)27, (char)28, (char)29,
(char)30, (char)31
};
private static readonly string[] controlCharacterNames = new string[]
{
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS",
"VT", "FF", "SO", "SI", "DLE", "DC1", "DC2", "DC3",
"DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS",
"RS", "US"
};
/// <summary>
/// Checks if an IP Address is a multi-cast address or not.
/// </summary>
/// <param name="Address">IP Address</param>
/// <returns>If the IP Address represents a multi-cast address.</returns>
public static bool IsMulticastAddress(IPAddress Address)
{
switch (Address.AddressFamily)
{
case AddressFamily.InterNetwork:
byte[] Bin = Address.GetAddressBytes();
return Bin[0] >= 224 && Bin[0] <= 239;
case AddressFamily.InterNetworkV6:
Bin = Address.GetAddressBytes();
return Bin[0] == 0xff;
default:
return false;
}
}
}
}