< Summary - Combined Code Coverage

Information
Class: NLightning.Daemon.Services.Ipc.LengthPrefixedIpcFraming
Assembly: NLightning.Daemon
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Daemon/Services/Ipc/IpcFraming.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 52
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReadAsync()0%4260%
WriteAsync()100%210%
ReadExactAsync()0%2040%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Daemon/Services/Ipc/IpcFraming.cs

#LineLine coverage
 1using System.Buffers;
 2using MessagePack;
 3
 4namespace NLightning.Daemon.Services.Ipc;
 5
 6using Daemon.Ipc.Interfaces;
 7using Transport.Ipc;
 8
 9/// <summary>
 10/// Length-prefixed MessagePack framing for IpcEnvelope.
 11/// </summary>
 12public sealed class LengthPrefixedIpcFraming : IIpcFraming
 13{
 14    public async Task<IpcEnvelope> ReadAsync(Stream stream, CancellationToken ct)
 15    {
 016        var header = new byte[4];
 017        await ReadExactAsync(stream, header, ct);
 018        var len = BitConverter.ToInt32(header, 0);
 019        if (len is <= 0 or > 10_000_000) throw new IOException("Invalid IPC frame length.");
 20
 021        var buffer = ArrayPool<byte>.Shared.Rent(len);
 22        try
 23        {
 024            await ReadExactAsync(stream, buffer.AsMemory(0, len), ct);
 025            return MessagePackSerializer.Deserialize<IpcEnvelope>(buffer.AsMemory(0, len), cancellationToken: ct);
 26        }
 27        finally
 28        {
 029            ArrayPool<byte>.Shared.Return(buffer);
 30        }
 031    }
 32
 33    public async Task WriteAsync(Stream stream, IpcEnvelope envelope, CancellationToken ct)
 34    {
 035        var payload = MessagePackSerializer.Serialize(envelope, cancellationToken: ct);
 036        var len = BitConverter.GetBytes(payload.Length);
 037        await stream.WriteAsync(len, ct);
 038        await stream.WriteAsync(payload, ct);
 039        await stream.FlushAsync(ct);
 040    }
 41
 42    private static async Task ReadExactAsync(Stream stream, Memory<byte> buffer, CancellationToken ct)
 43    {
 044        var total = 0;
 045        while (total < buffer.Length)
 46        {
 047            var read = await stream.ReadAsync(buffer[total..], ct);
 048            if (read == 0) throw new EndOfStreamException();
 049            total += read;
 50        }
 051    }
 52}