-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathFraming.cs
520 lines (452 loc) · 24.1 KB
/
Framing.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
//-----------------------------------------------------------------------
// <copyright file="Framing.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Akka.IO;
using Akka.Streams.Implementation.Fusing;
using Akka.Streams.Implementation.Stages;
using Akka.Streams.Stage;
using Akka.Util;
using Akka.Util.Internal.Collections;
namespace Akka.Streams.Dsl
{
/// <summary>
/// TBD
/// </summary>
public static class Framing
{
/// <summary>
/// Creates a Flow that handles decoding a stream of unstructured byte chunks into a stream of frames where the
/// incoming chunk stream uses a specific byte-sequence to mark frame boundaries.
///
/// The decoded frames will not include the separator sequence.
///
/// If there are buffered bytes (an incomplete frame) when the input stream finishes and <paramref name="allowTruncation"/> is set to
/// false then this Flow will fail the stream reporting a truncated frame.
/// </summary>
/// <param name="delimiter">The byte sequence to be treated as the end of the frame.</param>
/// <param name="maximumFrameLength">The maximum length of allowed frames while decoding. If the maximum length is exceeded this Flow will fail the stream.</param>
/// <param name="allowTruncation">If false, then when the last frame being decoded contains no valid delimiter this Flow fails the stream instead of returning a truncated frame.</param>
/// <returns>TBD</returns>
public static Flow<ByteString, ByteString, NotUsed> Delimiter(ByteString delimiter, int maximumFrameLength,
bool allowTruncation = false)
{
return Flow.Create<ByteString>()
.Via(new DelimiterFramingStage(delimiter, maximumFrameLength, allowTruncation))
.Named("DelimiterFraming");
}
/// <summary>
/// Creates a Flow that decodes an incoming stream of unstructured byte chunks into a stream of frames, assuming that
/// incoming frames have a field that encodes their length.
///
/// If the input stream finishes before the last frame has been fully decoded, this Flow will fail the stream reporting
/// a truncated frame.
/// </summary>
/// <param name="fieldLength">The length of the "Count" field in bytes</param>
/// <param name="maximumFramelength">The maximum length of allowed frames while decoding. If the maximum length is exceeded this Flow will fail the stream. This length *includes* the header (i.e the offset and the length of the size field)</param>
/// <param name="fieldOffset">The offset of the field from the beginning of the frame in bytes</param>
/// <param name="byteOrder">The <see cref="ByteOrder"/> to be used when decoding the field</param>
/// <exception cref="ArgumentException">
/// This exception is thrown when the specified <paramref name="fieldLength"/> is not equal to either 1, 2, 3 or 4.
/// </exception>
/// <returns>TBD</returns>
public static Flow<ByteString, ByteString, NotUsed> LengthField(int fieldLength, int maximumFramelength,
int fieldOffset = 0, ByteOrder byteOrder = ByteOrder.LittleEndian)
{
if (fieldLength is < 1 or > 4)
throw new ArgumentException("Length field length must be 1,2,3 or 4", nameof(fieldLength));
return Flow.Create<ByteString>()
.Via(new LengthFieldFramingStage(fieldLength, maximumFramelength, fieldOffset, byteOrder))
.Named("LengthFieldFraming");
}
/// <summary>
/// Creates a Flow that decodes an incoming stream of unstructured byte chunks into a stream of frames, assuming that
/// incoming frames have a field that encodes their length.
/// <para>
/// If the input stream finishes before the last frame has been fully decoded, this Flow will fail the stream reporting
/// a truncated frame.
/// </para>
/// </summary>
/// <param name="fieldLength">The length of the "Count" field in bytes</param>
/// <param name="maximumFrameLength">The maximum length of allowed frames while decoding. If the maximum length is exceeded this Flow will fail the stream. This length *includes* the header (i.e the offset and the length of the size field)</param>
/// <param name="fieldOffset">The offset of the field from the beginning of the frame in bytes.</param>
/// <param name="byteOrder">The <see cref="ByteOrder"/> to be used when decoding the field.</param>
/// <param name="computeFrameSize">
/// This function can be supplied if frame size is varied or needs to be computed in a special fashion.
/// For example, frame can have a shape like this: `[offset bytes][body size bytes][body bytes][footer bytes]`.
/// Then computeFrameSize can be used to compute the frame size: `(offset bytes, computed size) => (actual frame size)`.
/// "Actual frame size" must be equal or bigger than sum of `fieldOffset` and `fieldLength`, the operator fails otherwise.
/// </param>
/// <exception cref="ArgumentException">
/// This exception is thrown when the specified <paramref name="fieldLength"/> is not equal to either 1, 2, 3 or 4.
/// </exception>
/// <returns>TBD</returns>
public static Flow<ByteString, ByteString, NotUsed> LengthField(
int fieldLength,
int fieldOffset,
int maximumFrameLength,
ByteOrder byteOrder,
Func<IReadOnlyList<byte>, int, int> computeFrameSize)
{
if (fieldLength is < 1 or > 4)
throw new ArgumentException("Length field length must be 1,2,3 or 4", nameof(fieldLength));
return Flow.Create<ByteString>()
.Via(new LengthFieldFramingStage(fieldLength, maximumFrameLength, fieldOffset, byteOrder, computeFrameSize))
.Named("LengthFieldFraming");
}
/// <summary>
/// Returns a BidiFlow that implements a simple framing protocol. This is a convenience wrapper over <see cref="LengthField(int, int, int, ByteOrder)"/>
/// and simply attaches a length field header of four bytes (using big endian encoding) to outgoing messages, and decodes
/// such messages in the inbound direction. The decoded messages do not contain the header.
///
/// This BidiFlow is useful if a simple message framing protocol is needed (for example when TCP is used to send
/// individual messages) but no compatibility with existing protocols is necessary.
///
/// The encoded frames have the layout
/// {{{
/// [4 bytes length field, Big Endian][User Payload]
/// }}}
/// The length field encodes the length of the user payload excluding the header itself.
/// </summary>
/// <param name="maximumMessageLength">Maximum length of allowed messages. If sent or received messages exceed the configured limit this BidiFlow will fail the stream. The header attached by this BidiFlow are not included in this limit.</param>
/// <returns>TBD</returns>
public static BidiFlow<ByteString, ByteString, ByteString, ByteString, NotUsed> SimpleFramingProtocol(int maximumMessageLength)
{
return BidiFlow.FromFlowsMat(SimpleFramingProtocolEncoder(maximumMessageLength),
SimpleFramingProtocolDecoder(maximumMessageLength), Keep.Left);
}
/// <summary>
/// Protocol decoder that is used by <see cref="SimpleFramingProtocol"/>
/// </summary>
/// <param name="maximumMessageLength">TBD</param>
/// <returns>TBD</returns>
public static Flow<ByteString, ByteString, NotUsed> SimpleFramingProtocolDecoder(int maximumMessageLength)
{
return LengthField(4, maximumMessageLength + 4, 0, ByteOrder.BigEndian).Select(b => b.Slice(4));
}
/// <summary>
/// Protocol encoder that is used by <see cref="SimpleFramingProtocol"/>
/// </summary>
/// <param name="maximumMessageLength">TBD</param>
/// <returns>TBD</returns>
public static Flow<ByteString, ByteString, NotUsed> SimpleFramingProtocolEncoder(int maximumMessageLength)
{
return Flow.Create<ByteString>().Via(new SimpleFramingProtocolEncoderStage(maximumMessageLength));
}
/// <summary>
/// TBD
/// </summary>
public class FramingException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="FramingException" /> class.
/// </summary>
/// <param name="message">The message that describes the error. </param>
public FramingException(string message) : base(message)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FramingException"/> class.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext" /> that contains contextual information about the source or destination.</param>
protected FramingException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
private static int BigEndianDecoder(ref ReadOnlySpan<byte> enumerator, int length)
{
if (length > enumerator.Length)
{
throw new ArgumentException("Length exceeds the size of the enumerator.");
}
var result = 0;
// Assuming 'length' is 4 for a 32-bit integer.
// Adjust the loop and shift amounts if dealing with different sizes.
for (var i = 0; i < length; i++)
{
result |= enumerator[i] << ((length - 1 - i) * 8);
}
return result;
}
private static int LittleEndianDecoder(ref ReadOnlySpan<byte> span, int length)
{
if (length > span.Length)
{
throw new IndexOutOfRangeException("LittleEndianDecoder reached end of byte span");
}
var decoded = 0;
var highestOctetShift = (length - 1) << 3;
var mask = (int)(1L << (length << 3)) - 1;
for (var i = 0; i < length; i++)
{
// Shift and add the ith byte to 'decoded'. No need for >>>= as in JVM; just shift appropriately.
var shiftAmount = highestOctetShift - (i << 3);
decoded |= (span[i] & 0xFF) << shiftAmount;
}
return decoded & mask;
}
private sealed class SimpleFramingProtocolEncoderStage : SimpleLinearGraphStage<ByteString>
{
#region Logic
private sealed class Logic : InAndOutGraphStageLogic
{
private readonly SimpleFramingProtocolEncoderStage _stage;
public Logic(SimpleFramingProtocolEncoderStage stage) : base(stage.Shape)
{
_stage = stage;
SetHandlers(stage.Inlet, stage.Outlet, this);
}
public override void OnPush()
{
var message = Grab(_stage.Inlet);
var messageSize = message.Count;
if (messageSize > _stage._maximumMessageLength)
FailStage(new FramingException(
$"Maximum allowed message size is {_stage._maximumMessageLength} but tried to send {messageSize} bytes"));
else
{
var header = ByteString.CopyFrom(new[]
{
Convert.ToByte((messageSize >> 24) & 0xFF),
Convert.ToByte((messageSize >> 16) & 0xFF),
Convert.ToByte((messageSize >> 8) & 0xFF),
Convert.ToByte(messageSize & 0xFF)
});
Push(_stage.Outlet, header + message);
}
}
public override void OnPull() => Pull(_stage.Inlet);
}
#endregion
private readonly long _maximumMessageLength;
public SimpleFramingProtocolEncoderStage(long maximumMessageLength) : base("SimpleFramingProtocolEncoder")
{
_maximumMessageLength = maximumMessageLength;
}
protected override GraphStageLogic CreateLogic(Attributes inheritedAttributes) => new Logic(this);
}
private sealed class DelimiterFramingStage : SimpleLinearGraphStage<ByteString>
{
#region Logic
private sealed class Logic : InAndOutGraphStageLogic
{
private readonly DelimiterFramingStage _stage;
private readonly byte _firstSeparatorByte;
private ByteString _buffer = ByteString.Empty;
private int _nextPossibleMatch;
public Logic(DelimiterFramingStage stage) : base (stage.Shape)
{
_stage = stage;
_firstSeparatorByte = stage._separatorBytes[0];
SetHandlers(stage.Inlet, stage.Outlet, this);
}
public override void OnPush()
{
_buffer += Grab(_stage.Inlet);
DoParse();
}
public override void OnUpstreamFinish()
{
if (_buffer.IsEmpty)
CompleteStage();
else if (IsAvailable(_stage.Outlet))
DoParse();
// else swallow the termination and wait for pull
}
public override void OnPull() => DoParse();
private void TryPull()
{
if (IsClosed(_stage.Inlet))
{
if (_stage._allowTruncation)
{
Push(_stage.Outlet, _buffer);
CompleteStage();
}
else
FailStage(
new FramingException(
"Stream finished but there was a truncated final frame in the buffer"));
}
else
Pull(_stage.Inlet);
}
private void DoParse()
{
while (true)
{
var possibleMatchPosition = _buffer.IndexOf(_firstSeparatorByte, from: _nextPossibleMatch);
if (possibleMatchPosition > _stage._maximumLineBytes)
{
FailStage(new FramingException($"Read {_buffer.Count} bytes which is more than {_stage._maximumLineBytes} without seeing a line terminator"));
}
else if (possibleMatchPosition == -1)
{
if (_buffer.Count > _stage._maximumLineBytes)
FailStage(new FramingException($"Read {_buffer.Count} bytes which is more than {_stage._maximumLineBytes} without seeing a line terminator"));
else
{
// No matching character, we need to accumulate more bytes into the buffer
_nextPossibleMatch = _buffer.Count;
TryPull();
}
}
else if (possibleMatchPosition + _stage._separatorBytes.Count > _buffer.Count)
{
// We have found a possible match (we found the first character of the terminator
// sequence) but we don't have yet enough bytes. We remember the position to
// retry from next time.
_nextPossibleMatch = possibleMatchPosition;
TryPull();
}
else if (_buffer.HasSubstring(_stage._separatorBytes, possibleMatchPosition))
{
// Found a match
var parsedFrame = _buffer.Slice(0, possibleMatchPosition);
_buffer = _buffer.Slice(possibleMatchPosition + _stage._separatorBytes.Count);
_nextPossibleMatch = 0;
Push(_stage.Outlet, parsedFrame);
if (IsClosed(_stage.Inlet) && _buffer.IsEmpty)
CompleteStage();
}
else
{
// possibleMatchPos was not actually a match
_nextPossibleMatch++;
continue;
}
break;
}
}
}
#endregion
private readonly ByteString _separatorBytes;
private readonly int _maximumLineBytes;
private readonly bool _allowTruncation;
public DelimiterFramingStage(ByteString separatorBytes, int maximumLineBytes, bool allowTruncation) : base("DelimiterFraming")
{
_separatorBytes = separatorBytes;
_maximumLineBytes = maximumLineBytes;
_allowTruncation = allowTruncation;
}
protected override Attributes InitialAttributes { get; } = DefaultAttributes.DelimiterFraming;
protected override GraphStageLogic CreateLogic(Attributes inheritedAttributes) => new Logic(this);
public override string ToString() => "DelimiterFraming";
}
private sealed class LengthFieldFramingStage : SimpleLinearGraphStage<ByteString>
{
#region Logic
private sealed class Logic : InAndOutGraphStageLogic
{
private readonly LengthFieldFramingStage _stage;
private ByteString _buffer = ByteString.Empty;
private int _frameSize = int.MaxValue;
public Logic(LengthFieldFramingStage stage) : base(stage.Shape)
{
_stage = stage;
SetHandlers(stage.Inlet, stage.Outlet, this);
}
public override void OnPush()
{
_buffer += Grab(_stage.Inlet);
TryPushFrame();
}
public override void OnPull() => TryPushFrame();
public override void OnUpstreamFinish()
{
if (_buffer.IsEmpty)
CompleteStage();
else if (IsAvailable(_stage.Outlet))
TryPushFrame();
// else swallow the termination and wait for pull
}
/// <summary>
/// push, and reset frameSize and buffer
/// </summary>
private void PushFrame()
{
var emit = _buffer.Slice(0, _frameSize);
_buffer = _buffer.Slice(_frameSize);
_frameSize = int.MaxValue;
Push(_stage.Outlet, emit);
if (_buffer.IsEmpty && IsClosed(_stage.Inlet))
CompleteStage();
}
/// <summary>
/// try to push downstream, if failed then try to pull upstream
/// </summary>
private void TryPushFrame()
{
var bufferSize = _buffer.Count;
if (bufferSize >= _frameSize)
PushFrame();
else if (bufferSize >= _stage._minimumChunkSize)
{
var iterator = _buffer.Memory.Span.Slice(_stage._lengthFieldOffset);
var parsedLength = _stage._byteOrder switch {
ByteOrder.BigEndian => BigEndianDecoder(ref iterator, _stage._lengthFieldLength),
ByteOrder.LittleEndian => LittleEndianDecoder(ref iterator, _stage._lengthFieldLength),
_ => throw new NotSupportedException($"ByteOrder {_stage._byteOrder} is not supported")
};
// TODO: AVOID ARRAY COPYING AGAIN HERE
_frameSize = _stage._computeFrameSize.HasValue
? _stage._computeFrameSize.Value(_buffer.Slice(0, _stage._lengthFieldOffset).ToArray(), parsedLength)
: parsedLength + _stage._minimumChunkSize;
if (_frameSize > _stage._maximumFramelength)
FailStage(new FramingException(
$"Maximum allowed frame size is {_stage._maximumFramelength} but decoded frame header reported size {_frameSize}"));
else if (_stage._computeFrameSize.IsEmpty && parsedLength < 0)
FailStage(new FramingException(
$"Decoded frame header reported negative size {parsedLength}"));
else if (_frameSize < _stage._minimumChunkSize)
FailStage(new FramingException(
$"Computed frame size {_frameSize} is less than minimum chunk size {_stage._minimumChunkSize}"));
else if (bufferSize >= _frameSize)
PushFrame();
else
TryPull();
}
else
TryPull();
}
private void TryPull()
{
if (IsClosed(_stage.Inlet))
FailStage(new FramingException("Stream finished but there was a truncated final frame in the buffer"));
else
Pull(_stage.Inlet);
}
}
#endregion
private readonly int _lengthFieldLength;
private readonly int _maximumFramelength;
private readonly int _lengthFieldOffset;
private readonly int _minimumChunkSize;
private readonly ByteOrder _byteOrder;
private readonly Option<Func<IReadOnlyList<byte>, int, int>> _computeFrameSize;
// For the sake of binary compatibility
public LengthFieldFramingStage(int lengthFieldLength, int maximumFramelength, int lengthFieldOffset, ByteOrder byteOrder)
: this(lengthFieldLength, maximumFramelength, lengthFieldOffset, byteOrder, Option<Func<IReadOnlyList<byte>, int, int>>.None)
{ }
public LengthFieldFramingStage(
int lengthFieldLength,
int maximumFrameLength,
int lengthFieldOffset,
ByteOrder byteOrder,
Option<Func<IReadOnlyList<byte>, int, int>> computeFrameSize) : base("LengthFieldFramingStage")
{
_lengthFieldLength = lengthFieldLength;
_maximumFramelength = maximumFrameLength;
_lengthFieldOffset = lengthFieldOffset;
_minimumChunkSize = lengthFieldOffset + lengthFieldLength;
_computeFrameSize = computeFrameSize;
_byteOrder = byteOrder;
}
protected override GraphStageLogic CreateLogic(Attributes inheritedAttributes) => new Logic(this);
}
}
}