예제 #1
0
        public async ValueTask Connect()
        {
            var host          = _server.Host;
            var networkStream = await GetStream(_server);

            if (_encrypt)
            {
                networkStream = await EncryptStream(networkStream, host);
            }

            _networkstream = networkStream;

            _pipeline   = new ChunkingPipeline(networkStream, ChunkSize);
            _pipeReader = PipeReader.Create(_networkstream);

            _pipeWriter = PipeWriter.Create(_networkstream);
        }
        public async Task Send_GivenSequenceIsUnderChunkSize_ShouldWriteArrayOnce()
        {
            //Arrange
            var a          = new byte[] { 0x00, 0x01, 0x02, 0x03 };
            var b          = new byte[] { 0x04, 0x05, 0x06, 0x07 };
            var sequence   = new SequenceBuilder <byte>().Append(a).Append(b).Build();
            var mockStream = new MockStream();
            var sut        = new ChunkingPipeline(mockStream, 9);

            //Act
            await sut.Send(sequence);

            //Assert
            var expected = sequence.ToArray();
            var actual   = mockStream.GetReadOnlySequence();

            actual.ToArray().Should().Equal(expected);
            actual.IsSingleSegment.Should().BeTrue();
        }
        [InlineData(4, 6, 1, 4, 7, null)]  // Can merge b and c
        public async Task Send_GivenSequenceIsOverChunkSize_ShouldWriteMultipleArrays(int length1, int length2, int length3, int expected1, int expected2, int?expected3)
        {
            //Arrange
            var a          = Enumerable.Range(0, length1).Select(i => (byte)i).ToArray();
            var b          = Enumerable.Range(length1, length2).Select(i => (byte)i).ToArray();
            var c          = Enumerable.Range(length1 + length2, length3).Select(i => (byte)i).ToArray();
            var sequence   = new SequenceBuilder <byte>().Append(a).Append(b).Append(c).Build();
            var mockStream = new MockStream();
            var sut        = new ChunkingPipeline(mockStream, 8);

            //Act
            await sut.Send(sequence);

            //Assert
            var expected = sequence.ToArray();
            var actual   = mockStream.GetReadOnlySequence();

            actual.ToArray().Should().Equal(expected);
            GetNumberOfSegments(actual).Should().Be(expected3.HasValue ? 3 : 2);

            var segmentNumber = 0;

            foreach (var segment in actual)
            {
                switch (segmentNumber)
                {
                case 0:
                    segment.Length.Should().Be(expected1);
                    break;

                case 1:
                    segment.Length.Should().Be(expected2);
                    break;

                case 2:
                    expected3.Should().NotBeNull();
                    segment.Length.Should().Be(expected3);
                    break;
                }
                ++segmentNumber;
            }
        }