Typed messages
Each strongly typed message represents the corresponding SBE messages described in the SBE template, and message fields are represented as named properties.
Decoding example:
var buffer = new MemoryPointer(sbeEncodedMemoryRegion);
Negotiate500 negotiate = new(buffer);
Console.WriteLine(@$"hmacSignature={negotiate.HMACSignature},
accessKeyID={negotiate.AccessKeyID},
UUID={negotiate.UUID},
requestTimestamp={negotiate.RequestTimestamp},
session={negotiate.Session},
firm={negotiate.Firm}");
Because an SBE message can have a variable length, a customer code is responsible for calculating the required size and creating a corresponding buffer.
The current message length is accessible via the MessageLength property.
Encoding example:
var buffer = new MemoryPointer(new byte[65536]);
bool encode = true;
Negotiate500 negotiate = new(buffer, encode)
{
HMACSignature = "HMAC",
AccessKeyID = "AccessKey",
UUID = 1,
RequestTimestamp = (ulong)(DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1).Ticks) * 1000000 / TimeSpan.TicksPerMillisecond,
Session = "ABC",
Firm = "ABCDE"
};
Console.WriteLine($"SBE encoded memory region: {Encoding.Default.GetString(negotiate.Buffer[..negotiate.MessageLength])}");
The Generator instance can be used to generate typed message classes.
- The GenerateAssembly<THeader>(TemplateLibrary, string, string, string) method creates an assembly on disk, and the user project can reference this assembly.
- The GenerateSource<THeader>(TemplateLibrary, string, string, bool, bool) method generates the source code file; and the user project can include this file.
For example:
TemplateLibrary.LicenseStore = "../../../../../license";
TemplateLibrary library = TemplateLibrary.Parse(File.ReadAllText("ilinkbinary.xml"));
Generator generator = new();
generator.GenerateAssembly<CmeILinkHeader>(library, "Trading", "Trading.dll");
generator.GenerateSource<CmeILinkHeader>(library, "Trading", "Trading.cs");
Code Generator tool
The distribution package includes the "Code Generator" tool to generate an assembly or a source code file with typed messages.
For example:
dotnet OnixS.SimpleBinaryEncoding.CodeGenerator.dll ilinkbinary.xml CmeILinkHeader Trading
Repeating groups
Repeating groups are represented by strongly-typed classes that derive from the BaseGroup class. Group entry fields can be accessed via named properties.
To advance the repeating group to the next entry, use the MoveNext() method.
Example:
var buffer = new MemoryPointer(new byte[65536]);
bool encode = true;
MassQuoteAck545 massQuoteAck = new (buffer, encode);
// Create a repeating group with two entries.
massQuoteAck.SetGroup(Tag.NoQuoteEntries, 2);
massQuoteAck.NoQuoteEntries.MoveNext();
massQuoteAck.NoQuoteEntries.QuoteEntryID = 1;
massQuoteAck.NoQuoteEntries.SecurityID = 12212;
massQuoteAck.NoQuoteEntries.QuoteSetID = 452;
massQuoteAck.NoQuoteEntries.MoveNext();
massQuoteAck.NoQuoteEntries.QuoteEntryID = 2;
massQuoteAck.NoQuoteEntries.SecurityID = 141234424;
massQuoteAck.NoQuoteEntries.QuoteSetID = 32339;
MassQuoteAck545 massQuoteAck = new(buffer);
// Decode a repeating group.
while (massQuoteAck.NoQuoteEntries.MoveNext())
{
Console.WriteLine(@$"
QuoteEntryID={massQuoteAck.NoQuoteEntries.QuoteEntryID}
SecurityID={massQuoteAck.NoQuoteEntries.SecurityID}
QuoteSetID={massQuoteAck.NoQuoteEntries.QuoteSetID}
");
}