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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Hazel
{
/// <summary>
/// Holds statistics about the traffic through a <see cref="Connection"/>.
/// </summary>
/// <threadsafety static="true" instance="true"/>
public class ConnectionStatistics
{
private const int ExpectedMTU = 1200;
/// <summary>
/// The total number of messages sent.
/// </summary>
public int MessagesSent
{
get
{
return UnreliableMessagesSent + ReliableMessagesSent + FragmentedMessagesSent + AcknowledgementMessagesSent + HelloMessagesSent;
}
}
private int packetsSent;
public int PacketsSent => this.packetsSent;
private int reliablePacketsAcknowledged;
public int ReliablePacketsAcknowledged => this.reliablePacketsAcknowledged;
/// <summary>
/// The number of messages sent larger than 576 bytes. This is smaller than most default MTUs.
/// </summary>
/// <remarks>
/// This is the number of unreliable messages that were sent from the <see cref="Connection"/>, incremented
/// each time that LogUnreliableSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </remarks>
public int FragmentableMessagesSent
{
get
{
return fragmentableMessagesSent;
}
}
/// <summary>
/// The number of messages sent larger than 576 bytes.
/// </summary>
int fragmentableMessagesSent;
/// <summary>
/// The number of unreliable messages sent.
/// </summary>
/// <remarks>
/// This is the number of unreliable messages that were sent from the <see cref="Connection"/>, incremented
/// each time that LogUnreliableSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </remarks>
public int UnreliableMessagesSent
{
get
{
return unreliableMessagesSent;
}
}
/// <summary>
/// The number of unreliable messages sent.
/// </summary>
int unreliableMessagesSent;
/// <summary>
/// The number of reliable messages sent.
/// </summary>
/// <remarks>
/// This is the number of reliable messages that were sent from the <see cref="Connection"/>, incremented
/// each time that LogReliableSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </remarks>
public int ReliableMessagesSent
{
get
{
return reliableMessagesSent;
}
}
/// <summary>
/// The number of unreliable messages sent.
/// </summary>
int reliableMessagesSent;
/// <summary>
/// The number of fragmented messages sent.
/// </summary>
/// <remarks>
/// This is the number of fragmented messages that were sent from the <see cref="Connection"/>, incremented
/// each time that LogFragmentedSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </remarks>
public int FragmentedMessagesSent
{
get
{
return fragmentedMessagesSent;
}
}
/// <summary>
/// The number of fragmented messages sent.
/// </summary>
int fragmentedMessagesSent;
/// <summary>
/// The number of acknowledgement messages sent.
/// </summary>
/// <remarks>
/// This is the number of acknowledgements that were sent from the <see cref="Connection"/>, incremented
/// each time that LogAcknowledgementSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </remarks>
public int AcknowledgementMessagesSent
{
get
{
return acknowledgementMessagesSent;
}
}
/// <summary>
/// The number of acknowledgement messages sent.
/// </summary>
int acknowledgementMessagesSent;
/// <summary>
/// The number of hello messages sent.
/// </summary>
/// <remarks>
/// This is the number of hello messages that were sent from the <see cref="Connection"/>, incremented
/// each time that LogHelloSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </remarks>
public int HelloMessagesSent
{
get
{
return helloMessagesSent;
}
}
/// <summary>
/// The number of hello messages sent.
/// </summary>
int helloMessagesSent;
/// <summary>
/// The number of bytes of data sent.
/// </summary>
/// <remarks>
/// <para>
/// This is the number of bytes of data (i.e. user bytes) that were sent from the <see cref="Connection"/>,
/// accumulated each time that LogSend is called by the Connection. Messages that caused an error are not
/// counted and messages are only counted once all other operations in the send are complete.
/// </para>
/// <para>
/// For the number of bytes including protocol bytes see <see cref="TotalBytesSent"/>.
/// </para>
/// </remarks>
public long DataBytesSent
{
get
{
return Interlocked.Read(ref dataBytesSent);
}
}
/// <summary>
/// The number of bytes of data sent.
/// </summary>
long dataBytesSent;
/// <summary>
/// The number of bytes sent in total.
/// </summary>
/// <remarks>
/// <para>
/// This is the total number of bytes (the data bytes plus protocol bytes) that were sent from the
/// <see cref="Connection"/>, accumulated each time that LogSend is called by the Connection. Messages that
/// caused an error are not counted and messages are only counted once all other operations in the send are
/// complete.
/// </para>
/// <para>
/// For the number of data bytes excluding protocol bytes see <see cref="DataBytesSent"/>.
/// </para>
/// </remarks>
public long TotalBytesSent
{
get
{
return Interlocked.Read(ref totalBytesSent);
}
}
/// <summary>
/// The number of bytes sent in total.
/// </summary>
long totalBytesSent;
/// <summary>
/// The total number of messages received.
/// </summary>
public int MessagesReceived
{
get
{
return UnreliableMessagesReceived + ReliableMessagesReceived + FragmentedMessagesReceived + AcknowledgementMessagesReceived + helloMessagesReceived;
}
}
/// <summary>
/// The number of unreliable messages received.
/// </summary>
/// <remarks>
/// This is the number of unreliable messages that were received by the <see cref="Connection"/>, incremented
/// each time that LogUnreliableReceive is called by the Connection. Messages are counted before the receive event is invoked.
/// </remarks>
public int UnreliableMessagesReceived
{
get
{
return unreliableMessagesReceived;
}
}
/// <summary>
/// The number of unreliable messages received.
/// </summary>
int unreliableMessagesReceived;
/// <summary>
/// The number of reliable messages received.
/// </summary>
/// <remarks>
/// This is the number of reliable messages that were received by the <see cref="Connection"/>, incremented
/// each time that LogReliableReceive is called by the Connection. Messages are counted before the receive event is invoked.
/// </remarks>
public int ReliableMessagesReceived
{
get
{
return reliableMessagesReceived;
}
}
/// <summary>
/// The number of reliable messages received.
/// </summary>
int reliableMessagesReceived;
/// <summary>
/// The number of fragmented messages received.
/// </summary>
/// <remarks>
/// This is the number of fragmented messages that were received by the <see cref="Connection"/>, incremented
/// each time that LogFragmentedReceive is called by the Connection. Messages are counted before the receive event is invoked.
/// </remarks>
public int FragmentedMessagesReceived
{
get
{
return fragmentedMessagesReceived;
}
}
/// <summary>
/// The number of fragmented messages received.
/// </summary>
int fragmentedMessagesReceived;
/// <summary>
/// The number of acknowledgement messages received.
/// </summary>
/// <remarks>
/// This is the number of acknowledgement messages that were received by the <see cref="Connection"/>, incremented
/// each time that LogAcknowledgemntReceive is called by the Connection. Messages are counted before the receive event is invoked.
/// </remarks>
public int AcknowledgementMessagesReceived
{
get
{
return acknowledgementMessagesReceived;
}
}
/// <summary>
/// The number of acknowledgement messages received.
/// </summary>
int acknowledgementMessagesReceived;
/// <summary>
/// The number of ping messages received.
/// </summary>
/// <remarks>
/// This is the number of hello messages that were received by the <see cref="Connection"/>, incremented
/// each time that LogHelloReceive is called by the Connection. Messages are counted before the receive event is invoked.
/// </remarks>
public int PingMessagesReceived
{
get
{
return pingMessagesReceived;
}
}
/// <summary>
/// The number of hello messages received.
/// </summary>
int pingMessagesReceived;
/// <summary>
/// The number of hello messages received.
/// </summary>
/// <remarks>
/// This is the number of hello messages that were received by the <see cref="Connection"/>, incremented
/// each time that LogHelloReceive is called by the Connection. Messages are counted before the receive event is invoked.
/// </remarks>
public int HelloMessagesReceived
{
get
{
return helloMessagesReceived;
}
}
/// <summary>
/// The number of hello messages received.
/// </summary>
int helloMessagesReceived;
/// <summary>
/// The number of bytes of data received.
/// </summary>
/// <remarks>
/// <para>
/// This is the number of bytes of data (i.e. user bytes) that were received by the <see cref="Connection"/>,
/// accumulated each time that LogReceive is called by the Connection. Messages are counted before the receive
/// event is invoked.
/// </para>
/// <para>
/// For the number of bytes including protocol bytes see <see cref="TotalBytesReceived"/>.
/// </para>
/// </remarks>
public long DataBytesReceived
{
get
{
return Interlocked.Read(ref dataBytesReceived);
}
}
/// <summary>
/// The number of bytes of data received.
/// </summary>
long dataBytesReceived;
/// <summary>
/// The number of bytes received in total.
/// </summary>
/// <remarks>
/// <para>
/// This is the total number of bytes (the data bytes plus protocol bytes) that were received by the
/// <see cref="Connection"/>, accumulated each time that LogReceive is called by the Connection. Messages are
/// counted before the receive event is invoked.
/// </para>
/// <para>
/// For the number of data bytes excluding protocol bytes see <see cref="DataBytesReceived"/>.
/// </para>
/// </remarks>
public long TotalBytesReceived
{
get
{
return Interlocked.Read(ref totalBytesReceived);
}
}
/// <summary>
/// The number of bytes received in total.
/// </summary>
long totalBytesReceived;
public int MessagesResent { get { return messagesResent; } }
int messagesResent;
/// <summary>
/// Logs the sending of an unreliable data packet in the statistics.
/// </summary>
/// <param name="dataLength">The number of bytes of data sent.</param>
/// <remarks>
/// This should be called after the data has been sent and should only be called for data that is sent sucessfully.
/// </remarks>
internal void LogUnreliableSend(int dataLength)
{
Interlocked.Increment(ref unreliableMessagesSent);
Interlocked.Add(ref dataBytesSent, dataLength);
}
/// <param name="totalLength">The total number of bytes sent.</param>
internal void LogPacketSend(int totalLength)
{
Interlocked.Increment(ref this.packetsSent);
Interlocked.Add(ref totalBytesSent, totalLength);
if (totalLength > ExpectedMTU)
{
Interlocked.Increment(ref fragmentableMessagesSent);
}
}
/// <summary>
/// Logs the sending of a reliable data packet in the statistics.
/// </summary>
/// <param name="dataLength">The number of bytes of data sent.</param>
/// <remarks>
/// This should be called after the data has been sent and should only be called for data that is sent sucessfully.
/// </remarks>
internal void LogReliableSend(int dataLength)
{
Interlocked.Increment(ref reliableMessagesSent);
Interlocked.Add(ref dataBytesSent, dataLength);
}
/// <summary>
/// Logs the sending of a fragmented data packet in the statistics.
/// </summary>
/// <param name="dataLength">The number of bytes of data sent.</param>
/// <param name="totalLength">The total number of bytes sent.</param>
/// <remarks>
/// This should be called after the data has been sent and should only be called for data that is sent sucessfully.
/// </remarks>
internal void LogFragmentedSend(int dataLength)
{
Interlocked.Increment(ref fragmentedMessagesSent);
Interlocked.Add(ref dataBytesSent, dataLength);
}
/// <summary>
/// Logs the sending of a acknowledgement data packet in the statistics.
/// </summary>
/// <param name="totalLength">The total number of bytes sent.</param>
/// <remarks>
/// This should be called after the data has been sent and should only be called for data that is sent sucessfully.
/// </remarks>
internal void LogAcknowledgementSend()
{
Interlocked.Increment(ref acknowledgementMessagesSent);
}
/// <summary>
/// Logs the sending of a hellp data packet in the statistics.
/// </summary>
/// <param name="totalLength">The total number of bytes sent.</param>
/// <remarks>
/// This should be called after the data has been sent and should only be called for data that is sent sucessfully.
/// </remarks>
internal void LogHelloSend()
{
Interlocked.Increment(ref helloMessagesSent);
}
/// <summary>
/// Logs the receiving of an unreliable data packet in the statistics.
/// </summary>
/// <param name="dataLength">The number of bytes of data received.</param>
/// <param name="totalLength">The total number of bytes received.</param>
/// <remarks>
/// This should be called before the received event is invoked so it is up to date for subscribers to that event.
/// </remarks>
internal void LogUnreliableReceive(int dataLength, int totalLength)
{
Interlocked.Increment(ref unreliableMessagesReceived);
Interlocked.Add(ref dataBytesReceived, dataLength);
Interlocked.Add(ref totalBytesReceived, totalLength);
}
/// <summary>
/// Logs the receiving of a reliable data packet in the statistics.
/// </summary>
/// <param name="dataLength">The number of bytes of data received.</param>
/// <param name="totalLength">The total number of bytes received.</param>
/// <remarks>
/// This should be called before the received event is invoked so it is up to date for subscribers to that event.
/// </remarks>
internal void LogReliableReceive(int dataLength, int totalLength)
{
Interlocked.Increment(ref reliableMessagesReceived);
Interlocked.Add(ref dataBytesReceived, dataLength);
Interlocked.Add(ref totalBytesReceived, totalLength);
}
/// <summary>
/// Logs the receiving of a fragmented data packet in the statistics.
/// </summary>
/// <param name="dataLength">The number of bytes of data received.</param>
/// <param name="totalLength">The total number of bytes received.</param>
/// <remarks>
/// This should be called before the received event is invoked so it is up to date for subscribers to that event.
/// </remarks>
internal void LogFragmentedReceive(int dataLength, int totalLength)
{
Interlocked.Increment(ref fragmentedMessagesReceived);
Interlocked.Add(ref dataBytesReceived, dataLength);
Interlocked.Add(ref totalBytesReceived, totalLength);
}
/// <summary>
/// Logs the receiving of an acknowledgement data packet in the statistics.
/// </summary>
/// <param name="totalLength">The total number of bytes received.</param>
/// <remarks>
/// This should be called before the received event is invoked so it is up to date for subscribers to that event.
/// </remarks>
internal void LogAcknowledgementReceive(int totalLength)
{
Interlocked.Increment(ref acknowledgementMessagesReceived);
Interlocked.Add(ref totalBytesReceived, totalLength);
}
/// <summary>
/// Logs the unique acknowledgement of a ping or reliable data packet.
/// </summary>
internal void LogReliablePacketAcknowledged()
{
Interlocked.Increment(ref this.reliablePacketsAcknowledged);
}
/// <summary>
/// Logs the receiving of a hello data packet in the statistics.
/// </summary>
/// <param name="totalLength">The total number of bytes received.</param>
/// <remarks>
/// This should be called before the received event is invoked so it is up to date for subscribers to that event.
/// </remarks>
internal void LogPingReceive(int totalLength)
{
Interlocked.Increment(ref pingMessagesReceived);
Interlocked.Add(ref totalBytesReceived, totalLength);
}
/// <summary>
/// Logs the receiving of a hello data packet in the statistics.
/// </summary>
/// <param name="totalLength">The total number of bytes received.</param>
/// <remarks>
/// This should be called before the received event is invoked so it is up to date for subscribers to that event.
/// </remarks>
internal void LogHelloReceive(int totalLength)
{
Interlocked.Increment(ref helloMessagesReceived);
Interlocked.Add(ref totalBytesReceived, totalLength);
}
internal void LogMessageResent()
{
Interlocked.Increment(ref messagesResent);
}
}
}
|