forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocket.cs
More file actions
445 lines (362 loc) · 14.5 KB
/
WebSocket.cs
File metadata and controls
445 lines (362 loc) · 14.5 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.ClearScript.Util.Web
{
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "This class uses a custom method for deterministic teardown.")]
internal sealed class WebSocket
{
#region data
private readonly Socket socket;
private readonly bool isServerSocket;
private readonly Random random = MiscHelpers.CreateSeededRandom();
private readonly SemaphoreSlim receiveSemaphore = new SemaphoreSlim(1);
private readonly SemaphoreSlim sendSemaphore = new SemaphoreSlim(1);
private readonly InterlockedOneWayFlag closedFlag = new InterlockedOneWayFlag();
#endregion
#region constructors
internal WebSocket(Socket socket, bool isServerSocket)
{
this.socket = socket;
this.isServerSocket = isServerSocket;
}
#endregion
#region message I/O
public async Task<Message> ReceiveMessageAsync()
{
using (await receiveSemaphore.CreateLockScopeAsync().ConfigureAwait(false))
{
bool? isBinary = null;
var payloads = new List<byte[]>();
while (true)
{
var frame = await ReceiveFrameAsync().ConfigureAwait(false);
if (frame.OpCode == OpCodes.Text)
{
if (isBinary.HasValue)
{
throw new Exception(ErrorCode.ProtocolError, "Received unexpected text frame from WebSocket");
}
isBinary = false;
payloads.Add(frame.Payload);
if (frame.Final)
{
break;
}
}
else if (frame.OpCode == OpCodes.Binary)
{
if (isBinary.HasValue)
{
throw new Exception(ErrorCode.ProtocolError, "Received unexpected binary frame from WebSocket");
}
isBinary = true;
payloads.Add(frame.Payload);
if (frame.Final)
{
break;
}
}
else if (frame.OpCode == OpCodes.Continuation)
{
if (!isBinary.HasValue)
{
throw new Exception(ErrorCode.ProtocolError, "Received unexpected continuation frame from WebSocket");
}
payloads.Add(frame.Payload);
if (frame.Final)
{
break;
}
}
else if (frame.OpCode == OpCodes.Ping)
{
var response = new Frame { Final = true, OpCode = OpCodes.Pong, Payload = frame.Payload };
await SendFrameAsync(response).ConfigureAwait(false);
}
else if (frame.OpCode == OpCodes.Pong)
{
}
else if (frame.OpCode == OpCodes.Close)
{
var errorCode = ErrorCode.NormalClosure;
var message = "The WebSocket was closed by the remote endpoint";
if (frame.Payload.Length >= 2)
{
errorCode = (ErrorCode)frame.Payload.ToHostUInt16();
if (frame.Payload.Length > 2)
{
message = Encoding.UTF8.GetString(frame.Payload, 2, frame.Payload.Length - 2);
}
}
throw new Exception(errorCode, message);
}
else
{
throw new Exception(ErrorCode.ProtocolError, "Received unrecognized frame from WebSocket");
}
}
var length = 0L;
var overflow = false;
try
{
length = payloads.Aggregate(0L, (tempLength, segment) => checked(tempLength + segment.LongLength));
}
catch (OverflowException)
{
overflow = true;
}
const long maxLength = 64 * 1024 * 1024;
if (overflow || (length > maxLength))
{
throw new Exception(ErrorCode.MessageTooBig, "Incoming WebSocket message payload is too large");
}
var fullPayload = new byte[length];
var index = 0L;
foreach (var payload in payloads)
{
Array.Copy(payload, 0L, fullPayload, index, payload.LongLength);
index += payload.LongLength;
}
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
Debug.Assert(isBinary.HasValue);
return new Message { IsBinary = isBinary.Value, Payload = fullPayload };
}
}
public Task SendMessageAsync(byte[] payload, bool isBinary = false)
{
var frame = new Frame { Final = true, OpCode = isBinary ? OpCodes.Binary : OpCodes.Text, Payload = payload };
return SendFrameAsync(frame);
}
#endregion
#region frame I/O
private async Task<Frame> ReceiveFrameAsync()
{
var header = await socket.ReceiveBytesAsync(2).ConfigureAwait(false);
var final = header[0].Has(HeaderBits.Final);
var opCode = header[0].And(HeaderBits.OpCodeMask);
var masked = header[1].Has(HeaderBits.Masked);
ulong length = header[1].And(HeaderBits.LengthMask);
if (length == 126)
{
var lengthBytes = await socket.ReceiveBytesAsync(2).ConfigureAwait(false);
length = lengthBytes.ToHostUInt16();
}
else if (length == 127)
{
var lengthBytes = await socket.ReceiveBytesAsync(8).ConfigureAwait(false);
length = lengthBytes.ToHostUInt64() & 0x7FFFFFFFFFFFFFFF;
}
byte[] key = null;
if (masked)
{
key = await socket.ReceiveBytesAsync(4).ConfigureAwait(false);
}
const ulong maxLength = 64 * 1024 * 1024;
if (length > maxLength)
{
throw new Exception(ErrorCode.MessageTooBig, "Incoming WebSocket frame payload is too large");
}
Debug.Assert(length < long.MaxValue);
var payload = new byte[length];
if (length <= int.MaxValue)
{
await socket.ReceiveBytesAsync(payload, 0, Convert.ToInt32(length)).ConfigureAwait(false);
}
else
{
const int segmentLength = 1024 * 1024;
var segment = new byte[segmentLength];
var index = 0L;
for (; length > segmentLength; index += segmentLength)
{
await socket.ReceiveBytesAsync(segment, 0, segmentLength).ConfigureAwait(false);
Array.Copy(segment, 0L, payload, index, segmentLength);
length -= segmentLength;
}
Debug.Assert(length < int.MaxValue);
var remainingLength = Convert.ToInt32(length);
if (remainingLength > 0)
{
await socket.ReceiveBytesAsync(segment, 0, remainingLength).ConfigureAwait(false);
Array.Copy(segment, 0L, payload, index, remainingLength);
}
}
if (masked)
{
for (var index = 0L; index < payload.LongLength; index++)
{
payload[index] ^= key[index % 4];
}
}
return new Frame { Final = final, OpCode = opCode, Payload = payload };
}
private async Task SendFrameAsync(Frame frame)
{
using (await sendSemaphore.CreateLockScopeAsync().ConfigureAwait(false))
{
var header = new byte[2];
var masked = !isServerSocket;
header[0] = frame.OpCode.And(HeaderBits.OpCodeMask);
if (masked)
{
header[1] = HeaderBits.Masked;
}
if (frame.Final)
{
header[0] = header[0].Or(HeaderBits.Final);
}
var length = frame.Payload.LongLength;
byte[] lengthBytes = null;
if (length <= 125)
{
header[1] = header[1].Or(Convert.ToByte(length));
}
else if (length <= ushort.MaxValue)
{
header[1] = header[1].Or(126);
lengthBytes = Convert.ToUInt16(length).ToNetworkBytes();
}
else
{
header[1] = header[1].Or(127);
lengthBytes = Convert.ToUInt64(length).ToNetworkBytes();
}
await socket.SendBytesAsync(header).ConfigureAwait(false);
if (lengthBytes != null)
{
await socket.SendBytesAsync(lengthBytes).ConfigureAwait(false);
}
byte[] key = null;
if (masked)
{
key = new byte[4];
random.NextBytes(key);
await socket.SendBytesAsync(key).ConfigureAwait(false);
}
const int segmentLength = 1024 * 1024; // must be a multiple of 4
var segment = new byte[segmentLength];
var index = 0L;
for (; length > segmentLength; index += segmentLength)
{
Array.Copy(frame.Payload, index, segment, 0L, segmentLength);
if (masked)
{
for (var segmentIndex = 0; segmentIndex < segmentLength; segmentIndex++)
{
segment[segmentIndex] ^= key[segmentIndex % 4];
}
}
await socket.SendBytesAsync(segment).ConfigureAwait(false);
length -= segmentLength;
}
Debug.Assert(length < int.MaxValue);
var remainingLength = Convert.ToInt32(length);
if (remainingLength > 0)
{
Array.Copy(frame.Payload, index, segment, 0L, remainingLength);
if (masked)
{
for (var segmentIndex = 0; segmentIndex < remainingLength; segmentIndex++)
{
segment[segmentIndex] ^= key[segmentIndex % 4];
}
}
await socket.SendBytesAsync(segment, 0, remainingLength).ConfigureAwait(false);
}
}
}
private void SendFrameAsync(Frame frame, Action<bool> callback)
{
SendFrameAsync(frame).ContinueWith(task => callback(MiscHelpers.Try(task.Wait)));
}
#endregion
#region teardown
public void Close(ErrorCode errorCode, string message)
{
if (closedFlag.Set())
{
var payload = ((ushort)errorCode).ToNetworkBytes();
if (!string.IsNullOrEmpty(message))
{
payload = payload.Concat(Encoding.UTF8.GetBytes(message)).ToArray();
}
var frame = new Frame { Final = true, OpCode = OpCodes.Close, Payload = payload };
SendFrameAsync(frame, succeeded =>
{
socket.Close();
receiveSemaphore.Dispose();
sendSemaphore.Dispose();
});
}
}
#endregion
#region Nested type: Message
public sealed class Message
{
public bool IsBinary;
public byte[] Payload;
}
#endregion
#region Nested type: ErrorCode
internal enum ErrorCode
{
NormalClosure = 1000,
EndpointUnavailable = 1001,
ProtocolError = 1002,
InvalidMessageType = 1003,
InvalidPayloadData = 1007,
PolicyViolation = 1008,
MessageTooBig = 1009
}
#endregion
#region Nested type: Exception
[Serializable]
public sealed class Exception : System.Exception
{
public ErrorCode ErrorCode { get; private set; }
public Exception(ErrorCode errorCode, string message)
: base(message)
{
ErrorCode = errorCode;
}
}
#endregion
#region Nested type: HeaderBits
private static class HeaderBits
{
public const byte Final = 0x80;
public const byte OpCodeMask = 0x0F;
public const byte Masked = 0x80;
public const byte LengthMask = 0x7F;
}
#endregion
#region Nested type: OpCodes
private static class OpCodes
{
public const byte Continuation = 0x00;
public const byte Text = 0x01;
public const byte Binary = 0x02;
public const byte Close = 0x08;
public const byte Ping = 0x09;
public const byte Pong = 0x0A;
}
#endregion
#region Nested type: Frame
private sealed class Frame
{
public bool Final;
public byte OpCode;
public byte[] Payload;
}
#endregion
}
}