-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketSendBuffer.cs
419 lines (339 loc) · 14.5 KB
/
WebSocketSendBuffer.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
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using Maverick.WebSockets.Compression;
namespace Maverick.WebSockets
{
public sealed class WebSocketSendBuffer : IDisposable, IBufferWriter<Byte>
{
public WebSocketSendBuffer()
{
}
internal WebSocketSendBuffer( ZLibDeflater deflater )
{
m_deflater = deflater;
}
public void Dispose()
{
if ( Interlocked.Exchange( ref m_disposed, 1 ) == 0 )
{
m_completedLast?.Release();
m_completedLast = null;
m_completedFirst = null;
m_current.Reset();
if ( m_deflater != null )
{
m_deflater.Release();
m_deflater = null;
}
}
}
public void Advance( Int32 count ) => m_current.Advance( count );
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Span<Byte> GetSpan( Int32 sizeHint ) => GetMemory( sizeHint ).Span;
public Memory<Byte> GetMemory( Int32 sizeHint )
{
Retry:
if ( m_current.Available == 0 || m_current.Available < sizeHint )
{
// If nothing is written, discard the current memory and replace it with
// new one that is big enough
if ( m_current.Position == 0 )
{
m_current.Reset( WebSocketBase.Memory.Rent( WebSocketBase.AdjustSegmentSize( sizeHint ) ) );
if ( m_completedFirst == null && m_deflater == null )
{
// Reserve space for the header only if compression is not enabled.
// When compression is enabled, the header is reserved when we compress the memory.
m_current.Advance( WebSocketBase.MaxHeaderSize );
}
}
else
{
if ( m_deflater != null )
{
Deflate();
// Go back from the beginning now that we've freed up space
goto Retry;
}
// The socket doesn't support compression, just appent the segment
m_current.AppendTo( ref m_completedLast );
m_current = new BufferSegment( WebSocketBase.Memory, WebSocketBase.AdjustSegmentSize( sizeHint ) );
if ( m_completedFirst == null )
{
m_completedFirst = m_completedLast;
}
}
}
return m_current.AvailableMemory;
}
public void Write( ReadOnlySequence<Byte> value )
{
foreach ( var segment in value )
{
var target = GetMemory( segment.Length );
segment.CopyTo( target );
Advance( segment.Length );
}
}
public unsafe void Write( ReadOnlySpan<Char> value )
{
if ( value.Length > 0 )
{
if ( m_encoder == null )
{
m_encoder = WebSocketBase.Encoding.GetEncoder();
}
while ( true )
{
var span = GetSpan( 6/*The maximum length a utf-8 char can be*/ );
fixed ( Char* fixedValue = value )
fixed ( Byte* fixedBytes = span )
{
m_encoder.Convert( chars: fixedValue,
charCount: value.Length,
bytes: fixedBytes,
byteCount: span.Length,
flush: true,
out var charsUsed,
out var bytesUsed,
out var completed );
Advance( bytesUsed );
if ( completed )
{
Debug.Assert( value.Length == charsUsed );
break;
}
value = value.Slice( charsUsed );
}
}
}
}
internal WebSocketMessage ToMessage( WebSocketMessageType messageType, WebSocketBase socket )
{
if ( m_current.Position > 0 )
{
if ( m_deflater != null )
{
Deflate();
m_current.Reset();
}
else
{
// No compression, just append the block for sending
m_current.AppendTo( ref m_completedLast );
if ( m_completedFirst == null )
{
m_completedFirst = m_completedLast;
}
}
}
if ( m_deflater != null && m_completedLast != null )
{
FinishDeflater();
}
var offset = 0;
var compressed = m_completedLast != null && m_deflater != null;
if ( socket.Native )
{
if ( m_completedLast == null )
{
// The send procedure requires at least the message header
m_completedLast = new BufferSequenceSegment( WebSocketBase.Memory.Rent( WebSocketBase.MaxHeaderSize ), WebSocketBase.MaxHeaderSize );
m_completedFirst = m_completedLast;
}
offset = WriteHeader( messageType, compressed, socket.IsServer() );
if ( !socket.IsServer() )
{
ApplyMask();
}
}
var message = m_completedFirst == null ? (WebSocketMessage)new WebSocketControlMessage( messageType, Array.Empty<Byte>() ) :
new WebSocketDataMessage( messageType, m_completedFirst, offset, compressed );
m_completedFirst = null;
m_completedLast = null;
return message;
}
private void Deflate()
{
Debug.Assert( m_current.Position > 0 );
if ( m_completedLast == null )
{
m_completedLast = new BufferSequenceSegment( WebSocketBase.Memory.Rent( WebSocketBase.DefaultSegmentSize ), WebSocketBase.MaxHeaderSize );
m_completedFirst = m_completedLast;
}
var compressableBytes = m_current.WrittenMemory.Span;
while ( compressableBytes.Length > 0 )
{
if ( m_completedLast.Available == 0 )
{
m_completedLast = m_completedLast.Append( WebSocketBase.Memory.Rent( WebSocketBase.DefaultSegmentSize ), 0 );
}
m_deflater.Deflate( compressableBytes, m_completedLast.AvailableMemory.Span, out var consumed, out var written );
m_completedLast.Advance( written );
compressableBytes = compressableBytes.Slice( consumed );
}
// Move the current buffer to the start position
m_current.Advance( -m_current.Position );
}
private void FinishDeflater()
{
Debug.Assert( m_completedLast != null );
while ( true )
{
if ( m_completedLast.Available == 0 )
{
m_completedLast = m_completedLast.Append( WebSocketBase.Memory.Rent( WebSocketBase.DefaultSegmentSize ), 0 );
}
var flushByteCount = m_deflater.Finish( m_completedLast.AvailableMemory.Span, out var completed );
m_completedLast.Advance( flushByteCount );
if ( !completed )
{
// The flush needs more space to complete
m_completedLast = m_completedLast.Append( WebSocketBase.Memory.Rent( WebSocketBase.DefaultSegmentSize ), 0 );
}
else
{
break;
}
}
// The deflate stream always ends with 0x00 0x00 0xFF 0xFF
// but the websocket protocol doesn't want it.
if ( m_completedLast.Position >= 4 )
{
m_completedLast.Advance( -4 );
}
else
{
// The current block has less than 4 bytes, so we need to discard it
m_completedLast.Previous.Advance( m_completedLast.Position - 4 );
m_completedLast = m_completedLast.Previous;
m_completedLast.ReleaseNext();
}
}
/// <summary>
/// Writes the message header into the stream. The stream must always have
/// <see cref="WebSocketBase.MaxHeaderSize" /> space at the beginning.
/// </summary>
/// <returns>The offset from the beginning of the stream where the header starts.</returns>
private unsafe Int32 WriteHeader( WebSocketMessageType messageType, Boolean compressed, Boolean server )
{
var segment = m_completedFirst;
var messageSize = segment.Position - WebSocketBase.MaxHeaderSize;
while ( segment.Next != null )
{
segment = segment.Next;
messageSize += segment.Position;
}
Debug.Assert( messageSize >= 0 );
// Client header format:
// 1 bit - FIN - 1 if this is the final fragment in the message (it could be the only fragment), otherwise 0
// 1 bit - Compressed - 1 if message is compressed, otherwise 0
// 1 bit - RSV2 - Reserved - 0
// 1 bit - RSV3 - Reserved - 0
// 4 bits - Opcode - How to interpret the payload
// - 0x0 - continuation
// - 0x1 - text
// - 0x2 - binary
// - 0x8 - connection close
// - 0x9 - ping
// - 0xA - pong
// - (0x3 to 0x7, 0xB-0xF - reserved)
// 1 bit - Masked - 1 if the payload is masked, 0 if it's not. Must be 1 for the client
// 7 bits, 7+16 bits, or 7+64 bits - Payload length
// - For length 0 through 125, 7 bits storing the length
// - For lengths 126 through 2^16, 7 bits storing the value 126, followed by 16 bits storing the length
// - For lengths 2^16+1 through 2^64, 7 bits storing the value 127, followed by 64 bytes storing the length
// 0 or 4 bytes - Mask, if Masked is 1 - random value XOR'd with each 4 bytes of the payload, round-robin
// Length bytes - Payload data
fixed ( Byte* buffer = m_completedFirst.WrittenMemory.Span )
{
buffer[ 0 ] = 0b1000_0000; // We are always sending the message in a single frame
buffer[ 0 ] |= (Byte)messageType;
if ( compressed )
{
buffer[ 0 ] |= 0b01000000;
}
// Store the payload length.
var headerSize = 2;
if ( messageSize <= 125 )
{
buffer[ 1 ] = (Byte)messageSize;
}
else if ( messageSize <= UInt16.MaxValue )
{
buffer[ 1 ] = 126;
buffer[ 2 ] = (Byte)( messageSize / 256 );
buffer[ 3 ] = unchecked((Byte)messageSize);
headerSize += 2;
}
else
{
buffer[ 1 ] = 127;
var length = messageSize;
unchecked
{
for ( var i = 9; i >= 2; --i )
{
buffer[ i ] = (Byte)length;
length = length / 256;
}
}
headerSize += 8;
}
if ( !server ) // Generate the mask
{
buffer[ 1 ] |= 0x80;
headerSize += 4;
lock ( s_random )
{
unchecked
{
buffer[ headerSize - 4 ] = (Byte)s_random.Next( 256 );
buffer[ headerSize - 3 ] = (Byte)s_random.Next( 256 );
buffer[ headerSize - 2 ] = (Byte)s_random.Next( 256 );
buffer[ headerSize - 1 ] = (Byte)s_random.Next( 256 );
}
}
}
if ( headerSize != WebSocketBase.MaxHeaderSize )
{
// We need to block copy the header to be next to the message content
for ( var i = 0; i < headerSize; ++i )
{
buffer[ WebSocketBase.MaxHeaderSize - i - 1 ] = buffer[ headerSize - i - 1 ];
}
}
return WebSocketBase.MaxHeaderSize - headerSize;
}
}
private unsafe void ApplyMask()
{
var mask = default( Int32 );
var segment = m_completedFirst;
fixed ( Byte* fixedSpan = segment.WrittenMemory.Span )
{
mask = *(Int32*)( fixedSpan + WebSocketBase.MaxHeaderSize - 4/*The mask is always 4 bytes*/ );
}
WebSocket.ApplyMask( mask, segment.WrittenMemory.Span.Slice( WebSocketBase.MaxHeaderSize ), 0 );
var offset = segment.WrittenMemory.Length - WebSocketBase.MaxHeaderSize;
var nextSegment = segment.Next;
while ( nextSegment != null )
{
WebSocket.ApplyMask( mask, nextSegment.WrittenMemory.Span, offset );
offset += nextSegment.Position;
nextSegment = nextSegment.Next;
}
}
private Int32 m_disposed;
private BufferSequenceSegment m_completedFirst;
private BufferSequenceSegment m_completedLast;
private BufferSegment m_current;
private ZLibDeflater m_deflater;
private Encoder m_encoder;
private static readonly Random s_random = new Random();
}
}