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
|
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Impostor.Api.Net.Messages;
using Microsoft.Extensions.ObjectPool;
using Serilog;
namespace Impostor.Hazel.Udp
{
/// <summary>
/// Represents a connection that uses the UDP protocol.
/// </summary>
/// <inheritdoc />
public abstract partial class UdpConnection : NetworkConnection
{
protected static readonly byte[] EmptyDisconnectBytes = { (byte)UdpSendOption.Disconnect };
private static readonly ILogger Logger = Log.ForContext<UdpConnection>();
private readonly ConnectionListener _listener;
private readonly ObjectPool<MessageReader> _readerPool;
private readonly CancellationTokenSource _stoppingCts;
private bool _isDisposing;
private bool _isFirst = true;
private Task _executingTask;
protected UdpConnection(ConnectionListener listener, ObjectPool<MessageReader> readerPool)
{
_listener = listener;
_readerPool = readerPool;
_stoppingCts = new CancellationTokenSource();
Pipeline = Channel.CreateUnbounded<byte[]>(new UnboundedChannelOptions
{
SingleReader = true,
SingleWriter = true
});
}
internal Channel<byte[]> Pipeline { get; }
public Task StartAsync()
{
// Store the task we're executing
_executingTask = Task.Factory.StartNew(ReadAsync, TaskCreationOptions.LongRunning);
// If the task is completed then return it, this will bubble cancellation and failure to the caller
if (_executingTask.IsCompleted)
{
return _executingTask;
}
// Otherwise it's running
return Task.CompletedTask;
}
public void Stop()
{
// Stop called without start
if (_executingTask == null)
{
return;
}
// Signal cancellation to methods.
_stoppingCts.Cancel();
try
{
// Cancel reader.
Pipeline.Writer.Complete();
}
catch (ChannelClosedException)
{
// Already done.
}
// Remove references.
if (!_isDisposing)
{
Dispose(true);
}
}
private async Task ReadAsync()
{
var reader = new MessageReader(_readerPool);
while (!_stoppingCts.IsCancellationRequested)
{
var result = await Pipeline.Reader.ReadAsync(_stoppingCts.Token);
try
{
reader.Update(result);
await HandleReceive(reader);
}
catch (Exception e)
{
Logger.Error(e, "Exception during ReadAsync");
Dispose(true);
break;
}
}
}
/// <summary>
/// Writes the given bytes to the connection.
/// </summary>
/// <param name="bytes">The bytes to write.</param>
/// <param name="length"></param>
protected abstract ValueTask WriteBytesToConnection(byte[] bytes, int length);
/// <inheritdoc/>
public override async ValueTask SendAsync(IMessageWriter msg)
{
if (this._state != ConnectionState.Connected)
throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?");
byte[] buffer = new byte[msg.Length];
Buffer.BlockCopy(msg.Buffer, 0, buffer, 0, msg.Length);
switch (msg.SendOption)
{
case MessageType.Reliable:
ResetKeepAliveTimer();
AttachReliableID(buffer, 1, buffer.Length);
await WriteBytesToConnection(buffer, buffer.Length);
Statistics.LogReliableSend(buffer.Length - 3, buffer.Length);
break;
default:
await WriteBytesToConnection(buffer, buffer.Length);
Statistics.LogUnreliableSend(buffer.Length - 1, buffer.Length);
break;
}
}
/// <inheritdoc/>
/// <remarks>
/// <include file="DocInclude/common.xml" path="docs/item[@name='Connection_SendBytes_General']/*" />
/// <para>
/// Udp connections can currently send messages using <see cref="SendOption.None"/> and
/// <see cref="SendOption.Reliable"/>. Fragmented messages are not currently supported and will default to
/// <see cref="SendOption.None"/> until implemented.
/// </para>
/// </remarks>
public override async ValueTask SendBytes(byte[] bytes, MessageType sendOption = MessageType.Unreliable)
{
//Add header information and send
await HandleSend(bytes, (byte)sendOption);
}
/// <summary>
/// Handles the reliable/fragmented sending from this connection.
/// </summary>
/// <param name="data">The data being sent.</param>
/// <param name="sendOption">The <see cref="SendOption"/> specified as its byte value.</param>
/// <param name="ackCallback">The callback to invoke when this packet is acknowledged.</param>
/// <returns>The bytes that should actually be sent.</returns>
protected async ValueTask HandleSend(byte[] data, byte sendOption, Action ackCallback = null)
{
switch (sendOption)
{
case (byte)UdpSendOption.Ping:
case (byte)MessageType.Reliable:
case (byte)UdpSendOption.Hello:
await ReliableSend(sendOption, data, ackCallback);
break;
//Treat all else as unreliable
default:
await UnreliableSend(sendOption, data);
break;
}
}
/// <summary>
/// Handles the receiving of data.
/// </summary>
/// <param name="message">The buffer containing the bytes received.</param>
protected async ValueTask HandleReceive(MessageReader message)
{
// Check if the first message received is the hello packet.
if (_isFirst)
{
_isFirst = false;
// Slice 4 bytes to get handshake data.
if (_listener != null)
{
using (var handshake = message.Copy(4))
{
await _listener.InvokeNewConnection(handshake, this);
}
}
}
switch (message.Buffer[0])
{
//Handle reliable receives
case (byte)MessageType.Reliable:
await ReliableMessageReceive(message);
break;
//Handle acknowledgments
case (byte)UdpSendOption.Acknowledgement:
AcknowledgementMessageReceive(message.Buffer);
break;
//We need to acknowledge hello and ping messages but dont want to invoke any events!
case (byte)UdpSendOption.Ping:
await ProcessReliableReceive(message.Buffer, 1);
Statistics.LogHelloReceive(message.Length);
break;
case (byte)UdpSendOption.Hello:
await ProcessReliableReceive(message.Buffer, 1);
Statistics.LogHelloReceive(message.Length);
break;
case (byte)UdpSendOption.Disconnect:
using (var reader = message.Copy(1))
{
await DisconnectRemote("The remote sent a disconnect request", reader);
}
break;
//Treat everything else as unreliable
default:
using (var reader = message.Copy(1))
{
await InvokeDataReceived(reader, MessageType.Unreliable);
}
Statistics.LogUnreliableReceive(message.Length - 1, message.Length);
break;
}
}
/// <summary>
/// Sends bytes using the unreliable UDP protocol.
/// </summary>
/// <param name="sendOption">The SendOption to attach.</param>
/// <param name="data">The data.</param>
ValueTask UnreliableSend(byte sendOption, byte[] data)
{
return UnreliableSend(sendOption, data, 0, data.Length);
}
/// <summary>
/// Sends bytes using the unreliable UDP protocol.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="sendOption">The SendOption to attach.</param>
/// <param name="offset"></param>
/// <param name="length"></param>
async ValueTask UnreliableSend(byte sendOption, byte[] data, int offset, int length)
{
byte[] bytes = new byte[length + 1];
//Add message type
bytes[0] = sendOption;
//Copy data into new array
Buffer.BlockCopy(data, offset, bytes, bytes.Length - length, length);
//Write to connection
await WriteBytesToConnection(bytes, bytes.Length);
Statistics.LogUnreliableSend(length, bytes.Length);
}
/// <summary>
/// Sends a hello packet to the remote endpoint.
/// </summary>
/// <param name="bytes"></param>
/// <param name="acknowledgeCallback">The callback to invoke when the hello packet is acknowledged.</param>
protected ValueTask SendHello(byte[] bytes, Action acknowledgeCallback)
{
//First byte of handshake is version indicator so add data after
byte[] actualBytes;
if (bytes == null)
{
actualBytes = new byte[1];
}
else
{
actualBytes = new byte[bytes.Length + 1];
Buffer.BlockCopy(bytes, 0, actualBytes, 1, bytes.Length);
}
return HandleSend(actualBytes, (byte)UdpSendOption.Hello, acknowledgeCallback);
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
_isDisposing = true;
Stop();
DisposeKeepAliveTimer();
DisposeReliablePackets();
}
base.Dispose(disposing);
}
}
}
|