-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathBinaryTcpServer.cs
More file actions
830 lines (726 loc) · 22.6 KB
/
BinaryTcpServer.cs
File metadata and controls
830 lines (726 loc) · 22.6 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Networking.Sniffers;
using Waher.Runtime.Cache;
using Waher.Security;
using Waher.Runtime.Collections;
#if WINDOWS_UWP
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.Networking.Sockets;
#else
using System.Security.Authentication;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
#endif
namespace Waher.Networking
{
/// <summary>
/// Implements a binary TCP Server. The server adapts to network changes,
/// maintains a list of current connection, and removes unused connections
/// automatically.
/// </summary>
public class BinaryTcpServer : CommunicationLayer, IDisposable
#if !WINDOWS_UWP
, ITlsCertificateEndpoint
#endif
{
/// <summary>
/// Default Client-to-Client Connection backlog (10).
/// </summary>
public const int DefaultC2CConnectionBacklog = 10;
/// <summary>
/// Default Client-to-Server Connection backlog (100).
/// </summary>
public const int DefaultC2SConnectionBacklog = 100;
/// <summary>
/// Default buffer size (16384).
/// </summary>
public const int DefaultBufferSize = 16384;
#if WINDOWS_UWP
private ChunkedList<KeyValuePair<StreamSocketListener, Guid>> listeners = new ChunkedList<KeyValuePair<StreamSocketListener, Guid>>();
#else
private readonly string[] alpnProtocols;
private ChunkedList<TcpListener> listeners = new ChunkedList<TcpListener>();
private X509Certificate serverCertificate;
private ClientCertificates clientCertificates = ClientCertificates.NotUsed;
private bool trustClientCertificates = false;
private bool clientCertificateSettingsLocked = false;
private bool closed = false;
private readonly bool tls;
#endif
private Cache<Guid, ServerTcpConnection> connections;
private readonly object synchObj = new object();
private readonly bool c2s;
private long nrBytesRx = 0;
private long nrBytesTx = 0;
private int port;
/// <summary>
/// Creates a TCP server, waiting for incoming connections. Encryption is not
/// initiated.
/// </summary>
/// <param name="C2S">If the listener is for a client-to-server protocol (true),
/// or a client-to-client protocol (false)</param>
/// <param name="Port">Port number.</param>
/// <param name="ActivityTimeout">Time before closing unused client connections.</param>
/// <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 BinaryTcpServer(bool C2S, int Port, TimeSpan ActivityTimeout, bool DecoupledEvents, ISniffer[] Sniffers)
: base(DecoupledEvents, Sniffers)
{
this.c2s = C2S;
#if !WINDOWS_UWP
this.tls = false;
#endif
this.Init(Port, ActivityTimeout);
}
#if !WINDOWS_UWP
/// <summary>
/// Creates a TCP server, waiting for incoming connections. Encryption is
/// initiated for each connection request.
/// </summary>
/// <param name="C2S">If the listener is for a client-to-server protocol (true),
/// or a client-to-client protocol (false)</param>
/// <param name="Port">Port number.</param>
/// <param name="ActivityTimeout">Time before closing unused client connections.</param>
/// <param name="ServerCertificate">Server certificate.</param>
/// <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>
/// <param name="AlpnProtocols">TLS Application-Layer Protocol Negotiation (ALPN) Protocol IDs
/// https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids</param>
public BinaryTcpServer(bool C2S, int Port, TimeSpan ActivityTimeout, X509Certificate ServerCertificate, bool DecoupledEvents, ISniffer[] Sniffers,
params string[] AlpnProtocols)
: base(DecoupledEvents, Sniffers)
{
this.serverCertificate = ServerCertificate;
this.c2s = C2S;
this.tls = !(this.serverCertificate is null);
this.alpnProtocols = AlpnProtocols;
this.Init(Port, ActivityTimeout);
}
#endif
private void Init(int Port, TimeSpan ActivityTimeout)
{
if (ActivityTimeout <= TimeSpan.Zero)
throw new ArgumentException("Activity timeout must be positive.", nameof(ActivityTimeout));
this.port = Port;
this.connections = new Cache<Guid, ServerTcpConnection>(int.MaxValue, TimeSpan.MaxValue,
ActivityTimeout);
this.connections.Removed += this.Connections_Removed;
#if WINDOWS_UWP
NetworkInformation.NetworkStatusChanged += this.NetworkChange_NetworkAddressChanged;
#else
NetworkChange.NetworkAddressChanged += this.NetworkChange_NetworkAddressChanged;
#endif
}
/// <summary>
/// If the listener is for a client-to-server protocol (true),
/// or a client-to-client protocol (false)
/// </summary>
public bool C2S => this.c2s;
private async void NetworkChange_NetworkAddressChanged(object sender
#if !WINDOWS_UWP
, EventArgs e
#endif
)
{
try
{
await this.NetworkChanged();
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// Adapts the server to changes in the network. This method can be called automatically by calling the constructor accordingly.
/// </summary>
public async Task NetworkChanged()
{
try
{
#if WINDOWS_UWP
ChunkedList<KeyValuePair<StreamSocketListener, Guid>> Listeners = this.listeners;
this.listeners = new ChunkedList<KeyValuePair<StreamSocketListener, Guid>>();
await this.Open(Listeners);
foreach (KeyValuePair<StreamSocketListener, Guid> P in Listeners)
P.Key.Dispose();
#else
ChunkedList<TcpListener> Listeners = this.listeners;
this.listeners = new ChunkedList<TcpListener>();
await this.Open(Listeners);
foreach (TcpListener L in Listeners)
L.Stop();
#endif
await this.OnNetworkChanged.Raise(this, EventArgs.Empty);
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// Event raised when the network has been changed.
/// </summary>
public event EventHandlerAsync OnNetworkChanged = null;
#if WINDOWS_UWP
/// <summary>
/// Opens the server for incoming connection requests.
/// </summary>
/// <param name="Listeners">Optional list of existing listeners to reuse.</param>
/// <return>Number of network nterfaces where port was successfully opened, vs failed.</return>
public async Task<KeyValuePair<int, int>> Open(ChunkedList<KeyValuePair<StreamSocketListener, Guid>> Listeners)
{
int NrOpened = 0;
int NrFailed = 0;
try
{
StreamSocketListener Listener;
foreach (ConnectionProfile Profile in NetworkInformation.GetConnectionProfiles())
{
if (Profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.None)
continue;
Listener = null;
int i = 0;
Listeners?.ForEach((P) =>
{
StreamSocketListener L = P.Key;
Guid AdapterId = P.Value;
if (AdapterId == Profile.NetworkAdapter.NetworkAdapterId)
{
Listener = L;
Listeners.RemoveAt(i);
return false;
}
i++;
return true;
});
if (Listener is null)
{
try
{
Listener = new StreamSocketListener();
await Listener.BindServiceNameAsync(this.port.ToString(), SocketProtectionLevel.PlainSocket, Profile.NetworkAdapter);
Listener.ConnectionReceived += this.Listener_ConnectionReceived;
NrOpened++;
this.listeners.Add(new KeyValuePair<StreamSocketListener, Guid>(Listener, Profile.NetworkAdapter.NetworkAdapterId));
}
catch (Exception ex)
{
NrFailed++;
Log.Exception(ex, Profile.ProfileName);
}
}
else
{
NrOpened++;
this.listeners.Add(new KeyValuePair<StreamSocketListener, Guid>(Listener, Profile.NetworkAdapter.NetworkAdapterId));
}
}
}
catch (Exception ex)
{
Log.Exception(ex);
}
return new KeyValuePair<int, int>(NrOpened, NrFailed);
}
/// <summary>
/// Closes the server from incoming connection requests.
/// </summary>
/// <param name="CloseConnectedClients">If connected clients should be
/// closed as well.</param>
public void Close(bool CloseConnectedClients)
{
ChunkedList<KeyValuePair<StreamSocketListener, Guid>> Listeners = this.listeners;
this.listeners = new ChunkedList<KeyValuePair<StreamSocketListener, Guid>>();
foreach (KeyValuePair<StreamSocketListener, Guid> P in Listeners)
P.Key.Dispose();
if (CloseConnectedClients)
this.connections.Clear();
}
#else
/// <summary>
/// Opens the server for incoming connection requests.
/// </summary>
/// <param name="Listeners">Optional list of existing listeners to reuse.</param>
public Task Open(ChunkedList<TcpListener> Listeners)
{
return this.Open(Listeners, out _, out _);
}
/// <summary>
/// Opens the server for incoming connection requests.
/// </summary>
/// <param name="Listeners">Optional list of existing listeners to reuse.</param>
/// <param name="NrOpened">Number of network interfaces where the port was successfully opened.</param>
/// <param name="NrFailed">Number of network interfaces where the port was not possible to be opened.</param>
public Task Open(ChunkedList<TcpListener> Listeners, out int NrOpened, out int NrFailed)
{
NrOpened = 0;
NrFailed = 0;
try
{
TcpListener Listener;
foreach (NetworkInterface Interface in NetworkInterface.GetAllNetworkInterfaces())
{
if (Interface.OperationalStatus != OperationalStatus.Up)
continue;
IPInterfaceProperties Properties = Interface.GetIPProperties();
foreach (UnicastIPAddressInformation UnicastAddress in Properties.UnicastAddresses)
{
if ((UnicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4) ||
(UnicastAddress.Address.AddressFamily == AddressFamily.InterNetworkV6 && Socket.OSSupportsIPv6))
{
Listener = null;
IPEndPoint DesiredEndpoint = new IPEndPoint(UnicastAddress.Address, this.port);
if (!(Listeners is null))
{
foreach (TcpListener L in Listeners)
{
if ((!this.tls) && L.LocalEndpoint == DesiredEndpoint)
{
Listener = L;
Listeners.Remove(L);
break;
}
}
}
if (Listener is null)
{
try
{
Listener = new TcpListener(UnicastAddress.Address, this.port);
Listener.Start(this.c2s ? DefaultC2SConnectionBacklog : DefaultC2CConnectionBacklog);
Task T = this.ListenForIncomingConnections(Listener);
NrOpened++;
this.listeners.Add(Listener);
}
catch (SocketException)
{
NrFailed++;
Log.Error("Unable to open port for listening.",
new KeyValuePair<string, object>("Address", UnicastAddress.Address.ToString()),
new KeyValuePair<string, object>("Port", this.port));
}
catch (Exception ex)
{
NrFailed++;
Log.Exception(ex, UnicastAddress.Address.ToString() + ":" + this.port);
}
}
else
{
NrOpened++;
this.listeners.Add(Listener);
}
}
}
}
}
catch (Exception ex)
{
Log.Exception(ex);
}
return Task.CompletedTask;
}
/// <summary>
/// Closes the server from incoming connection requests.
/// </summary>
/// <param name="CloseConnectedClients">If connected clients should be
/// closed as well.</param>
public void Close(bool CloseConnectedClients)
{
ChunkedList<TcpListener> Listeners = this.listeners;
this.listeners = new ChunkedList<TcpListener>();
foreach (TcpListener Listener in Listeners)
Listener.Stop();
if (CloseConnectedClients)
this.connections.Clear();
}
#endif
/// <summary>
/// Opens the server for incoming connection requests.
/// </summary>
public Task Open()
{
return this.Open(null);
}
/// <summary>
/// Closes the server from incoming connection requests. Connected clients
/// are also closed.
/// </summary>
public void Close()
{
this.Close(true);
}
/// <summary>
/// <see cref="IDisposable.Dispose"/>
/// </summary>
public void Dispose()
{
#if WINDOWS_UWP
NetworkInformation.NetworkStatusChanged -= this.NetworkChange_NetworkAddressChanged;
#else
this.closed = true;
NetworkChange.NetworkAddressChanged -= this.NetworkChange_NetworkAddressChanged;
#endif
this.Close(true);
this.connections.Dispose();
}
#if !WINDOWS_UWP
/// <summary>
/// Updates the server certificate
/// </summary>
/// <param name="ServerCertificate">Server Certificate.</param>
public void UpdateCertificate(X509Certificate ServerCertificate)
{
this.serverCertificate = ServerCertificate;
}
/// <summary>
/// Configures Mutual-TLS capabilities of the server. Affects all connections, all resources.
/// </summary>
/// <param name="ClientCertificates">If client certificates are not used, optional or required.</param>
/// <param name="TrustClientCertificates">If client certificates should be trusted, even if they do not validate.</param>
/// <param name="LockSettings">If client certificate settings should be locked.</param>
public void ConfigureMutualTls(ClientCertificates ClientCertificates, bool TrustClientCertificates, bool LockSettings)
{
if (this.clientCertificateSettingsLocked)
throw new InvalidOperationException("Mutual TLS settings locked.");
this.clientCertificates = ClientCertificates;
this.trustClientCertificates = TrustClientCertificates;
this.clientCertificateSettingsLocked = LockSettings;
}
/// <summary>
/// If client certificates are not used, optional or required.
/// </summary>
public ClientCertificates ClientCertificates => this.clientCertificates;
/// <summary>
/// If client certificates should be trusted, even if they do not validate.
/// </summary>
public bool TrustClientCertificates => this.trustClientCertificates;
#endif
private async Task<bool> AcceptConnection(ServerTcpConnection Connection)
{
ServerConnectionAcceptEventArgs e = new ServerConnectionAcceptEventArgs(Connection);
if (!await this.OnAccept.Raise(this, e, false))
e.Accept = false;
return e.Accept;
}
/// <summary>
/// Event raised when a client tries to connect to the server. An event handler
/// can set the <see cref="ServerConnectionAcceptEventArgs.Accept"/> property
/// to control if the server should accept the connection or not.
/// </summary>
public event EventHandlerAsync<ServerConnectionAcceptEventArgs> OnAccept;
#if WINDOWS_UWP
private void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
try
{
StreamSocket Client = args.Socket;
if (NetworkingModule.Stopping)
{
Client?.Dispose();
return;
}
BinaryTcpClient BinaryTcpClient = new BinaryTcpClient(Client, this.DecoupledEvents);
BinaryTcpClient.Bind(true);
ServerTcpConnection Connection = new ServerTcpConnection(this, BinaryTcpClient);
if (this.AcceptConnection(Connection).Result)
{
Task.Run(async () =>
{
try
{
this.Information("Connection accepted from " + Client.Information.RemoteAddress.ToString() + ":" + Client.Information.RemotePort + ".");
await this.Added(Connection);
}
catch (Exception ex2)
{
Log.Exception(ex2);
}
});
BinaryTcpClient.Continue();
}
else
{
Task.Run(async () =>
{
try
{
this.Warning("Connection rejected from " + Client.Information.RemoteAddress.ToString() + ":" + Client.Information.RemotePort + ".");
await Connection.Client.DisposeAsync();
}
catch (Exception ex2)
{
Log.Exception(ex2);
}
});
}
}
catch (SocketException)
{
// Ignore
}
catch (Exception ex)
{
if (this.listeners is null)
return;
Log.Exception(ex);
}
}
#else
private async Task ListenForIncomingConnections(TcpListener Listener)
{
try
{
while (!this.closed && !NetworkingModule.Stopping)
{
try
{
TcpClient Client;
try
{
Client = await Listener.AcceptTcpClientAsync();
if (this.closed || NetworkingModule.Stopping)
{
Client?.Dispose();
return;
}
}
catch (InvalidOperationException)
{
this.listeners?.Remove(Listener);
return;
}
if (!(Client is null))
{
BinaryTcpClient BinaryTcpClient = new BinaryTcpClient(Client, this.DecoupledEvents);
BinaryTcpClient.Bind(true);
ServerTcpConnection Connection = new ServerTcpConnection(this, BinaryTcpClient);
if (await this.AcceptConnection(Connection))
{
this.Information("Connection accepted from " + BinaryTcpClient.RemoteEndPoint + ".");
if (this.tls)
{
Task T = this.SwitchToTls(Connection);
}
else
{
BinaryTcpClient.Continue();
await this.Added(Connection);
}
}
else
{
this.Warning("Connection rejected from " + Client.Client.RemoteEndPoint.ToString() + ".");
await Connection.Client.DisposeAsync();
}
}
}
catch (SocketException)
{
// Ignore
}
catch (ObjectDisposedException)
{
// Ignore
}
catch (NullReferenceException)
{
// Ignore
}
catch (Exception ex)
{
if (this.closed || this.listeners is null)
break;
bool Found = false;
foreach (TcpListener L in this.listeners)
{
if (L == Listener)
{
Found = true;
break;
}
}
if (Found)
Log.Exception(ex);
else
break; // Removed, for instance due to network change
}
}
}
catch (Exception ex)
{
if (this.closed || this.listeners is null)
return;
Log.Exception(ex);
}
}
private async Task SwitchToTls(ServerTcpConnection Connection)
{
try
{
this.Information("Switching to TLS.");
await Connection.Client.UpgradeToTlsAsServer(this.serverCertificate, Crypto.SecureTls,
this.clientCertificates, null, this.trustClientCertificates, this.alpnProtocols);
if (this.HasSniffers)
{
StringBuilder sb = new StringBuilder();
sb.Append("TLS established");
sb.Append(". Cipher Strength: ");
sb.Append(Connection.Client.CipherStrength.ToString());
sb.Append(", Hash Strength: ");
sb.Append(Connection.Client.HashStrength.ToString());
sb.Append(", Key Exchange Strength: ");
sb.Append(Connection.Client.KeyExchangeStrength.ToString());
this.Information(sb.ToString());
if (!(Connection.Client.RemoteCertificate is null))
{
sb.Clear();
sb.Append("Remote Certificate received. Valid: ");
sb.Append(Connection.Client.RemoteCertificateValid.ToString());
sb.Append(", Subject: ");
sb.Append(Connection.Client.RemoteCertificate.Subject);
sb.Append(", Issuer: ");
sb.Append(Connection.Client.RemoteCertificate.Issuer);
sb.Append(", S/N: ");
sb.Append(Convert.ToBase64String(Connection.Client.RemoteCertificate.GetSerialNumber()));
sb.Append(", Hash: ");
sb.Append(Convert.ToBase64String(Connection.Client.RemoteCertificate.GetCertHash()));
this.Information(sb.ToString());
}
}
Connection.Client.Continue();
await this.Added(Connection);
}
catch (AuthenticationException ex)
{
await this.LoginFailure(ex, Connection);
}
catch (SocketException)
{
await Connection.Client.DisposeAsync();
}
catch (Win32Exception ex)
{
await this.LoginFailure(ex, Connection);
}
catch (IOException)
{
await Connection.Client.DisposeAsync();
}
catch (Exception ex)
{
await Connection.Client.DisposeAsync();
Log.Exception(ex);
}
}
private async Task LoginFailure(Exception ex, ServerTcpConnection Connection)
{
Exception ex2 = Log.UnnestException(ex);
await this.OnTlsUpgradeError.Raise(this, new ServerTlsErrorEventArgs(Connection, ex2), false);
await Connection.Client.DisposeAsync();
}
/// <summary>
/// Event raised when a client is unable to switch to TLS.
/// </summary>
public event EventHandlerAsync<ServerTlsErrorEventArgs> OnTlsUpgradeError;
#endif
private Task Added(ServerTcpConnection Connection)
{
lock (this.connections)
{
this.connections[Connection.Id] = Connection;
}
return this.OnClientConnected.Raise(this, new ServerConnectionEventArgs(Connection));
}
/// <summary>
/// Event raised when a client has connected.
/// </summary>
public event EventHandlerAsync<ServerConnectionEventArgs> OnClientConnected;
private async Task Connections_Removed(object Sender, CacheItemEventArgs<Guid, ServerTcpConnection> e)
{
try
{
BinaryTcpClient Client = e.Value.Client;
if (!(Client is null))
await Client.DisposeAsync();
await this.OnClientDisconnected.Raise(this, new ServerConnectionEventArgs(e.Value));
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
/// <summary>
/// Event raised when a client has been disconnected.
/// </summary>
public event EventHandlerAsync<ServerConnectionEventArgs> OnClientDisconnected;
internal void Remove(ServerTcpConnection Connection)
{
this.connections.Remove(Connection.Id);
}
internal async Task DataReceived(ServerTcpConnection Connection, bool ConstantBuffer,
byte[] Buffer, int Offset, int Count)
{
this.connections?.ContainsKey(Connection.Id); // Refreshes timer for connection.
lock (this.synchObj)
{
this.nrBytesRx += Count;
}
if (this.HasSniffers)
this.ReceiveBinary(ConstantBuffer, Buffer, Offset, Count);
await this.OnDataReceived.Raise(this, new ServerConnectionDataEventArgs(Connection, ConstantBuffer, Buffer, Offset, Count));
}
/// <summary>
/// Event raisde when data has been received from a client.
/// </summary>
public event EventHandlerAsync<ServerConnectionDataEventArgs> OnDataReceived;
internal void DataSent(bool ConstantBuffer, byte[] Data)
{
lock (this.synchObj)
{
this.nrBytesTx += Data.Length;
}
this.TransmitBinary(ConstantBuffer, Data);
}
/// <summary>
/// Number of bytes received
/// </summary>
public long NrBytesRx
{
get
{
lock (this.synchObj)
{
return this.nrBytesRx;
}
}
}
/// <summary>
/// Number of bytes transmitted
/// </summary>
public long NrBytesTx
{
get
{
lock (this.synchObj)
{
return this.nrBytesTx;
}
}
}
}
}