Ejemplo n.º 1
0
        /// <summary>
        /// Read a <see cref="Request.EvaluationResult"/> request
        /// </summary>
        /// <param name="from">Origin</param>
        /// <param name="expression">Expression</param>
        /// <param name="result">Evaluation result</param>
        /// <returns>Number of bytes read</returns>
        public static int ReadEvaluationResult(ReadOnlySpan <byte> from, out string expression, out object result)
        {
            EvaluationResultHeader header = MemoryMarshal.Read <EvaluationResultHeader>(from);
            int bytesRead = Marshal.SizeOf <EvaluationResultHeader>();

            // Read expression
            ReadOnlySpan <byte> unicodeExpression = from.Slice(bytesRead, header.ExpressionLength);

            expression = Encoding.UTF8.GetString(unicodeExpression);
            bytesRead += header.ExpressionLength;

            // Read value
            switch (header.Type)
            {
            case DataType.Int:
                result = header.IntValue;
                break;

            case DataType.UInt:
                result = header.UIntValue;
                break;

            case DataType.Float:
                result = header.FloatValue;
                break;

            case DataType.IntArray:
                int[] intArray = new int[header.IntValue];
                for (int i = 0; i < header.IntValue; i++)
                {
                    intArray[i] = MemoryMarshal.Read <int>(from[bytesRead..]);
Ejemplo n.º 2
0
        /// <summary>
        /// Read a <see cref="Request.EvaluationResult"/> request
        /// </summary>
        /// <param name="from">Origin</param>
        /// <param name="expression">Expression</param>
        /// <param name="result">Evaluation result</param>
        /// <returns>Number of bytes read</returns>
        public static int ReadEvaluationResult(ReadOnlySpan <byte> from, out string expression, out object result)
        {
            EvaluationResultHeader header = MemoryMarshal.Cast <byte, EvaluationResultHeader>(from)[0];
            int bytesRead = Marshal.SizeOf(header);

            // Read expression
            ReadOnlySpan <byte> unicodeExpression = from.Slice(bytesRead, header.ExpressionLength);

            expression = Encoding.UTF8.GetString(unicodeExpression);
            bytesRead += header.ExpressionLength;

            // Read value
            switch (header.Type)
            {
            case DataType.Int:
                result = header.IntValue;
                break;

            case DataType.UInt:
                result = header.UIntValue;
                break;

            case DataType.Float:
                result = header.FloatValue;
                break;

            case DataType.IntArray:
                int[] intArray = new int[header.IntValue];
                for (int i = 0; i < header.IntValue; i++)
                {
                    intArray[i] = MemoryMarshal.Read <int>(from.Slice(bytesRead));
                    bytesRead  += Marshal.SizeOf <int>();
                }
                result = intArray;
                break;

            case DataType.UIntArray:
                uint[] uintArray = new uint[header.IntValue];
                for (int i = 0; i < header.IntValue; i++)
                {
                    uintArray[i] = MemoryMarshal.Read <uint>(from.Slice(bytesRead));
                    bytesRead   += Marshal.SizeOf <uint>();
                }
                result = uintArray;
                break;

            case DataType.FloatArray:
                float[] floatArray = new float[header.IntValue];
                for (int i = 0; i < header.IntValue; i++)
                {
                    floatArray[i] = MemoryMarshal.Read <float>(from.Slice(bytesRead));
                    bytesRead    += Marshal.SizeOf <float>();
                }
                result = floatArray;
                break;

            case DataType.String:
                result     = Encoding.UTF8.GetString(from.Slice(bytesRead, header.IntValue));
                bytesRead += header.IntValue;
                break;

            case DataType.DriverId:
                result = new DriverId(header.UIntValue);
                break;

            case DataType.DriverIdArray:
                DriverId[] driverIdArray = new DriverId[header.IntValue];
                for (int i = 0; i < header.IntValue; i++)
                {
                    driverIdArray[i] = new DriverId(MemoryMarshal.Read <uint>(from.Slice(bytesRead)));
                    bytesRead       += Marshal.SizeOf <uint>();
                }
                result = driverIdArray;
                break;

            case DataType.Bool:
                result = Convert.ToBoolean(header.IntValue);
                break;

            case DataType.BoolArray:
                bool[] boolArray = new bool[header.IntValue];
                for (int i = 0; i < header.IntValue; i++)
                {
                    boolArray[i] = Convert.ToBoolean(MemoryMarshal.Read <byte>(from.Slice(bytesRead)));
                    bytesRead   += Marshal.SizeOf <byte>();
                }
                result = boolArray;
                break;

            case DataType.Expression:
                string errorMessage = Encoding.UTF8.GetString(from.Slice(bytesRead, header.IntValue));
                result = new CodeParserException(errorMessage);
                break;

            default:
                result = null;
                break;
            }

            return(AddPadding(bytesRead));
        }