Add Serials

This commit is contained in:
Dan Clark
2025-02-07 09:03:02 +00:00
parent 63f448f490
commit a08f86eb74
19 changed files with 270 additions and 1 deletions
+46
View File
@@ -0,0 +1,46 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Text;
namespace Toolkit.Foundation;
public class SerialLineReader(Stream stream) :
SerialReader<string>(stream)
{
private readonly PipeReader reader = PipeReader.Create(stream);
public override async IAsyncEnumerable<string> ReadAsync()
{
while (true)
{
ReadResult result = await reader.ReadAsync();
ReadOnlySequence<byte> buffer = result.Buffer;
while (TryReadLine(ref buffer, out ReadOnlySequence<byte> line))
{
yield return EncodingExtensions.GetString(Encoding.UTF8, line);
}
reader.AdvanceTo(buffer.Start, buffer.End);
if (result.IsCompleted)
{
break;
}
}
}
private bool TryReadLine(ref ReadOnlySequence<byte> buffer, out ReadOnlySequence<byte> line)
{
SequencePosition? position = buffer.PositionOf((byte)'\n');
if (position == null)
{
line = default;
return false;
}
line = buffer.Slice(0, position.Value);
buffer = buffer.Slice(buffer.GetPosition(1, position.Value));
return true;
}
}