private async Task <bool> ReadPackage(Dictionary <ReadItem, S7DataItemSpecification> result, ReadPackage normalized)
        {
            var id = GetNextReferenceId();

            using (var dgmem = S7ReadJobDatagram.TranslateToMemory(S7ReadJobDatagram.BuildRead(_s7Context, id, normalized.Items), out var dgmemLength))
            {
                using (var sendData = _transport.Build(dgmem.Memory.Slice(0, dgmemLength), out var sendLength))
                {
                    try
                    {
                        IEnumerable <S7DataItemSpecification> readResults = null;
                        CallbackHandler <IEnumerable <S7DataItemSpecification> > cbh;
                        using (await SemaphoreGuard.Async(_concurrentJobs).ConfigureAwait(false))
                        {
                            cbh = new CallbackHandler <IEnumerable <S7DataItemSpecification> >(id);
                            _readHandler.TryAdd(cbh.Id, cbh);
                            try
                            {
                                if (await _transport.Client.SendAsync(sendData.Memory.Slice(0, sendLength)).ConfigureAwait(false) != SocketError.Success)
                                {
                                    return(false);
                                }
                                readResults = await cbh.Event.WaitAsync(_s7Context.Timeout).ConfigureAwait(false);
                            }
                            finally
                            {
                                _readHandler.TryRemove(cbh.Id, out _);
                            }
                        }

                        HandlerErrorResult(id, readResults, cbh);

                        BildResults(result, normalized, readResults);
                    }
                    catch (TaskCanceledException)
                    {
                        ThrowHelper.ThrowTimeoutException();
                    }
                }
            }
            return(true);
        }
        private static IEnumerable <ReadPackage> CreateReadPackages(SiemensPlcProtocolContext s7Context, IEnumerable <ReadItem> vars)
        {
            var result = new List <ReadPackage>();

            foreach (var item in vars.OrderByDescending(x => x.NumberOfItems).ToList())
            {
                var currentPackage = result.FirstOrDefault(package => package.TryAdd(item));
                if (currentPackage == null)
                {
                    if (item.NumberOfItems > s7Context.ReadItemMaxLength)
                    {
                        var    bytesToRead = item.NumberOfItems;
                        ushort processed   = 0;
                        while (bytesToRead > 0)
                        {
                            var slice = Math.Min(s7Context.ReadItemMaxLength, bytesToRead);
                            var child = ReadItem.CreateChild(item, (item.Offset + processed), slice);
                            if (slice < s7Context.ReadItemMaxLength)
                            {
                                currentPackage = result.FirstOrDefault(package => package.TryAdd(child));
                            }

                            if (currentPackage == null)
                            {
                                currentPackage = new ReadPackage(s7Context.PduSize);
                                if (currentPackage.TryAdd(child))
                                {
                                    if (currentPackage.Full)
                                    {
                                        yield return(currentPackage.Return());

                                        if (currentPackage.Handled)
                                        {
                                            currentPackage = null;
                                        }
                                    }
                                    else
                                    {
                                        result.Add(currentPackage);
                                    }
                                }
                                else
                                {
                                    ThrowHelper.ThrowCouldNotAddPackageException(nameof(ReadPackage));
                                }
                            }
                            processed   += slice;
                            bytesToRead -= slice;
                        }
                    }
                    else
                    {
                        currentPackage = new ReadPackage(s7Context.PduSize);
                        result.Add(currentPackage);
                        if (!currentPackage.TryAdd(item))
                        {
                            ThrowHelper.ThrowCouldNotAddPackageException(nameof(ReadPackage));
                        }
                    }
                }

                if (currentPackage != null)
                {
                    if (currentPackage.Full)
                    {
                        yield return(currentPackage.Return());
                    }

                    if (currentPackage.Handled)
                    {
                        result.Remove(currentPackage);
                    }
                }
            }
            foreach (var package in result)
            {
                yield return(package.Return());
            }
        }
Example #3
0
        private async Task <bool> ReadPackage(Dictionary <ReadItem, S7DataItemSpecification> result, ReadPackage normalized)
        {
            var id = GetNextReferenceId();

            using (var dgmem = S7ReadJobDatagram.TranslateToMemory(S7ReadJobDatagram.BuildRead(_s7Context, id, normalized.Items), out var dgmemLength))
            {
                using (var sendData = _transport.Build(dgmem.Memory.Slice(0, dgmemLength), out var sendLength))
                {
                    try
                    {
                        IEnumerable <S7DataItemSpecification> readResults            = null;
                        CallbackHandler <IEnumerable <S7DataItemSpecification> > cbh = null;
                        try
                        {
                            if (_concurrentJobs == null)
                            {
                                return(false);
                            }
                            using (await SemaphoreGuard.Async(_concurrentJobs).ConfigureAwait(false))
                            {
                                cbh = new CallbackHandler <IEnumerable <S7DataItemSpecification> >(id);
                                if (_readHandler.TryAdd(id, cbh))
                                {
                                    _logger?.LogTrace("Read handler with id {id} was added.", id);
                                    try
                                    {
                                        if (await _transport.Connection.SendAsync(sendData.Memory.Slice(0, sendLength)).ConfigureAwait(false) != SocketError.Success)
                                        {
                                            // we cancel return false, because if on esend faild we expect also all other ones failed.
                                            _logger?.LogWarning("Could not send read package with reference <{id}>.", id);
                                            return(false);
                                        }
                                        readResults = await cbh.Event.WaitAsync(_s7Context.Timeout).ConfigureAwait(false);
                                    }
                                    finally
                                    {
                                        _readHandler.TryRemove(id, out _);
                                        _logger?.LogTrace("Read handler with id {id} was removed.", id);
                                    }
                                }
                                else
                                {
                                    _logger?.LogWarning("Could not add read handler with reference <{id}>.", id);
                                }
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            if (cbh == null)
                            {
                                return(false);
                            }
                        }

                        HandlerErrorResult(id, readResults, cbh);

                        BildResults(result, normalized, readResults);
                    }
                    catch (TaskCanceledException)
                    {
                        ThrowHelper.ThrowTimeoutException();
                    }
                }
            }
            return(true);
        }
        private static void BildResults(Dictionary <ReadItem, S7DataItemSpecification> result, ReadPackage normalized, IEnumerable <S7DataItemSpecification> readResults)
        {
            var items = normalized.Items.GetEnumerator();

            foreach (var item in readResults)
            {
                if (items.MoveNext())
                {
                    var current = items.Current;
                    if (current.IsPart)
                    {
                        if (!result.TryGetValue(current.Parent, out var parent) || parent == null)
                        {
                            parent = new S7DataItemSpecification
                            {
                                TransportSize = item.TransportSize,
                                Length        = current.Parent.NumberOfItems,
                                Data          = new byte[current.Parent.NumberOfItems]
                            };
                            result[current.Parent] = parent;
                        }

                        parent.ReturnCode = item.ReturnCode;

                        item.Data.CopyTo(parent.Data.Slice(current.Offset - current.Parent.Offset, current.NumberOfItems));
                    }
                    else
                    {
                        result[current] = item;
                    }
                }
            }
        }
Example #5
0
        private IEnumerable <ReadPackage> CreateReadPackages(SiemensPlcProtocolContext s7Context, IEnumerable <ReadItem> vars)
        {
            var result = new List <ReadPackage>();

            foreach (var item in vars.ToList().OrderByDescending(x => x.Length))
            {
                var currentPackage = result.FirstOrDefault(package => package.TryAdd(item));
                if (currentPackage == null)
                {
                    if (item.Length > s7Context.ReadItemMaxLength)
                    {
                        ushort bytesToRead = item.Length;
                        ushort processed   = 0;
                        while (bytesToRead > 0)
                        {
                            var slice = Math.Min(_s7Context.ReadItemMaxLength, bytesToRead);
                            var child = ReadItem.CreateChild(item, (ushort)(item.Offset + processed), slice);
                            if (slice < _s7Context.ReadItemMaxLength)
                            {
                                currentPackage = result.FirstOrDefault(package => package.TryAdd(child));
                            }

                            if (currentPackage == null)
                            {
                                currentPackage = new ReadPackage(s7Context.PduSize);
                                if (currentPackage.TryAdd(child))
                                {
                                    if (currentPackage.Full)
                                    {
                                        yield return(currentPackage.Return());

                                        if (currentPackage.Handled)
                                        {
                                            currentPackage = null;
                                        }
                                    }
                                    else
                                    {
                                        result.Add(currentPackage);
                                    }
                                }
                                else
                                {
                                    throw new InvalidOperationException();
                                }
                            }
                            processed   += slice;
                            bytesToRead -= slice;
                        }
                    }
                    else
                    {
                        currentPackage = new ReadPackage(s7Context.PduSize);
                        result.Add(currentPackage);
                        if (!currentPackage.TryAdd(item))
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }

                if (currentPackage != null)
                {
                    if (currentPackage.Full)
                    {
                        yield return(currentPackage.Return());
                    }

                    if (currentPackage.Handled)
                    {
                        result.Remove(currentPackage);
                    }
                }
            }
            foreach (var package in result)
            {
                yield return(package.Return());
            }
        }