Class SseReader
Incremental reader for the audio.cpp server's text/event-stream responses.
public sealed class SseReader
- Inheritance
-
SseReader
- Inherited Members
Remarks
The reader consumes a stream without requiring the connection to close. Events are
framed the way the server writes them (data: <json>\n\n, terminated by
data: [DONE]\n\n); comment lines (: prefix) and event: fields
follow the SSE rules, and multi-line data: payloads are joined with \n
per spec even though the server never emits them.
Size limit: a single line may be at most the configured maximum (16 MiB by default).
The server documents that task.stream.done carries the whole result audio on
one line, about 640 KB of Base64 per 10 s of 24 kHz audio, so callers reading long
spoken replies should size this via the constructor.
Constructors
SseReader(Stream)
Creates a reader over stream with the default line limit.
public SseReader(Stream stream)
Parameters
streamStreamThe response content stream to read events from.
SseReader(Stream, int)
Creates a reader with an explicit maximum line size.
public SseReader(Stream stream, int maxLineBytes)
Parameters
streamStreamThe response content stream to read events from.
maxLineBytesintMaximum size of one line, in bytes; must be positive.
Exceptions
- ArgumentOutOfRangeException
maxLineBytesis not positive.
Fields
DefaultMaxLineBytes
Default maximum size of one line: 16 MiB.
public const int DefaultMaxLineBytes = 16777216
Field Value
DoneSentinel
The literal the server writes to terminate a healthy stream.
public const string DoneSentinel = "[DONE]"
Field Value
Methods
EnumerateAsync(CancellationToken)
Enumerates events lazily. Available on all target frameworks because the
library references Microsoft.Bcl.AsyncInterfaces on netstandard2.0.
public IAsyncEnumerable<SseEvent> EnumerateAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token observed while reading the stream.
Returns
- IAsyncEnumerable<SseEvent>
The event sequence; iteration ends when
[DONE]or connection close is observed.
ReadEventsAsync(Func<SseEvent, Task>, CancellationToken)
Reads events until data: [DONE], the stream end, or cancellation.
public Task ReadEventsAsync(Func<SseEvent, Task> onEvent, CancellationToken cancellationToken = default)
Parameters
onEventFunc<SseEvent, Task>Callback invoked for each event; iteration stops if it throws.
cancellationTokenCancellationTokenCancellation token observed while reading the stream.
Returns
- Task
Completes when the stream terminates or the callback stops consuming events.
ThrowIfErrorEvent(string)
Throws AudioCppException when data is an SSE
error event ({"type":"error","error":{"message":…}}). SSE errors carry no
error type or HTTP status per the server contract.
public static void ThrowIfErrorEvent(string data)
Parameters
datastringThe
data:payload of one event.
TryReadEventAsync(CancellationToken)
Reads the next event, or null once the stream terminates
([DONE] or connection end).
public Task<SseEvent?> TryReadEventAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token observed while reading.