Пример #1
0
        public static IEnumerable <DataTransferDatagram> Build(Rfc1006ProtocolContext context, Memory <byte> rawPayload)
        {
            var result  = new List <DataTransferDatagram>();
            var payload = rawPayload;

            do
            {
                var frame = payload.Slice(0, Math.Min(payload.Length, context.FrameSizeSending));
                payload = payload.Slice(frame.Length);

                var current = new DataTransferDatagram
                {
                    _payload = MemoryPool <byte> .Shared.Rent(frame.Length)
                };
                current.Payload = current._payload.Memory.Slice(0, frame.Length);

                frame.CopyTo(current.Payload);
                if (payload.Length > 0)
                {
                    current.TpduNr = 0x00;
                }

                current.Tkpt.Length = Convert.ToUInt16(frame.Length + Rfc1006ProtocolContext.DataHeaderSize);
                result.Add(current);
            } while (payload.Length > 0);
            return(result);
        }
Пример #2
0
        public static DataTransferDatagram TranslateFromMemory(Memory <byte> buffer,
                                                               Rfc1006ProtocolContext context,
                                                               out bool needMoteData,
                                                               out int processed)
        {
            var datagram = TranslateFromMemory(buffer, out processed);

            if (datagram != null)
            {
                if (datagram.TpduNr == EndOfTransmition)
                {
                    if (context.FrameBuffer.Any())
                    {
                        ApplyPayloadFromFrameBuffer(context.FrameBuffer, datagram);
                    }
                    needMoteData = false;
                    return(datagram);
                }
                else if (!datagram.Payload.IsEmpty)
                {
                    AddPayloadToFrameBuffer(context.FrameBuffer, datagram);
                }
            }
            needMoteData = true;
            return(datagram);
        }
Пример #3
0
        public void CalcLength(Rfc1006ProtocolContext context, out byte li, out ushort length)
        {
            const int optionsMinLength        = 7;
            const int TpduCrWithoutProperties = 6;
            var       tmp = Convert.ToUInt16(optionsMinLength + context.SourceTsap.Length + context.DestTsap.Length + TpduCrWithoutProperties);

            length = (ushort)(tmp + TpktHeaderSize + 1); // add 1 because li is without li
            li     = Convert.ToByte(tmp);
        }
Пример #4
0
        public static ConnectionRequestDatagram BuildCr(Rfc1006ProtocolContext context)
        {
            context.CalcLength(context, out var li, out var length);
            var result = new ConnectionRequestDatagram
            {
                Li = li,
                SizeTpduReceiving = context.SizeTpduReceiving,
                SourceTsapLength  = Convert.ToByte(context.SourceTsap.Length),
                SourceTsap        = context.SourceTsap,
                DestTsapLength    = Convert.ToByte(context.DestTsap.Length),
                DestTsap          = context.DestTsap
            };

            result.Tkpt.Length = length;
            return(result);
        }
Пример #5
0
        public ConnectionConfirmedDatagram BuildCc(Rfc1006ProtocolContext context, ConnectionRequestDatagram req)
        {
            context.CalcLength(context, out byte li, out ushort length);
            context.SizeTpduSending = req.SizeTpduReceiving;
            var result = new ConnectionConfirmedDatagram
            {
                Li = li,
                SizeTpduReceiving = context.SizeTpduReceiving,
                SourceTsapLength  = req.DestTsapLength,
                SourceTsap        = req.SourceTsap,
                DestTsapLength    = req.DestTsapLength,
                DestTsap          = req.DestTsap
            };

            result.Tkpt.Length = length;
            return(result);
        }
Пример #6
0
        public static DataTransferDatagram TranslateFromMemory(Memory <byte> buffer,
                                                               Rfc1006ProtocolContext context,
                                                               out bool needMoteData,
                                                               out int processed)
        {
            var datagram = TranslateFromMemory(buffer, out processed);

            if (datagram.TpduNr == EndOfTransmition)
            {
                Memory <byte> payload = Memory <byte> .Empty;
                if (context.FrameBuffer.Any())
                {
                    context.FrameBuffer.Add(new Tuple <Memory <byte>, int>(datagram.Payload, datagram.Payload.Length));
                    var length = context.FrameBuffer.Sum(x => x.Item1.Length);
                    payload = new byte[length];
                    var index = 0;
                    foreach (var item in context.FrameBuffer)
                    {
                        item.Item1.Slice(0, item.Item2).CopyTo(payload.Slice(index));
                        if (!ReferenceEquals(datagram.Payload, item.Item1))
                        {
                            ArrayPool <byte> .Shared.Return(item.Item1.ToArray());
                        }
                        index += item.Item2;
                    }
                    datagram.Payload = payload;
                    context.FrameBuffer.Clear();
                }

                needMoteData = false;
                return(datagram);
            }
            else if (!datagram.Payload.IsEmpty)
            {
                Memory <byte> copy = ArrayPool <byte> .Shared.Rent(datagram.Payload.Length);

                datagram.Payload.CopyTo(copy);
                context.FrameBuffer.Add(new Tuple <Memory <byte>, int>(copy, datagram.Payload.Length));
            }
            needMoteData = true;
            return(datagram);
        }