Table of Contents

Class DefaultMessageSerializer<T>

Namespace
DotCompute.Abstractions.Messaging
Assembly
DotCompute.Abstractions.dll

Default binary serializer for IRingKernelMessage types.

public sealed class DefaultMessageSerializer<T> : IMessageSerializer<T> where T : IRingKernelMessage, new()

Type Parameters

T

The message type implementing IRingKernelMessage.

Inheritance
DefaultMessageSerializer<T>
Implements
Inherited Members

Remarks

This implementation uses the built-in Serialize/Deserialize methods from IRingKernelMessage. The serialized format is determined by the message implementation (typically via source generator).

Format (from IRingKernelMessage): - MessageId (16 bytes Guid) - Priority (1 byte) - CorrelationId (17 bytes: 1 byte null flag + 16 bytes Guid if present) - Application data (variable, type-specific)

Properties

MaxSerializedSize

Gets the maximum serialized size for messages of type T.

public int MaxSerializedSize { get; }

Property Value

int

Remarks

This is used to allocate staging buffers. For variable-size messages, return a conservative upper bound. For fixed-size messages, return the exact size.

Methods

Deserialize(ReadOnlySpan<byte>)

Deserializes a message from the provided source span.

public T Deserialize(ReadOnlySpan<byte> source)

Parameters

source ReadOnlySpan<byte>

The source buffer containing serialized message data.

Returns

T

The deserialized message.

Remarks

This method must be thread-safe. It may allocate managed memory for the message instance.

Exceptions

ArgumentException

Thrown if source contains invalid or corrupted data.

GetSerializedSize(T)

Gets the serialized size for a specific message instance.

public int GetSerializedSize(T message)

Parameters

message T

The message to measure.

Returns

int

The number of bytes required to serialize this message.

Remarks

For fixed-size messages, this always returns MaxSerializedSize. For variable-size messages, this returns the exact size for this instance.

Serialize(T, Span<byte>)

Serializes a message into the provided destination span.

public int Serialize(T message, Span<byte> destination)

Parameters

message T

The message to serialize.

destination Span<byte>

The destination buffer (must be at least MaxSerializedSize bytes).

Returns

int

The actual number of bytes written to destination.

Remarks

This method must be thread-safe. It should not allocate managed memory.

Exceptions

ArgumentException

Thrown if destination is too small.