71 lines
3.5 KiB
C#
71 lines
3.5 KiB
C#
using Grpc.Core;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Protos;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
|
|
|
|
/// <summary>
|
|
/// The per-call feature set the server derived for one RPC: what the caller asked for, and what this server
|
|
/// will actually honour (requested ∩ supported). A handler serves a feature only when it is in
|
|
/// <see cref="Effective"/>; an empty effective set means exactly the v1 behaviour.
|
|
/// </summary>
|
|
public sealed record DataCallFeatures(DataFeatures Requested, DataFeatures Effective)
|
|
{
|
|
public static readonly DataCallFeatures None = new(DataFeatures.None, DataFeatures.None);
|
|
}
|
|
|
|
/// <summary>Formats and parses <see cref="DataConnectionProtoContract.FeaturesHeader"/>.</summary>
|
|
public static class DataFeatureHeader
|
|
{
|
|
public const string HistoryToken = "history";
|
|
public const string ConditionsToken = "conditions";
|
|
|
|
/// <summary>The header value for <paramref name="features"/>, or null when nothing is requested (no header is sent).</summary>
|
|
public static string? Format(DataFeatures features)
|
|
{
|
|
var tokens = new List<string>(2);
|
|
if ((features & DataFeatures.History) != 0) tokens.Add(HistoryToken);
|
|
if ((features & DataFeatures.Conditions) != 0) tokens.Add(ConditionsToken);
|
|
return tokens.Count == 0 ? null : string.Join(',', tokens);
|
|
}
|
|
|
|
/// <summary>Adds the header only for a non-empty feature set, so a v1-equivalent call stays byte-identical.</summary>
|
|
public static void Apply(Metadata headers, DataFeatures features)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(headers);
|
|
if (Format(features) is { } value) headers.Add(DataConnectionProtoContract.FeaturesHeader, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The union of every <see cref="DataConnectionProtoContract.FeaturesHeader"/> entry. Unknown, empty and
|
|
/// whitespace tokens are ignored (forward compatibility). A binary (<c>-bin</c>) entry or a summed length over
|
|
/// <see cref="DataConnectionProtoContract.MaxFeaturesHeaderLength"/> throws <see cref="ArgumentException"/>,
|
|
/// which the server maps to <c>InvalidArgument</c>.
|
|
/// </summary>
|
|
public static DataFeatures Parse(Metadata? headers)
|
|
{
|
|
if (headers is null) return DataFeatures.None;
|
|
var features = DataFeatures.None;
|
|
var length = 0;
|
|
foreach (var entry in headers)
|
|
{
|
|
if (!IsFeaturesKey(entry.Key)) continue;
|
|
if (entry.IsBinary) throw new ArgumentException("Invalid data feature negotiation.");
|
|
var value = entry.Value ?? string.Empty;
|
|
length += value.Length;
|
|
if (length > DataConnectionProtoContract.MaxFeaturesHeaderLength) throw new ArgumentException("Invalid data feature negotiation.");
|
|
foreach (var token in value.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
if (string.Equals(token, HistoryToken, StringComparison.OrdinalIgnoreCase)) features |= DataFeatures.History;
|
|
else if (string.Equals(token, ConditionsToken, StringComparison.OrdinalIgnoreCase)) features |= DataFeatures.Conditions;
|
|
}
|
|
}
|
|
return features;
|
|
}
|
|
|
|
// gRPC lower-cases keys; "-bin" is the binary form of the same logical header.
|
|
private static bool IsFeaturesKey(string key) =>
|
|
string.Equals(key, DataConnectionProtoContract.FeaturesHeader, StringComparison.OrdinalIgnoreCase)
|
|
|| string.Equals(key, DataConnectionProtoContract.FeaturesHeader + "-bin", StringComparison.OrdinalIgnoreCase);
|
|
}
|