Class StreamManager
Reads continuously from any Stream on a dedicated high-priority thread and raises DataReceived whenever new data arrives.
Implements
Inherited Members
Namespace: MOSAIC.Components.IO
Assembly: MOSAIC.dll
Syntax
public sealed class StreamManager : IDisposable
Remarks
Designed for high sample-rate devices (2 kHz+). Key performance characteristics:
- Dedicated Thread (not threadpool) — avoids starvation under load.
- ArrayPool<T> rental — zero per-read heap allocations in the hot path.
- Synchronous Read(byte[], int, int) — avoids async state-machine overhead.
- SpinWait(int) + Sleep(int) hybrid idle — sub-ms wake-up when data resumes after a dry read.
Originally created because SerialPort.DataReceived is unreliable
(see https://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport).
Works with serial ports, TCP sockets, file streams, or any other Stream.
Constructors
StreamManager(Stream, int, int)
Initialises a new StreamManager.
Declaration
public StreamManager(Stream stream, int bufferSize = 1024, int delayMs = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The underlying stream to read from. |
| int | bufferSize | Maximum bytes per read. Should match or exceed the expected frame/packet size. Defaults to 1024. |
| int | delayMs | Milliseconds to sleep when a read returns zero bytes. Prevents busy-spin on streams that return immediately with 0 when idle. Defaults to 1 ms. Set to 0 if the stream blocks until data arrives (e.g. NetworkStream). |
Fields
_bufferSize
Declaration
private readonly int _bufferSize
Field Value
| Type | Description |
|---|---|
| int |
_delayMs
Declaration
private readonly int _delayMs
Field Value
| Type | Description |
|---|---|
| int |
_disposed
Declaration
private volatile bool _disposed
Field Value
| Type | Description |
|---|---|
| bool |
_readThread
Declaration
private Thread? _readThread
Field Value
| Type | Description |
|---|---|
| Thread |
_running
Declaration
private volatile bool _running
Field Value
| Type | Description |
|---|---|
| bool |
_stream
Declaration
private readonly Stream _stream
Field Value
| Type | Description |
|---|---|
| Stream |
Methods
Dispose()
Stops the reader thread. Does not close the stream (caller owns it).
Declaration
public void Dispose()
ReadLoop()
Core read loop. Runs on a dedicated thread until stopped or the stream faults.
Declaration
private void ReadLoop()
StartStreaming()
Starts the reader on a dedicated high-priority background thread. Does nothing if already running.
Declaration
public void StartStreaming()
StopStreaming()
Signals the reader thread to stop and waits for it to exit (up to 2 s). Safe to call multiple times.
Declaration
public void StopStreaming()
Write(byte[])
Writes a byte array to the underlying stream (synchronous).
Declaration
public void Write(byte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | data |
Write(ReadOnlySpan<byte>)
Writes a span to the underlying stream (synchronous, zero-copy).
Declaration
public void Write(ReadOnlySpan<byte> data)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | data |
Events
DataReceived
Fired on the reader thread whenever new data has been read from the stream.
Declaration
public event Action<byte[], int>? DataReceived
Event Type
| Type | Description |
|---|---|
| Action<byte[], int> |
Remarks
Performance contract: The byte array is a pooled rental. The handler
must not hold a reference beyond the callback scope — copy if needed.
The int parameter is the number of valid bytes (the array may be larger).
The callback executes on the reader thread. Keep handlers fast to avoid blocking subsequent reads. For heavy processing, enqueue into a ConcurrentQueue<T> or ring buffer.