protected override int OnUnmarshalParameter(int i, RpcMethodParameterCollection outParameters, IList <IDataType> outParameterTypes, byte[] buffer, int offset, ref object[] outValues) { ITcAdsDataType dataType = (ITcAdsDataType)outParameterTypes[i]; object obj2 = outValues[i]; return(this.unmarshal(buffer, offset, dataType, out obj2)); }
public static string ToString(ITcAdsDataType dataType, object value) { AdsDatatypeId dataTypeId = dataType.BaseType.DataTypeId; if (dataTypeId == AdsDatatypeId.ADST_INT16) { return(EnumTypeConverter <short> .ToString(dataType, (short)value)); } if (dataTypeId == AdsDatatypeId.ADST_INT32) { return(EnumTypeConverter <int> .ToString(dataType, (int)value)); } switch (dataTypeId) { case AdsDatatypeId.ADST_INT8: return(EnumTypeConverter <sbyte> .ToString(dataType, (sbyte)value)); case AdsDatatypeId.ADST_UINT8: return(EnumTypeConverter <byte> .ToString(dataType, (byte)value)); case AdsDatatypeId.ADST_UINT16: return(EnumTypeConverter <ushort> .ToString(dataType, (ushort)value)); case AdsDatatypeId.ADST_UINT32: return(EnumTypeConverter <uint> .ToString(dataType, (uint)value)); case AdsDatatypeId.ADST_INT64: return(EnumTypeConverter <long> .ToString(dataType, (long)value)); case AdsDatatypeId.ADST_UINT64: return(EnumTypeConverter <ulong> .ToString(dataType, (ulong)value)); } throw new ArgumentException("Wrong data type!"); }
public static bool TryCheckIndices(IList <int[]> indices, ITcAdsDataType type) { if (indices == null) { throw new ArgumentNullException("indices"); } if (type.Category != DataTypeCategory.Array) { throw new ArgumentOutOfRangeException("type"); } bool flag = true; bool isOversamplingArray = type.IsOversamplingArray; IDataType baseType = type; foreach (int[] numArray in indices) { ITcAdsDataType type3 = (ITcAdsDataType)baseType; flag |= TryCheckIndices(numArray, type3.Dimensions.LowerBounds, type3.Dimensions.UpperBounds, isOversamplingArray); if (!flag) { break; } baseType = type.BaseType; } return(flag); }
private int GetSizeByName(string type) { ITcAdsDataType typeByName = this.GetTypeByName(type); if (typeByName != null) { return(typeByName.Size); } ITcAdsDataType type3 = null; if ((this._defaultTypes != null) && this._defaultTypes.TryGetDataType(type, out type3)) { return(type3.Size); } int length = -1; bool isUnicode = false; if (DataTypeStringParser.TryParseString(type, out length, out isUnicode)) { int num2 = 1; if (isUnicode) { num2 = 2; } length = (length + 1) * num2; } return(length); }
protected override int OnMarshalParameter(int i, RpcMethodParameterCollection inParameters, IList <IDataType> inParameterTypes, object[] parameterValues, byte[] buffer, int offset) { ITcAdsDataType type = (ITcAdsDataType)inParameterTypes[i]; object val = parameterValues[i]; AdsDatatypeId dataTypeId = type.DataTypeId; int num = 0; if (PrimitiveTypeConverter.CanMarshal(type.Category)) { byte[] data = null; PrimitiveTypeConverter.Marshal(dataTypeId, val, out data); num = copyHelper(buffer, offset, data); } else { if (type.Category != DataTypeCategory.String) { throw new MarshalException($"Cannot marshal complex type '{type.Name}'"); } int length = -1; bool isUnicode = false; DataTypeStringParser.TryParseString(type.Name, out length, out isUnicode); string str = (string)val; byte[] source = null; PlcStringConverter converter = isUnicode ? PlcStringConverter.UnicodeVariableLength : PlcStringConverter.DefaultVariableLength; if (converter.MarshalSize(str) > type.ByteSize) { throw new MarshalException("String size mismatch"); } source = converter.Marshal(str); num = copyHelper(buffer, offset, source); } return(num); }
internal TcAdsSymbolInfo GetSubSymbol(TcAdsSymbolInfo parent, int subIndex, bool dereference) { if (parent == null) { throw new ArgumentNullException("parent"); } TcAdsSymbolInfo info = null; try { ITcAdsDataType dataType = parent.DataType; ITcAdsDataType type2 = null; if (dataType != null) { type2 = (ITcAdsDataType)dataType.ResolveType(DataTypeResolveStrategy.AliasReference); } if (type2 != null) { int count = parent.SubSymbols.Count; if (((type2 != null) && (type2.SubItems.Count > 0)) && (subIndex < type2.SubItems.Count)) { info = this._subSymbolFactory.CreateSubSymbol(parent, subIndex); } else if ((type2 != null) && ((type2.Dimensions.Count > 0) || DataTypeStringParser.IsArray(type2.Name))) { info = this._subSymbolFactory.CreateArrayElement(parent, (TcAdsDataType)type2, subIndex); } else if ((type2 != null) && type2.IsReference) { this.SetPlatformPointerSize(type2.Size); } else if ((subIndex == 0) && type2.IsPointer) { this.SetPlatformPointerSize(type2.Size); if (dereference) { string str; DataTypeStringParser.TryParsePointer(type2.Name, out str); TcAdsDataType referencedType = (TcAdsDataType)this.ResolveDataType(str); if ((referencedType != null) & dereference) { bool flag = (referencedType.Flags & AdsDataTypeFlags.AnySizeArray) == AdsDataTypeFlags.AnySizeArray; if ((referencedType.Size > 0) | flag) { info = this._subSymbolFactory.CreatePointerSymbol(parent, referencedType); } } } } } } catch (Exception exception) { Module.Trace.TraceError($"ParentSymbol: {parent.Name}", exception); throw; } return(info); }
private void expandDataType(ITcAdsDataType cloneType) { ITcAdsDataType baseType = cloneType.BaseType; if (baseType != null) { this.expandDataType(baseType); } }
internal static string ToString(ITcAdsDataType enumType, T val) { string nameValue = null; if (!EnumTypeConverter <T> .TryGetName(enumType, val, out nameValue)) { throw new ArgumentOutOfRangeException("val"); } return(nameValue); }
private TcAdsSymbolInfo CreateSymbolInfo(AdsSymbolEntry symbolEntry, int index) { ITcAdsDataType typeByName = this.GetTypeByName(symbolEntry.type); if (typeByName == null) { typeByName = (ITcAdsDataType)this.ResolveDataType(symbolEntry.type); } return(new TcAdsSymbolInfo(this, null, index, symbolEntry, (TcAdsDataType)typeByName)); }
public static bool IsJagged(ITcAdsDataType type) { bool flag = false; if (((type.Category == DataTypeCategory.Array) && (type.BaseType != null)) && (type.BaseType.Category == DataTypeCategory.Array)) { flag = true; } return(flag); }
private ITcAdsDataType GetTypeByName(string type) { if (string.IsNullOrEmpty(type)) { throw new ArgumentException(); } ITcAdsDataType ret = null; this._dataTypes.TryGetDataType(type, out ret); return(ret); }
public bool TryGetReference(out string referencedType) { ITcAdsDataType type = this.ResolveType(DataTypeResolveStrategy.AliasReference); string typeName = this.typeName; if (type != null) { typeName = type.Name; } return(DataTypeStringParser.TryParseReference(typeName, out referencedType)); }
public bool TryResolveType(string name, out IDataType ret) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("typeName"); } ITcAdsDataType type = null; if ((this._dataTypes != null) && !this._dataTypes.TryGetDataType(name, out type)) { int length = 0; bool isUnicode = false; AdsDatatypeArrayInfo[] dims = null; if (DataTypeStringParser.TryParseString(name, out length, out isUnicode)) { AdsDatatypeId dataType = isUnicode ? AdsDatatypeId.ADST_WSTRING : AdsDatatypeId.ADST_STRING; type = new TcAdsDataType(name, dataType, isUnicode ? ((uint)((length + 1) * 2)) : ((uint)(length + 1)), AdsDataTypeFlags.DataType, DataTypeCategory.String, typeof(string), this); } else { string str; if (DataTypeStringParser.TryParsePointer(name, out str)) { ITcAdsDataType type2 = (ITcAdsDataType)this.ResolveDataType(str); type = new TcAdsDataType(name, AdsDatatypeId.ADST_BIGTYPE, (uint)this.PlatformPointerSize, AdsDataTypeFlags.DataType, DataTypeCategory.Pointer, null, this); } else if (DataTypeStringParser.TryParseReference(name, out str)) { ITcAdsDataType type3 = (ITcAdsDataType)this.ResolveDataType(str); type = new TcAdsDataType(name, AdsDatatypeId.ADST_BIGTYPE, (uint)this.PlatformPointerSize, AdsDataTypeFlags.DataType, DataTypeCategory.Reference, null, this); } else if (DataTypeStringParser.TryParseArray(name, out dims, out str)) { ITcAdsDataType type4 = (ITcAdsDataType)this.ResolveDataType(str); if (type4 != null) { type = new TcAdsDataType(name, str, (uint)type4.Size, dims, this); } } } if (type == null) { this.OnTypeResolveError(name); } else { this._dataTypes.Add(type); this.OnTypeCreated(type); } } ret = type; return(ret != null); }
public bool TryResolveType(string name, out IDataType type) { ITcAdsDataType type2 = null; if (this.TryLoadType(name, true, out type2) == AdsErrorCode.NoError) { type = type2; return(true); } type = null; return(false); }
private int unmarshal(byte[] buffer, int offset, ITcAdsDataType dataType, out object value) { AdsDatatypeId dataTypeId = dataType.DataTypeId; value = null; int size = dataType.Size; if (!PrimitiveTypeConverter.CanMarshal(dataType.Category)) { throw new MarshalException($"Cannot unmarshal complex type '{dataType.Name}'"); } return(PrimitiveTypeConverter.Default.Unmarshal(dataType, buffer, offset, out value)); }
private static EnumValue <T> GetEntry(ITcAdsDataType enumType, T value) { EnumValue <T> value2 = null; foreach (EnumValue <T> value3 in enumType.EnumValues) { T primitive = value3.Primitive; if (primitive.Equals(value)) { value2 = value3; break; } } return(value2); }
private TcAdsDataTypeCollection collectDataTypes(int count, uint[] dataTypeOffsets, AdsBinaryReader dataTypeReader) { TcAdsDataTypeCollection types = new TcAdsDataTypeCollection(); types.AddRange(this._defaultTypes); AdsDataTypeEntry[] entryArray = new AdsDataTypeEntry[count]; for (int i = 0; i < count; i++) { entryArray[i] = this.GetDataTypeEntry(i, dataTypeOffsets, dataTypeReader); } for (int j = 0; j < count; j++) { if (!(this._streamIncludesBuildInTypes && this._defaultTypes.Contains(entryArray[j].entryName))) { TcAdsDataType item = new TcAdsDataType(entryArray[j], this); if (item.IsPointer || item.IsReference) { this.SetPlatformPointerSize(item.Size); Type managedType = typeof(ulong); if (item.Size == 4) { managedType = typeof(uint); } for (int k = 0; k < types.Count; k++) { ITcAdsDataType dataType = types[k]; if (dataType.Size <= 0) { if (dataType.Category == DataTypeCategory.Alias) { ITcAdsDataType type4 = types.ResolveType(dataType, DataTypeResolveStrategy.Alias); if ((type4 != null) && (type4.Category == DataTypeCategory.Pointer)) { ((TcAdsDataType)dataType).SetSize(item.Size, managedType); } } if (dataType.Category == DataTypeCategory.Pointer) { ((TcAdsDataType)dataType).SetSize(item.Size, managedType); } } } } types.Add(item); } } return(types); }
internal TcAdsSymbolInfo CreateSubSymbol(TcAdsSymbolInfo parent, int subIndex) { if (parent == null) { throw new ArgumentNullException("parent"); } ITcAdsDataType dataType = parent.DataType; if (dataType == null) { throw new ArgumentException("DataType not specified", "parent"); } if (((subIndex < 0) || (dataType.SubItems == null)) || (subIndex >= dataType.SubItems.Count)) { throw new ArgumentOutOfRangeException("subIndex"); } TcAdsSymbolInfo subSymbol = null; if (parent.IsPointer && (subIndex > 0)) { throw new ArgumentOutOfRangeException("subIndex"); } TcAdsSubItem typeSubEntry = (TcAdsSubItem)dataType.SubItems[subIndex]; TcAdsDataType typeByName = (TcAdsDataType)this._parser.GetTypeByName(typeSubEntry.Name); if (typeSubEntry != null) { if (typeByName != null) { typeSubEntry.AlignSubItemToType(typeByName); } subSymbol = new TcAdsSymbolInfo(this._parser, parent, subIndex) { size = (uint)typeSubEntry.Size, dataTypeId = typeSubEntry.DataTypeId, typeEntryFlags = this.createTypeEntryFlags(parent, typeSubEntry), flags = this.createSubSymbolFlags(parent, typeSubEntry), shortName = typeSubEntry.SubItemName }; subSymbol.instancePath = $"{parent.instancePath}.{subSymbol.shortName}"; subSymbol.typeName = typeSubEntry.Name; subSymbol.comment = typeSubEntry.Comment; subSymbol.arrayInfo = typeSubEntry.ArrayInfo; subSymbol.attributes = typeSubEntry.Attributes; this.calcFieldAddress(parent, subSymbol, typeSubEntry, out subSymbol.indexGroup, out subSymbol.indexOffset); } return(subSymbol); }
public static bool TryGetJaggedDimensions(ITcAdsDataType type, out IList <IDimensionCollection> dimLengths) { List <IDimensionCollection> list = new List <IDimensionCollection>(); for (ITcAdsDataType type2 = type; (type2 != null) && (type2.Category == DataTypeCategory.Array); type2 = type2.BaseType) { list.Add(type2.Dimensions); } if (list.Count > 0) { dimLengths = list; return(true); } dimLengths = null; return(false); }
internal TcAdsSymbolInfo(AdsParseSymbols symbolParser, TcAdsSymbolInfo parent, int subIndex, AdsSymbolEntry symbolEntry, TcAdsDataType typeEntry) : this(symbolParser, parent, subIndex) { if (symbolEntry == null) { throw new ArgumentNullException("symbolEntry"); } this.indexGroup = symbolEntry.indexGroup; this.indexOffset = symbolEntry.indexOffset; this.size = symbolEntry.size; this.dataTypeId = (AdsDatatypeId)symbolEntry.dataType; if (typeEntry != null) { this.typeEntryFlags = typeEntry.Flags; } if (this.typeEntryFlags.HasFlag(AdsDataTypeFlags.None | AdsDataTypeFlags.Static)) { this.indexGroup = 0xf019; this.indexOffset = 0; } this.flags = symbolEntry.flags; this.instancePath = symbolEntry.name; this.shortName = symbolEntry.name; this.typeName = symbolEntry.type; this.comment = symbolEntry.comment; this.dataType = typeEntry; if (((symbolEntry.array != null) || (typeEntry == null)) || !typeEntry.IsArray) { this.arrayInfo = symbolEntry.array; } else { this.arrayInfo = typeEntry.Dimensions.ToArray(); if (typeEntry.Size != this.size) { this.size = (uint)typeEntry.Size; } } if (symbolEntry.attributes != null) { this.attributes = new TypeAttributeCollection(symbolEntry.attributes).AsReadOnly(); } else { this.attributes = null; } }
internal void WriteArray(object value, ITcAdsDataType arrayType, AdsBinaryWriter writer, int writerOffset) { Array array = value as Array; if (array == null) { throw new ArgumentException($"Cannot convert data type of symbol to this type. Expected an array, actual type is {value.GetType()}", "type"); } AdsDatatypeArrayInfo[] arrayInfo = ((TcAdsDataType)arrayType).ArrayInfo; checkArrayDimensions(array, arrayInfo, false); AdsArrayDimensionsInfo info = new AdsArrayDimensionsInfo(arrayInfo); int elements = info.Elements; int byteSize = arrayType.ByteSize / elements; DataTypeCategory category = arrayType.BaseType.Category; int num3 = 0; foreach (int[] numArray in new ArrayIndexIterator(info.LowerBounds, info.UpperBounds, true)) { if ((array.Rank == 1) && (num3 >= array.Length)) { break; } if (category == DataTypeCategory.Primitive) { this.WritePrimitiveValue(arrayType.BaseType.Name, array.GetValue(numArray), ((TcAdsDataType)arrayType.BaseType).ManagedType, arrayType.DataTypeId, byteSize, writer, writerOffset); } else if (category == DataTypeCategory.Enum) { this.WriteEnumValue(arrayType.BaseType.Name, array.GetValue(numArray), (TcAdsDataType)arrayType, writer, writerOffset); } else if (category == DataTypeCategory.Struct) { this.WriteStruct(array.GetValue(numArray), arrayType.BaseType.SubItems, writer, writerOffset); } else { if (category != DataTypeCategory.Array) { throw new ArgumentException("Cannot convert ads array type of symbol to this type.", "type"); } this.WriteArray(array.GetValue(numArray), arrayType.BaseType, writer, writerOffset); } writerOffset += byteSize; num3++; } }
private AdsDatatypeId GetDataTypeId(string type) { ITcAdsDataType typeByName = this.GetTypeByName(type); if (typeByName != null) { return(typeByName.DataTypeId); } ITcAdsDataType type3 = null; if ((this._defaultTypes != null) && this._defaultTypes.TryGetDataType(type, out type3)) { return(type3.DataTypeId); } int length = -1; bool isUnicode = false; return(!DataTypeStringParser.TryParseString(type, out length, out isUnicode) ? AdsDatatypeId.ADST_BIGTYPE : (!isUnicode ? AdsDatatypeId.ADST_STRING : AdsDatatypeId.ADST_WSTRING)); }
internal TcAdsSymbolInfo(AdsParseSymbols symbolParser, TcAdsSymbolInfo parent, int subIndex, TcAdsDataType typeEntry) : this(symbolParser, parent, subIndex) { if (typeEntry == null) { throw new ArgumentNullException("typeEntry"); } this.indexGroup = (uint)parent.IndexGroup; this.indexOffset = (uint)parent.IndexOffset; this.size = (uint)typeEntry.Size; this.dataTypeId = typeEntry.DataTypeId; this.typeEntryFlags = typeEntry.Flags; this.flags = DataTypeFlagConverter.Convert(typeEntry.Flags); this.instancePath = parent.Name; this.shortName = parent.ShortName; this.typeName = typeEntry.Name; this.comment = typeEntry.Comment; this.dataType = typeEntry; this.arrayInfo = typeEntry.ArrayInfo; this.attributes = typeEntry.Attributes; }
private static EnumValue <T> GetEntry(ITcAdsDataType enumType, string value) { StringComparer ordinalIgnoreCase = StringComparer.OrdinalIgnoreCase; using (IEnumerator <IEnumValue> enumerator = enumType.EnumValues.GetEnumerator()) { while (true) { if (!enumerator.MoveNext()) { break; } EnumValue <T> current = (EnumValue <T>)enumerator.Current; if (ordinalIgnoreCase.Compare(current.Name, value) == 0) { return(current); } } } return(null); }
internal static T ToValue(ITcAdsDataType enumType, string stringValue) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (stringValue == null) { throw new ArgumentNullException("value"); } if (enumType.Category == DataTypeCategory.Enum) { throw new ArgumentException("Specified type is not an enum type!", "enumType"); } T local = default(T); if (!EnumTypeConverter <T> .TryGetValue(enumType, stringValue, out local)) { throw new ArgumentOutOfRangeException("stringValue"); } return(local); }
internal static bool TryGetName(ITcAdsDataType enumType, T value, out string nameValue) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (value == null) { throw new ArgumentNullException("value"); } if (enumType.Category != DataTypeCategory.Enum) { throw new ArgumentException("Specified type is not an enum type!", "enumType"); } EnumValue <T> entry = EnumTypeConverter <T> .GetEntry(enumType, value); if (entry != null) { nameValue = entry.Name; return(true); } nameValue = null; return(false); }
internal static bool TryGetValue(ITcAdsDataType enumType, string stringValue, out T value) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (string.IsNullOrEmpty(stringValue)) { throw new ArgumentOutOfRangeException("stringValue"); } if (enumType.Category == DataTypeCategory.Enum) { throw new ArgumentException("Specified type is not an enum type!", "enumType"); } EnumValue <T> entry = EnumTypeConverter <T> .GetEntry(enumType, stringValue); if (entry != null) { value = entry.Primitive; return(true); } value = default(T); return(false); }
public AdsErrorCode TryLoadType(string name, bool lookup, out ITcAdsDataType type) { if (string.IsNullOrEmpty(name)) { throw new ArgumentOutOfRangeException("name"); } if (lookup) { DataTypeInfoTable table = this; lock (table) { if (this._dataTypes.TryGetDataType(name, out type)) { return(AdsErrorCode.NoError); } } } int length = 0; bool isUnicode = false; if (DataTypeStringParser.TryParseString(name, out length, out isUnicode)) { int byteCount = 0; AdsDatatypeId dataType = AdsDatatypeId.ADST_VOID; if (isUnicode) { char[] chars = new char[] { 'a' }; byteCount = Encoding.Unicode.GetByteCount(chars); dataType = AdsDatatypeId.ADST_WSTRING; } else { char[] chars = new char[] { 'a' }; byteCount = Encoding.ASCII.GetByteCount(chars); dataType = AdsDatatypeId.ADST_STRING; } type = new TcAdsDataType(name, dataType, (uint)((length + 1) * byteCount), DataTypeCategory.String, typeof(string)); DataTypeInfoTable table2 = this; lock (table2) { this._dataTypes.Add(type); } this.OnTypeGenerated(type); return(AdsErrorCode.NoError); } AdsStream stream = new AdsStream(name.Length + 1); using (AdsBinaryWriter writer = new AdsBinaryWriter(stream)) { writer.WritePlcString(name, name.Length + 1, this._encoding); AdsStream rdDataStream = new AdsStream(0xffff); try { int num3 = this._adsClient.ReadWrite(0xf011, 0, rdDataStream, stream); } catch (AdsErrorException exception1) { if (exception1.ErrorCode != AdsErrorCode.DeviceSymbolNotFound) { throw; } type = null; } using (AdsBinaryReader reader = new AdsBinaryReader(rdDataStream)) { AdsDataTypeEntry entry = new AdsDataTypeEntry(true, this._encoding, reader); type = new TcAdsDataType(entry, this); DataTypeInfoTable table3 = this; lock (table3) { this._dataTypes.Add(type); } } } if (type != null) { return(AdsErrorCode.NoError); } this.OnResolveError(name); return(AdsErrorCode.DeviceSymbolNotFound); }
internal static DataTypeCategory FromType(ITcAdsDataType type) => (!DataTypeStringParser.IsReference(type.Name) ? (!DataTypeStringParser.IsPointer(type.Name) ? (!DataTypeStringParser.IsSubRange(type.Name) ? (!type.HasSubItemInfo ? (!type.HasArrayInfo ? (!type.HasEnumInfo ? (string.IsNullOrEmpty(type.BaseTypeName) ? (!DataTypeStringParser.IsString(type.Name) ? FromId(type.DataTypeId, type.Name) : DataTypeCategory.String) : DataTypeCategory.Alias) : DataTypeCategory.Enum) : DataTypeCategory.Array) : DataTypeCategory.Struct) : DataTypeCategory.SubRange) : DataTypeCategory.Pointer) : DataTypeCategory.Reference);
internal ITcAdsDataType ResolveType(DataTypeResolveStrategy type) { ITcAdsDataType dataType = this.DataType; return((dataType == null) ? null : ((ITcAdsDataType)dataType.ResolveType(type))); }