-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cs
More file actions
521 lines (431 loc) · 13.9 KB
/
Connection.cs
File metadata and controls
521 lines (431 loc) · 13.9 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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Timers;
namespace Terralite
{
/// <summary>
/// This class holds data for a network connection to a single end point.
/// </summary>
public class Connection
{
public const byte R_DISCONNECT = 1;
public const byte R_TIMEOUT = 2;
/// <summary>
/// Whether this connection is currently connected to its
/// remote end point
/// </summary>
public bool Connected { get; private set; }
/// <summary>
/// Where this connection is connected to
/// </summary>
public EndPoint EndPoint { get; private set; }
/// <summary>
/// Connection ID
/// </summary>
public int ID { get; private set; }
/// <summary>
/// Maximum number of packet send retries
/// </summary>
public int MaxTries { get; set; }
public delegate void ReceiveEvent(int connId, byte[] data);
public event ReceiveEvent Receive;
public delegate void DisconnectEvent(int connId, byte reason);
public event DisconnectEvent Disconnect;
private ReliableConnection _reliableConnection;
private int _generatedHandshakeNumber;
private int _receivedHandshakeNumber;
private Dictionary<byte, GuaranteedPacket> _guaranteedPackets;
private byte _nextSendID = 1;
private Dictionary<byte, Timer> _timers;
private Timer _handshakeTimeout;
private Timer _handshakeInitTimer;
/// <summary>
/// Sends pings to keep alive the connection
/// </summary>
private Timer _keepAlive;
/// <summary>
/// Handles the connection timing out
/// </summary>
private Timer _timeout;
private OrderedDictionary _orderedPackets;
private byte _nextExpectedID;
private bool _firstPacket = true;
private byte[][] _multiPacket = null;
public Connection(ReliableConnection reliableConnection, EndPoint endPoint, int id)
{
_reliableConnection = reliableConnection;
EndPoint = endPoint;
ID = id;
MaxTries = reliableConnection.MaxRetries;
_guaranteedPackets = new Dictionary<byte, GuaranteedPacket>();
_timers = new Dictionary<byte, Timer>();
_orderedPackets = new OrderedDictionary();
_keepAlive = new Timer(_reliableConnection.KeepAlivePingTime * 1000);
_keepAlive.Elapsed += (o, e) =>
{
SendPing();
};
_timeout = new Timer(_reliableConnection.ConnectionTimeout * 1000)
{
AutoReset = false
};
_timeout.Elapsed += (o, e) =>
{
OnTimeout();
};
_handshakeInitTimer = new Timer(_reliableConnection.ConnectInterval * 1000);
_handshakeInitTimer.Elapsed += (o, e) =>
{
InitiateHandshakePhase1();
};
_handshakeTimeout = new Timer(_reliableConnection.ConnectTimeout * 1000);
_handshakeTimeout.Elapsed += (o, e) =>
{
_handshakeInitTimer.Stop();
_handshakeTimeout.Stop();
};
}
/// <summary>
/// Clears the list of ReceiveEvents
/// </summary>
public void ClearReceiveEvents()
{
Receive = null;
}
/// <summary>
/// Clears the list of DisconnectEvents
/// </summary>
public void ClearDisconnectEvents()
{
Disconnect = null;
}
/// <summary>
/// Creates a guaranteed packet from the data passed in.
/// </summary>
/// <param name="packet">Packet data</param>
public void SendPacket(byte[] packet)
{
GuaranteedPacket guaranteedPacket = new GuaranteedPacket(_nextSendID, packet);
_guaranteedPackets.Add(_nextSendID, guaranteedPacket);
_nextSendID = (byte)((_nextSendID + 1) % byte.MaxValue);
Timer timer = new Timer(_reliableConnection.RetryInterval * 1000)
{
AutoReset = true
};
timer.Elapsed += (sender, e) =>
{
_reliableConnection.Send(ID, guaranteedPacket.ByteArray, guaranteedPacket.Header);
};
timer.Start();
_timers.Add(guaranteedPacket.PacketID, timer);
_reliableConnection.Send(ID, guaranteedPacket.ByteArray, guaranteedPacket.Header);
}
/// <summary>
/// Called when data is received from the managing ReliableConnection.
/// </summary>
/// <param name="buffer"></param>
public void ProcessData(byte[] buffer)
{
//pieces[0] = byte[] header
//pieces[1] = byte[] data
byte[][] pieces = buffer[0] != Packet.MULTI ? Utils.Split(buffer) : ProcessMulti(buffer);
if (OnPreReceive(pieces[0], pieces[1]))
OnReceive(pieces[1]);
}
/// <summary>
/// Handles storing multi-part packets and reconstructing when all
/// have arrived.
/// </summary>
/// <param name="buffer">One part of the whole packet</param>
/// <returns>null if all data hasn't been received, otherwise
/// Two byte arrays</returns>
private byte[][] ProcessMulti(byte[] buffer)
{
//Create a byte array of arrays for storing the parts of this multi packet
if (_multiPacket == null)
_multiPacket = new byte[buffer[1]][];
//Initialize this slot in the array and copy
_multiPacket[buffer[2]] = new byte[buffer.Length - 3];
Array.Copy(buffer, 3, _multiPacket[buffer[2]], 0, _multiPacket[buffer[2]].Length);
//If we don't have all the packets, return null
for (int i = 0; i < _multiPacket.Length; i++)
if (_multiPacket[i] == null)
return null;
MemoryStream ms = new MemoryStream();
for (int i = 0; i < _multiPacket.Length; i++)
ms.Write(_multiPacket[i], 0, _multiPacket[i].Length);
byte[] whole = new byte[ms.Length];
ms.Read(whole, 0, whole.Length);
_multiPacket = null;
return Utils.Split(whole);
}
/// <summary>
/// Called when the connection handshake finishes
/// </summary>
private void OnConnected()
{
_reliableConnection.Log("Successfully connected");
Connected = true;
_handshakeInitTimer.Stop();
_handshakeTimeout.Stop();
_keepAlive.Start();
_timeout.Start();
}
/// <summary>
/// Called before <c>OnReceive</c> to do packet preprocessing.
/// </summary>
/// <param name="header">Header of packet</param>
/// <param name="data">Packet data to do preprocessing with</param>
/// <returns>Whether or not <c>OnReceive</c> needs to be called</returns>
public bool OnPreReceive(byte[] header, byte[] data)
{
byte type = header[0];
if (type < Packet.MIN_VALUE || type > Packet.MAX_VALUE)
{
_reliableConnection.Log($"Got unknown packet type {type}");
return false;
}
RestartTimeout();
byte packetID;
switch (type)
{
case Packet.INIT:
int value = BitConverter.ToInt32(data, 0);
InitiateHandshakePhase2(value);
return false;
case Packet.INIT_ACK:
int valueA = BitConverter.ToInt32(data, 0);
int valueB = BitConverter.ToInt32(data, 4);
FinalizeHandshake(valueA, valueB);
return false;
case Packet.INIT_FIN:
int a = BitConverter.ToInt32(data, 0);
int b = BitConverter.ToInt32(data, 4);
if (_receivedHandshakeNumber != a)
{
_reliableConnection.Log($"RX handshake number error during connection handshake. Expected {_receivedHandshakeNumber} got {a}.");
_reliableConnection.Log("Connection will be closed.");
_reliableConnection.Disconnect(ID);
}
else if (_generatedHandshakeNumber + 1 != b)
{
_reliableConnection.Log($"Generated handshake number error during connection handshake. Expected {_generatedHandshakeNumber + 1} got {b}.");
_reliableConnection.Log("Connection will be closed.");
_reliableConnection.Disconnect(ID);
}
_reliableConnection.Log("Handshake complete!");
OnConnected();
return false;
case Packet.NON_RELIABLE:
return true;
case Packet.PING:
SendPingAck();
return false;
case Packet.RELIABLE:
packetID = header[1];
SendAck(packetID);
if (!_reliableConnection.UseOrdering)
return true;
else
{
if (_firstPacket)
_nextExpectedID = packetID;
//if already timeout waiting for this packet
if (packetID < _nextExpectedID)
return false;
//if got next expected id
else if (packetID == _nextExpectedID)
{
_nextExpectedID = (byte)((_nextExpectedID + 1) % byte.MaxValue);
//while we have the next sequential packet, call OnReceive for it
while (_orderedPackets.Contains(_nextExpectedID))
{
OrderedPacket op = (OrderedPacket)_orderedPackets[(object)_nextExpectedID];
_orderedPackets.Remove(_nextExpectedID);
_nextExpectedID = (byte)((_nextExpectedID + 1) % byte.MaxValue);
OnReceive(op.Data);
}
return true;
}
_orderedPackets.Add(packetID, new OrderedPacket(this, packetID, data));
return false;
}
case Packet.ACK:
packetID = header[1];
if (!_guaranteedPackets.ContainsKey(packetID))
{
_reliableConnection.Log($"Didn't send packet id {packetID}");
return false;
}
_reliableConnection.Log($"Got ack for id {packetID}");
ClearPacket(packetID);
return false;
default:
return false;
}
}
/// <summary>
/// Called when data has been received. Invokes all functions
/// stored in the Receive event.
/// </summary>
/// <param name="data">Data that was received</param>
/// <remarks>
/// Note this function does not do anything if <c>Receive</c> is <c>null</c>.
/// </remarks>
public void OnReceive(byte[] data)
{
Receive?.Invoke(ID, data);
}
/// <summary>
/// Called when the connection times out, i.e. no data is received for
/// reliableConnection.ConnectionTimeout seconds.
/// </summary>
private void OnTimeout()
{
_reliableConnection.Log($"Connection {ID} timed out");
Disconnect?.Invoke(ID, R_TIMEOUT);
_keepAlive.Stop();
_reliableConnection.Disconnect(ID);
}
/// <summary>
/// Called when the remote end disconnected. Invokes all functions
/// stored in the Disconnect event.
/// </summary>
public void OnDisconnect()
{
Disconnect?.Invoke(ID, R_DISCONNECT);
_keepAlive.Stop();
_timeout.Stop();
}
/// <summary>
/// Initiates the handshake process with the remove endpoint associated with
/// connection ID <paramref name="connectionId"/>
/// </summary>
/// <param name="connectionId">The connection ID to initiate a handshake on</param>
internal void InitiateHandshakePhase1()
{
_handshakeInitTimer.Start();
_handshakeTimeout.Start();
_reliableConnection.Log("Initiating handshake phase 1");
_generatedHandshakeNumber = new Random().Next();
_reliableConnection.Log($"Handshake a: {_generatedHandshakeNumber}");
_reliableConnection.Send(ID, BitConverter.GetBytes(_generatedHandshakeNumber), Packet.HEADER_INIT);
}
/// <summary>
/// Phase two of the handshake process
/// </summary>
/// <param name="valueA">The random value received from the remote end point</param>
private void InitiateHandshakePhase2(int valueA)
{
_reliableConnection.Log("Initiating handshake phase 2");
_reliableConnection.Log($"Handshake a: {valueA}");
valueA++;
_receivedHandshakeNumber = valueA;
_generatedHandshakeNumber = new Random().Next();
_reliableConnection.Log($"Handshake b: {_generatedHandshakeNumber}");
byte[] data = new byte[sizeof(int) * 2];
byte[] num1 = BitConverter.GetBytes(valueA);
byte[] num2 = BitConverter.GetBytes(_generatedHandshakeNumber);
Array.Copy(num1, data, num1.Length);
Array.Copy(num2, 0, data, 4, num2.Length);
_reliableConnection.Send(ID, data, Packet.HEADER_INIT_ACK);
}
/// <summary>
/// Finalizes the handshake process
/// </summary>
/// <param name="valueA">The value received to be checked</param>
/// <param name="valueB">The random value received from the remote end point to be incremented</param>
private void FinalizeHandshake(int valueA, int valueB)
{
if (_generatedHandshakeNumber + 1 != valueA)
{
_reliableConnection.Log($"Error finalizing connection handshake. Expected {_generatedHandshakeNumber + 1} got {valueA}");
_reliableConnection.Log("Connection will be closed.");
_reliableConnection.Disconnect(ID);
}
valueB++;
_reliableConnection.Log($"Finalizing handshake: {valueB}");
byte[] data = new byte[sizeof(int) * 2];
byte[] num1 = BitConverter.GetBytes(valueA);
byte[] num2 = BitConverter.GetBytes(valueB);
Array.Copy(num1, data, num1.Length);
Array.Copy(num2, 0, data, 4, num2.Length);
_reliableConnection.Send(ID, data, Packet.HEADER_INIT_FIN);
OnConnected();
}
/// <summary>
/// Sends a ping packet.
/// </summary>
private void SendPing()
{
_reliableConnection.Send(ID, null, Packet.PING_PACKET);
}
/// <summary>
/// Sends an acknowledgement packet for the received ping.
/// </summary>
private void SendPingAck()
{
_reliableConnection.Send(ID, null, Packet.PING_ACK_PACKET);
}
/// <summary>
/// Sends an acknowledgement packet for <paramref name="packetid"/>.
/// </summary>
/// <param name="packetid">The packet id to acknowledge</param>
private void SendAck(byte packetid)
{
_reliableConnection.Send(ID, new byte[] { packetid }, Packet.ACK_PACKET);
}
/// <summary>
/// Called to restart the connection timeout timer.
/// </summary>
private void RestartTimeout()
{
if (!Connected) return;
_timeout.Stop();
_timeout.Start();
}
public void ClearTimers()
{
_timeout.Stop();
_keepAlive.Stop();
}
/// <summary>
/// Clears the specified packet id from sending.
/// </summary>
/// <param name="id">Packet id to clear</param>
public void ClearPacket(byte id)
{
_guaranteedPackets.Remove(id);
_timers[id].Dispose();
_timers.Remove(id);
}
/// <summary>
/// Clears all outgoing packets.
/// </summary>
public void ClearAllPackets()
{
foreach (KeyValuePair<byte, GuaranteedPacket> pair in _guaranteedPackets)
_timers[pair.Key].Dispose();
_guaranteedPackets.Clear();
_timers.Clear();
}
/// <summary>
/// Class to hold data for handling an ordered packet
/// </summary>
private class OrderedPacket
{
public byte PacketID { get; set; }
public byte[] Data { get; set; }
private Connection connection;
public OrderedPacket(Connection c, byte packetID, byte[] packet)
{
connection = c;
PacketID = packetID;
Data = packet;
}
}
}
}