Files
CydandClaude Fable 5 3b2af7b79a Phase 8A (1/2): de-Span the serial layer, swap JSON to Newtonsoft
Prepares RioJoy.Core for the net40 (Windows XP) target, which has no
System.Memory, ValueTask, or System.Text.Json:
- IRioTransport and the whole protocol/framing layer now use byte[] +
  Task (RioPacket.Payload, PacketParser/Builder, RioChecksum, replies,
  AnalogReport, RioHidReport). At 9600 baud Span bought nothing; the
  SerialPortTransport bridge copies disappear entirely.
- ConfigStore/OverlayTemplateStore switch to Newtonsoft 13 with the
  same conventions (indented, PascalCase, string enums, null-skipping);
  verified against the real STJ-written config.json and regions.json
  (load + round-trip). System.Memory and System.Text.Json packages
  dropped.

275 tests green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 20:36:19 -05:00

55 lines
2.0 KiB
C#

using System.Threading.Channels;
using RioJoy.Core.Serial;
namespace RioJoy.Core.Tests.Serial;
/// <summary>
/// In-memory <see cref="IRioTransport"/> for driving <see cref="RioSerialLink"/>
/// in tests: enqueue inbound chunks, inspect outbound writes.
/// </summary>
internal sealed class FakeTransport : IRioTransport
{
private readonly Channel<byte[]> _incoming = Channel.CreateUnbounded<byte[]>();
private readonly Channel<byte[]> _writes = Channel.CreateUnbounded<byte[]>();
public string Description => "fake";
/// <summary>Outbound writes, in order. Each call to WriteAsync yields one item.</summary>
public ChannelReader<byte[]> Writes => _writes.Reader;
/// <summary>Queue an inbound chunk for the receive loop to read.</summary>
public void Enqueue(params byte[] data) => _incoming.Writer.TryWrite(data);
/// <summary>Signal that no more inbound data will arrive (transport closed).</summary>
public void CompleteIncoming() => _incoming.Writer.TryComplete();
public async Task<int> ReadAsync(byte[] buffer, CancellationToken cancellationToken)
{
while (await _incoming.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false))
{
if (_incoming.Reader.TryRead(out byte[]? chunk))
{
Array.Copy(chunk, buffer, chunk.Length);
return chunk.Length;
}
}
return 0; // completed
}
public Task WriteAsync(byte[] data, CancellationToken cancellationToken)
{
_writes.Writer.TryWrite((byte[])data.Clone());
return Task.CompletedTask;
}
/// <summary>Read the next outbound write, failing if none arrives in time.</summary>
public async Task<byte[]> NextWriteAsync(TimeSpan? timeout = null)
{
using var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(5));
return await _writes.Reader.ReadAsync(cts.Token).ConfigureAwait(false);
}
public void Dispose() { }
}