Refactor: Streamline code formatting, introduce cleaner structure, and remove unused Class1. Add .editorconfig for consistent coding style.
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
namespace CommIpc;
|
||||
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="1.2.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -4,12 +4,7 @@ namespace CommIpc;
|
||||
|
||||
/// <summary>
|
||||
/// Single protocol unit sent over the pipe. This is intentionally generic.
|
||||
///
|
||||
///
|
||||
/// Transport framing: 4-byte little-endian length prefix + UTF-8 JSON bytes.
|
||||
/// </summary>
|
||||
public sealed record IpcFrame(
|
||||
string Kind,
|
||||
string? CorrelationId = null,
|
||||
JsonElement? Payload = null,
|
||||
DateTimeOffset? Timestamp = null
|
||||
);
|
||||
public sealed record IpcFrame(string Kind, string? CorrelationId = null, JsonElement? Payload = null, DateTimeOffset? Timestamp = null);
|
||||
|
||||
@@ -4,9 +4,5 @@ namespace CommIpc;
|
||||
|
||||
internal static class IpcJson
|
||||
{
|
||||
public static readonly JsonSerializerOptions Options = new()
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
};
|
||||
public static readonly JsonSerializerOptions Options = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false };
|
||||
}
|
||||
|
||||
@@ -8,10 +8,7 @@ public static class IpcProtocol
|
||||
// Keep the prototype safe from accidental runaway memory usage.
|
||||
public const int DefaultMaxFrameBytes = 4 * 1024 * 1024; // 4 MiB
|
||||
|
||||
public static async Task WriteFrameAsync(
|
||||
Stream stream,
|
||||
IpcFrame frame,
|
||||
CancellationToken cancellationToken = default)
|
||||
public static async Task WriteFrameAsync(Stream stream, IpcFrame frame, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// Always include a timestamp if the sender didn't set one.
|
||||
if (frame.Timestamp is null)
|
||||
@@ -32,10 +29,7 @@ public static class IpcProtocol
|
||||
await stream.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static async Task<IpcFrame?> ReadFrameAsync(
|
||||
Stream stream,
|
||||
int maxFrameBytes = DefaultMaxFrameBytes,
|
||||
CancellationToken cancellationToken = default)
|
||||
public static async Task<IpcFrame?> ReadFrameAsync(Stream stream, int maxFrameBytes = DefaultMaxFrameBytes, CancellationToken cancellationToken = default)
|
||||
{
|
||||
byte[] header = ArrayPool<byte>.Shared.Rent(4);
|
||||
try
|
||||
@@ -50,10 +44,7 @@ public static class IpcProtocol
|
||||
throw new EndOfStreamException("Unexpected end of stream while reading frame header.");
|
||||
}
|
||||
|
||||
int len = header[0]
|
||||
| (header[1] << 8)
|
||||
| (header[2] << 16)
|
||||
| (header[3] << 24);
|
||||
int len = header[0] | (header[1] << 8) | (header[2] << 16) | (header[3] << 24);
|
||||
|
||||
if (len < 0)
|
||||
{
|
||||
@@ -106,18 +97,12 @@ public static class IpcProtocol
|
||||
return element.Value.Deserialize<T>(IpcJson.Options);
|
||||
}
|
||||
|
||||
private static async Task<int> ReadExactOrEofAsync(
|
||||
Stream stream,
|
||||
byte[] buffer,
|
||||
int offset,
|
||||
int count,
|
||||
CancellationToken cancellationToken)
|
||||
private static async Task<int> ReadExactOrEofAsync(Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
{
|
||||
int total = 0;
|
||||
while (total < count)
|
||||
{
|
||||
int n = await stream.ReadAsync(buffer.AsMemory(offset + total, count - total), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
int n = await stream.ReadAsync(buffer.AsMemory(offset + total, count - total), cancellationToken).ConfigureAwait(false);
|
||||
if (n == 0)
|
||||
{
|
||||
return total; // EOF
|
||||
|
||||
Reference in New Issue
Block a user