Ejemplo n.º 1
0
        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);
        }
        public int Encode(Car car, DirectBuffer directBuffer, int bufferOffset)
        {
            _car.WrapForEncodeAndApplyHeader(directBuffer, bufferOffset, _messageHeader);

            const int srcOffset = 0;

            car.SerialNumber = 1234;
            car.ModelYear    = 2013;
            car.Available    = BooleanType.T;
            car.Code         = Baseline.Model.A;
            car.SetVehicleCode(VehicleCodeEncoding.GetBytes("CODE12"), 0);

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

            car.Extras              = OptionalExtras.CruiseControl | OptionalExtras.SportsPack;
            car.Engine.Capacity     = 2000;
            car.Engine.NumCylinders = 4;
            car.Engine.SetManufacturerCode(ManufacturerCodeEncoding.GetBytes("123"), 0);
            car.Engine.Efficiency         = 35;
            car.Engine.BoosterEnabled     = BooleanType.T;
            car.Engine.Booster.BoostType  = BoostType.NITROUS;
            car.Engine.Booster.HorsePower = 200;

            var fuelFigures = car.FuelFiguresCount(3);

            fuelFigures.Next();
            fuelFigures.Speed = 30;
            fuelFigures.Mpg   = 35.9f;
            fuelFigures.SetUsageDescription(UsageDescriptionEncoding.GetBytes("Urban Cycle"));

            fuelFigures.Next();
            fuelFigures.Speed = 55;
            fuelFigures.Mpg   = 49.0f;
            fuelFigures.SetUsageDescription(UsageDescriptionEncoding.GetBytes("Combined Cycle"));

            fuelFigures.Next();
            fuelFigures.Speed = 75;
            fuelFigures.Mpg   = 40.0f;
            fuelFigures.SetUsageDescription(UsageDescriptionEncoding.GetBytes("Highway Cycle"));

            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;

            byte[] Manufacturer   = ManufacturerEncoding.GetBytes("Honda");
            byte[] Model          = ModelEncoding.GetBytes("Civic VTi");
            byte[] ActivationCode = ActivationCodeEncoding.GetBytes("code");

            car.SetManufacturer(Manufacturer, srcOffset, Manufacturer.Length);
            car.SetModel(Model, srcOffset, Model.Length);
            car.SetActivationCode(ActivationCode, srcOffset, ActivationCode.Length);

            return(car.Size);
        }