// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
usingSystem;
usingSystem.Text;
usingBaseline;
usingOrg.SbeTool.Sbe.Dll;
classExample
{
publicstaticvoidExampleMain()
{
// This byte array is used for encoding and decoding, this is what you would send on the wire or save to disk
varbyteBuffer=newbyte[4096];
// You need to "wrap" the array with a DirectBuffer, this class is used by the generated code to read and write efficiently to the underlying byte array
vardirectBuffer=newDirectBuffer(byteBuffer);
constshortSchemaVersion=0;
intbufferOffset=0;
varMessageHeader=newMessageHeader();
varCar=newCar();
// Before encoding a message we need to create a SBE header which specify what we are going to encode (this will allow the decoder to detect that it's an encoded 'car' object)
// We will probably simplify this part soon, so the header gets applied automatically, but for now it's manual
MessageHeader.Wrap(directBuffer,bufferOffset,Car.SchemaVersion);// position the MessageHeader on the DirectBuffer, at the correct position
MessageHeader.BlockLength=Car.BlockLength;// size that a car takes on the wire
MessageHeader.SchemaId=Car.SchemaId;
MessageHeader.TemplateId=Car.TemplateId;// identifier for the car object (SBE template ID)
MessageHeader.Version=Car.SchemaVersion;// this can be overriden if we want to support different versions of the car object (advanced functionality)
// Now that we have encoded the header in the byte array we can encode the car object itself
bufferOffset+=MessageHeader.Size;
CarExample.Encode(Car,directBuffer,bufferOffset);
// Now we have encoded the message is the byte array, we are going to decode it
// first we decode the header (in a real world scenario you would need the header to decide which SBE decoder you are going to use
bufferOffset=0;
// position the MessageHeader object at the beginning of the array
// we have written all the constant length fields, now we can write the repeatable groups
varfuelFigures=car.FuelFiguresCount(3);// we specify that we are going to write 3 FueldFigures (the API is not very .NET friendly yet, we will address that)
fuelFigures.Next();// move to the first element
fuelFigures.Speed=30;
fuelFigures.Mpg=35.9f;
fuelFigures.Next();// second
fuelFigures.Speed=55;
fuelFigures.Mpg=49.0f;
fuelFigures.Next();
fuelFigures.Speed=75;
fuelFigures.Mpg=40.0f;
Car.PerformanceFiguresGroupperfFigures=car.PerformanceFiguresCount(2);// demonstrates how to create a nested group
perfFigures.Next();
perfFigures.OctaneRating=95;
Car.PerformanceFiguresGroup.AccelerationGroupacceleration=perfFigures.AccelerationCount(3).Next();// this group is going to be nested in the first element of the previous group
sb.Append("\ncar.extras.cruiseControl=").Append((extras&OptionalExtras.CruiseControl)==OptionalExtras.CruiseControl);// this is how you can find out if a specific flag is set in a flag enum