afd504d5ae
A fresh StreamEvents is never replayed, so an initial value published before
the gateway registers the stream's subscriber is lost (lmxopcua#865 —
slow-changing ScanState). Clients had nothing deterministic to wait on.
Contract (additive, proto3):
- StreamEventsRequest.emit_stream_attached = 3 (bool, default false)
- MxEvent.stream_attached = 15 (optional StreamAttached)
- message StreamAttached { uint64 live_after_sequence = 1; }
- OpenSessionReply capability "stream-events-attached-ack"
(GatewayCapabilities.StreamEventsAttachedAck)
When requested, EventStreamService yields the sentinel as the FIRST message,
after AttachEventSubscriber/AttachEventSubscriberWithReplay has returned, so
receipt implies registration; refused attaches never see it. It precedes the
ReplayGap sentinel and replay on a resume. Shape mirrors ReplayGap: family
UNSPECIFIED, no body, worker_sequence 0.
Opt-in rather than always-on: ScadaBridge's MxGateway adapter stores every
streamed event's worker_sequence as its resume cursor, so an unrequested
sentinel would reset it to 0. Default wire behaviour is unchanged.
.NET client: StreamEventsAsync/StreamEventItemsAsync(after, emitStreamAttached),
MxEventStreamItem.IsStreamAttached, MxGatewaySession.SupportsStreamAttachedAck,
and OpenEventStreamAsync -> MxGatewayEventStream (awaits the sentinel, with
attach timeout; refuses older gateways).
Regenerated Contracts/Generated, the client protoset, Go and Python bindings
(also picks up comment-only drift already on main), synced the Rust vendored
proto and set emit_stream_attached in the Rust StreamEventsRequest literals.
No package versions bumped.
Tests: ordering (sentinel first, then events), race proof (event fanned right
after the sentinel is delivered — mutation-checked: fails when the sentinel is
yielded before registration), resume-with-gap order, refused attach emits no
sentinel, default emits none, contract field numbers, capability, and .NET
client wire tests (await, no-capability, older gateway, timeout).
104 lines
4.7 KiB
C#
104 lines
4.7 KiB
C#
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
|
|
|
namespace ZB.MOM.WW.MxGateway.Client;
|
|
|
|
/// <summary>
|
|
/// One item yielded by the typed event stream. It is either a normal MXAccess
|
|
/// <see cref="MxEvent"/>, a reconnect-replay <em>gap sentinel</em>, or — only on a stream
|
|
/// that asked for it — the <em>stream-attached sentinel</em> (#227).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The gateway emits a single sentinel <see cref="MxEvent"/> at the head of a
|
|
/// <c>StreamEvents</c> stream that was resumed via
|
|
/// <c>StreamEventsRequest.after_worker_sequence</c> when the requested sequence
|
|
/// predates the oldest event still retained in the session replay ring — i.e.
|
|
/// events were evicted and cannot be replayed. On that sentinel the
|
|
/// <c>MxEvent.replay_gap</c> field is set, <see cref="MxEvent.Family"/> is
|
|
/// <see cref="MxEventFamily.Unspecified"/>, the <c>body</c> oneof is unset, and
|
|
/// no per-item fields are populated.
|
|
/// </para>
|
|
/// <para>
|
|
/// This wrapper makes that sentinel observable instead of forcing the consumer
|
|
/// to inspect the raw <see cref="MxEvent"/>. It never synthesizes or swallows an
|
|
/// event: the gap is exactly the gateway's own sentinel, exposed with
|
|
/// <see cref="IsReplayGap"/> set and <see cref="ReplayGap"/> populated. Every
|
|
/// other event flows through unchanged with <see cref="IsReplayGap"/> false.
|
|
/// </para>
|
|
/// <para>
|
|
/// When <see cref="IsReplayGap"/> is <see langword="true"/> the consumer has
|
|
/// missed events and MUST discard local state and re-snapshot. To resume without
|
|
/// incurring another gap, reconnect with
|
|
/// <c>after_worker_sequence = ReplayGap.OldestAvailableSequence - 1</c>.
|
|
/// </para>
|
|
/// <para>
|
|
/// When the stream was opened with <c>emit_stream_attached</c>, its first item is the
|
|
/// stream-attached sentinel: <see cref="IsStreamAttached"/> is <see langword="true"/> and
|
|
/// <see cref="StreamAttached"/> is populated. It means the gateway has registered the stream,
|
|
/// so every event published from then on is delivered. Its <c>worker_sequence</c> is 0 and
|
|
/// must not be used as a resume cursor.
|
|
/// </para>
|
|
/// </remarks>
|
|
public sealed class MxEventStreamItem
|
|
{
|
|
private MxEventStreamItem(MxEvent gatewayEvent, ReplayGap? replayGap, StreamAttached? streamAttached)
|
|
{
|
|
Event = gatewayEvent;
|
|
ReplayGap = replayGap;
|
|
StreamAttached = streamAttached;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The underlying raw <see cref="MxEvent"/>. For a normal event this is the
|
|
/// MXAccess event itself; for a replay-gap item this is the gateway's
|
|
/// sentinel event whose only meaningful payload is <see cref="ReplayGap"/>.
|
|
/// Never <see langword="null"/>.
|
|
/// </summary>
|
|
public MxEvent Event { get; }
|
|
|
|
/// <summary>
|
|
/// The reconnect-replay gap payload when this item is a gap sentinel;
|
|
/// otherwise <see langword="null"/>. Read
|
|
/// <see cref="Contracts.Proto.ReplayGap.RequestedAfterSequence"/> and
|
|
/// <see cref="Contracts.Proto.ReplayGap.OldestAvailableSequence"/> to learn
|
|
/// which events were lost.
|
|
/// </summary>
|
|
public ReplayGap? ReplayGap { get; }
|
|
|
|
/// <summary>
|
|
/// <see langword="true"/> when this item is a reconnect-replay gap sentinel:
|
|
/// the consumer missed events and must discard local state and re-snapshot.
|
|
/// Resume without another gap by reconnecting with
|
|
/// <c>after_worker_sequence = ReplayGap.OldestAvailableSequence - 1</c>.
|
|
/// </summary>
|
|
public bool IsReplayGap => ReplayGap is not null;
|
|
|
|
/// <summary>
|
|
/// The attach acknowledgement when this item is the stream-attached sentinel (#227);
|
|
/// otherwise <see langword="null"/>. Only a stream opened with
|
|
/// <c>emit_stream_attached</c> carries it, and only as its first item.
|
|
/// </summary>
|
|
public StreamAttached? StreamAttached { get; }
|
|
|
|
/// <summary>
|
|
/// <see langword="true"/> when this item is the stream-attached sentinel: the gateway has
|
|
/// registered the stream, so an advise issued after this point cannot lose its initial value
|
|
/// to the attach race.
|
|
/// </summary>
|
|
public bool IsStreamAttached => StreamAttached is not null;
|
|
|
|
/// <summary>
|
|
/// Wraps a raw stream <see cref="MxEvent"/> as a typed item, classifying it
|
|
/// as a replay-gap sentinel when <c>MxEvent.replay_gap</c> is present.
|
|
/// </summary>
|
|
/// <param name="gatewayEvent">The raw event from the <c>StreamEvents</c> stream.</param>
|
|
/// <returns>A typed item exposing either the normal event or the replay gap.</returns>
|
|
internal static MxEventStreamItem From(MxEvent gatewayEvent)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(gatewayEvent);
|
|
|
|
// For a message-typed proto3 field, presence is a non-null reference.
|
|
return new MxEventStreamItem(gatewayEvent, gatewayEvent.ReplayGap, gatewayEvent.StreamAttached);
|
|
}
|
|
}
|