private async Task <IByteBuffer> ReceiveBufferAsync()
        {
            try
            {
                var messageSizeBytes = new byte[4];
                await _stream.ReadBytesAsync(messageSizeBytes, 0, 4, _backgroundTaskCancellationToken).ConfigureAwait(false);

                var messageSize = BitConverter.ToInt32(messageSizeBytes, 0);
                EnsureMessageSizeIsValid(messageSize);
                var inputBufferChunkSource = new InputBufferChunkSource(BsonChunkPool.Default);
                var buffer = ByteBufferFactory.Create(inputBufferChunkSource, messageSize);
                buffer.Length = messageSize;
                buffer.SetBytes(0, messageSizeBytes, 0, 4);
                await _stream.ReadBytesAsync(buffer, 4, messageSize - 4, _backgroundTaskCancellationToken).ConfigureAwait(false);

                _lastUsedAtUtc = DateTime.UtcNow;
                buffer.MakeReadOnly();
                return(buffer);
            }
            catch (Exception ex)
            {
                var wrappedException = WrapException(ex, "receiving a message from the server");
                ConnectionFailed(wrappedException);
                throw wrappedException;
            }
        }
        private async Task <bool> ReceiveBackgroundTask(CancellationToken cancellationToken)
        {
            try
            {
                var messageSizeBytes = new byte[4];
                await _stream.FillBufferAsync(messageSizeBytes, 0, 4, cancellationToken).ConfigureAwait(false);

                var messageSize = BitConverter.ToInt32(messageSizeBytes, 0);
                var buffer      = ByteBufferFactory.Create(BsonChunkPool.Default, messageSize);
                buffer.WriteBytes(0, messageSizeBytes, 0, 4);
                await _stream.FillBufferAsync(buffer, 4, messageSize - 4, cancellationToken).ConfigureAwait(false);

                _lastUsedAtUtc = DateTime.UtcNow;
                var responseToBytes = new byte[4];
                buffer.ReadBytes(8, responseToBytes, 0, 4);
                var responseTo = BitConverter.ToInt32(responseToBytes, 0);
                _inboundDropbox.Post(responseTo, buffer);
                return(true);
            }
            catch (Exception ex)
            {
                ConnectionFailed(ex);
                return(false);
            }
        }
Beispiel #3
0
        public void Create_should_throw_when_minimumCapacity_is_invalid(
            [Values(-1, 0)]
            int minimumCapacity)
        {
            var mockChunkSource = new Mock <IBsonChunkSource>();

            Action action = () => ByteBufferFactory.Create(mockChunkSource.Object, minimumCapacity);

            action.ShouldThrow <ArgumentOutOfRangeException>().And.ParamName.Should().Be("minimumCapacity");
        }
Beispiel #4
0
        public void Create_should_return_MultiChunkBuffer_when_multiple_chunks_are_required(
            [Values(65, 128)]
            int minimumCapacity)
        {
            var chunkSource = new BsonChunkPool(1, 64);

            var result = ByteBufferFactory.Create(chunkSource, minimumCapacity);

            result.Should().BeOfType <MultiChunkBuffer>();
        }
Beispiel #5
0
        public void Create_should_return_SingleChunkBuffer_when_a_single_chunk_is_sufficient(
            [Values(1, 63, 64)]
            int minimumCapacity)
        {
            var chunkSource = new BsonChunkPool(1, 64);

            var result = ByteBufferFactory.Create(chunkSource, minimumCapacity);

            result.Should().BeOfType <SingleChunkBuffer>();
        }
Beispiel #6
0
        public void Create_should_return_expected_result(
            [Values(1, 63, 64, 65, 128)]
            int minimumCapacity)
        {
            var chunkSource = new BsonChunkPool(1, 64);

            var result = ByteBufferFactory.Create(chunkSource, minimumCapacity);

            result.Capacity.Should().BeGreaterOrEqualTo(minimumCapacity);
        }
Beispiel #7
0
        public void TestGetMultipleChunkSlice()
        {
            var chunkSize = BsonChunkPool.Default.ChunkSize;
            var length    = chunkSize * 3;

            using (var buffer = ByteBufferFactory.Create(BsonChunkPool.Default, length))
            {
                buffer.MakeReadOnly();
                var slice = buffer.GetSlice(chunkSize, chunkSize + 1);
                Assert.IsInstanceOf <MultiChunkBuffer>(slice);
            }
        }
        public void TestGetSingleChunkSlice()
        {
            var chunkSize = BsonChunkPool.Default.ChunkSize;
            var capacity  = chunkSize * 3;

            using (var buffer = ByteBufferFactory.Create(BsonChunkPool.Default, capacity))
            {
                buffer.Length = capacity;
                buffer.MakeReadOnly();
                var slice = buffer.GetSlice(chunkSize, 1);
                Assert.IsInstanceOf <SingleChunkBuffer>(slice);
            }
        }
Beispiel #9
0
        public void Create_should_throw_when_chunkSource_is_null()
        {
            Action action = () => ByteBufferFactory.Create(null, 0);

            action.ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("chunkSource");
        }