Class UDPClient
Bidirectional UDP communication block for streaming vector data.
JSON configuration
Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.
JSON configuration:
{
"UDPStream": {
"Type": "UDPClient",
"Params": [ 5005, "double", "127.0.0.1", 5006 ],
"DesiredRate": 200,
"Path": "C:/Data/session1"
}
}
Params:
| Index | Description |
|---|---|
| 0 | ReceivePort (int) — local port to listen on (1–65535). |
| 1 | ParsingFormat (string) — "float", "double" or "uint16". |
| 2 | RemoteHost (string, optional) — target IP for sending. |
| 3 | RemotePort (int, optional) — target port for sending. Must be specified together with RemoteHost. |
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.Streaming
Assembly: MOSAIC.dll
Syntax
public class UDPClient : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable, IReceivePort
Remarks
Overview: The UDPClient block provides both sending and receiving of MathNet.Numerics.LinearAlgebra.Vector<T> data over UDP. Received packets are parsed into vectors and published downstream. Upstream data arriving via OnReceive(object, object) is serialised and sent to the configured remote endpoint.
Parsing formats:
- Float — 32-bit IEEE 754 floats (4 bytes per channel).
- Double — 64-bit IEEE 754 doubles (8 bytes per channel).
- Uint16 — unsigned 16-bit little-endian integers (2 bytes per value).
The number of channels is inferred from the packet size.
Receive-only mode: If remoteHost and remotePort are omitted,
the block only listens on the configured receive port.
CSV logging: When a Path is specified, Dumper
automatically logs every published vector via Publish(object).
Constructors
UDPClient(string, double, int, ParsingFormat, string?, int?)
Initializes a new UDPClient block.
Declaration
public UDPClient(string name, double desiredRate, int receivePort, UDPClient.ParsingFormat format, string? remoteHost = null, int? remotePort = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Display name. |
| double | desiredRate | Processing rate in Hz (typically inherited). |
| int | receivePort | Local port to listen on (1–65535). |
| UDPClient.ParsingFormat | format | Binary parsing format. |
| string | remoteHost | Remote host IP for sending, or null for receive-only. |
| int? | remotePort | Remote port for sending, or null for receive-only. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown if ports are out of valid range. |
Fields
_channelNum
Number of channels in the current send buffer.
Declaration
private int _channelNum
Field Value
| Type | Description |
|---|---|
| int |
_disposed
Whether the block has been disposed.
Declaration
private bool _disposed
Field Value
| Type | Description |
|---|---|
| bool |
_format
Binary parsing format for UDP packets.
Declaration
private readonly UDPClient.ParsingFormat _format
Field Value
| Type | Description |
|---|---|
| UDPClient.ParsingFormat |
_receiver
UDP client for receiving data on the local port.
Declaration
private readonly UdpClient _receiver
Field Value
| Type | Description |
|---|---|
| UdpClient |
_remoteEndpoint
Remote endpoint for sending, or null for receive-only mode.
Declaration
private readonly IPEndPoint? _remoteEndpoint
Field Value
| Type | Description |
|---|---|
| IPEndPoint |
_sendBuffer
Preallocated send buffer, resized when channel count changes.
Declaration
private byte[]? _sendBuffer
Field Value
| Type | Description |
|---|---|
| byte[] |
_sendLock
Semaphore guarding concurrent send operations.
Declaration
private readonly SemaphoreSlim _sendLock
Field Value
| Type | Description |
|---|---|
| SemaphoreSlim |
_sender
UDP client for sending data to the remote endpoint.
Declaration
private readonly UdpClient _sender
Field Value
| Type | Description |
|---|---|
| UdpClient |
Properties
BytesReceived
Total bytes received.
Declaration
public long BytesReceived { get; private set; }
Property Value
| Type | Description |
|---|---|
| long |
BytesSent
Total bytes sent.
Declaration
public long BytesSent { get; private set; }
Property Value
| Type | Description |
|---|---|
| long |
DumpFilePrefix
Recordings from this block are named <block>_udp.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
Format
Current parsing format.
Declaration
public UDPClient.ParsingFormat Format { get; }
Property Value
| Type | Description |
|---|---|
| UDPClient.ParsingFormat |
JsonTypeName
Block type identifier for JSON serialization. Override for a custom name.
Declaration
protected override string JsonTypeName { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
MaxInputs
At most one input. Attaching an upstream block additionally serialises its vectors to the configured remote endpoint; leaving it unattached is receive-only mode.
Declaration
public override int MaxInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
MinInputs
Zero inputs — the block is a pure receiver, publishing whatever arrives on ReceivePort. That is the common configuration, and declaring it here is what makes the block a valid graph source in the palette and the graph editor.
Declaration
public override int MinInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
PacketsReceived
Total UDP packets received.
Declaration
public long PacketsReceived { get; private set; }
Property Value
| Type | Description |
|---|---|
| long |
PacketsSent
Total UDP packets sent.
Declaration
public long PacketsSent { get; private set; }
Property Value
| Type | Description |
|---|---|
| long |
ReceivePort
Local port the receiver is listening on.
Declaration
public int ReceivePort { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
RemoteHost
Remote host IP for sending, or null for receive-only.
Declaration
public string? RemoteHost { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
RemotePort
Remote port for sending, or null for receive-only.
Declaration
public int? RemotePort { get; set; }
Property Value
| Type | Description |
|---|---|
| int? |
Visualization
This block's visualization, or null when it has none.
Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Overrides
Remarks
Blocks own their scope as a public Viz property rather than inheriting one, so the
base class has no way to reach it. Overriding this is what lets the rate below be wired up
automatically. With correct publication metadata, the bundle derives the time axis for
one vector per publication or fixed-size contiguous sample packets from the feed shape.
When a plot needs a rate that publication frequency and feed shape cannot express (for
example, variable-sized packets or overlapping windows), declare it with
Viz.UpdateSignalRate. An explicit declaration wins over the derived rate. A block
with several bundles must configure each one; this hook exposes only one bundle.
Viz
Visualization helper for binding received/sent data to the UI scope.
Declaration
public BlockVisualization Viz { get; set; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Methods
AsyncReceiveCallback(IAsyncResult)
Handles an incoming UDP packet, parses it, and publishes the result.
Declaration
private void AsyncReceiveCallback(IAsyncResult ar)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncResult | ar |
ConfigureInput(IServiceProvider, JsonModel)
Creates a UDPClient from a JSON pipeline definition.
Declaration
public static UDPClient ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | sp | Service provider for dependency injection. |
| JsonModel | m | JSON model. See the class-level example for |
Returns
| Type | Description |
|---|---|
| UDPClient | A configured UDPClient instance. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown if a configured parameter is invalid. |
Dispose()
Disposes UDP sockets, semaphore, visualization, and base class resources (including CSV dumper if configured).
Declaration
public override void Dispose()
Overrides
GetJsonParams()
Block-specific parameters for JSON. Override to provide custom parameters.
Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
| Type | Description |
|---|---|
| IReadOnlyList<object> | The values for Params, in the order this block's |
Overrides
OnReceive(object, object)
Sends upstream data over UDP and publishes it downstream.
Declaration
protected override void OnReceive(object sender, object data)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | The upstream block. |
| object | data | Expected to be a MathNet.Numerics.LinearAlgebra.Vector<T> of double. Other types are logged and ignored. |
Overrides
ParseAsDoubles(byte[])
Parses a byte array as 64-bit doubles and returns a vector.
Declaration
private static Vector<double> ParseAsDoubles(byte[] bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ParseAsFloats(byte[])
Parses a byte array as 32-bit floats and returns a double vector.
Declaration
private static Vector<double> ParseAsFloats(byte[] bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ParseAsUInt16(byte[])
Parses unsigned 16-bit little-endian values, including values above 32767.
Declaration
private static Vector<double> ParseAsUInt16(byte[] bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes |
Returns
| Type | Description |
|---|---|
| Vector<double> |
ParseFormat(string?)
Parses a format string into a UDPClient.ParsingFormat enum value.
Declaration
private static UDPClient.ParsingFormat ParseFormat(string? format)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format |
Returns
| Type | Description |
|---|---|
| UDPClient.ParsingFormat |
ParseReceivedData(byte[])
Parses received bytes into a vector. Returns null on failure.
Declaration
private Vector<double>? ParseReceivedData(byte[] bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes |
Returns
| Type | Description |
|---|---|
| Vector<double> |
SendUdpAsync(Vector<double>)
Serialises and sends a vector asynchronously over UDP.
Declaration
private void SendUdpAsync(Vector<double> vector)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector<double> | vector |
StartReceiving()
Starts the asynchronous receive loop.
Declaration
private void StartReceiving()
Events
PacketReceivedEvent
Raised after a packet is received.
Declaration
public event EventHandler<PacketEventArgs>? PacketReceivedEvent
Event Type
| Type | Description |
|---|---|
| EventHandler<PacketEventArgs> |
PacketSentEvent
Raised after a packet is sent.
Declaration
public event EventHandler<PacketEventArgs>? PacketSentEvent
Event Type
| Type | Description |
|---|---|
| EventHandler<PacketEventArgs> |
StatusChanged
Raised when the block's status changes (e.g. errors).
Declaration
public event EventHandler<string>? StatusChanged
Event Type
| Type | Description |
|---|---|
| EventHandler<string> |