using System.Threading.Channels;
namespace XFEWarframe.Client.Runtime;
/// <summary>Bounded diagnostics: native game callbacks never wait for file I/O.</summary>
public sealed class AsyncClientLog : IAsyncDisposable
{
private readonly Channel<string> queue;
private readonly Task worker = Task.CompletedTask;
private readonly Func<string, ValueTask>? sink;
private readonly string? path;
private long dropped;
private string? failure;
public long Dropped => Interlocked.Read(ref dropped);
public string? Failure => Volatile.Read(ref failure);
public AsyncClientLog(string path, int capacity = 512) : this(capacity) { this.path = path; worker = Task.Run(Consume); }
public AsyncClientLog(Func<string, ValueTask> sink, int capacity = 512) : this(capacity) { this.sink = sink; worker = Task.Run(Consume); }
private AsyncClientLog(int capacity) => queue = Channel.CreateBounded<string>(new BoundedChannelOptions(capacity)
{ SingleReader = true, FullMode = BoundedChannelFullMode.Wait, AllowSynchronousContinuations = false });
public bool TryWrite(string message)
{
if (queue.Writer.TryWrite(message)) return true;
Interlocked.Increment(ref dropped); return false;
}
private async Task Consume()
{
StreamWriter? file = null;
try
{
if (path is not null) file = new StreamWriter(new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 16384, FileOptions.Asynchronous));
while (await queue.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (queue.Reader.TryRead(out var message))
{
if (sink is not null) await sink(message).ConfigureAwait(false);
else await file!.WriteLineAsync(message).ConfigureAwait(false);
}
if (file is not null) await file.FlushAsync().ConfigureAwait(false);
}
}
catch (Exception ex) { Volatile.Write(ref failure, ex.GetType().Name); queue.Writer.TryComplete(); }
finally { if (file is not null) try { await file.DisposeAsync().ConfigureAwait(false); } catch { } }
}
public async ValueTask DisposeAsync() { queue.Writer.TryComplete(); await worker.ConfigureAwait(false); }
}
using System.Threading.Channels;
namespace XFEWarframe.Client.Runtime;
/// <summary>Bounded diagnostics: native game callbacks never wait for file I/O.</summary>
public sealed class AsyncClientLog : IAsyncDisposable
{
private readonly Channel<string> queue;
private readonly Task worker = Task.CompletedTask;
private readonly Func<string, ValueTask>? sink;
private readonly string? path;
private long dropped;
private string? failure;
public long Dropped => Interlocked.Read(ref dropped);
public string? Failure => Volatile.Read(ref failure);
public AsyncClientLog(string path, int capacity = 512) : this(capacity) { this.path = path; worker = Task.Run(Consume); }
public AsyncClientLog(Func<string, ValueTask> sink, int capacity = 512) : this(capacity) { this.sink = sink; worker = Task.Run(Consume); }
private AsyncClientLog(int capacity) => queue = Channel.CreateBounded<string>(new BoundedChannelOptions(capacity)
{ SingleReader = true, FullMode = BoundedChannelFullMode.Wait, AllowSynchronousContinuations = false });
public bool TryWrite(string message)
{
if (queue.Writer.TryWrite(message)) return true;
Interlocked.Increment(ref dropped); return false;
}
private async Task Consume()
{
StreamWriter? file = null;
try
{
if (path is not null) file = new StreamWriter(new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 16384, FileOptions.Asynchronous));
while (await queue.Reader.WaitToReadAsync().ConfigureAwait(false))
{
while (queue.Reader.TryRead(out var message))
{
if (sink is not null) await sink(message).ConfigureAwait(false);
else await file!.WriteLineAsync(message).ConfigureAwait(false);
}
if (file is not null) await file.FlushAsync().ConfigureAwait(false);
}
}
catch (Exception ex) { Volatile.Write(ref failure, ex.GetType().Name); queue.Writer.TryComplete(); }
finally { if (file is not null) try { await file.DisposeAsync().ConfigureAwait(false); } catch { } }
}
public async ValueTask DisposeAsync() { queue.Writer.TryComplete(); await worker.ConfigureAwait(false); }
}