/// <summary> /// Optimized deserialization of a LogMessagePacket based on the current packet definition /// </summary> public new void ReadFieldsFast(IFieldReader reader) { base.ReadFieldsFast(reader); m_ID = reader.ReadGuid(); m_Caption = reader.ReadString(); m_Severity = (LogMessageSeverity)reader.ReadInt32(); m_LogSystem = reader.ReadString(); m_CategoryName = reader.ReadString(); m_UserName = reader.ReadString(); m_Description = reader.ReadString(); m_Details = reader.ReadString(); m_ThreadIndex = reader.ReadInt32(); m_ThreadId = reader.ReadInt32(); m_MethodName = reader.ReadString(); m_ClassName = reader.ReadString(); m_FileName = reader.ReadString(); m_LineNumber = reader.ReadInt32(); string[] typeNames = reader.ReadStringArray(); string[] messages = reader.ReadStringArray(); string[] sources = reader.ReadStringArray(); string[] stackTraces = reader.ReadStringArray(); Guid applicationUserId = reader.ReadGuid(); if (m_ThreadIndex == 0) { m_ThreadIndex = m_ThreadId; // Zero isn't legal, so it must not have had it. Fall back to ThreadId. } if (applicationUserId != Guid.Empty) { ApplicationUser applicationUser; m_SessionPacketCache.Users.TryGetValue(applicationUserId, out applicationUser); UserPacket = applicationUser.Packet; } //these are supposed to be parallel arrays - assume they're all the same size. int arrayLength = typeNames.GetLength(0); var exceptions = new IExceptionInfo[arrayLength]; // local holder to build it up IExceptionInfo lastException = null; for (int i = 0; i < arrayLength; i++) { IExceptionInfo exception = new ExceptionInfoPacket() { TypeName = typeNames[i], Message = messages[i], Source = sources[i], StackTrace = stackTraces[i], }; exceptions[i] = exception; if (lastException != null) { ((ExceptionInfoPacket)lastException).InnerException = exception; //we are the inner exception to our parent. } lastException = exception; } m_ExceptionChain = exceptions; // Set the rehydrated ExceptionInfo[] array property }
private PacketDefinition(IFieldReader reader) { m_TypeName = reader.ReadString(); m_Version = reader.ReadInt32(); int fieldCount = reader.ReadInt32(); for (int i = 0; i < fieldCount; i++) { string fieldName = reader.ReadString(); FieldType fieldType = (FieldType)reader.ReadInt32(); m_Fields.Add(new FieldDefinition(fieldName, fieldType)); } // Handle the possiblity that a Packet aggregates lower level packets int subPacketCount = reader.ReadInt32(); m_SubPackets = new List <PacketDefinition>(); for (int i = 0; i < subPacketCount; i++) { // We need to call the static ReadPacketDefinition(reader) in order to // read and process the cacheable, version, and dynamic name fields which // also exist for each subPacket definition. Fixed as part of Case #165 PacketDefinition subPacket = ReadPacketDefinition(reader); m_SubPackets.Add(subPacket); } }
/// <summary> /// Returns a PacketDefinition from the stream (including nested PacketDefinition /// objects for cases in which an IPacket is subclassed and has serialized state /// at multiple levels). /// </summary> /// <param name="reader">Stream to read data from</param> /// <returns>PacketDefinition (including nested definitions for subclassed packets)</returns> public static PacketDefinition ReadPacketDefinition(IFieldReader reader) { bool cachedPacket = reader.ReadBool(); int nestingDepth = reader.ReadInt32(); if (nestingDepth < 1) { throw new GibraltarException(string.Format(CultureInfo.InvariantCulture, "While reading the definition of the next packet, the number of types in the definition was read as {0} which is less than 1.", nestingDepth)); } string dynamicTypeName = reader.ReadString(); PacketDefinition[] definitions = new PacketDefinition[nestingDepth]; for (int i = 0; i < nestingDepth; i++) { definitions[i] = new PacketDefinition(reader); if (i > 0) { definitions[i].m_BasePacket = definitions[i - 1]; } } PacketDefinition topLevelDefinition = definitions[nestingDepth - 1]; topLevelDefinition.m_IsCachable = cachedPacket; topLevelDefinition.m_DynamicTypeName = dynamicTypeName; return(topLevelDefinition); }
/// <summary> /// Reads an array of type T from the stream. /// </summary> /// <param name="reader">Data stream to read</param> /// <returns>Array of type T</returns> public T[] Read(IFieldReader reader) { // The array always starts with its length. Since the length can't be // negative we pass it unsigned (which gives slightly better compression) int length = (int)reader.ReadUInt32(); T[] array = new T[length]; // The array values are stored as a seuqnece of runs. Each run represents // either a sequence of repeating values or a sequence of unique values. int index = 0; while (index < length) { int runLength = reader.ReadInt32(); if (runLength > 0) { // a positive runLength indicates a run of repeating values. // So, we only need to store the actual value once. T value = ReadValue(reader); for (int i = 0; i < runLength; i++) { array[index++] = value; } } else // runLength < 0 { // a negative runLength indicates a run of unique values for (int i = runLength; i < 0; i++) { // in this case, we need to store each value T value = ReadValue(reader); array[index++] = value; } } } return(array); }
/// <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)); } } }