Refactor: Streamline code formatting, introduce cleaner structure, and remove unused Class1. Add .editorconfig for consistent coding style.
This commit is contained in:
@@ -39,11 +39,7 @@ Console.CancelKeyPress += (_, e) =>
|
||||
appCts.Cancel();
|
||||
};
|
||||
|
||||
using var pipe = new NamedPipeClientStream(
|
||||
serverName: ".",
|
||||
pipeName: pipeName,
|
||||
direction: PipeDirection.InOut,
|
||||
options: PipeOptions.Asynchronous);
|
||||
using var pipe = new NamedPipeClientStream(serverName: ".", pipeName: pipeName, direction: PipeDirection.InOut, options: PipeOptions.Asynchronous);
|
||||
|
||||
await pipe.ConnectAsync(appCts.Token);
|
||||
|
||||
@@ -62,10 +58,9 @@ async Task SendAsync(IpcFrame frame, CancellationToken ct)
|
||||
}
|
||||
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Hello,
|
||||
Payload: IpcProtocol.ToJsonElement(new { childId = childId.Value, pid = Environment.ProcessId })),
|
||||
appCts.Token);
|
||||
new IpcFrame(Kind: IpcKinds.Hello, Payload: IpcProtocol.ToJsonElement(new { childId = childId.Value, pid = Environment.ProcessId })),
|
||||
appCts.Token
|
||||
);
|
||||
|
||||
CancellationTokenSource? workCts = null;
|
||||
Task? workTask = null;
|
||||
@@ -102,38 +97,64 @@ async Task StartWorkAsync(string correlationId, int steps, int delayMs, Cancella
|
||||
workCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
var wct = workCts.Token;
|
||||
|
||||
workTask = Task.Run(async () =>
|
||||
{
|
||||
await SendAsync(new IpcFrame(
|
||||
Kind: IpcKinds.Log,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { level = "info", message = $"Child {childId}: work started ({steps} steps)." })), wct);
|
||||
|
||||
for (int i = 1; i <= steps; i++)
|
||||
workTask = Task.Run(
|
||||
async () =>
|
||||
{
|
||||
wct.ThrowIfCancellationRequested();
|
||||
await Task.Delay(delayMs, wct);
|
||||
|
||||
double percent = (i * 100.0) / steps;
|
||||
await SendAsync(new IpcFrame(
|
||||
Kind: IpcKinds.Progress,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { step = i, total = steps, percent })), wct);
|
||||
|
||||
if (i % Math.Max(1, steps / 5) == 0)
|
||||
{
|
||||
await SendAsync(new IpcFrame(
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Log,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { level = "debug", message = $"Child {childId}: reached step {i}/{steps}." })), wct);
|
||||
}
|
||||
}
|
||||
Payload: IpcProtocol.ToJsonElement(new { level = "info", message = $"Child {childId}: work started ({steps} steps)." })
|
||||
),
|
||||
wct
|
||||
);
|
||||
|
||||
await SendAsync(new IpcFrame(
|
||||
Kind: IpcKinds.Result,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { message = $"Child {childId}: work finished." })), wct);
|
||||
}, wct);
|
||||
for (int i = 1; i <= steps; i++)
|
||||
{
|
||||
wct.ThrowIfCancellationRequested();
|
||||
await Task.Delay(delayMs, wct);
|
||||
|
||||
double percent = (i * 100.0) / steps;
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Progress,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(
|
||||
new
|
||||
{
|
||||
step = i,
|
||||
total = steps,
|
||||
percent,
|
||||
}
|
||||
)
|
||||
),
|
||||
wct
|
||||
);
|
||||
|
||||
if (i % Math.Max(1, steps / 5) == 0)
|
||||
{
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Log,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { level = "debug", message = $"Child {childId}: reached step {i}/{steps}." })
|
||||
),
|
||||
wct
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Result,
|
||||
CorrelationId: correlationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { message = $"Child {childId}: work finished." })
|
||||
),
|
||||
wct
|
||||
);
|
||||
},
|
||||
wct
|
||||
);
|
||||
}
|
||||
|
||||
try
|
||||
@@ -149,33 +170,41 @@ try
|
||||
switch (frame.Kind)
|
||||
{
|
||||
case IpcKinds.Ping:
|
||||
await SendAsync(new IpcFrame(
|
||||
Kind: IpcKinds.Pong,
|
||||
CorrelationId: frame.CorrelationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { childId = childId.Value })), appCts.Token);
|
||||
await SendAsync(
|
||||
new IpcFrame(Kind: IpcKinds.Pong, CorrelationId: frame.CorrelationId, Payload: IpcProtocol.ToJsonElement(new { childId = childId.Value })),
|
||||
appCts.Token
|
||||
);
|
||||
break;
|
||||
|
||||
case IpcKinds.StartWork:
|
||||
{
|
||||
var payload = IpcProtocol.FromJsonElement<StartWorkPayload>(frame.Payload) ?? new StartWorkPayload();
|
||||
string corr = frame.CorrelationId ?? Guid.NewGuid().ToString("N");
|
||||
await StartWorkAsync(corr, payload.Steps, payload.DelayMs, appCts.Token);
|
||||
break;
|
||||
}
|
||||
{
|
||||
var payload = IpcProtocol.FromJsonElement<StartWorkPayload>(frame.Payload) ?? new StartWorkPayload();
|
||||
string corr = frame.CorrelationId ?? Guid.NewGuid().ToString("N");
|
||||
await StartWorkAsync(corr, payload.Steps, payload.DelayMs, appCts.Token);
|
||||
break;
|
||||
}
|
||||
|
||||
case IpcKinds.CancelWork:
|
||||
await CancelWorkAsync();
|
||||
await SendAsync(new IpcFrame(
|
||||
Kind: IpcKinds.Log,
|
||||
CorrelationId: frame.CorrelationId ?? currentWorkCorrelationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { level = "info", message = $"Child {childId}: work cancelled." })), appCts.Token);
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Log,
|
||||
CorrelationId: frame.CorrelationId ?? currentWorkCorrelationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { level = "info", message = $"Child {childId}: work cancelled." })
|
||||
),
|
||||
appCts.Token
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
await SendAsync(new IpcFrame(
|
||||
Kind: IpcKinds.Error,
|
||||
CorrelationId: frame.CorrelationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { message = $"Unknown command kind '{frame.Kind}'." })), appCts.Token);
|
||||
await SendAsync(
|
||||
new IpcFrame(
|
||||
Kind: IpcKinds.Error,
|
||||
CorrelationId: frame.CorrelationId,
|
||||
Payload: IpcProtocol.ToJsonElement(new { message = $"Unknown command kind '{frame.Kind}'." })
|
||||
),
|
||||
appCts.Token
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user