1764eff1cf
Worker-009: WorkerFrameWriter serialized twice and WorkerFrameReader allocated a payload byte[] per frame. The writer now serializes once into a single prefix+payload buffer; the reader rents the payload buffer from ArrayPool and honors the logical frame length. Worker-010: VariantConverter projected a uint+Time value as a full FILETIME, producing a near-1601 timestamp. The FILETIME projection is now gated on `value is long`; uint falls through to the integer projection. Worker-011: replaced the opaque retryAttempts formula in WorkerPipeClient with MaxRetryAttempts = int.MaxValue, leaving the connect deadline as the sole bound. Worker-012: rewrote stale "future PR / polls on a Timer" comments in AlarmDispatcher, AlarmCommandHandler, MxAccessAlarmEventSink and MxAccessEventMapper to match the shipped, post-Worker-001 behavior. Worker-013 (re-triaged): already resolved — StaMessagePumpTests and MxAccessStaSessionTests cover the pump and poll loop directly. Worker-014: moved IAlarmCommandHandler into its own file so AlarmCommandHandler.cs declares one public type. Worker-015: clarified the MxAccessBaseEventSink.EnqueueEvent overflow-catch comment explaining the deliberate double RecordFault no-op. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Google.Protobuf;
|
|
using MxGateway.Contracts.Proto;
|
|
|
|
namespace MxGateway.Worker.Ipc;
|
|
|
|
/// <summary>Reads length-prefixed WorkerEnvelope protobuf frames from a stream.</summary>
|
|
public sealed class WorkerFrameReader
|
|
{
|
|
private readonly WorkerFrameProtocolOptions _options;
|
|
private readonly Stream _stream;
|
|
|
|
/// <summary>Initializes the reader with a stream and protocol options.</summary>
|
|
/// <param name="stream">Stream to read frames from.</param>
|
|
/// <param name="options">Protocol options for frame validation.</param>
|
|
public WorkerFrameReader(
|
|
Stream stream,
|
|
WorkerFrameProtocolOptions options)
|
|
{
|
|
_stream = stream ?? throw new ArgumentNullException(nameof(stream));
|
|
_options = options ?? throw new ArgumentNullException(nameof(options));
|
|
}
|
|
|
|
/// <summary>Reads and validates a single length-prefixed frame from the stream.</summary>
|
|
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
|
|
public async Task<WorkerEnvelope> ReadAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
byte[] lengthPrefix = new byte[sizeof(uint)];
|
|
await ReadExactlyOrThrowAsync(lengthPrefix, lengthPrefix.Length, cancellationToken).ConfigureAwait(false);
|
|
|
|
uint payloadLength = ReadUInt32LittleEndian(lengthPrefix);
|
|
if (payloadLength == 0)
|
|
{
|
|
throw new WorkerFrameProtocolException(
|
|
WorkerFrameProtocolErrorCode.MalformedLength,
|
|
"Worker frame payload length must be greater than zero.");
|
|
}
|
|
|
|
if (payloadLength > _options.MaxMessageBytes)
|
|
{
|
|
throw new WorkerFrameProtocolException(
|
|
WorkerFrameProtocolErrorCode.MessageTooLarge,
|
|
$"Worker frame payload length {payloadLength} exceeds the configured maximum of {_options.MaxMessageBytes} bytes.");
|
|
}
|
|
|
|
// Rent the payload buffer from the shared pool rather than allocating
|
|
// a fresh byte[] per frame. ParseFrom copies whatever it needs into
|
|
// the parsed message, so the rented buffer can be returned as soon as
|
|
// parsing completes.
|
|
int length = checked((int)payloadLength);
|
|
byte[] payload = ArrayPool<byte>.Shared.Rent(length);
|
|
WorkerEnvelope envelope;
|
|
try
|
|
{
|
|
await ReadExactlyOrThrowAsync(payload, length, cancellationToken).ConfigureAwait(false);
|
|
|
|
try
|
|
{
|
|
envelope = WorkerEnvelope.Parser.ParseFrom(payload, 0, length);
|
|
}
|
|
catch (InvalidProtocolBufferException exception)
|
|
{
|
|
throw new WorkerFrameProtocolException(
|
|
WorkerFrameProtocolErrorCode.InvalidEnvelope,
|
|
"Worker frame payload is not a valid WorkerEnvelope protobuf message.",
|
|
exception);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ArrayPool<byte>.Shared.Return(payload);
|
|
}
|
|
|
|
WorkerEnvelopeValidator.Validate(envelope, _options);
|
|
|
|
return envelope;
|
|
}
|
|
|
|
private static uint ReadUInt32LittleEndian(byte[] buffer)
|
|
{
|
|
return (uint)buffer[0]
|
|
| ((uint)buffer[1] << 8)
|
|
| ((uint)buffer[2] << 16)
|
|
| ((uint)buffer[3] << 24);
|
|
}
|
|
|
|
private async Task ReadExactlyOrThrowAsync(
|
|
byte[] buffer,
|
|
int count,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
int offset = 0;
|
|
while (offset < count)
|
|
{
|
|
int bytesRead = await _stream
|
|
.ReadAsync(buffer, offset, count - offset, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
if (bytesRead == 0)
|
|
{
|
|
throw new WorkerFrameProtocolException(
|
|
WorkerFrameProtocolErrorCode.EndOfStream,
|
|
"Worker frame ended before the expected number of bytes were read.");
|
|
}
|
|
|
|
offset += bytesRead;
|
|
}
|
|
}
|
|
}
|