Ejemplo n.º 1
0
    public static void ExampleMain()
    {
        // This byte array is used for encoding and decoding, this is what you would send on the wire or save to disk
        var byteBuffer = new byte[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
        var         directBuffer          = new DirectBuffer(byteBuffer);
        const short baselineSchemaVersion = 0;
        int         bufferOffset          = 0;

        var baselineMessageHeader  = new Baseline.MessageHeader();
        var baselineCar            = new Baseline.Car();
        var extensionMessageHeader = new Extension.MessageHeader();
        var extensionCar           = new Extension.Car();

        // 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
        baselineMessageHeader.Wrap(directBuffer, bufferOffset, baselineSchemaVersion); // position the MessageHeader on the DirectBuffer, at the correct position
        baselineMessageHeader.BlockLength = Baseline.Car.BlockLength;                  // size that a car takes on the wire
        baselineMessageHeader.SchemaId    = Baseline.Car.SchemaId;
        baselineMessageHeader.TemplateId  = Baseline.Car.TemplateId;                   // identifier for the car object (SBE template ID)
        baselineMessageHeader.Version     = Baseline.Car.SchemaVersion;                // this can be overridden 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 += Baseline.MessageHeader.Size;
        Baseline.ExtensionExample.Encode(baselineCar, 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
        baselineMessageHeader.Wrap(directBuffer, bufferOffset, baselineSchemaVersion);

        // Extract infos from the header
        // In a real app you would use that to lookup the applicable flyweight to decode this type of message based on templateId and version.
        int actingBlockLength = baselineMessageHeader.BlockLength;
        int actingVersion     = baselineMessageHeader.Version;

        bufferOffset += Baseline.MessageHeader.Size;
        // now we decode the message
        Baseline.ExtensionExample.Decode(baselineCar, directBuffer, bufferOffset, actingBlockLength, actingVersion);

        // Now let's check we can decode that as an extension
        bufferOffset = Extension.MessageHeader.Size; // Start after the header
        // schemaId = Extension.Car.SchemaId;
        // extensionMessageHeader.Wrap(directBuffer, bufferOffset, extensionSchemaVersion);
        // actingBlockLength = extensionMessageHeader.BlockLength;
        // actingVersion = extensionMessageHeader.Version;

        Extension.ExtensionExample.Decode(extensionCar, directBuffer, bufferOffset, actingBlockLength, actingVersion);

        // And now let's encode an extension and decode it as baseline
        Extension.ExtensionExample.Encode(extensionCar, directBuffer, Extension.MessageHeader.Size);
        Baseline.ExtensionExample.Decode(baselineCar, directBuffer, (int)Baseline.MessageHeader.Size, (int)Extension.Car.BlockLength, (int)Baseline.Car.SchemaVersion);
    }
Ejemplo n.º 2
0
 public void WrapForEncode(Car parentMessage, DirectBuffer buffer, int count)
 {
     _parentMessage = parentMessage;
     _buffer = buffer;
     _dimensions.Wrap(buffer, parentMessage.Limit, _actingVersion);
     _dimensions.BlockLength = (ushort)6;
     _dimensions.NumInGroup = (byte)count;
     _index = -1;
     _count = count;
     _blockLength = 6;
     parentMessage.Limit = parentMessage.Limit + SbeHeaderSize;
 }
Ejemplo n.º 3
0
 public Car()
 {
     _parentMessage = this;
 }
Ejemplo n.º 4
0
 public void WrapForDecode(Car parentMessage, DirectBuffer buffer, int actingVersion)
 {
     _parentMessage = parentMessage;
     _buffer = buffer;
     _dimensions.Wrap(buffer, parentMessage.Limit, actingVersion);
     _blockLength = _dimensions.BlockLength;
     _count = _dimensions.NumInGroup;
     _actingVersion = actingVersion;
     _index = -1;
     _parentMessage.Limit = parentMessage.Limit + SbeHeaderSize;
 }
Ejemplo n.º 5
0
 public void WrapForEncode(Car parentMessage, DirectBuffer buffer, int count)
 {
     _parentMessage = parentMessage;
     _buffer = buffer;
     _dimensions.Wrap(buffer, parentMessage.Position, _actingVersion);
     _dimensions.NumInGroup = (byte)count;
     _dimensions.BlockLength = (ushort)1;
     _index = -1;
     _count = count;
     _blockLength = 1;
     const int dimensionsHeaderSize = 3;
     parentMessage.Position = parentMessage.Position + dimensionsHeaderSize;
 }
Ejemplo n.º 6
0
        private static void Decode(Car car,
            DirectBuffer directBuffer,
            int bufferOffset,
            int actingBlockLength,
            int actingVersion)
        {
            var buffer = new byte[128];
            var sb = new StringBuilder();

            car.WrapForDecode(directBuffer, bufferOffset, actingBlockLength, actingVersion);

            sb.Append("\ncar.templateId=").Append(Car.TemplateId);
            sb.Append("\ncar.serialNumber=").Append(car.SerialNumber);
            sb.Append("\ncar.modelYear=").Append(car.ModelYear);
            sb.Append("\ncar.available=").Append(car.Available);
            sb.Append("\ncar.code=").Append(car.Code);

            sb.Append("\ncar.someNumbers=");
            for (int i = 0, size = Car.SomeNumbersLength; i < size; i++)
            {
                sb.Append(car.GetSomeNumbers(i)).Append(", ");
            }

            sb.Append("\ncar.vehicleCode=");
            for (int i = 0, size = Car.VehicleCodeLength; i < size; i++)
            {
                sb.Append((char) car.GetVehicleCode(i));
            }

            OptionalExtras extras = car.Extras;
            sb.Append("\ncar.extras.cruiseControl=").Append((extras & OptionalExtras.CruiseControl) == OptionalExtras.CruiseControl);
            sb.Append("\ncar.extras.sportsPack=").Append((extras & OptionalExtras.SportsPack) == OptionalExtras.SportsPack);
            sb.Append("\ncar.extras.sunRoof=").Append((extras & OptionalExtras.SunRoof) == OptionalExtras.SunRoof);

            Engine engine = car.Engine;
            sb.Append("\ncar.engine.capacity=").Append(engine.Capacity);
            sb.Append("\ncar.engine.numCylinders=").Append(engine.NumCylinders);
            sb.Append("\ncar.engine.maxRpm=").Append(engine.MaxRpm);
            sb.Append("\ncar.engine.manufacturerCode=");
            for (int i = 0, size = Engine.ManufacturerCodeLength; i < size; i++)
            {
                sb.Append((char) engine.GetManufacturerCode(i));
            }

            int length = engine.GetFuel(buffer, 0, buffer.Length);

            sb.Append("\ncar.engine.fuel=").Append(Encoding.ASCII.GetString(buffer, 0, length));

            var fuelFiguresGroup = car.FuelFigures;
            while (fuelFiguresGroup.HasNext)
            {
                var fuelFigures = fuelFiguresGroup.Next();
                sb.Append("\ncar.fuelFigures.speed=").Append(fuelFigures.Speed);
                sb.Append("\ncar.fuelFigures.mpg=").Append(fuelFigures.Mpg);
            }

            var performanceFiguresGroup = car.PerformanceFigures;
            while (performanceFiguresGroup.HasNext)
            {
                var performanceFigures = performanceFiguresGroup.Next();
                sb.Append("\ncar.performanceFigures.octaneRating=").Append(performanceFigures.OctaneRating);

                var accelerationGroup = performanceFigures.Acceleration;
                while (accelerationGroup.HasNext)
                {
                    var acceleration = accelerationGroup.Next();
                    sb.Append("\ncar.performanceFigures.acceleration.mph=").Append(acceleration.Mph);
                    sb.Append("\ncar.performanceFigures.acceleration.seconds=").Append(acceleration.Seconds);
                }
            }

            length = car.GetMake(buffer, 0, buffer.Length);
            sb.Append("\ncar.make=").Append(Encoding.GetEncoding(Car.MakeCharacterEncoding).GetString(buffer, 0, length));

            length = car.GetModel(buffer, 0, buffer.Length);
            sb.Append("\ncar.model=").Append(Encoding.GetEncoding(Car.ModelCharacterEncoding).GetString(buffer, 0, length));

            sb.Append("\ncar.size=").Append(car.Size);

            Console.WriteLine(sb.ToString());
        }
Ejemplo n.º 7
0
        private static int Encode(Car car, DirectBuffer directBuffer, int bufferOffset)
        {
            int srcOffset = 0;

            car.WrapForEncode(directBuffer, bufferOffset);
            car.SerialNumber = 1234;
            car.ModelYear = 2013;
            car.Available = BooleanType.TRUE;
            car.Code = Model.A;
            car.SetVehicleCode(_vehicleCode, srcOffset);

            for (int i = 0, size = Car.SomeNumbersLength; i < size; i++)
            {
                car.SetSomeNumbers(i, i);
            }

            car.Extras = OptionalExtras.CruiseControl | OptionalExtras.SunRoof;

            car.Engine.Capacity = 2000;
            car.Engine.NumCylinders = 4;
            car.Engine.SetManufacturerCode(_manufacturerCode, srcOffset);

            var fuelFigures = car.FuelFiguresCount(3);
            fuelFigures.Next();
            fuelFigures.Speed = 30;
            fuelFigures.Mpg = 35.9f;

            fuelFigures.Next();
            fuelFigures.Speed = 55;
            fuelFigures.Mpg = 49.0f;

            fuelFigures.Next();
            fuelFigures.Speed = 75;
            fuelFigures.Mpg = 40.0f;

            Car.PerformanceFiguresGroup perfFigures = car.PerformanceFiguresCount(2);
            perfFigures.Next();
            perfFigures.OctaneRating = 95;

            Car.PerformanceFiguresGroup.AccelerationGroup acceleration = perfFigures.AccelerationCount(3).Next();
            acceleration.Mph = 30;
            acceleration.Seconds = 4.0f;

            acceleration.Next();
            acceleration.Mph = 60;
            acceleration.Seconds = 7.5f;

            acceleration.Next();
            acceleration.Mph = 100;
            acceleration.Seconds = 12.2f;

            perfFigures.Next();
            perfFigures.OctaneRating = 99;
            acceleration = perfFigures.AccelerationCount(3).Next();

            acceleration.Mph = 30;
            acceleration.Seconds = 3.8f;

            acceleration.Next();
            acceleration.Mph = 60;
            acceleration.Seconds = 7.1f;

            acceleration.Next();
            acceleration.Mph = 100;
            acceleration.Seconds = 11.8f;

            car.SetMake(_make, srcOffset, _make.Length);
            car.SetMake(_model, srcOffset, _model.Length);

            return car.Size;
        }
        private static void Decode(Car car,
            DirectBuffer directBuffer,
            int bufferOffset,
            int actingBlockLength,
            int actingVersion)
        {
            var buffer = new byte[128];
            var sb = new StringBuilder();

            // position the car flyweight just after the header on the DirectBuffer
            car.WrapForDecode(directBuffer, bufferOffset, actingBlockLength, actingVersion);

            // decode the car properties on by one, directly from the buffer
            sb.Append("\ncar.templateId=").Append(Car.TemplateId);
            sb.Append("\ncar.serialNumber=").Append(car.SerialNumber);
            sb.Append("\ncar.modelYear=").Append(car.ModelYear);
            sb.Append("\ncar.available=").Append(car.Available);
            sb.Append("\ncar.code=").Append(car.Code);

            sb.Append("\ncar.someNumbers=");
            for (int i = 0, size = Car.SomeNumbersLength; i < size; i++)
            {
                sb.Append(car.GetSomeNumbers(i)).Append(", ");
            }

            sb.Append("\ncar.vehicleCode=");
            for (int i = 0, size = Car.VehicleCodeLength; i < size; i++)
            {
                sb.Append((char) car.GetVehicleCode(i));
            }

            OptionalExtras extras = car.Extras;
            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
            sb.Append("\ncar.extras.sportsPack=").Append((extras & OptionalExtras.SportsPack) == OptionalExtras.SportsPack);
            sb.Append("\ncar.extras.sunRoof=").Append((extras & OptionalExtras.SunRoof) == OptionalExtras.SunRoof);

            Engine engine = car.Engine;
            sb.Append("\ncar.engine.capacity=").Append(engine.Capacity);
            sb.Append("\ncar.engine.numCylinders=").Append(engine.NumCylinders);
            sb.Append("\ncar.engine.maxRpm=").Append(engine.MaxRpm);
            sb.Append("\ncar.engine.manufacturerCode=");
            for (int i = 0, size = Engine.ManufacturerCodeLength; i < size; i++)
            {
                sb.Append((char) engine.GetManufacturerCode(i));
            }

            int length = engine.GetFuel(buffer, 0, buffer.Length);

            sb.Append("\ncar.engine.fuel=").Append(Encoding.ASCII.GetString(buffer, 0, length)); // string requires a bit of work to decode

            var fuelFiguresGroup = car.FuelFigures; // decode a repeatable group (we will change the API to support foreach soon)
            while (fuelFiguresGroup.HasNext)
            {
                var fuelFigures = fuelFiguresGroup.Next();
                sb.Append("\ncar.fuelFigures.speed=").Append(fuelFigures.Speed);
                sb.Append("\ncar.fuelFigures.mpg=").Append(fuelFigures.Mpg);
            }

            // the nested group
            var performanceFiguresGroup = car.PerformanceFigures;
            while (performanceFiguresGroup.HasNext)
            {
                var performanceFigures = performanceFiguresGroup.Next();
                sb.Append("\ncar.performanceFigures.octaneRating=").Append(performanceFigures.OctaneRating);

                var accelerationGroup = performanceFigures.Acceleration;
                while (accelerationGroup.HasNext)
                {
                    var acceleration = accelerationGroup.Next();
                    sb.Append("\ncar.performanceFigures.acceleration.mph=").Append(acceleration.Mph);
                    sb.Append("\ncar.performanceFigures.acceleration.seconds=").Append(acceleration.Seconds);
                }
            }

            // variable length fields
            length = car.GetMake(buffer, 0, buffer.Length);
            sb.Append("\ncar.make=").Append(Encoding.GetEncoding(Car.MakeCharacterEncoding).GetString(buffer, 0, length));

            length = car.GetModel(buffer, 0, buffer.Length);
            sb.Append("\ncar.model=").Append(Encoding.GetEncoding(Car.ModelCharacterEncoding).GetString(buffer, 0, length));

            sb.Append("\ncar.size=").Append(car.Size);

            Console.WriteLine(sb.ToString());
        }
        private static int Encode(Car car, DirectBuffer directBuffer, int bufferOffset)
        {
            const int srcOffset = 0;

            // we position the car encoder on the direct buffer, at the correct offset (ie. just after the header)
            car.WrapForEncode(directBuffer, bufferOffset);
            car.SerialNumber = 1234; // we set the different fields, just as normal properties and they get written straight to the underlying byte buffer
            car.ModelYear = 2013;
            car.Available = BooleanType.TRUE; // enums are supports
            car.Code = Model.A;
            car.SetVehicleCode(_vehicleCode, srcOffset); // we set a constant string

            for (int i = 0, size = Car.SomeNumbersLength; i < size; i++)
            {
                car.SetSomeNumbers(i, i); // this property is defined as a constant length array of integers
            }

            car.Extras = OptionalExtras.CruiseControl | OptionalExtras.SunRoof; // bit set (flag enums in C#) are supported

            car.Engine.Capacity = 2000;
            car.Engine.NumCylinders = 4;
            car.Engine.SetManufacturerCode(_manufacturerCode, srcOffset);

            // we have written all the constant length fields, now we can write the repeatable groups

            var fuelFigures = 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.PerformanceFiguresGroup perfFigures = car.PerformanceFiguresCount(2); // demonstrates how to create a nested group
            perfFigures.Next();
            perfFigures.OctaneRating = 95;

            Car.PerformanceFiguresGroup.AccelerationGroup acceleration = perfFigures.AccelerationCount(3).Next(); // this group is going to be nested in the first element of the previous group
            acceleration.Mph = 30;
            acceleration.Seconds = 4.0f;

            acceleration.Next();
            acceleration.Mph = 60;
            acceleration.Seconds = 7.5f;

            acceleration.Next();
            acceleration.Mph = 100;
            acceleration.Seconds = 12.2f;

            perfFigures.Next();
            perfFigures.OctaneRating = 99;
            acceleration = perfFigures.AccelerationCount(3).Next();

            acceleration.Mph = 30;
            acceleration.Seconds = 3.8f;

            acceleration.Next();
            acceleration.Mph = 60;
            acceleration.Seconds = 7.1f;

            acceleration.Next();
            acceleration.Mph = 100;
            acceleration.Seconds = 11.8f;

            // once we have written all the repeatable groups we can write the variable length properties (you would use that for strings, byte[], etc)

            car.SetMake(_make, srcOffset, _make.Length);
            car.SetMake(_model, srcOffset, _model.Length);

            return car.Size;
        }