コード例 #1
0
        private static (MemorySegment head, MemorySegment tail) SplitByteArrayIntoSegments(byte[] bytes, int splitBy)
        {
            int parts = bytes.Length / splitBy;

            if (parts * splitBy < bytes.Length)
            {
                parts += 1;
            }

            var           head = new MemorySegment(new ReadOnlyMemory <byte>());
            MemorySegment tail = head;

            for (var i = 0; i < parts; i++)
            {
                int offset       = i * splitBy;
                int memoryLength = offset + splitBy > bytes.Length
                    ? splitBy - (offset + splitBy - bytes.Length)
                    : splitBy;

                var memory = new ReadOnlyMemory <byte>(bytes, offset, memoryLength);
                tail = tail.Append(memory);
            }

            return(head, tail);
        }
コード例 #2
0
        public void ModbusFrameReader_ReadFrame_ReturnsAnErrorResponseForEachFunctionWhenTheErrorBitIsSet(byte function)
        {
            // Given
            var errorFunction = (byte)(function | 0x80);
            var header        = new byte[]
            {
                0x00, 0x01, // TransactionID
                0x00, 0x00, // Protocol ID
                0x00, 0x03, // Message length
                0x20,       // Device address
            };
            var data = new byte[]
            {
                errorFunction,  // Functional code with changed bit
                0x01            // Error Code
            };

            var first    = new MemorySegment <byte>(header);
            var last     = first.Append(data);
            var sequence = new ReadOnlySequence <byte>(first, 0, last, data.Length);

            // When
            var reader   = new ModbusFrameReader(sequence);
            var position = reader.ReadFrame(out var frame);

            // Then
            var read = Assert.IsType <ResponseError>(frame.Pdu);

            Assert.Equal(ModbusErrorCode.UnknownFunction, read.ErrorCode);
        }
コード例 #3
0
        public void ModbusFrameReader_ReadFrame_CorrectlyReadsAFrameSplitOverMultipleSegments()
        {
            // Given
            var arrayOne   = new byte[] { 0x00, 0x01, };
            var arrayTwo   = new byte[] { 0x00, 0x00, };
            var arrayThree = new byte[] { 0x00, 0x04, };
            var arrayFour  = new byte[] { 0x09 };
            var arrayFive  = new byte[] { 0x01 };
            var arraySix   = new byte[] { 0x01, 0x01, };

            var first = new MemorySegment <byte>(arrayOne);
            var last  = first.Append(arrayTwo).Append(arrayThree).Append(arrayFour).Append(arrayFive).Append(arraySix);

            var sequence = new ReadOnlySequence <byte>(first, 0, last, 2);

            // When
            var reader   = new ModbusFrameReader(sequence);
            var position = reader.ReadFrame(out var frame);

            // Then
            Assert.Equal(1, frame.Header.TransactionID);
            Assert.Equal(9, frame.Header.UnitID);

            var read = Assert.IsType <ResponseReadCoils>(frame.Pdu);

            Assert.Equal(new byte[] { 1 }, read.Coils);
        }
コード例 #4
0
        public void ReadUShortSequence()
        {
            var bytes    = new byte[] { 0 };
            var sequence = new ReadOnlySequence <byte>(bytes);

            Assert.Throws <ArgumentException>(() => _ = BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian));

            bytes    = new byte[] { 48, 57, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));

            bytes    = new byte[] { 57, 48, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.LittleEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));

            var bytes1       = new byte[] { 48 };
            var bytes2       = new byte[] { 57, 0, 0, 0 };
            var firstSegment = new MemorySegment <byte>(bytes1);
            var lastSegment  = firstSegment.Append(bytes2);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
            Assert.That(BytesExtensions.ReadUShort(ref sequence, Endianness.BigEndian), Is.EqualTo(12345));
            Assert.That(sequence.Length, Is.EqualTo(3));
        }
コード例 #5
0
        public void ReadIntSequence()
        {
            var bytes    = new byte[] { 0 };
            var sequence = new ReadOnlySequence <byte>(bytes);

            Assert.Throws <ArgumentException>(() => _ = BytesExtensions.ReadInt(ref sequence));

            bytes    = new byte[] { 7, 91, 205, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadInt(ref sequence), Is.EqualTo(123456789));
            Assert.That(sequence.Length, Is.EqualTo(12));

            bytes    = new byte[] { 21, 205, 91, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            sequence = new ReadOnlySequence <byte>(bytes);
            Assert.That(BytesExtensions.ReadInt(ref sequence, Endianness.LittleEndian), Is.EqualTo(123456789));
            Assert.That(sequence.Length, Is.EqualTo(12));

            var bytes1       = new byte[] { 7, 91 };
            var bytes2       = new byte[] { 205, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var firstSegment = new MemorySegment <byte>(bytes1);
            var lastSegment  = firstSegment.Append(bytes2);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);
            Assert.That(BytesExtensions.ReadInt(ref sequence), Is.EqualTo(123456789));
            Assert.That(sequence.Length, Is.EqualTo(12));
        }
コード例 #6
0
        public void CreateReadOnlySequence()
        {
            var arrayOne   = new[] { 0, 1, 2, 3, 4 };
            var arrayTwo   = new[] { 5, 6, 7, 8, 9 };
            var arrayThree = new[] { 10, 11, 12, 13, 14 };

            var firstSegment = new MemorySegment <int>(arrayOne);
            var lastSegment  = firstSegment.Append(arrayTwo).Append(arrayThree);

            var sequence = new ReadOnlySequence <int>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);

            var ints = new Span <int>(new int[arrayOne.Length + arrayTwo.Length + arrayThree.Length]);

            sequence.Fill(ints);

            Assert.AreEqual(0, sequence.Length);

            var i = 0;

            for (var j = 0; j < arrayOne.Length; j++, i++)
            {
                Assert.AreEqual(arrayOne[j], ints[i]);
            }
            for (var j = 0; j < arrayTwo.Length; j++, i++)
            {
                Assert.AreEqual(arrayTwo[j], ints[i]);
            }
            for (var j = 0; j < arrayThree.Length; j++, i++)
            {
                Assert.AreEqual(arrayThree[j], ints[i]);
            }

            // but maybe creating segments is expensive and a pipe would be better...
        }
コード例 #7
0
        public void TryRead_FromSequenceReader_NotValidSegmentedSequence()
        {
            MemorySegment <byte>    memorySegment1   = new MemorySegment <byte>(new byte[] { 192 });
            MemorySegment <byte>    memorySegment2   = memorySegment1.Append(new byte[] { 0, 0, 0, 0, 0, 2 });
            ReadOnlySequence <byte> readOnlySequence = new ReadOnlySequence <byte>(
                memorySegment1, 0, memorySegment2, memorySegment2.Memory.Length);
            SequenceReader <byte> sequenceReader = new SequenceReader <byte>(readOnlySequence);
            bool isSuccess = VariableLengthIntegerHelper.TryRead(ref sequenceReader,
                                                                 out long value);

            Assert.False(isSuccess);
            Assert.Equal(0, value);
        }
コード例 #8
0
        public void GetInteger_NotValidSegmentedSequence()
        {
            MemorySegment <byte>    memorySegment1   = new MemorySegment <byte>(new byte[] { 192 });
            MemorySegment <byte>    memorySegment2   = memorySegment1.Append(new byte[] { 0, 0, 0, 0, 0, 2 });
            ReadOnlySequence <byte> readOnlySequence = new ReadOnlySequence <byte>(
                memorySegment1, 0, memorySegment2, memorySegment2.Memory.Length);
            long result = VariableLengthIntegerHelper.GetInteger(readOnlySequence,
                                                                 out SequencePosition consumed, out SequencePosition examined);

            Assert.Equal(-1, result);
            Assert.Equal(0, consumed.GetInteger());
            Assert.Equal(6, examined.GetInteger());
        }
コード例 #9
0
        public void TryRead_FromSequenceReader_InitialFourByteLengthMask_SegmentedSequence()
        {
            MemorySegment <byte>    memorySegment1   = new MemorySegment <byte>(new byte[] { 192 });
            MemorySegment <byte>    memorySegment2   = memorySegment1.Append(new byte[] { 0, 0, 0, 0, 0, 0, 2 });
            ReadOnlySequence <byte> readOnlySequence = new ReadOnlySequence <byte>(
                memorySegment1, 0, memorySegment2, memorySegment2.Memory.Length);
            SequenceReader <byte> sequenceReader = new SequenceReader <byte>(readOnlySequence);
            bool isSuccess = VariableLengthIntegerHelper.TryRead(ref sequenceReader,
                                                                 out long value);

            Assert.True(isSuccess);
            Assert.Equal(2, value);
            Assert.Equal(7, sequenceReader.CurrentSpanIndex);
        }
コード例 #10
0
ファイル: MemorySegmentTest.cs プロジェクト: Infarh/MathCore
    public static void Run()
    {
        string[] lines =
        {
            "123.",
            "..\"Hello,.World!,.QWE\".",
            ".Value.",
            "\"123,23",
        };

        var line       = string.Join(',', lines);
        var line_array = line.ToCharArray().AsMemory();
        var values     = CSVParseTest.ParseLine(line_array).ToList();

        var segment_start = new MemorySegment <char>(values[1]);
        var segment_end   = segment_start.Append(values[3]);

        var pp = new Pipe();

        Writer(pp.Writer);
コード例 #11
0
        public void ModbusFrameReader_ReadFrame_ParsesAReadInputRegistersResponse()
        {
            // Given
            var header = new byte[]
            {
                0x00, 0xCE, // Transaction ID
                0x00, 0x00, // Protocol ID
                0x00, 0x09, // Message length
                0x12,       // Device id / Unit id
            };
            var data = new byte[]
            {
                0x04,       // Function code
                0x06,       // Number of bytes more
                0xAA, 0x00, // Register value Hi and Li (AI0)
                0xCC, 0xBB, // Register value Hi and Li (AI1)
                0xEE, 0xDD, // Register value Hi and Li (AI2)
            };

            var first    = new MemorySegment <byte>(header);
            var last     = first.Append(data);
            var sequence = new ReadOnlySequence <byte>(first, 0, last, data.Length);

            // When
            var reader   = new ModbusFrameReader(sequence);
            var position = reader.ReadFrame(out var frame);

            // Then
            Assert.Equal(206, frame.Header.TransactionID);
            Assert.Equal(18, frame.Header.UnitID);

            var response = Assert.IsType <ResponseReadInputRegisters>(frame.Pdu);

            Assert.Equal(
                new ushort[] { 43520, 52411, 61149, },
                response.Results
                );

            Assert.Equal(sequence.End, position);
        }
コード例 #12
0
        public async Task MultiSegmentSendWillNotSendEmptyEndOfMessageFrame()
        {
            using (var feature = new TestWebSocketConnectionFeature())
            {
                var serverSocket = await feature.AcceptAsync();

                var firstSegment  = new byte[] { 1 };
                var secondSegment = new byte[] { 15 };

                var first = new MemorySegment <byte>(firstSegment);
                var last  = first.Append(secondSegment);

                var sequence = new ReadOnlySequence <byte>(first, 0, last, last.Memory.Length);

                Assert.False(sequence.IsSingleSegment);

                await serverSocket.SendAsync(sequence, WebSocketMessageType.Text);

                // Run the client socket
                var client = feature.Client.ExecuteAndCaptureFramesAsync();

                await serverSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, default);

                var messages = await client.TimeoutAfterAsync(TimeSpan.FromSeconds(5));

                Assert.Equal(2, messages.Received.Count);

                // First message: 1 byte, endOfMessage false
                Assert.Single(messages.Received[0].Buffer);
                Assert.Equal(1, messages.Received[0].Buffer[0]);
                Assert.False(messages.Received[0].EndOfMessage);

                // Second message: 1 byte, endOfMessage true
                Assert.Single(messages.Received[1].Buffer);
                Assert.Equal(15, messages.Received[1].Buffer[0]);
                Assert.True(messages.Received[1].EndOfMessage);
            }
        }
コード例 #13
0
        public ReadOnlySequence <T> ToSequence()
        {
            var a1 = ArrayOne();
            var a2 = ArrayTwo();

            if (a1.IsEmpty && a2.IsEmpty)
            {
                return(ReadOnlySequence <T> .Empty);
            }
            if (a1.IsEmpty)
            {
                return(new ReadOnlySequence <T>(a2));
            }
            if (a2.IsEmpty)
            {
                return(new ReadOnlySequence <T>(a1));
            }

            var first  = new MemorySegment(a1);
            var second = first.Append(a2);

            return(new ReadOnlySequence <T>(first, 0, second, a2.Length));
        }
コード例 #14
0
        public void FillSequence()
        {
            var sequence = new ReadOnlySequence <byte>(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            var bytes    = new byte[20];
            var span     = new Span <byte>(bytes);

            try
            {
                span.Fill(ref sequence);
                Assert.Fail("Expected exception.");
            }
            catch (ArgumentOutOfRangeException) { /* expected */ }

            bytes = new byte[8];
            span  = new Span <byte>(bytes);

            span.Fill(ref sequence);
            Assert.That(sequence.Length, Is.EqualTo(2));
            for (var i = 0; i < 8; i++)
            {
                Assert.That(bytes[i], Is.EqualTo(i));
            }

            var bytes1       = new byte[] { 0, 1, 2, 3, 4 };
            var bytes2       = new byte[] { 5, 6, 7, 8, 9 };
            var firstSegment = new MemorySegment <byte>(bytes1);
            var lastSegment  = firstSegment.Append(bytes2);

            sequence = new ReadOnlySequence <byte>(firstSegment, 0, lastSegment, lastSegment.Memory.Length);

            span.Fill(ref sequence);
            Assert.That(sequence.Length, Is.EqualTo(2));
            for (var i = 0; i < 8; i++)
            {
                Assert.That(bytes[i], Is.EqualTo(i));
            }
        }
コード例 #15
0
        public void ModbusFrameReader_ReadFrame_ParsesAWriteSingleCoilResponseTurnedOff()
        {
            // Given
            var header = new byte[]
            {
                0x00, 0x01, // Transaction ID
                0x00, 0x00, // Protocol ID
                0x00, 0x06, // Message length
                0x09,       // Device id / Unit id
            };
            var data = new byte[]
            {
                0x05,       // Function code
                0x00,       // Hi Register Address byte
                0x04,       // Lo Register Address byte
                0x00,       // Hi Byte Meaning
                0x00,       // Lo Byte Meaning
            };

            var first    = new MemorySegment <byte>(header);
            var last     = first.Append(data);
            var sequence = new ReadOnlySequence <byte>(first, 0, last, 5);

            // When
            var reader   = new ModbusFrameReader(sequence);
            var position = reader.ReadFrame(out var frame);

            // Then
            Assert.Equal(1, frame.Header.TransactionID);
            Assert.Equal(9, frame.Header.UnitID);

            var response = Assert.IsType <ResponseWriteSingleCoil>(frame.Pdu);

            Assert.False(response.Result);

            Assert.Equal(sequence.End, position);
        }
コード例 #16
0
        public void ModbusFrameReader_ReadFrame_ParsesAWriteMultipleCoilsResponse()
        {
            // Given
            var header = new byte[]
            {
                0x00, 0x01, // Transaction ID
                0x00, 0x00, // Protocol ID
                0x00, 0x06, // Message length
                0x20,       // Device id / Unit id
            };
            var data = new byte[]
            {
                0x0F,       // Function code
                0x00,       // Address of the first byte of register Hi
                0x12,       // Address of the first byte of register Lo
                0x00,       // Number of recorded reg. Hi byte
                0x04,       // Number of recorded reg. Lo bytes
            };

            var first    = new MemorySegment <byte>(header);
            var last     = first.Append(data);
            var sequence = new ReadOnlySequence <byte>(first, 0, last, data.Length);

            // When
            var reader   = new ModbusFrameReader(sequence);
            var position = reader.ReadFrame(out var frame);

            // Then
            Assert.Equal(1, frame.Header.TransactionID);
            Assert.Equal(32, frame.Header.UnitID);

            var response = Assert.IsType <ResponseWriteMultipleCoils>(frame.Pdu);

            Assert.Equal(18, response.Address);
            Assert.Equal(4, response.Quantity);
        }
コード例 #17
0
        public void ModbusFrameReader_ReadFrame_ParsesAReadDiscreteInputsResponse()
        {
            // Given
            var header = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x09, };
            var data   = new byte[] { 0x02, 0x01, 0x01, };

            var first    = new MemorySegment <byte>(header);
            var last     = first.Append(data);
            var sequence = new ReadOnlySequence <byte>(first, 0, last, 3);

            // When
            var reader   = new ModbusFrameReader(sequence);
            var position = reader.ReadFrame(out var frame);

            // Then
            Assert.Equal(1, frame.Header.TransactionID);
            Assert.Equal(9, frame.Header.UnitID);

            var read = Assert.IsType <ResponseReadDiscreteInputs>(frame.Pdu);

            Assert.Equal(new byte[] { 1 }, read.Coils);

            Assert.Equal(sequence.End, position);
        }