Beispiel #1
0
        /// <summary>
        /// Read any packet based solely on its PacketDefinition
        /// </summary>
        /// <param name="definition">PacketDefinition describing the next packet in the stream</param>
        /// <param name="reader">Data stream to be read</param>
        public GenericPacket(PacketDefinition definition, IFieldReader reader)
        {
            if (definition.BasePacket != null)
            {
                m_BasePacket = new GenericPacket(definition.BasePacket, reader);
            }

            m_Definition  = definition;
            m_FieldValues = new object[definition.Fields.Count];

            for (int index = 0; index < definition.Fields.Count; index++)
            {
                switch (definition.Fields[index].FieldType)
                {
                case FieldType.Bool:
                    m_FieldValues[index] = reader.ReadBool();
                    break;

                case FieldType.BoolArray:
                    m_FieldValues[index] = reader.ReadBoolArray();
                    break;

                case FieldType.String:
                    m_FieldValues[index] = reader.ReadString();
                    break;

                case FieldType.StringArray:
                    m_FieldValues[index] = reader.ReadStringArray();
                    break;

                case FieldType.Int32:
                    m_FieldValues[index] = reader.ReadInt32();
                    break;

                case FieldType.Int32Array:
                    m_FieldValues[index] = reader.ReadInt32Array();
                    break;

                case FieldType.Int64:
                    m_FieldValues[index] = reader.ReadInt64();
                    break;

                case FieldType.Int64Array:
                    m_FieldValues[index] = reader.ReadInt64Array();
                    break;

                case FieldType.UInt32:
                    m_FieldValues[index] = reader.ReadUInt32();
                    break;

                case FieldType.UInt32Array:
                    m_FieldValues[index] = reader.ReadUInt32Array();
                    break;

                case FieldType.UInt64:
                    m_FieldValues[index] = reader.ReadUInt64();
                    break;

                case FieldType.UInt64Array:
                    m_FieldValues[index] = reader.ReadUInt64Array();
                    break;

                case FieldType.Double:
                    m_FieldValues[index] = reader.ReadDouble();
                    break;

                case FieldType.DoubleArray:
                    m_FieldValues[index] = reader.ReadDoubleArray();
                    break;

                case FieldType.TimeSpan:
                    m_FieldValues[index] = reader.ReadTimeSpan();
                    break;

                case FieldType.TimeSpanArray:
                    m_FieldValues[index] = reader.ReadTimeSpanArray();
                    break;

                case FieldType.DateTime:
                    m_FieldValues[index] = reader.ReadDateTime();
                    break;

                case FieldType.DateTimeArray:
                    m_FieldValues[index] = reader.ReadDateTimeArray();
                    break;

                case FieldType.Guid:
                    m_FieldValues[index] = reader.ReadGuid();
                    break;

                case FieldType.GuidArray:
                    m_FieldValues[index] = reader.ReadGuidArray();
                    break;

                case FieldType.DateTimeOffset:
                    m_FieldValues[index] = reader.ReadDateTimeOffset();
                    break;

                case FieldType.DateTimeOffsetArray:
                    m_FieldValues[index] = reader.ReadDateTimeOffsetArray();
                    break;

                default:
#if DEBUG
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }
#endif
                    throw new InvalidOperationException(string.Format("The field type {0} is unknown so we can't deserialize the packet ", definition.Fields[index].FieldType));
                }
            }
        }