Example #1
0
        private static async ValueTask <TransformSource> ReadAsync(PipeReader reader,
                                                                   Func <TransformSource, bool> canTransform)
        {
            var source = new TransformSource()
            {
                Reader = reader,
                Memory = ReadOnlySequence <byte> .Empty,
            };

            try
            {
                while (true)
                {
                    var read = await reader.ReadAsync();

                    var buffer = read.Buffer;
                    source.IsCompleted = read.IsCompleted;

                    if (buffer.IsEmpty && read.IsCompleted)
                    {
                        return(source);
                    }

                    SequencePosition consumed;
                    //SequencePosition examined = buffer.End;
                    source.Memory = buffer;

                    if (canTransform(source))
                    {
                        // will need to be advanced and/or completed
                        return(source);
                    }
                    else
                    {
                        consumed = buffer.GetPosition(0);
                    }

                    reader.AdvanceTo(consumed);//, examined);
                }
            }
            catch (Exception err)
            {
                reader.Complete(err);
                return(source);
            }
        }
Example #2
0
        public static async ValueTask ContinuousReadAsync(PipeReader reader, PipeWriter writer,
                                                          Func <TransformSource, bool> canTransform,
                                                          Func <TransformSource, Task <TransformResult> > transform)
        {
            var source = new TransformSource(writer)
            {
                Reader = reader,
                Memory = ReadOnlySequence <byte> .Empty,
            };

            try
            {
                while (true)
                {
                    var read = await reader.ReadAsync();

                    var buffer = read.Buffer;
                    if (buffer.IsEmpty && read.IsCompleted)
                    {
                        break;
                    }

                    SequencePosition consumed;
                    //SequencePosition examined = buffer.End;
                    source.Memory = buffer;
                    if (canTransform(source))
                    {
                        var transformResult = await transform(source);

                        writer.Advance(transformResult.Result.Length);
                        //var writeResult = await writer.WriteAsync(transformResult.Result);
                        ////if (writeResult.IsCompleted)
                        ////{
                        ////    writer.Complete();
                        ////    break;
                        ////}

                        var flushResult = await writer.FlushAsync();

                        if (flushResult.IsCanceled)
                        {
                            break;
                        }

                        consumed = source.Consumed;// buffer.GetPosition(transformResult.Consumed);
                    }
                    else
                    {
                        consumed = buffer.GetPosition(0);
                    }

                    //foreach (var memoryChunk in buffer)
                    //{
                    //    // process memoryChunk.Span
                    //    // do we have enough data to process this data?
                    //}

                    reader.AdvanceTo(consumed);//, examined);
                }

                reader.Complete();
                writer.Complete();
            }
            catch (Exception err)
            {
                reader.Complete(err);
            }
        }