mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-20 00:31:32 +08:00
bc840201ab
- Three-phase dotnet format sweep (whitespace -> style -> analyzers) scoped to files changed since merge-base with channelrefinements (5 projects) - Mostly mechanical: operator wrapping (&& -> end-of-line), fluent StringBuilder chains, using ordering, missing exception XML docs - Fix CA2000 in CapturingMessageSocketTests (using var for inner sockets + TestSinks so they're disposed even if the wrapping CapturingMessageSocket constructor throws) - Document the auto-added PcapDiagnosticsException(string?, int) constructor - Keep DecodeServiceCallsAsync's local var (return await + earlier await foreach triggers RCS1174 if inlined) Verified: - All 5 affected projects build with 0 errors - 3-phase dotnet format --verify-no-changes is clean - 53/53 Opc.Ua.Bindings.Pcap.Tests pass on net10.0
111 lines
4.2 KiB
C#
111 lines
4.2 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
|
|
*
|
|
* OPC Foundation MIT License 1.00
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* The complete license agreement can be found here:
|
|
* http://opcfoundation.org/License/MIT/1.00/
|
|
* ======================================================================*/
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Opc.Ua.Bindings.Pcap.Capture;
|
|
using Opc.Ua.Bindings.Pcap.Frame;
|
|
using Opc.Ua.Bindings.Pcap.Models;
|
|
|
|
namespace Opc.Ua.Bindings.Pcap.Formats
|
|
{
|
|
/// <summary>
|
|
/// Formats captured frames as human-readable text.
|
|
/// </summary>
|
|
public sealed class TextFormatter : ITraceFormatter
|
|
{
|
|
private readonly ILogger m_logger;
|
|
|
|
/// <summary>
|
|
/// Constructs a text formatter.
|
|
/// </summary>
|
|
public TextFormatter(ILogger? logger = null)
|
|
{
|
|
m_logger = logger ?? NullLogger.Instance;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public FormatKind Kind => FormatKind.Text;
|
|
|
|
/// <inheritdoc/>
|
|
public string MimeType => "text/plain";
|
|
|
|
/// <inheritdoc/>
|
|
public bool IsBinary => false;
|
|
|
|
/// <inheritdoc/>
|
|
public async ValueTask<FormatResult> FormatAsync(
|
|
ICaptureSource source,
|
|
long? maxFrames,
|
|
CancellationToken ct)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(source);
|
|
|
|
m_logger.LogDebug("Formatting capture as text.");
|
|
StringBuilder builder = new();
|
|
long count = 0;
|
|
await foreach (CaptureFrame frame in source.ReadCapturedFramesAsync(maxFrames, ct)
|
|
.WithCancellation(ct)
|
|
.ConfigureAwait(false))
|
|
{
|
|
ReadOnlySpan<byte> data = frame.Data.Span;
|
|
string messageType = FrameFormatHelpers.GetMessageType(data) ?? "-";
|
|
string channelId = FrameFormatHelpers.GetChannelId(data)?.ToString(CultureInfo.InvariantCulture) ?? "-";
|
|
string tokenId = FrameFormatHelpers.GetTokenId(data)?.ToString(CultureInfo.InvariantCulture) ?? "-";
|
|
|
|
builder.Append(FrameFormatHelpers.FormatTimestamp(frame.Timestamp))
|
|
.Append(' ')
|
|
.Append(FrameFormatHelpers.FormatDirection(frame.Direction))
|
|
.Append(' ')
|
|
.Append(frame.Data.Length.ToString(CultureInfo.InvariantCulture))
|
|
.Append("B ")
|
|
.Append(messageType)
|
|
.Append(" channel=")
|
|
.Append(channelId)
|
|
.Append(" token=")
|
|
.Append(tokenId)
|
|
.AppendLine();
|
|
count++;
|
|
}
|
|
|
|
return new FormatResult
|
|
{
|
|
Kind = Kind,
|
|
MimeType = MimeType,
|
|
Bytes = Encoding.UTF8.GetBytes(builder.ToString()),
|
|
FramesFormatted = count
|
|
};
|
|
}
|
|
}
|
|
}
|