/
ConnectionConfig.cs
540 lines (514 loc) · 13 KB
/
ConnectionConfig.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
// Decompiled with JetBrains decompiler
// Type: UnityEngine.Networking.ConnectionConfig
// Assembly: UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: B9965B63-74A2-480C-BCFC-887FBCF7E9A7
// Assembly location: C:\Program Files\Unity 5.1.0b6\Editor\Data\Managed\UnityEngine.dll
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityEngine.Networking
{
/// <summary>
///
/// <para>
/// This class defines parameters of connection between two peers, this definition includes various timeouts and sizes as well as channel configuration.
/// </para>
///
/// </summary>
[Serializable]
public class ConnectionConfig
{
[SerializeField]
internal List<ChannelQOS> m_Channels = new List<ChannelQOS>();
private const int g_MinPacketSize = 128;
[SerializeField]
private ushort m_PacketSize;
[SerializeField]
private ushort m_FragmentSize;
[SerializeField]
private uint m_ResendTimeout;
[SerializeField]
private uint m_DisconnectTimeout;
[SerializeField]
private uint m_ConnectTimeout;
[SerializeField]
private uint m_MinUpdateTimeout;
[SerializeField]
private uint m_PingTimeout;
[SerializeField]
private uint m_ReducedPingTimeout;
[SerializeField]
private uint m_AllCostTimeout;
[SerializeField]
private byte m_NetworkDropThreshold;
[SerializeField]
private byte m_OverflowDropThreshold;
[SerializeField]
private byte m_MaxConnectionAttempt;
[SerializeField]
private uint m_AckDelay;
[SerializeField]
private ushort m_MaxCombinedReliableMessageSize;
[SerializeField]
private ushort m_MaxCombinedReliableMessageCount;
[SerializeField]
private ushort m_MaxSentMessageQueueSize;
[SerializeField]
private bool m_IsAcksLong;
/// <summary>
///
/// <para>
/// What is a maximum packet size (in Bytes) (including payload and all header). Packet can contain multiple messages inside.
/// </para>
///
/// </summary>
public ushort PacketSize
{
get
{
return this.m_PacketSize;
}
set
{
this.m_PacketSize = value;
}
}
/// <summary>
///
/// <para>
/// What should be maximum fragment size (in Bytes) for fragmented messages.
/// </para>
///
/// </summary>
public ushort FragmentSize
{
get
{
return this.m_FragmentSize;
}
set
{
this.m_FragmentSize = value;
}
}
/// <summary>
///
/// <para>
/// Minimum timeout (in ms) which library will wait before it will resend reliable message.
/// </para>
///
/// </summary>
public uint ResendTimeout
{
get
{
return this.m_ResendTimeout;
}
set
{
this.m_ResendTimeout = value;
}
}
/// <summary>
///
/// <para>
/// How long (in ms) library will wait before it will consider connection as disconnected.
/// </para>
///
/// </summary>
public uint DisconnectTimeout
{
get
{
return this.m_DisconnectTimeout;
}
set
{
this.m_DisconnectTimeout = value;
}
}
/// <summary>
///
/// <para>
/// Timeout in ms which library will wait before it will send another connection request.
/// </para>
///
/// </summary>
public uint ConnectTimeout
{
get
{
return this.m_ConnectTimeout;
}
set
{
this.m_ConnectTimeout = value;
}
}
/// <summary>
///
/// <para>
/// Minimal send update timeout (in ms) for connection. this timeout could be increased by library if flow control will required.
/// </para>
///
/// </summary>
public uint MinUpdateTimeout
{
get
{
return this.m_MinUpdateTimeout;
}
set
{
this.m_MinUpdateTimeout = value;
}
}
/// <summary>
///
/// <para>
/// Timeout in ms between control protocol messages.
/// </para>
///
/// </summary>
public uint PingTimeout
{
get
{
return this.m_PingTimeout;
}
set
{
this.m_PingTimeout = value;
}
}
/// <summary>
///
/// <para>
/// Timeout in ms for control messages which library will use before it will accumulate statistics.
/// </para>
///
/// </summary>
public uint ReducedPingTimeout
{
get
{
return this.m_ReducedPingTimeout;
}
set
{
this.m_ReducedPingTimeout = value;
}
}
/// <summary>
///
/// <para>
/// Defines timeout in ms after that message with AllCost deliver qos will force resend without acknowledgement waiting.
/// </para>
///
/// </summary>
public uint AllCostTimeout
{
get
{
return this.m_AllCostTimeout;
}
set
{
this.m_AllCostTimeout = value;
}
}
/// <summary>
///
/// <para>
/// How many (in %) packet need to be dropped due network condition before library will throttle send rate.
/// </para>
///
/// </summary>
public byte NetworkDropThreshold
{
get
{
return this.m_NetworkDropThreshold;
}
set
{
this.m_NetworkDropThreshold = value;
}
}
/// <summary>
///
/// <para>
/// How many (in %) packet need to be dropped due lack of internal bufferes before library will throttle send rate.
/// </para>
///
/// </summary>
public byte OverflowDropThreshold
{
get
{
return this.m_OverflowDropThreshold;
}
set
{
this.m_OverflowDropThreshold = value;
}
}
/// <summary>
///
/// <para>
/// How many attempt library will get before it will consider the connection as disconnected.
/// </para>
///
/// </summary>
public byte MaxConnectionAttempt
{
get
{
return this.m_MaxConnectionAttempt;
}
set
{
this.m_MaxConnectionAttempt = value;
}
}
/// <summary>
///
/// <para>
/// How long in ms receiver will wait before it will force send acknowledgements back without waiting any payload.
/// </para>
///
/// </summary>
public uint AckDelay
{
get
{
return this.m_AckDelay;
}
set
{
this.m_AckDelay = value;
}
}
/// <summary>
///
/// <para>
/// Maximum size of reliable message which library will consider as small and will try to combine in one "array of messages" message.
/// </para>
///
/// </summary>
public ushort MaxCombinedReliableMessageSize
{
get
{
return this.m_MaxCombinedReliableMessageSize;
}
set
{
this.m_MaxCombinedReliableMessageSize = value;
}
}
/// <summary>
///
/// <para>
/// Maximum amount of small reliable messages which will combine in one "array of messages". Useful if you are going to send a lot of small reliable messages.
/// </para>
///
/// </summary>
public ushort MaxCombinedReliableMessageCount
{
get
{
return this.m_MaxCombinedReliableMessageCount;
}
set
{
this.m_MaxCombinedReliableMessageCount = value;
}
}
/// <summary>
///
/// <para>
/// Defines maximum messages which will wait for sending before user will receive error on Send() call.
/// </para>
///
/// </summary>
public ushort MaxSentMessageQueueSize
{
get
{
return this.m_MaxSentMessageQueueSize;
}
set
{
this.m_MaxSentMessageQueueSize = value;
}
}
/// <summary>
///
/// <para>
/// If it is true, connection will use 64 bit mask to acknowledge received reliable messages.
/// </para>
///
/// </summary>
public bool IsAcksLong
{
get
{
return this.m_IsAcksLong;
}
set
{
this.m_IsAcksLong = value;
}
}
/// <summary>
///
/// <para>
/// Return amount of channels for current configuration.
/// </para>
///
/// </summary>
public int ChannelCount
{
get
{
return this.m_Channels.Count;
}
}
/// <summary>
///
/// <para>
/// Allow access to channels list.
/// </para>
///
/// </summary>
public List<ChannelQOS> Channels
{
get
{
return this.m_Channels;
}
}
/// <summary>
///
/// <para>
/// Will create default connection config or will copy them from another.
/// </para>
///
/// </summary>
/// <param name="config">Connection config.</param>
public ConnectionConfig()
{
this.m_PacketSize = (ushort) 1500;
this.m_FragmentSize = (ushort) 500;
this.m_ResendTimeout = 1200U;
this.m_DisconnectTimeout = 2000U;
this.m_ConnectTimeout = 2000U;
this.m_MinUpdateTimeout = 10U;
this.m_PingTimeout = 500U;
this.m_ReducedPingTimeout = 100U;
this.m_AllCostTimeout = 20U;
this.m_NetworkDropThreshold = (byte) 5;
this.m_OverflowDropThreshold = (byte) 5;
this.m_MaxConnectionAttempt = (byte) 10;
this.m_AckDelay = 33U;
this.m_MaxCombinedReliableMessageSize = (ushort) 100;
this.m_MaxCombinedReliableMessageCount = (ushort) 10;
this.m_MaxSentMessageQueueSize = (ushort) 128;
this.m_IsAcksLong = false;
}
/// <summary>
///
/// <para>
/// Will create default connection config or will copy them from another.
/// </para>
///
/// </summary>
/// <param name="config">Connection config.</param>
public ConnectionConfig(ConnectionConfig config)
{
if (config == null)
throw new NullReferenceException("config is not defined");
this.m_PacketSize = config.m_PacketSize;
this.m_FragmentSize = config.m_FragmentSize;
this.m_ResendTimeout = config.m_ResendTimeout;
this.m_DisconnectTimeout = config.m_DisconnectTimeout;
this.m_ConnectTimeout = config.m_ConnectTimeout;
this.m_MinUpdateTimeout = config.m_MinUpdateTimeout;
this.m_PingTimeout = config.m_PingTimeout;
this.m_ReducedPingTimeout = config.m_ReducedPingTimeout;
this.m_AllCostTimeout = config.m_AllCostTimeout;
this.m_NetworkDropThreshold = config.m_NetworkDropThreshold;
this.m_OverflowDropThreshold = config.m_OverflowDropThreshold;
this.m_MaxConnectionAttempt = config.m_MaxConnectionAttempt;
this.m_AckDelay = config.m_AckDelay;
this.m_MaxCombinedReliableMessageSize = config.MaxCombinedReliableMessageSize;
this.m_MaxCombinedReliableMessageCount = config.m_MaxCombinedReliableMessageCount;
this.m_MaxSentMessageQueueSize = config.m_MaxSentMessageQueueSize;
this.m_IsAcksLong = config.m_IsAcksLong;
using (List<ChannelQOS>.Enumerator enumerator = config.m_Channels.GetEnumerator())
{
while (enumerator.MoveNext())
this.m_Channels.Add(new ChannelQOS(enumerator.Current));
}
}
/// <summary>
///
/// <para>
/// Validate parameters of connection config. Will throw exceptions if parameters are incorrect.
/// </para>
///
/// </summary>
/// <param name="config"/>
public static void Validate(ConnectionConfig config)
{
if ((int) config.m_PacketSize < 128)
throw new ArgumentOutOfRangeException("PacketSize should be > " + 128.ToString());
if ((int) config.m_FragmentSize >= (int) config.m_PacketSize - 128)
throw new ArgumentOutOfRangeException("FragmentSize should be < PacketSize - " + 128.ToString());
if (config.m_Channels.Count > (int) byte.MaxValue)
throw new ArgumentOutOfRangeException("Channels number should be less than 256");
}
/// <summary>
///
/// <para/>
///
/// </summary>
/// <param name="value">Add new channel to configuration.</param>
/// <returns>
///
/// <para>
/// Channel id, user can use this id to send message via this channel.
/// </para>
///
/// </returns>
public byte AddChannel(QosType value)
{
if (this.m_Channels.Count > (int) byte.MaxValue)
throw new ArgumentOutOfRangeException("Channels Count should be less than 256");
this.m_Channels.Add(new ChannelQOS(value));
return (byte) (this.m_Channels.Count - 1);
}
/// <summary>
///
/// <para>
/// Return the QoS set for the given channel or throw an out of range exception.
/// </para>
///
/// </summary>
/// <param name="idx">Index in array.</param>
/// <returns>
///
/// <para>
/// Channel QoS.
/// </para>
///
/// </returns>
public QosType GetChannel(byte idx)
{
if ((int) idx >= this.m_Channels.Count)
throw new ArgumentOutOfRangeException("requested index greater than maximum channels count");
return this.m_Channels[(int) idx].QOS;
}
}
}