StellaOps.Router.Transport.Udp

Router UDP transport, client side: sends the frames of a microservice to a gateway as UDP datagrams.

library
Component
Router
Licence
Apache-2.0
Target framework
net10.0
Latest release
1.0.1-20261117T070237Z

Install

dotnet add package StellaOps.Router.Transport.Udp --version 1.0.1-20261117T070237Z

Namespaces

NamespaceTypes
StellaOps.Router.Transport.Udp5

namespace StellaOps.Router.Transport.Udp

TypeKindDescription
UdpFrameProtocolclassHandles serialization and deserialization of frames for UDP transport.
UdpTransportClientclassUDP transport client implementation for microservices.
UdpTransportClientServiceCollectionExtensionsclassExtension methods for registering UDP transport services.
UdpTransportOptionsclassOptions for UDP transport configuration.
UdpTransportPluginclassPlugin for the client side of the UDP transport, which a microservice connects with.

class UdpFrameProtocol

public static class UdpFrameProtocol

Handles serialization and deserialization of frames for UDP transport. Frame format: [1-byte frame type][1-byte correlation length N][N bytes UTF-8 correlation id][remaining data] (TransportFrameHeader). The correlation id travels unchanged, so the gateway’s ULID correlations survive the round trip.

Members

MemberKindSummary
GetHeaderSize()methodGets the smallest valid datagram size: a header without a correlation id.
ParseFrame(ReadOnlySpan<byte>)methodParses a frame from a datagram.
SerializeFrame(Frame)methodSerializes a frame to a datagram.

method GetHeaderSize() #

public static int GetHeaderSize()

Gets the smallest valid datagram size: a header without a correlation id.

method ParseFrame(ReadOnlySpan<byte>) #

public static Frame ParseFrame(ReadOnlySpan<byte> data)

Parses a frame from a datagram.

Parameters

  • data: The datagram data.

Returns

The parsed frame.

Exceptions

  • InvalidOperationException: Thrown when the datagram header is malformed.

method SerializeFrame(Frame) #

public static byte[] SerializeFrame(Frame frame)

Serializes a frame to a datagram.

Parameters

  • frame: The frame to serialize.

Returns

The serialized datagram bytes.

Exceptions

class UdpTransportClient

public sealed class UdpTransportClient : ITransportClient, IMicroserviceTransport, IAsyncDisposable

UDP transport client implementation for microservices. UDP transport does not support streaming.

Members

MemberKindSummary
UdpTransportClient(IOptions<UdpTransportOptions>, ILogger<UdpTransportClient>)constructorInitializes a new instance of the UdpTransportClientclass.
OnCancelReceivedeventEvent raised when a CANCEL frame is received.
OnRequestReceivedeventEvent raised when a REQUEST frame is received.
CancelAllInflight(string)methodCancels all in-flight handlers.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken)methodConnects to the gateway.
ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken)methodConnects to the gateway and registers the instance, its endpoints, schemas and OpenAPI metadata in the HELLO datagram.
DisconnectAsync()methodDisconnects from the gateway.
DisposeAsync()methodSee IAsyncDisposable.DisposeAsync().
SendCancelAsync(ConnectionState, Guid, string)methodSends a cancellation request.
SendHeartbeatAsync(HeartbeatPayload, CancellationToken)methodSends a heartbeat.
SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken)methodSends a request and waits for a response.
SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)methodSends a request whose body is read from a stream and hands the microservice’s response to readResponse.

constructor UdpTransportClient(IOptions<UdpTransportOptions>, ILogger<UdpTransportClient>) #

public UdpTransportClient(IOptions<UdpTransportOptions> options, ILogger<UdpTransportClient> logger)

Initializes a new instance of the UdpTransportClientclass.

event OnCancelReceived #

public event Func<Guid, string?, Task>? OnCancelReceived

Event raised when a CANCEL frame is received.

event OnRequestReceived #

public event Func<Frame, CancellationToken, Task<Frame>>? OnRequestReceived

Event raised when a REQUEST frame is received.

method CancelAllInflight(string) #

public void CancelAllInflight(string reason)

Cancels all in-flight handlers.

method ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, CancellationToken) #

public Task ConnectAsync(InstanceDescriptor instance, IReadOnlyList<EndpointDescriptor> endpoints, CancellationToken cancellationToken)

Connects to the gateway.

Parameters

  • instance: The instance descriptor.
  • endpoints: The endpoints to register.
  • cancellationToken: Cancellation token.

method ConnectAsync(InstanceDescriptor, IReadOnlyList<EndpointDescriptor>, IReadOnlyDictionary<string, SchemaDefinition>, ServiceOpenApiInfo, CancellationToken) #

public Task ConnectAsync(InstanceDescriptor instance, IReadOnlyList<EndpointDescriptor> endpoints, IReadOnlyDictionary<string, SchemaDefinition>? schemas, ServiceOpenApiInfo? openApiInfo, CancellationToken cancellationToken)

Connects to the gateway and registers the instance, its endpoints, schemas and OpenAPI metadata in the HELLO datagram. The whole HELLO must fit one datagram (UdpTransportOptions.MaxDatagramSize); a larger one fails with PayloadTooLargeExceptionbefore anything is sent.

Parameters

  • instance: The instance descriptor.
  • endpoints: The endpoints to register.
  • schemas: Schema definitions referenced by the endpoints.
  • openApiInfo: OpenAPI metadata for the service.
  • cancellationToken: Cancellation token.

method DisconnectAsync() #

public Task DisconnectAsync()

Disconnects from the gateway.

method DisposeAsync() #

public ValueTask DisposeAsync()

See IAsyncDisposable.DisposeAsync().

method SendCancelAsync(ConnectionState, Guid, string) #

public Task SendCancelAsync(ConnectionState connection, Guid correlationId, string? reason = null)

Sends a cancellation request.

Parameters

  • connection: The connection to use.
  • correlationId: The correlation ID of the request to cancel.
  • reason: Optional reason for cancellation.

method SendHeartbeatAsync(HeartbeatPayload, CancellationToken) #

public Task SendHeartbeatAsync(HeartbeatPayload heartbeat, CancellationToken cancellationToken)

Sends a heartbeat.

method SendRequestAsync(ConnectionState, Frame, TimeSpan, CancellationToken) #

public Task<Frame> SendRequestAsync(ConnectionState connection, Frame requestFrame, TimeSpan timeout, CancellationToken cancellationToken)

Sends a request and waits for a response.

Parameters

  • connection: The connection to use.
  • requestFrame: The request frame.
  • timeout: The timeout for the request.
  • cancellationToken: Cancellation token.

Returns

The response frame.

method SendStreamingAsync(ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken) #

public Task SendStreamingAsync(ConnectionState connection, Frame requestHeader, Stream requestBody, Func<ResponseFrame, Stream, Task> readResponse, PayloadLimits limits, CancellationToken cancellationToken)

Sends a request whose body is read from a stream and hands the microservice’s response to readResponse. TransportStreamingDispatch.SendBufferedAsync(ITransportClient, ConnectionState, Frame, Stream, Func<ResponseFrame, Stream, Task>, PayloadLimits, CancellationToken)implements it for transports whose microservice side answers a request with one response frame.

Parameters

  • connection: The connection to use.
  • requestHeader: The request header frame.
  • requestBody: The request body stream.
  • readResponse: Callback that receives the response head (the status code and headers of the microservice’s response; its ResponseFrame.Payloadis empty) and the response body stream.
  • limits: Payload limits to enforce.
  • cancellationToken: Cancellation token.

class UdpTransportClientServiceCollectionExtensions

public static class UdpTransportClientServiceCollectionExtensions

Extension methods for registering UDP transport services.

Members

MemberKindSummary
AddUdpTransportClient(IServiceCollection, Action<UdpTransportOptions>)extension methodAdds UDP transport client services to the service collection.

extension method AddUdpTransportClient(IServiceCollection, Action<UdpTransportOptions>) #

public static IServiceCollection AddUdpTransportClient(this IServiceCollection services, Action<UdpTransportOptions>? configure = null)

Adds UDP transport client services to the service collection.

Parameters

  • services: The service collection.
  • configure: Optional configuration action.

Returns

The service collection.

class UdpTransportOptions

public sealed class UdpTransportOptions

Options for UDP transport configuration.

Members

MemberKindSummary
UdpTransportOptions()constructorCreates an instance of UdpTransportOptions.
AllowBroadcastpropertyGets or sets whether to allow broadcast.
BindAddresspropertyGets or sets the bind address for the server.
DefaultTimeoutpropertyGets or sets the default timeout for requests.
HostpropertyGets or sets the host to connect to (client only).
MaxDatagramSizepropertyGets or sets the maximum datagram size in bytes.
PortpropertyGets or sets the port to listen on/connect to.
ReceiveBufferSizepropertyGets or sets the receive buffer size.
SendBufferSizepropertyGets or sets the send buffer size.

constructor UdpTransportOptions() #

public UdpTransportOptions()

Creates an instance of UdpTransportOptions.

property AllowBroadcast #

public bool AllowBroadcast { get; set; }

Gets or sets whether to allow broadcast.

property BindAddress #

public IPAddress BindAddress { get; set; }

Gets or sets the bind address for the server.

property DefaultTimeout #

public TimeSpan DefaultTimeout { get; set; }

Gets or sets the default timeout for requests.

property Host #

public string? Host { get; set; }

Gets or sets the host to connect to (client only).

property MaxDatagramSize #

public int MaxDatagramSize { get; set; }

Gets or sets the maximum datagram size in bytes. Conservative default well under typical MTU of 1500 bytes.

property Port #

public int Port { get; set; }

Gets or sets the port to listen on/connect to. Default: TransportDefaultPorts.Udp(5102), the gateway options’ default.

property ReceiveBufferSize #

public int ReceiveBufferSize { get; set; }

Gets or sets the receive buffer size.

property SendBufferSize #

public int SendBufferSize { get; set; }

Gets or sets the send buffer size.

class UdpTransportPlugin

public sealed class UdpTransportPlugin : IRouterTransportPlugin

Plugin for the client side of the UDP transport, which a microservice connects with. The server side is UdpTransportServerPlugin in StellaOps.Router.Transport.Udp.Server; both carry the transport name udp.

Members

MemberKindSummary
UdpTransportPlugin()constructorCreates an instance of UdpTransportPlugin.
AllowInsecureTransportsEnvVarconstantEnvironment variable that enables loading the UDP transport plugin.
DisplayNamepropertyGets the human-readable display name for this transport.
ModepropertyGets the side of the transport this plugin registers: RouterTransportMode.Client, RouterTransportMode.Server, or RouterTransportMode.Both.
TransportNamepropertyGets the transport name (e.g., “tcp”, “tls”, “udp”, “rabbitmq”, “inmemory”, “messaging”), shared by the client and the server plugin of one transport.
IsInsecureTransportsAllowed()methodReturns true when UdpTransportPlugin.AllowInsecureTransportsEnvVaris set to a truthy value (case-insensitive: true, 1, yes).
Register(RouterTransportRegistrationContext)methodRegisters transport services into the DI container for the side the context asks for.
RegisterShared(RouterTransportRegistrationContext, Type)methodThe registration the client and the server plugin of the UDP transport share: the insecure-transport guard (UdpTransportPlugin.AllowInsecureTransportsEnvVar) and the UdpTransportOptionsbinding of the context’s configuration section (Router:Transport:Udp when the context names none).

constructor UdpTransportPlugin() #

public UdpTransportPlugin()

Creates an instance of UdpTransportPlugin.

constant AllowInsecureTransportsEnvVar #

public const string AllowInsecureTransportsEnvVar = "STELLAOPS_ROUTER_ALLOW_INSECURE_TRANSPORTS"

Environment variable that enables loading the UDP transport plugin. UDP is unencrypted, unauthenticated, and unordered at the network layer; the runtime guard refuses to register the plugin unless the operator explicitly opts in.

property DisplayName #

public string DisplayName { get; }

Gets the human-readable display name for this transport.

property Mode #

public RouterTransportMode Mode { get; }

Gets the side of the transport this plugin registers: RouterTransportMode.Client, RouterTransportMode.Server, or RouterTransportMode.Both. IRouterTransportPlugin.Register(RouterTransportRegistrationContext)refuses a context that asks for a side outside this value (RouterTransportRegistrationContext.EnsureSupportedBy(IRouterTransportPlugin)).

property TransportName #

public string TransportName { get; }

Gets the transport name (e.g., “tcp”, “tls”, “udp”, “rabbitmq”, “inmemory”, “messaging”), shared by the client and the server plugin of one transport.

method IsInsecureTransportsAllowed() #

public static bool IsInsecureTransportsAllowed()

Returns true when UdpTransportPlugin.AllowInsecureTransportsEnvVaris set to a truthy value (case-insensitive: true, 1, yes).

method Register(RouterTransportRegistrationContext) #

public void Register(RouterTransportRegistrationContext context)

Registers transport services into the DI container for the side the context asks for.

Parameters

  • context: The registration context containing services, configuration, and transport mode.

Exceptions

  • InvalidOperationException: The context asks for a side this plugin does not provide.

method RegisterShared(RouterTransportRegistrationContext, Type) #

public static void RegisterShared(RouterTransportRegistrationContext context, Type loggerCategory)

The registration the client and the server plugin of the UDP transport share: the insecure-transport guard (UdpTransportPlugin.AllowInsecureTransportsEnvVar) and the UdpTransportOptionsbinding of the context’s configuration section (Router:Transport:Udp when the context names none). Both plugins call it from UdpTransportPlugin.Register(RouterTransportRegistrationContext); a plugin for either side of the transport calls it the same way.

Parameters

  • context: The registration context of the plugin.
  • loggerCategory: The logger category of the warning written when the guard is open.

Exceptions

  • InvalidOperationException: The insecure-transport guard is closed.