Streamfab.keepstreams.generic.hook-smeagol-ther... Apr 2026

public override async ValueTask<int> ReadAsync( Memory<byte> destination, CancellationToken cancellationToken = default)

var listener = new DiagnosticListener("StreamFab.KeepStreams.HookSmeagol"); listener.Subscribe(new MyObserver()); These events are invaluable when you need to without modifying the hook code itself. 8. Common pitfalls & how to avoid them | Pitfall | Symptom | Fix | |---------|---------|-----| | Double‑dispose | ObjectDisposedException on later reads/writes. | Ensure the hook does not call Dispose on the inner stream unless it owns it. The wrapper already disposes the inner stream once. | | Blocking async hooks | Thread‑pool starvation, deadlocks. | Never use .Result / .Wait() inside async hook methods; always await . | | Changing CanSeek | Consumer thinks the stream is seekable but it isn’t. | Propagate CanSeek from the inner stream unchanged; if you need to add seeking (e.g., buffering), expose a new wrapper type rather than HookSmeagol . | | Unbounded memory growth | Hook buffers grow without limit (e.g., a logging hook that stores every payload). | Use bounded buffers or stream the data to a file/DB as it arrives. | | Incorrect async signature | ValueTask returned but not awaited → lost exceptions. | Always await the returned ValueTask inside the wrapper (the library already does this). | 9. Sample end‑to‑end usage Below is a short, self‑contained console demo that composes three hooks: StreamFab.KeepStreams.Generic.Hook-Smeagol-TheR...

public sealed class LoggingHook : IStreamHook { public void BeforeRead(IHookContext ctx, byte[] buffer, int offset, int count) => Console.WriteLine($"[LOG] About to read | Ensure the hook does not call Dispose

// 3. Post‑hook (e.g., logging, decryption, metrics) await _hook.AfterReadAsync(_ctx, destination.Slice(0, bytesRead), cancellationToken) .ConfigureAwait(false); | Never use

// 2. The inner stream performs the real read int bytesRead = _inner.Read(buffer, offset, count);

| Event name | Payload | |------------|---------| | ReadStart | StreamId, Count, Timestamp | | ReadStop | StreamId, BytesRead, ElapsedMs | | WriteStart | StreamId, Count, Timestamp | | WriteStop | StreamId, BytesWritten, ElapsedMs | | Error | StreamId, Exception, Operation |