Wrap() public method

public Wrap ( Adaptive.SimpleBinaryEncoding.DirectBuffer buffer, int offset, int actingVersion ) : void
buffer Adaptive.SimpleBinaryEncoding.DirectBuffer
offset int
actingVersion int
return void
Example #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);
    }