Пример #1
0
        /// <summary>
        /// Reads an instance of a type from the buffer,
        /// </summary>
        private int ReadType(Context context, out ComplexValue complexValue)
        {
            complexValue = null;

            TypeDescription type = context.Type;

            byte[] buffer     = context.Buffer;
            int    startIndex = context.Index;

            byte bitOffset = 0;

            ArrayList fieldValues = new ArrayList();

            for (int ii = 0; ii < type.Field.Length; ii++)
            {
                FieldType field = type.Field[ii];

                ComplexValue fieldValue = new ComplexValue();

                fieldValue.Name  = (field.Name != null && field.Name.Length != 0)?field.Name:"[" + ii.ToString() + "]";
                fieldValue.Type  = null;
                fieldValue.Value = null;

                // check if additional padding is required after the end of a bit field.
                if (bitOffset != 0)
                {
                    if (field.GetType() != typeof(BitString))
                    {
                        context.Index++;
                        bitOffset = 0;
                    }
                }

                int bytesRead = 0;

                if (IsArrayField(field))
                {
                    bytesRead = ReadArrayField(context, field, ii, fieldValues, out fieldValue.Value);
                }
                else if (field.GetType() == typeof(TypeReference))
                {
                    object typeValue = null;

                    bytesRead = ReadField(context, (TypeReference)field, out typeValue);

                    // assign a name appropriate for the current context.
                    fieldValue.Name  = field.Name;
                    fieldValue.Type  = ((ComplexValue)typeValue).Type;
                    fieldValue.Value = ((ComplexValue)typeValue).Value;
                }
                else
                {
                    bytesRead = ReadField(context, field, ii, fieldValues, out fieldValue.Value, ref bitOffset);
                }

                if (bytesRead == 0 && bitOffset == 0)
                {
                    throw new InvalidDataInBufferException("Could not read field '" + field.Name + "' in type '" + type.TypeID + "'.");
                }

                context.Index += bytesRead;

                // assign a value for field type.
                if (fieldValue.Type == null)
                {
                    fieldValue.Type = Opc.Convert.ToString(fieldValue.Value.GetType());
                }

                fieldValues.Add(fieldValue);
            }

            // skip padding bits at the end of a type.
            if (bitOffset != 0)
            {
                context.Index++;
            }

            complexValue = new ComplexValue();

            complexValue.Name  = type.TypeID;
            complexValue.Type  = type.TypeID;
            complexValue.Value = (ComplexValue[])fieldValues.ToArray(typeof(ComplexValue));

            return(context.Index - startIndex);
        }
Пример #2
0
        /// <summary>
        /// Writes an instance of a type to the buffer.
        /// </summary>
        private int WriteType(Context context, ComplexValue namedValue)
        {
            TypeDescription type = context.Type;

            byte[] buffer     = context.Buffer;
            int    startIndex = context.Index;

            ComplexValue[] fieldValues = null;

            if (namedValue.Value == null || namedValue.Value.GetType() != typeof(ComplexValue[]))
            {
                throw new InvalidDataToWriteException("Type instance does not contain field values.");
            }

            fieldValues = (ComplexValue[])namedValue.Value;

            if (fieldValues.Length != type.Field.Length)
            {
                throw new InvalidDataToWriteException("Type instance does not contain the correct number of fields.");
            }

            byte bitOffset = 0;

            for (int ii = 0; ii < type.Field.Length; ii++)
            {
                FieldType    field      = type.Field[ii];
                ComplexValue fieldValue = fieldValues[ii];

                if (bitOffset != 0)
                {
                    if (field.GetType() != typeof(BitString))
                    {
                        context.Index++;
                        bitOffset = 0;
                    }
                }

                int bytesWritten = 0;

                if (IsArrayField(field))
                {
                    bytesWritten = WriteArrayField(context, field, ii, fieldValues, fieldValue.Value);
                }
                else if (field.GetType() == typeof(TypeReference))
                {
                    bytesWritten = WriteField(context, (TypeReference)field, fieldValue);
                }
                else
                {
                    bytesWritten = WriteField(context, field, ii, fieldValues, fieldValue.Value, ref bitOffset);
                }

                if (bytesWritten == 0 && bitOffset == 0)
                {
                    throw new InvalidDataToWriteException("Could not write field '" + field.Name + "' in type '" + type.TypeID + "'.");
                }

                context.Index += bytesWritten;
            }

            if (bitOffset != 0)
            {
                context.Index++;
            }

            return(context.Index - startIndex);
        }
Пример #3
0
        // Token: 0x06000369 RID: 873 RVA: 0x000096B4 File Offset: 0x000086B4
        private int ReadType(Context context, out ComplexValue complexValue)
        {
            complexValue = null;
            TypeDescription type      = context.Type;
            int             index     = context.Index;
            byte            b         = 0;
            ArrayList       arrayList = new ArrayList();

            for (int i = 0; i < type.Field.Length; i++)
            {
                FieldType    fieldType     = type.Field[i];
                ComplexValue complexValue2 = new ComplexValue();
                complexValue2.Name  = ((fieldType.Name != null && fieldType.Name.Length != 0) ? fieldType.Name : ("[" + i.ToString() + "]"));
                complexValue2.Type  = null;
                complexValue2.Value = null;
                if (b != 0 && fieldType.GetType() != typeof(BitString))
                {
                    context.Index++;
                    b = 0;
                }
                int num;
                if (base.IsArrayField(fieldType))
                {
                    num = this.ReadArrayField(context, fieldType, i, arrayList, out complexValue2.Value);
                }
                else if (fieldType.GetType() == typeof(TypeReference))
                {
                    object obj = null;
                    num = this.ReadField(context, (TypeReference)fieldType, out obj);
                    complexValue2.Name  = fieldType.Name;
                    complexValue2.Type  = ((ComplexValue)obj).Type;
                    complexValue2.Value = ((ComplexValue)obj).Value;
                }
                else
                {
                    num = this.ReadField(context, fieldType, i, arrayList, out complexValue2.Value, ref b);
                }
                if (num == 0 && b == 0)
                {
                    throw new InvalidDataInBufferException(string.Concat(new string[]
                    {
                        "Could not read field '",
                        fieldType.Name,
                        "' in type '",
                        type.TypeID,
                        "'."
                    }));
                }
                context.Index += num;
                if (complexValue2.Type == null)
                {
                    complexValue2.Type = Convert.ToString(complexValue2.Value.GetType());
                }
                arrayList.Add(complexValue2);
            }
            if (b != 0)
            {
                context.Index++;
            }
            complexValue       = new ComplexValue();
            complexValue.Name  = type.TypeID;
            complexValue.Type  = type.TypeID;
            complexValue.Value = (ComplexValue[])arrayList.ToArray(typeof(ComplexValue));
            return(context.Index - index);
        }