/// <summary> /// 元数据转换成第三方数据 /// </summary> /// <param name="metadataObject">元数据集合</param> /// <param name="id">属性对应key</param> /// <param name="data">属性对应byte数组</param> /// <param name="offsetStart">属性在数组中的偏移值</param> /// <param name="length">属性在byte数组中的长度</param> /// <exception cref="ArgumentNullException">参数不能为空</exception> public void DataProcessor(MetadataContainer metadataObject, byte id, byte[] data, int offsetStart, uint length) { if (metadataObject == null) { throw new ArgumentNullException("metadataObject"); } uint arrayLength = BitConverter.ToUInt32(data, offsetStart); ResourceBlock[] resourceBlocks = new ResourceBlock[arrayLength]; offsetStart += 4; for (int j = 0; j < arrayLength; j++) { uint resouceBlockLength = BitConverter.ToUInt32(data, offsetStart); if (resouceBlockLength == 0) { resourceBlocks[j] = null; offsetStart += 4; } else { resourceBlocks[j] = MetadataObjectEngine.GetObject(data, (uint)offsetStart, resouceBlockLength); offsetStart += (int)resouceBlockLength; } } metadataObject.SetAttribute(id, new ResourceBlockArrayStored(resourceBlocks)); }
/// <summary> /// 解析元数据 /// </summary> /// <param name="data">总BUFF长度</param> /// <param name="offset">可用偏移量</param> /// <param name="count">可用长度</param> /// <returns> /// 返回能否解析的一个标示 /// </returns> public override List <T> Parse <T>(byte[] data, int offset, int count) { int totalLength; List <T> messages = new List <T>(); try { while (count > 0) { totalLength = BitConverter.ToInt32(data, offset); if (totalLength > data.Length) { _tracing.Error("#Parse message failed, illegal total length. #length: " + totalLength); return(messages); } int markRangeCount = BitConverter.ToUInt16(data, offset + 4); int markRangeLength = markRangeCount * 5 + offset; int protocolId = data[markRangeLength + 6]; int serviceId = data[markRangeLength + 7]; int detailsId = data[markRangeLength + 8]; MetadataContainer message; try { message = MetadataObjectEngine.GetObject(data, (uint)offset, (uint)totalLength); if (message == null) { _tracing.Error( "#Parse message failed, parse result = null. #protocol id={0}, service id={1}, detalis id={2}: ", protocolId, serviceId, detailsId); return(messages); } } catch (System.Exception ex) { _tracing.Error(ex, "#Parse message failed."); continue; } finally { offset += totalLength; count -= totalLength; } messages.Add((T)(object)message); if (data.Length - offset < 4) { break; } } return(messages); } catch (System.Exception ex) { _tracing.Error(ex, "#Parse message failed."); return(messages); } }
/// <summary> /// 初始化普通类型元数据转第三方数据的处理器 /// </summary> /// <exception cref="ArgumentNullException">参数不能为空</exception> private static unsafe void InitializeDataProcessorDictionary() { DataActions.Add((byte)PropertyTypes.Boolean, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new BooleanValueStored(DefaultValue.Boolean)); } else { metadataObject.SetAttribute(id, new BooleanValueStored(BitConverter.ToBoolean(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Byte, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new ByteValueStored(DefaultValue.Byte)); } else { metadataObject.SetAttribute(id, new ByteValueStored(byteData[offsetStart])); } }); DataActions.Add((byte)PropertyTypes.SByte, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new SByteValueStored(DefaultValue.SByte)); } else { fixed(byte *pByte = (&byteData[offsetStart])) metadataObject.SetAttribute(id, new SByteValueStored(*(sbyte *)pByte)); } }); DataActions.Add((byte)PropertyTypes.Char, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new CharValueStored(DefaultValue.Char)); } else { metadataObject.SetAttribute(id, new CharValueStored(BitConverter.ToChar(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Int16, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new Int16ValueStored(DefaultValue.Int16)); } else { metadataObject.SetAttribute(id, new Int16ValueStored(BitConverter.ToInt16(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.UInt16, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new UInt16ValueStored(DefaultValue.UInt16)); } else { metadataObject.SetAttribute(id, new UInt16ValueStored(BitConverter.ToUInt16(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Int32, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new Int32ValueStored(DefaultValue.Int32)); } else { metadataObject.SetAttribute(id, new Int32ValueStored(BitConverter.ToInt32(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.UInt32, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new UInt32ValueStored(DefaultValue.UInt32)); } else { metadataObject.SetAttribute(id, new UInt32ValueStored(BitConverter.ToUInt32(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Int64, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new Int64ValueStored(DefaultValue.Int64)); } else { metadataObject.SetAttribute(id, new Int64ValueStored(BitConverter.ToInt64(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.UInt64, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new UInt64ValueStored(DefaultValue.UInt64)); } else { metadataObject.SetAttribute(id, new UInt64ValueStored(BitConverter.ToUInt64(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Float, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new FloatValueStored(DefaultValue.Float)); } else { metadataObject.SetAttribute(id, new FloatValueStored(BitConverter.ToSingle(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Double, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new DoubleValueStored(DefaultValue.Double)); } else { metadataObject.SetAttribute(id, new DoubleValueStored(BitConverter.ToDouble(byteData, offsetStart))); } }); DataActions.Add((byte)PropertyTypes.Decimal, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new DecimalValueStored(DefaultValue.Decimal)); } else { fixed(byte *pByte = (&byteData[offsetStart])) metadataObject.SetAttribute(id, new DecimalValueStored(*(decimal *)pByte)); } }); DataActions.Add((byte)PropertyTypes.String, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new StringValueStored(null)); } else { metadataObject.SetAttribute(id, new StringValueStored(Encoding.UTF8.GetString(byteData, offsetStart, (int)offsetLength))); } }); DataActions.Add((byte)PropertyTypes.DateTime, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new DateTimeValueStored(DefaultValue.DateTime)); } else { metadataObject.SetAttribute(id, new DateTimeValueStored(new DateTime(BitConverter.ToInt64(byteData, offsetStart)))); } }); DataActions.Add((byte)PropertyTypes.Guid, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new GuidValueStored(DefaultValue.Guid)); } else { fixed(byte *pByte = (&byteData[offsetStart])) metadataObject.SetAttribute(id, new GuidValueStored(*(Guid *)pByte)); } }); DataActions.Add((byte)PropertyTypes.IPEndPoint, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { fixed(byte *pData = (&byteData[offsetStart])) { IPEndPoint iep = new IPEndPoint(new IPAddress(BitConverter.GetBytes(*(int *)pData)), *(int *)(pData + 4)); metadataObject.SetAttribute(id, new IPEndPointValueStored(iep)); } }); DataActions.Add((byte)PropertyTypes.IntPtr, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new IntPtrValueStored(DefaultValue.IntPtr)); } else { metadataObject.SetAttribute(id, new IntPtrValueStored(new IntPtr(BitConverter.ToInt32(byteData, offsetStart)))); } }); DataActions.Add((byte)PropertyTypes.ResourceBlock, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { metadataObject.SetAttribute(id, new ResourceBlockStored(MetadataObjectEngine.GetObject(byteData, (uint)offsetStart, offsetLength))); }); DataActions.Add((byte)PropertyTypes.TimeSpan, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { if (offsetLength == 0) { metadataObject.SetAttribute(id, new TimeSpanValueStored(DefaultValue.TimeSpan)); } else { fixed(byte *pData = (&byteData[offsetStart])) metadataObject.SetAttribute(id, new TimeSpanValueStored(new TimeSpan(*(long *)pData))); } }); DataActions.Add((byte)PropertyTypes.BitFlag, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { metadataObject.SetAttribute(id, new BitFlagValueStored(new BitFlag(byteData[offsetStart]))); }); DataActions.Add((byte)PropertyTypes.Blob, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { metadataObject.SetAttribute(id, new BlobValueStored(new Blob(byteData, offsetStart, (int)offsetLength))); }); DataActions.Add((byte)PropertyTypes.Null, delegate(ResourceBlock metadataObject, byte id, byte[] byteData, int offsetStart, uint offsetLength) { metadataObject.SetAttribute(id, new NullValueStored()); }); }