|
while (!cancellation.IsCancellationRequested) |
|
{ |
|
var read = await pipereader.ReadAsync(cancellation); |
|
var buffer = read.Buffer; |
|
if (buffer.IsEmpty && read.IsCompleted) { break; } |
|
var position = buffer.Start; |
|
|
|
//Due to the nature of Pipelines as simple binary pipes, all Transport adapters assemble a standard message frame whether or not the underlying transport signals length, EoM, etc. |
|
var (Length, IsEndOfMessage) = MessageFramePeek(buffer); |
|
if (buffer.Length < Length + MESSAGEFRAMESIZE) { pipereader.AdvanceTo(buffer.Start, buffer.End); continue; } //Don't have a complete message yet. Tell the pipe that we've evaluated up to the current buffer end, but cannot yet consume it. |
|
|
|
await Process(Length, buffer.Slice(position = buffer.GetPosition(MESSAGEFRAMESIZE, position), Length)); |
|
pipereader.AdvanceTo(position = buffer.GetPosition(Length, position)); |
|
//TODO UNIT TEST- this should work now too!!! Need to evaluate if there is more than one packet in the pipe including edges like part of the length bytes are there but not all. |
|
} |
Doesn't properly handle the exit condition and could result in an infinite loop if partial data is sent and the read is completed.
See https://github.com/davidfowl/DocsStaging/blob/master/Pipelines.md#code-samples for more information.