private static Type ReadDictionaryType(byte[] data, ref int offset, out ReadDelegate keyReadDelegate, out ReadDelegate valueReadDelegate) { Type clrArrayType; Type dictArrayType; if (offset >= (data.Length - 2)) { keyReadDelegate = null; valueReadDelegate = null; return(null); } GpTypeV17 gpType = (GpTypeV17)data[offset]; GpTypeV17 ev2 = (GpTypeV17)data[offset + 1]; offset += 2; if (gpType == GpTypeV17.Unknown) { clrArrayType = typeof(object); keyReadDelegate = new ReadDelegate(GpBinaryByteReaderV17.Read); } else { clrArrayType = GetClrArrayType(gpType); keyReadDelegate = readDelegates[(int)gpType]; } switch (ev2) { case GpTypeV17.Unknown: dictArrayType = typeof(object); valueReadDelegate = new ReadDelegate(GpBinaryByteReaderV17.Read); break; case GpTypeV17.Dictionary: dictArrayType = ReadDictionaryType(data, ref offset); valueReadDelegate = new ReadDelegate(GpBinaryByteReaderV17.ReadDictionary); break; case GpTypeV17.Array: dictArrayType = GetDictArrayType(data, ref offset); valueReadDelegate = new ReadDelegate(GpBinaryByteReaderV17.Read); break; case GpTypeV17.ObjectArray: dictArrayType = typeof(object[]); valueReadDelegate = new ReadDelegate(GpBinaryByteReaderV17.ReadObjectArray); break; default: dictArrayType = GetClrArrayType(ev2); valueReadDelegate = readDelegates[(int)ev2]; break; } return(typeof(Dictionary <,>).MakeGenericType(new Type[] { clrArrayType, dictArrayType })); }
public static Type GetClrArrayType(GpTypeV17 gpType) { switch (gpType) { case GpTypeV17.CompressedInt: case GpTypeV17.Integer: return(typeof(int)); case GpTypeV17.CompressedLong: case GpTypeV17.Long: return(typeof(long)); case GpTypeV17.Byte: return(typeof(byte)); case GpTypeV17.Double: return(typeof(double)); case GpTypeV17.EventData: return(typeof(EventData)); case GpTypeV17.Float: return(typeof(float)); case GpTypeV17.Hashtable: return(typeof(Hashtable)); case GpTypeV17.Short: return(typeof(short)); case GpTypeV17.Boolean: return(typeof(bool)); case GpTypeV17.OperationResponse: return(typeof(OperationResponse)); case GpTypeV17.OperationRequest: return(typeof(OperationRequest)); case GpTypeV17.String: return(typeof(string)); case GpTypeV17.Vector: return(typeof(ArrayList)); case GpTypeV17.ByteArray: return(typeof(byte[])); case GpTypeV17.ShortArray: return(typeof(short[])); } return(null); }
private static bool Read(byte[] data, ref int offset, GpTypeV17 protocolType, out object result) { ReadDelegate delegate2 = readDelegates[(int)protocolType]; if (delegate2 != null) { return(delegate2(data, ref offset, out result)); } if (log.IsDebugEnabled) { log.DebugFormat("Unknown GpType {0} at position {1} at {2}", new object[] { protocolType, (int)offset, new StackTrace(true) }); } result = null; return(false); }
public static bool Read(byte[] data, ref int offset, out object result) { if (offset >= data.Length) { if (log.IsDebugEnabled) { log.DebugFormat("Invalid offset for data specified: offset={0}, length={1}", new object[] { (int)offset, data.Length }); } result = null; return(false); } GpTypeV17 protocolType = (GpTypeV17)data[offset]; offset++; return(Read(data, ref offset, protocolType, out result)); }
public static void Write(Stream stream, object value) { if (value == null) { stream.WriteByte(0x2a); } else { Type type = value.GetType(); if (type.IsArray) { WriteArray(stream, type, value); } else { GpTypeV17 gpType = GetGpType(type); WriteDelegate delegate2 = writeMethods[(int)gpType]; if (delegate2 != null) { stream.WriteByte((byte)gpType); delegate2(stream, value); } else { CustomTypeInfo info; if (CustomTypeCache.TryGet(type, out info)) { WriteCustomType(stream, info, value, true); } else if (type == typeof(RawCustomValue)) { WriteCustomType(stream, (RawCustomValue)value, true); } else { if (type != typeof(RawCustomArray)) { throw new InvalidDataException("cannot serialize(): " + type); } WriteCustomTypeArray(stream, (RawCustomArray)value, true); } } } } }
private static bool WriteArrayHeader(Stream stream, Type type) { Type elementType = type.GetElementType(); while (elementType.IsArray) { stream.WriteByte(0x79); elementType = elementType.GetElementType(); } GpTypeV17 gpType = GetGpType(elementType); if (gpType == GpTypeV17.Unknown) { return(false); } stream.WriteByte((byte)(gpType | ((GpTypeV17)0x80))); return(true); }
private static Type GetDictArrayType(byte[] data, ref int offset) { GpTypeV17 gpType = (GpTypeV17)data[offset++]; int num = 0; while (gpType == GpTypeV17.Array) { num++; gpType = (GpTypeV17)data[offset++]; } Type type2 = GetClrArrayType(gpType).MakeArrayType(); for (int i = 0; i < num; i++) { type2 = type2.MakeArrayType(); } return(type2); }
private static Type ReadDictionaryType(byte[] data, ref int offset) { Type clrArrayType; Type dictArrayType; if (offset > (data.Length - 2)) { return(null); } GpTypeV17 gpType = (GpTypeV17)data[offset++]; GpTypeV17 ev2 = (GpTypeV17)data[offset++]; if (gpType == GpTypeV17.Unknown) { clrArrayType = typeof(object); } else { clrArrayType = GetClrArrayType(gpType); } switch (ev2) { case GpTypeV17.Unknown: dictArrayType = typeof(object); break; case GpTypeV17.Dictionary: dictArrayType = ReadDictionaryType(data, ref offset); break; case GpTypeV17.Array: dictArrayType = GetDictArrayType(data, ref offset); break; default: dictArrayType = GetClrArrayType(ev2); break; } return(typeof(Dictionary <,>).MakeGenericType(new Type[] { clrArrayType, dictArrayType })); }
private static void GetDictionaryDelegates(Type type, out WriteDelegate keyWriteDelegate, out WriteDelegate valueWriteDelegate) { Type[] genericArguments = type.GetGenericArguments(); if (genericArguments[0] == typeof(object)) { keyWriteDelegate = new WriteDelegate(GpBinaryByteWriterV17.Write); } else { if (!genericArguments[0].IsPrimitive && (genericArguments[0] != typeof(string))) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with key type: " + genericArguments[0]); } GpTypeV17 gpType = GetGpType(genericArguments[0]); keyWriteDelegate = writeMethods[(int)gpType]; if (keyWriteDelegate == null) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with key type: " + genericArguments[0]); } } if (genericArguments[1] == typeof(object)) { valueWriteDelegate = new WriteDelegate(GpBinaryByteWriterV17.Write); } else { GpTypeV17 ev2 = GetGpType(genericArguments[1]); valueWriteDelegate = writeMethods[(int)ev2]; if (valueWriteDelegate == null) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with value type: " + genericArguments[1]); } if (ev2 == GpTypeV17.Dictionary) { valueWriteDelegate = new WriteDelegate(GpBinaryByteWriterV17.WriteDictionary2); } } }
private static void WriteDictionaryHeader(Stream stream, Type type, out WriteDelegate keyWriteDelegate, out WriteDelegate valueWriteDelegate) { Type[] genericArguments = type.GetGenericArguments(); if (genericArguments[0] == typeof(object)) { stream.WriteByte(0); keyWriteDelegate = new WriteDelegate(GpBinaryByteWriterV17.Write); } else { if (!genericArguments[0].IsPrimitive && (genericArguments[0] != typeof(string))) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with key type: " + genericArguments[0]); } GpTypeV17 gpType = GetGpType(genericArguments[0]); keyWriteDelegate = writeMethods[(int)gpType]; if (keyWriteDelegate == null) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with key type: " + genericArguments[0]); } stream.WriteByte((byte)gpType); } if (genericArguments[1] == typeof(object)) { stream.WriteByte(0); valueWriteDelegate = new WriteDelegate(GpBinaryByteWriterV17.Write); } else if (genericArguments[1].IsArray) { if (!WriteArrayType(stream, genericArguments[1], out valueWriteDelegate)) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with value type: " + genericArguments[1]); } } else { GpTypeV17 ev2 = GetGpType(genericArguments[1]); valueWriteDelegate = writeMethods[(int)ev2]; if (valueWriteDelegate == null) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with value type: " + genericArguments[1]); } switch (ev2) { case GpTypeV17.Array: if (!WriteArrayHeader(stream, genericArguments[1])) { throw new InvalidDataException("Unexpected - cannot serialize Dictionary with value type: " + genericArguments[1]); } break; case GpTypeV17.Dictionary: WriteDelegate delegate2; WriteDelegate delegate3; stream.WriteByte((byte)ev2); WriteDictionaryHeader(stream, genericArguments[1], out delegate2, out delegate3); return; default: stream.WriteByte((byte)ev2); break; } } }
public static bool TryReadOperationResponse(byte[] data, ref int pos, out object result) { object obj2; byte num; if (pos > (data.Length - 4)) { result = null; return(false); } OperationResponse response = new OperationResponse { OperationCode = data[pos], ReturnCode = (short)((data[pos + 1] << 8) | data[pos + 2]) }; GpTypeV17 ev = (GpTypeV17)data[pos + 3]; pos += 4; switch (ev) { case GpTypeV17.Null: obj2 = null; break; case GpTypeV17.String: if (!ReadString(data, ref pos, out obj2)) { result = null; return(false); } break; default: result = null; return(false); } response.DebugMessage = (string)obj2; if (!BigEndianReader.TryReadByte(data, ref pos, out num)) { result = null; return(false); } Dictionary <byte, object> dictionary = new Dictionary <byte, object>(num); for (short i = 0; i < num; i = (short)(i + 1)) { byte num3; object obj3; if (!BigEndianReader.TryReadByte(data, ref pos, out num3)) { result = null; return(false); } if (Read(data, ref pos, out obj3)) { dictionary[num3] = obj3; } else { result = null; return(false); } } response.Parameters = dictionary; result = response; return(true); }