b36c8d8716
GrpcDataConnection implements IDataHistoryService (explicitly; ReadHistoryAsync/ReleaseHistoryAsync avoid an overload clash with ReadAsync): the capabilities probe always asks for history, other calls send it only when the selected endpoint advertised it; per-call deadline min(remaining, HistoryTimeoutMilliseconds); FAILED_PRECONDITION/UNIMPLEMENTED => BadHistoryOperationUnsupported; chunks assembled within the request budget (BadTooManyOperations past it); page tokens wrapped with the issuing endpoint + serving incarnation so a resumed read never moves; release forwarded. The deployment lease gates history on the row's HistoryRead bit and carries HistoryTimeout. Decoded capabilities drop HistoryRead without the history field. X0 tests that used bit 32 as the unknown bit now use a still-reserved bit.
64 lines
4.7 KiB
C#
64 lines
4.7 KiB
C#
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
/// <summary>Strictly caller-bound local data facade; it creates no namespace projection.</summary>
|
|
public sealed class LocalDataConnection : IDataConnection, IDataConnectionPublicationGate, IDataConnectionDiscoveryGate,
|
|
IDataHistoryService, IDataHistoryTransport
|
|
{
|
|
private readonly LocalDataServices _services;
|
|
private readonly DataAuthenticatedContext _context;
|
|
private int _disposed;
|
|
internal LocalDataConnection(LocalDataServices services, DataAuthenticatedContext context, DataTarget target)
|
|
{ _services = services; _context = context; Target = target; }
|
|
public DataTarget Target { get; }
|
|
public TimeProvider Clock => _services.Clock;
|
|
public bool CanPublish(ServingIdentity expectedServing) => Volatile.Read(ref _disposed) == 0 && _services.CanPublish(_context, expectedServing, Target);
|
|
public bool CanPublishCapabilities(DataCapabilities expected) => Volatile.Read(ref _disposed) == 0
|
|
&& _services.CanPublishCapabilities(_context, Target, expected);
|
|
private void Check() => ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
|
public Task<DataCapabilities> GetCapabilitiesAsync(DataBudget budget, CancellationToken cancellationToken)
|
|
{ Check(); return _services.GetCapabilitiesAsync(_context, Target, budget, cancellationToken); }
|
|
public Task<DataBrowseBranch> BrowseAsync(BrowseRequest request, CancellationToken cancellationToken)
|
|
{ Check(); _services.RequireTargetRole(Target); return _services.BrowseAsync(_context, Target, request, cancellationToken); }
|
|
public Task<IReadOnlyList<DataItemResult<DataMetadata>>> GetMetadataAsync(MetadataRequest request, CancellationToken cancellationToken)
|
|
{ Check(); _services.RequireTargetRole(Target); return _services.GetMetadataAsync(_context, Target, request, cancellationToken); }
|
|
public Task<IReadOnlyList<ReadResult>> ReadAsync(ReadRequest request, CancellationToken cancellationToken)
|
|
{ Check(); _services.RequireTargetRole(Target); return _services.ReadAsync(_context, Target, request, cancellationToken); }
|
|
public Task<IReadOnlyList<WriteItemResult>> WriteAsync(WriteBatch request, CancellationToken cancellationToken)
|
|
{ Check(); return _services.WriteAsync(_context, request, cancellationToken); }
|
|
public Task<IDataSubscription> SubscribeAsync(CancellationToken cancellationToken)
|
|
{ Check(); return _services.SubscribeAsync(_context, Target, cancellationToken); }
|
|
/// <summary>
|
|
/// Direct runtime-consumer history (H2): this facade is the channel, so its origin continuations live only as long as
|
|
/// the facade and are cleared on <see cref="DisposeAsync"/>. Answers are never chunked.
|
|
/// </summary>
|
|
public Task<IReadOnlyList<DataHistoryReadResult>> ReadHistoryAsync(DataHistoryReadRequest request, CancellationToken cancellationToken)
|
|
{ Check(); return _services.ReadHistoryAsync(_context, Target, request, OwnChannel, chunked: false, cancellationToken); }
|
|
public Task ReleaseHistoryAsync(IReadOnlyList<byte[]> continuations, CancellationToken cancellationToken)
|
|
{ Check(); return _services.ReleaseHistoryAsync(_context, continuations, OwnChannel, cancellationToken); }
|
|
Task<IReadOnlyList<DataHistoryReadResult>> IDataHistoryService.ReadAsync(DataHistoryReadRequest request, CancellationToken cancellationToken) =>
|
|
ReadHistoryAsync(request, cancellationToken);
|
|
Task IDataHistoryService.ReleaseAsync(IReadOnlyList<byte[]> continuations, CancellationToken cancellationToken) =>
|
|
ReleaseHistoryAsync(continuations, cancellationToken);
|
|
/// <inheritdoc />
|
|
public Task<IReadOnlyList<DataHistoryReadResult>> ReadForTransportAsync(DataHistoryReadRequest request, DataHistoryChannel channel,
|
|
CancellationToken cancellationToken)
|
|
{ Check(); return _services.ReadHistoryAsync(_context, Target, request, channel, chunked: true, cancellationToken); }
|
|
/// <inheritdoc />
|
|
public Task ReleaseForTransportAsync(IReadOnlyList<byte[]> continuations, DataHistoryChannel channel, CancellationToken cancellationToken)
|
|
{ Check(); return _services.ReleaseHistoryAsync(_context, continuations, channel, cancellationToken); }
|
|
private readonly CancellationTokenSource _closed = new();
|
|
private DataHistoryChannel? _channel;
|
|
private DataHistoryChannel OwnChannel => _channel ??= new("facade:" + Guid.NewGuid().ToString("N"), _closed.Token);
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) == 0)
|
|
{
|
|
try { _closed.Cancel(); } catch (AggregateException) { }
|
|
_closed.Dispose();
|
|
}
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|