protected void OnNotification(int handle, long timeStamp, byte[] data) { if (!base._disposed) { QueueElement item = new QueueElement(handle, timeStamp, data); try { int count; Queue <QueueElement> queue = this._queue; lock (queue) { count = this._queue.Count; } if (this._bPeak) { if (count >= 0x2328) { if ((DateTime.Now.Ticks - this._bPeakStart) > 0x2faf080L) { if (!this._bPeakError) { this._bPeakError = true; TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.ClientQueueFull); } return; } } else { this._bPeak = false; this._bPeakError = false; } } else if (count >= 0x2710) { this._bPeak = true; this._bPeakStart = DateTime.Now.Ticks; } Queue <QueueElement> queue2 = this._queue; lock (queue2) { this._queue.Enqueue(item); this._enqueueEvent.Set(); } } catch (Exception exception) { base.OnNotificationError(exception); } } }
private unsafe void loadSymbols(TimeSpan timeout) { switch (this._settings.SymbolsLoadMode) { case SymbolsLoadMode.Flat: this._symbols = new SymbolCollection(InstanceCollectionMode.Path); break; case SymbolsLoadMode.VirtualTree: case SymbolsLoadMode.DynamicTree: this._symbols = new SymbolCollection(InstanceCollectionMode.PathHierarchy); break; default: throw new NotSupportedException(); } int num = this._connection.Timeout; if (this.UploadInfo.SymbolsBlockSize > 0) { AdsStream input = new AdsStream(this.UploadInfo.SymbolsBlockSize); using (BinaryReader reader = new BinaryReader(input)) { this._connection.Timeout = (int)timeout.TotalMilliseconds; try { int num2 = 0x10000; if (this.UploadInfo.SymbolsBlockSize >= num2) { uint num3 = 0; int num4 = 0; int num5 = 0; int num6 = 0; int length = 0; while (true) { int dataRead = 0; while (true) { AdsErrorCode code; ref byte pinned numRef; length = ((num4 + num2) <= input.Length) ? num2 : (((int)input.Length) - num4); try { byte[] buffer; if (((buffer = input.GetBuffer()) == null) || (buffer.Length == 0)) { numRef = null; } else { numRef = buffer; } code = this._connection.RawInterface.Read(0xf00b, 0x80000000 | num3, length, (void *)(numRef + num4), false, out dataRead); } finally { numRef = null; } if (code != AdsErrorCode.NoError) { if (code == AdsErrorCode.DeviceInvalidSize) { num2 *= 4; } else { TcAdsDllWrapper.ThrowAdsException(code); } } if (code == AdsErrorCode.NoError) { num2 = 0x10000; num5 = num4; num6 = 0; input.Position = num4; num6 = reader.ReadInt32(); while (true) { if ((num6 > 0) && (num3 < this.UploadInfo.SymbolCount)) { num5 += num6; input.Position = num5; num3++; if ((num5 < (num4 + num2)) && (num5 < input.Length)) { num6 = reader.ReadInt32(); continue; } } num4 = num5; if ((num3 < this.UploadInfo.SymbolCount) && (num4 < input.Length)) { break; } break; } break; } } } } else { this._connection.Read(0xf00b, 0, input); } SymbolParser.ParseSymbols(input, this.UploadInfo.StringEncoding, this._symbolFactorServices); }
public void OnReadCycle(object sender, EventArgs e) { ITimer timer = this._timer; lock (timer) { if (!this._bStopTimer) { try { List <CycleListEntry> list = new List <CycleListEntry>(); uint tickCount = NativeMethods.GetTickCount(); long timeStamp = DateTime.Now.ToFileTime(); int num3 = 0; while (true) { if (num3 >= this._initialNotes.Count) { foreach (KeyValuePair <int, CycleTableEntry> pair in this._cycleTable) { int key = pair.Key; CycleTableEntry entry = pair.Value; entry.timerCount++; uint num5 = (tickCount - entry.lastRead) / entry.timerCount; if ((tickCount - entry.lastRead) >= key) { int num6 = 0; while (true) { if (num6 >= entry.cycleList.Count) { entry.lastRead = tickCount; entry.timerCount = 0; break; } list.Add(entry.cycleList[num6]); num6++; } } } if (list.Count != 0) { if (this._bSumupRead) { int rdLength = list.Count * 4; int wrLength = list.Count * sizeof(VariableInfo); byte[] wrData = new byte[wrLength]; int num10 = 0; while (true) { ref byte pinned numRef; if (num10 >= list.Count) { int num9; byte[] rdData = new byte[rdLength]; if (base._syncPort.ReadWrite(0xf080, (uint)list.Count, 0, rdLength, rdData, 0, wrLength, wrData, false, out num9) != AdsErrorCode.NoError) { CycleListEntry entry3 = list[0]; byte[] data = new byte[entry3.variable.length]; AdsErrorCode adsErrorCode = base._syncPort.Read(entry3.variable.indexGroup, entry3.variable.indexOffset, 0, entry3.variable.length, data, false, out num9); if (adsErrorCode != AdsErrorCode.NoError) { TcAdsDllWrapper.ThrowAdsException(adsErrorCode); } this._bSumupRead = false; } if (this._bSumupRead) { int num11 = list.Count * 4; int num12 = 0; while (num12 < list.Count) { CycleListEntry entry4 = list[num12]; bool flag2 = false; int index = 0; while (true) { if (index < 4) { if (rdData[index + (num12 * 4)] == 0) { index++; continue; } flag2 = true; } if (flag2) { if ((base._syncWindow == null) || !base._bSynchronize) { QueueElement[] elements = new QueueElement[] { new QueueElement(entry4.handle, timeStamp, new byte[0]) }; base.OnSyncNotification(elements); } else { QueueElement[] elements = new QueueElement[] { new QueueElement(entry4.handle, timeStamp, new byte[0]) }; base._syncWindow.PostNotification(elements); } num11 += entry4.variable.length; } else { index = 0; if ((num12 >= this._initialNotes.Count) && (entry4.transMode == 4)) { index = 0; while ((index < entry4.variable.length) && (rdData[num11 + index] == entry4.data[index])) { index++; } } if (index != entry4.variable.length) { while (true) { if (index >= entry4.variable.length) { if ((base._syncWindow == null) || !base._bSynchronize) { QueueElement[] elements = new QueueElement[] { new QueueElement(entry4.handle, timeStamp, entry4.data) }; base.OnSyncNotification(elements); } else { QueueElement[] elements = new QueueElement[] { new QueueElement(entry4.handle, timeStamp, entry4.data) }; base._syncWindow.PostNotification(elements); } break; } entry4.data[index] = rdData[num11 + index]; index++; } } num11 += entry4.variable.length; } num12++; break; } } } break; } CycleListEntry entry2 = list[num10]; try { byte[] buffer3; if (((buffer3 = wrData) == null) || (buffer3.Length == 0)) { numRef = null; } else { numRef = buffer3; } numRef[num10 * sizeof(VariableInfo)] = (byte)entry2.variable; } finally { numRef = null; } rdLength += entry2.variable.length; num10++; } } if (!this._bSumupRead) { for (int i = 0; i < list.Count; i++) { int num15; CycleListEntry entry5 = list[i]; byte[] data = new byte[entry5.variable.length]; AdsErrorCode adsErrorCode = base._syncPort.Read(entry5.variable.indexGroup, entry5.variable.indexOffset, 0, entry5.variable.length, data, false, out num15); if (adsErrorCode != AdsErrorCode.NoError) { if (adsErrorCode != AdsErrorCode.DeviceInvalidOffset) { TcAdsDllWrapper.ThrowAdsException(adsErrorCode); } else if ((base._syncWindow == null) || !base._bSynchronize) { QueueElement[] elements = new QueueElement[] { new QueueElement(entry5.handle, timeStamp, new byte[0]) }; base.OnSyncNotification(elements); } else { QueueElement[] elements = new QueueElement[] { new QueueElement(entry5.handle, timeStamp, new byte[0]) }; base._syncWindow.PostNotification(elements); } } int index = 0; if ((i >= this._initialNotes.Count) && (entry5.transMode == 4)) { index = 0; while ((index < entry5.variable.length) && (data[index] == entry5.data[index])) { index++; } } if (index != entry5.variable.length) { while (true) { if (index >= entry5.variable.length) { if ((base._syncWindow == null) || !base._bSynchronize) { QueueElement[] elements = new QueueElement[] { new QueueElement(entry5.handle, timeStamp, entry5.data) }; base.OnSyncNotification(elements); } else { QueueElement[] elements = new QueueElement[] { new QueueElement(entry5.handle, timeStamp, entry5.data) }; base._syncWindow.PostNotification(elements); } break; } entry5.data[index] = data[index]; index++; } } } } } break; } list.Add(this._initialNotes[num3]); num3++; } }
private unsafe void readSymbols(AdsStream symbolStream, SymbolUploadInfo info, int initialBlockSize) { int num = initialBlockSize; if (info.SymbolsBlockSize < num) { this._adsClient.Read(0xf00b, 0, symbolStream); } else { uint num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; int length = 0; BinaryReader reader = new BinaryReader(symbolStream); while (true) { int dataRead = 0; while (true) { byte[] buffer; length = ((num3 + num) <= symbolStream.Length) ? num : (((int)symbolStream.Length) - num3); if (((buffer = symbolStream.GetBuffer()) == null) || (buffer.Length == 0)) { numRef = null; } else { numRef = buffer; } AdsErrorCode adsErrorCode = this._adsClient.RawInterface.Read(0xf00b, 0x80000000 | num2, length, (void *)(numRef + num3), false, out dataRead); fixed(byte *numRef = null) { if (adsErrorCode != AdsErrorCode.NoError) { if (adsErrorCode == AdsErrorCode.DeviceInvalidSize) { num *= 4; } else { TcAdsDllWrapper.ThrowAdsException(adsErrorCode); } } if (adsErrorCode == AdsErrorCode.NoError) { num = initialBlockSize; num4 = num3; num5 = 0; symbolStream.Position = num3; num5 = reader.ReadInt32(); while (true) { if ((num5 > 0) && (num2 < info.SymbolCount)) { num4 += num5; symbolStream.Position = num4; num2++; if ((num4 < (num3 + num)) && (num4 < symbolStream.Length)) { num5 = reader.ReadInt32(); continue; } } num3 = num4; if ((num2 < info.SymbolCount) && (num3 < symbolStream.Length)) { break; } symbolStream.Position = 0L; return; } break; } } } } } }
public object ReadSymbol(string symbolPath, Type managedType, bool bReloadInfo) { AdsErrorCode code2; if (bReloadInfo) { this.Cleanup(); } TcAdsSymbol adsSymbol = (TcAdsSymbol)this.GetSymbol(symbolPath, true); if (adsSymbol == null) { TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.DeviceSymbolNotFound); } TcAdsDataType type = (TcAdsDataType)this._datatypeTable.ResolveDataType(adsSymbol.TypeName); if (type == null) { throw new NotSupportedException("Type of symbol not supported"); } if (adsSymbol.IndexGroup != 0xf005L) { AdsErrorCode adsErrorCode = this.updateSymbolHandle(adsSymbol); if (adsErrorCode == AdsErrorCode.ClientSyncTimeOut) { TcAdsDllWrapper.ThrowAdsException(adsErrorCode); } } AdsStream stream = new AdsStream(adsSymbol.Size); bool flag = true; AdsBinaryReader reader = new AdsBinaryReader(stream); int num2 = 0; goto TR_001C; TR_000F: num2++; TR_001C: while (true) { int num3; if (!((num2 < 2) & flag)) { object obj2 = null; int num = 0; TcAdsDataType adsType = (TcAdsDataType)type.ResolveType(DataTypeResolveStrategy.AliasReference); if (adsType.IsEnum) { num = this.InitializeEnum(adsType.Name, managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2); return(obj2); } if (adsType.IsArray) { num = this.InitializeArray(managedType, adsType, reader, 0, -1, out obj2); return(obj2); } if (adsType.IsStruct) { num = this.InitializeStruct(adsType.SubItems, managedType, reader, 0, out obj2); return(obj2); } if (adsType.IsPointer) { num = this.InitializePointerType(managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2); return(obj2); } if (adsType.IsPrimitive) { num = this.InitializePrimitiveType(adsType.Name, managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2); return(obj2); } if (!adsType.IsPointer) { throw new NotSupportedException("Type of symbol not supported"); } num = this.InitializePointerType(managedType, adsSymbol.DataTypeId, adsSymbol.Size, reader, 0, out obj2); return(obj2); } flag = false; code2 = this._adsClient.RawInterface.Read((uint)adsSymbol.IndexGroup, (uint)adsSymbol.IndexOffset, 0, (int)stream.Length, stream.GetBuffer(), false, out num3); if (code2 > AdsErrorCode.DeviceInvalidOffset) { if ((code2 != AdsErrorCode.DeviceSymbolNotFound) && (code2 != AdsErrorCode.DeviceSymbolVersionInvalid)) { break; } } else if (code2 == AdsErrorCode.NoError) { goto TR_000F; } else if (code2 != AdsErrorCode.DeviceInvalidOffset) { break; } if (adsSymbol.IndexGroup == 0xf005L) { uint num4; this._adsClient.RawInterface.Write(0xf006, 0, (uint)adsSymbol.IndexOffset, false); if (this._adsClient.RawInterface.ReadWrite(0xf003, 0, symbolPath, false, out num4) == AdsErrorCode.NoError) { adsSymbol.IndexGroup = 0xf005L; adsSymbol.IndexOffset = num4; flag = true; } } goto TR_000F; } TcAdsDllWrapper.ThrowAdsException(code2); goto TR_000F; }
public void WriteSymbol(string name, object value, bool bReloadInfo) { AdsErrorCode code2; if (bReloadInfo) { this.Cleanup(); } TcAdsSymbol adsSymbol = (TcAdsSymbol)this.GetSymbol(name, true); if (adsSymbol == null) { TcAdsDllWrapper.ThrowAdsException(AdsErrorCode.DeviceSymbolNotFound); } TcAdsDataType type = (TcAdsDataType)((TcAdsDataType)this._datatypeTable.ResolveDataType(adsSymbol.TypeName)).ResolveType(DataTypeResolveStrategy.AliasReference); if (type == null) { throw new NotSupportedException("Type of symbol not supported"); } if (adsSymbol.IndexGroup != 0xf005L) { AdsErrorCode adsErrorCode = this.updateSymbolHandle(adsSymbol); if (adsErrorCode == AdsErrorCode.ClientSyncTimeOut) { TcAdsDllWrapper.ThrowAdsException(adsErrorCode); } } AdsStream stream = new AdsStream(adsSymbol.Size); AdsBinaryWriter writer = new AdsBinaryWriter(stream); if (type.IsEnum) { this.WriteEnumValue(adsSymbol.Name, value, type, writer, 0); } else if (type.IsArray) { this.WriteArray(value, type, writer, 0); } else if (type.IsStruct) { this.WriteStruct(value, type.SubItems, writer, 0); } else if (type.IsPointer) { this.WritePointerValue(adsSymbol.Name, value, adsSymbol.DataTypeId, adsSymbol.Size, writer, 0); } else { if (!type.IsPrimitive) { throw new NotSupportedException("Type of symbol not supported"); } this.WritePrimitiveValue(adsSymbol.Name, value, type.ManagedType, adsSymbol.DataTypeId, adsSymbol.Size, writer, 0); } bool flag = true; int num = 0; goto TR_000F; TR_0002: num++; TR_000F: while (true) { if (!((num < 2) & flag)) { return; } flag = false; code2 = this._adsClient.RawInterface.Write((uint)adsSymbol.IndexGroup, (uint)adsSymbol.IndexOffset, 0, (int)stream.Length, stream.GetBuffer(), false); if (code2 > AdsErrorCode.DeviceInvalidOffset) { if ((code2 != AdsErrorCode.DeviceSymbolNotFound) && (code2 != AdsErrorCode.DeviceSymbolVersionInvalid)) { break; } } else if (code2 == AdsErrorCode.NoError) { goto TR_0002; } else if (code2 != AdsErrorCode.DeviceInvalidOffset) { break; } if (adsSymbol.IndexGroup == 0xf005L) { uint num2; this._adsClient.RawInterface.Write(0xf006, 0, (uint)adsSymbol.IndexOffset, false); if (this._adsClient.RawInterface.ReadWrite(0xf003, 0, name, false, out num2) == AdsErrorCode.NoError) { adsSymbol.IndexGroup = 0xf005L; adsSymbol.IndexOffset = num2; flag = true; } } goto TR_0002; } TcAdsDllWrapper.ThrowAdsException(code2); goto TR_0002; }