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
TThe 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
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
sourceReadOnlySpan<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
sourcecontains invalid or corrupted data.
GetSerializedSize(T)
Gets the serialized size for a specific message instance.
public int GetSerializedSize(T message)
Parameters
messageTThe 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
messageTThe message to serialize.
destinationSpan<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
destinationis too small.