public override void ChannelActive(IChannelHandlerContext ctx)
            {
                CompositeBufferPartialWriteDoesNotCorruptDataInitServerConfig(ctx.Channel.Configuration, _soSndBuf);
                // First single write
                int offset = _soSndBuf - 100;

                ctx.WriteAsync(_expectedContent.RetainedSlice(_expectedContent.ReaderIndex, offset));

                // Build and write CompositeByteBuf
                CompositeByteBuffer compositeByteBuf = ctx.Allocator.CompositeBuffer();

                compositeByteBuf.AddComponent(true,
                                              _expectedContent.RetainedSlice(_expectedContent.ReaderIndex + offset, 50));
                offset += 50;
                compositeByteBuf.AddComponent(true,
                                              _expectedContent.RetainedSlice(_expectedContent.ReaderIndex + offset, 200));
                offset += 200;
                ctx.WriteAsync(compositeByteBuf);

                // Write a single buffer that is smaller than the second component of the CompositeByteBuf
                // above but small enough to fit in the remaining space allowed by the soSndBuf amount.
                ctx.WriteAsync(_expectedContent.RetainedSlice(_expectedContent.ReaderIndex + offset, 50));
                offset += 50;

                // Write the remainder of the content
                ctx.WriteAndFlushAsync(_expectedContent.RetainedSlice(_expectedContent.ReaderIndex + offset,
                                                                      _expectedContent.ReadableBytes - _expectedContent.ReaderIndex - offset))
                .CloseOnComplete(ctx.Channel);
            }
        public void ReferenceCounts3()
        {
            IByteBuffer c1 = Unpooled.Buffer().WriteByte(1);
            var         c2 = (IByteBuffer)Unpooled.Buffer().WriteByte(2).Retain();
            var         c3 = (IByteBuffer)Unpooled.Buffer().WriteByte(3).Retain(2);

            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

            Assert.Equal(1, buf.ReferenceCount);

            var components = new List <IByteBuffer>
            {
                c1,
                c2,
                c3
            };

            buf.AddComponents(components);

            // Ensure that c[123]'s refCount did not change.
            Assert.Equal(1, c1.ReferenceCount);
            Assert.Equal(2, c2.ReferenceCount);
            Assert.Equal(3, c3.ReferenceCount);

            Assert.Equal(1, buf[0].ReferenceCount);
            Assert.Equal(2, buf[1].ReferenceCount);
            Assert.Equal(3, buf[2].ReferenceCount);

            c3.Release(2);
            c2.Release();
            buf.Release();
        }
Exemple #3
0
        protected static CompositeByteBuffer ToLeakAwareBuffer(CompositeByteBuffer buf)
        {
            IResourceLeakTracker leak;

            switch (ResourceLeakDetector.Level)
            {
            case ResourceLeakDetector.DetectionLevel.Simple:
                leak = AbstractByteBuffer.LeakDetector.Track(buf);
                if (leak != null)
                {
                    buf = new SimpleLeakAwareCompositeByteBuffer(buf, leak);
                }
                break;

            case ResourceLeakDetector.DetectionLevel.Advanced:
            case ResourceLeakDetector.DetectionLevel.Paranoid:
                leak = AbstractByteBuffer.LeakDetector.Track(buf);
                if (leak != null)
                {
                    buf = new AdvancedLeakAwareCompositeByteBuffer(buf, leak);
                }
                break;

            case ResourceLeakDetector.DetectionLevel.Disabled:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(buf);
        }
Exemple #4
0
        public void TestNioBuffersExpand2()
        {
            TestChannel channel = new TestChannel();

            ChannelOutboundBuffer buffer = new ChannelOutboundBuffer(channel);

            CompositeByteBuffer comp = Unpooled.CompositeBuffer(256);
            IByteBuffer         buf  = Unpooled.DirectBuffer().WriteBytes(Encoding.ASCII.GetBytes("buf1"));

            for (int i = 0; i < 65; i++)
            {
                comp.AddComponent(true, buf.Copy());
            }
            buffer.AddMessage(comp, comp.ReadableBytes, channel.VoidPromise());

            Assert.Equal(0, buffer.NioBufferCount); // Should still be 0 as not flushed yet
            buffer.AddFlush();
            var buffers = buffer.GetSharedBufferList();

            Assert.Equal(65, buffer.NioBufferCount);
            for (int i = 0; i < buffer.NioBufferCount; i++)
            {
                if (i < 65)
                {
                    Assert.Equal(buffers[i], buf.GetIoBuffer(buf.ReaderIndex, buf.ReadableBytes));
                }
                else
                {
                    Assert.Null(buffers[i].Array);
                }
            }
            Release(buffer);
            buf.Release();
        }
Exemple #5
0
        public void TestNioBuffersMaxCount()
        {
            TestChannel channel = new TestChannel();

            ChannelOutboundBuffer buffer = new ChannelOutboundBuffer(channel);

            CompositeByteBuffer comp = Unpooled.CompositeBuffer(256);
            IByteBuffer         buf  = Unpooled.DirectBuffer().WriteBytes(Encoding.ASCII.GetBytes("buf1"));

            for (int i = 0; i < 65; i++)
            {
                comp.AddComponent(true, buf.Copy());
            }
            Assert.Equal(65, comp.IoBufferCount);
            buffer.AddMessage(comp, comp.ReadableBytes, channel.VoidPromise());
            Assert.Equal(0, buffer.NioBufferCount); // Should still be 0 as not flushed yet
            buffer.AddFlush();
            int maxCount = 10;                      // less than comp.nioBufferCount()
            var buffers  = buffer.GetSharedBufferList(maxCount, int.MaxValue);

            Assert.True(buffer.NioBufferCount <= maxCount); // Should not be greater than maxCount
            for (int i = 0; i < buffer.NioBufferCount; i++)
            {
                Assert.Equal(buffers[i], buf.GetIoBuffer(buf.ReaderIndex, buf.ReadableBytes));
            }
            Release(buffer);
            buf.Release();
        }
        protected sealed override IByteBuffer Wrap(CompositeByteBuffer buffer)
        {
            var tracker      = new NoopResourceLeakTracker();
            var leakAwareBuf = (WrappedCompositeByteBuffer)this.Wrap(buffer, tracker);

            this.trackers.Enqueue(tracker);
            return(leakAwareBuf);
        }
        public void DuplicateEmpty()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

            Assert.Equal(0, buf.NumComponents);
            Assert.Equal(0, buf.Duplicate().ReadableBytes);

            buf.Release();
        }
Exemple #8
0
        public IByteBuffer ToByteBuffer()
        {
            CompositeByteBuffer compositeBuffer = ArrayPooled.CompositeBuffer();
            _ = compositeBuffer.AddComponents(this.value);
            _ = compositeBuffer.SetWriterIndex(this.size);
            _ = compositeBuffer.SetReaderIndex(0);

            return compositeBuffer;
        }
Exemple #9
0
        private IByteBuffer CompressContent(IChannelHandlerContext ctx, WebSocketFrame msg)
        {
            if (_encoder is null)
            {
                _encoder = new EmbeddedChannel(
                    ZlibCodecFactory.NewZlibEncoder(
                        ZlibWrapper.None,
                        _compressionLevel,
                        _windowSize,
                        8));
            }

            _ = _encoder.WriteOutbound(msg.Content.Retain());

            CompositeByteBuffer fullCompressedContent = ctx.Allocator.CompositeBuffer();

            while (true)
            {
                var partCompressedContent = _encoder.ReadOutbound <IByteBuffer>();
                if (partCompressedContent is null)
                {
                    break;
                }

                if (!partCompressedContent.IsReadable())
                {
                    _ = partCompressedContent.Release();
                    continue;
                }

                _ = fullCompressedContent.AddComponent(true, partCompressedContent);
            }

            if (fullCompressedContent.NumComponents <= 0)
            {
                _ = fullCompressedContent.Release();
                ThrowHelper.ThrowCodecException_CannotReadCompressedBuf();
            }

            if (msg.IsFinalFragment && _noContext)
            {
                Cleanup();
            }

            IByteBuffer compressedContent;

            if (RemoveFrameTail(msg))
            {
                int realLength = fullCompressedContent.ReadableBytes - FrameTail.ReadableBytes;
                compressedContent = fullCompressedContent.Slice(0, realLength);
            }
            else
            {
                compressedContent = fullCompressedContent;
            }
            return(compressedContent);
        }
Exemple #10
0
        public void Run(AddressBook addressBook, bool isCompositeBuffer)
        {
            var channel = new EmbeddedChannel(
                new ProtobufVarint32FrameDecoder(),
                new ProtobufDecoder(AddressBook.Parser),
                new ProtobufVarint32LengthFieldPrepender(),
                new ProtobufEncoder());

            Assert.True(channel.WriteOutbound(addressBook));
            var buffer = channel.ReadOutbound <IByteBuffer>();

            Assert.NotNull(buffer);
            Assert.True(buffer.ReadableBytes > 0);

            var data = new byte[buffer.ReadableBytes];

            buffer.ReadBytes(data);

            IByteBuffer inputBuffer;

            if (isCompositeBuffer)
            {
                inputBuffer = new CompositeByteBuffer(UnpooledByteBufferAllocator.Default, 2,
                                                      Unpooled.CopiedBuffer(data, 0, 2),
                                                      Unpooled.CopiedBuffer(data, 2, data.Length - 2));
            }
            else
            {
                inputBuffer = Unpooled.WrappedBuffer(data);
            }

            Assert.True(channel.WriteInbound(inputBuffer));

            var message = channel.ReadInbound <IMessage>();

            Assert.NotNull(message);
            Assert.IsType <AddressBook>(message);
            var roundTripped = (AddressBook)message;

            Assert.Equal(addressBook.People.Count, roundTripped.People.Count);
            for (int i = 0; i < addressBook.People.Count; i++)
            {
                Assert.Equal(addressBook.People[i].Id, roundTripped.People[i].Id);
                Assert.Equal(addressBook.People[i].Email, roundTripped.People[i].Email);
                Assert.Equal(addressBook.People[i].Name, roundTripped.People[i].Name);

                Assert.Equal(addressBook.People[i].Phones.Count, roundTripped.People[i].Phones.Count);
                for (int j = 0; j < addressBook.People[i].Phones.Count; j++)
                {
                    Assert.Equal(addressBook.People[i].Phones[j].Type, roundTripped.People[i].Phones[j].Type);
                    Assert.Equal(addressBook.People[i].Phones[j].Number, roundTripped.People[i].Phones[j].Number);
                }
            }

            Assert.False(channel.Finish());
        }
        public void RemoveLastComponent()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 1, 2 }));
            Assert.Equal(1, buf.NumComponents);
            buf.RemoveComponent(0);
            Assert.Equal(0, buf.NumComponents);
            buf.Release();
        }
        public IByteBuffer ToByteBuffer()
        {
            CompositeByteBuffer compositeBuffer = Unpooled.CompositeBuffer(this.value.Count);

            compositeBuffer.AddComponents(this.value);
            compositeBuffer.SetWriterIndex(this.size);
            compositeBuffer.SetReaderIndex(0);

            return(compositeBuffer);
        }
Exemple #13
0
        public void TestCompositeBufferDecoder()
        {
            string      s               = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFiY2Rl";
            IByteBuffer src1            = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(s.Substring(0, 10)));
            IByteBuffer src2            = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(s.Substring(10)));
            var         src             = new CompositeByteBuffer(src1.Allocator, 2, src1, src2);
            IByteBuffer expectedDecoded = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"));

            TestDecode(src, expectedDecoded);
        }
Exemple #14
0
        static IByteBuffer NewCompositeBuffer(IByteBufferAllocator alloc)
        {
            CompositeByteBuffer compositeByteBuf = alloc.CompositeBuffer();

            compositeByteBuf.AddComponent(true, alloc.DirectBuffer(4).WriteInt(100));
            compositeByteBuf.AddComponent(true, alloc.DirectBuffer(8).WriteLong(123));
            compositeByteBuf.AddComponent(true, alloc.DirectBuffer(8).WriteLong(456));
            Assert.Equal(ExpectedBytes, compositeByteBuf.ReadableBytes);
            return(compositeByteBuf);
        }
        public void ComponentMustBeDuplicate()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

            buf.AddComponent(Unpooled.Buffer(4, 6).SetIndex(1, 3));
            Assert.IsAssignableFrom <AbstractDerivedByteBuffer>(buf[0]);
            Assert.Equal(4, buf[0].Capacity);
            Assert.Equal(6, buf[0].MaxCapacity);
            Assert.Equal(2, buf[0].ReadableBytes);
            buf.Release();
        }
        public void AddEmptyBufferRelease()
        {
            CompositeByteBuffer cbuf = Unpooled.CompositeBuffer();
            IByteBuffer         buf  = Unpooled.Buffer();

            Assert.Equal(1, buf.ReferenceCount);
            cbuf.AddComponent(buf);
            Assert.Equal(1, buf.ReferenceCount);

            cbuf.Release();
            Assert.Equal(0, buf.ReferenceCount);
        }
Exemple #17
0
            public override void AddFrame(IByteBuffer data)
            {
                if (!(Element.Buffer is CompositeByteBuffer))
                {
                    throw new DicomImagingException("Expected pixel data element to have a CompositeByteBuffer");
                }

                CompositeByteBuffer buffer = Element.Buffer as CompositeByteBuffer;

                buffer.Buffers.Add(data);

                NumberOfFrames++;
            }
        public void CopyEmpty()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

            Assert.Equal(0, buf.NumComponents);

            IByteBuffer copy = buf.Copy();

            Assert.Equal(0, copy.ReadableBytes);

            buf.Release();
            copy.Release();
        }
Exemple #19
0
        public void Run(Person.Types.PhoneType phoneType, string number, bool isCompositeBuffer)
        {
            var phoneNumber = new Person.Types.PhoneNumber
            {
                Type   = phoneType,
                Number = number
            };

            var channel = new EmbeddedChannel(
                new ProtobufVarint32FrameDecoder(),
                new ProtobufDecoder(Person.Types.PhoneNumber.Parser),
                new ProtobufVarint32LengthFieldPrepender(),
                new ProtobufEncoder());

            Assert.True(channel.WriteOutbound(phoneNumber));
            var buffer = channel.ReadOutbound <IByteBuffer>();

            Assert.NotNull(buffer);
            Assert.True(buffer.ReadableBytes > 0);

            var data = new byte[buffer.ReadableBytes];

            buffer.ReadBytes(data);

            IByteBuffer inputBuffer;

            if (isCompositeBuffer)
            {
                inputBuffer = new CompositeByteBuffer(UnpooledByteBufferAllocator.Default, 2,
                                                      Unpooled.CopiedBuffer(data, 0, 2),
                                                      Unpooled.CopiedBuffer(data, 2, data.Length - 2));
            }
            else
            {
                inputBuffer = Unpooled.WrappedBuffer(data);
            }

            Assert.True(channel.WriteInbound(inputBuffer));

            var message = channel.ReadInbound <IMessage>();

            Assert.NotNull(message);
            Assert.IsType <Person.Types.PhoneNumber>(message);
            var roundTripped = (Person.Types.PhoneNumber)message;

            Assert.Equal(phoneNumber.Type, roundTripped.Type);
            Assert.Equal(phoneNumber.Number, roundTripped.Number);

            Assert.False(channel.Finish());
        }
Exemple #20
0
        public void CompositeDirectBuffer(bool preferDirect)
        {
            IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
            CompositeByteBuffer  buffer    = allocator.CompositeDirectBuffer();

            try
            {
                this.AssertCompositeByteBuffer(buffer, this.DefaultMaxComponents);
            }
            finally
            {
                buffer.Release();
            }
        }
        public void CompositeHeapBufferWithCapacity()
        {
            IByteBufferAllocator allocator = this.NewAllocator();
            CompositeByteBuffer  buffer    = allocator.CompositeHeapBuffer(8);

            try
            {
                this.AssertCompositeByteBuffer(buffer, 8);
            }
            finally
            {
                buffer.Release();
            }
        }
Exemple #22
0
        public void CompositeDirectBufferWithCapacity(bool preferDirect, int maxNumComponents)
        {
            IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
            CompositeByteBuffer  buffer    = allocator.CompositeDirectBuffer(maxNumComponents);

            try
            {
                this.AssertCompositeByteBuffer(buffer, maxNumComponents);
            }
            finally
            {
                buffer.Release();
            }
        }
        public void NestedLayout()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

            buf.AddComponent(
                Unpooled.CompositeBuffer()
                .AddComponent(Unpooled.WrappedBuffer(new byte[] { 1, 2 }))
                .AddComponent(Unpooled.WrappedBuffer(new byte[] { 3, 4 })).Slice(1, 2));

            ArraySegment <byte>[] nioBuffers = buf.GetIoBuffers(0, 2);
            Assert.Equal(2, nioBuffers.Length);
            Assert.Equal((byte)2, nioBuffers[0].Array[nioBuffers[0].Offset]);
            Assert.Equal((byte)3, nioBuffers[1].Array[nioBuffers[1].Offset]);
            buf.Release();
        }
 protected override void Dispose(bool disposing)
 {
     base.Dispose(disposing);
     if (disposing)
     {
         TaskCompletionSource <int> p = _readCompletionSource;
         if (p is object)
         {
             _readCompletionSource = null;
             _ = p.TrySetResult(0);
         }
         _ownedInputBuffer.SafeRelease();
         _ownedInputBuffer = null;
     }
 }
        public void FullConsolidation()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer(int.MaxValue);

            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 1 }));
            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 2, 3 }));
            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 4, 5, 6 }));
            buf.Consolidate();

            Assert.Equal(1, buf.NumComponents);
            Assert.True(buf.HasArray);
            Assert.NotNull(buf.Array);
            Assert.Equal(0, buf.ArrayOffset);

            buf.Release();
        }
Exemple #26
0
        public void GetByteRange_WhenBufferIsSmallerThanRequestedCount_Throws()
        {
            // Arrange
            var bytes1              = Enumerable.Repeat((byte)1, 10).ToArray();
            var bytes2              = Enumerable.Repeat((byte)2, 10).ToArray();
            var bytes3              = Enumerable.Repeat((byte)3, 10).ToArray();
            var memoryByteBuffer1   = new MemoryByteBuffer(bytes1);
            var memoryByteBuffer2   = new MemoryByteBuffer(bytes2);
            var memoryByteBuffer3   = new MemoryByteBuffer(bytes3);
            var compositeByteBuffer = new CompositeByteBuffer(memoryByteBuffer1, memoryByteBuffer2, memoryByteBuffer3);

            // Act + Assert
            var actual = new byte[10];

            Assert.Throws <ArgumentException>(() => compositeByteBuffer.GetByteRange(0, 20, actual));
        }
Exemple #27
0
        public static ReadableBuffer Composite(IEnumerable <ReadableBuffer> buffers)
        {
            Contract.Requires(buffers != null);

            CompositeByteBuffer composite = Unpooled.CompositeBuffer();

            foreach (ReadableBuffer buf in buffers)
            {
                IByteBuffer byteBuffer = buf.buffer;
                if (byteBuffer.ReadableBytes > 0)
                {
                    composite.AddComponent(byteBuffer);
                }
            }

            return(new ReadableBuffer(composite));
        }
        public void RangedConsolidation()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer(int.MaxValue);

            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 1 }));
            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 2, 3 }));
            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 4, 5, 6 }));
            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 7, 8, 9, 10 }));
            buf.Consolidate(1, 2);

            Assert.Equal(3, buf.NumComponents);
            Assert.Equal(Unpooled.WrappedBuffer(new byte[] { 1 }), buf[0]);
            Assert.Equal(Unpooled.WrappedBuffer(new byte[] { 2, 3, 4, 5, 6 }), buf[1]);
            Assert.Equal(Unpooled.WrappedBuffer(new byte[] { 7, 8, 9, 10 }), buf[2]);

            buf.Release();
        }
Exemple #29
0
            public override void AddFrame(IByteBuffer data)
            {
                if (!(Element.Buffer is CompositeByteBuffer))
                {
                    throw new DicomImagingException("Expected pixel data element to have a CompositeByteBuffer.");
                }

                CompositeByteBuffer buffer = Element.Buffer as CompositeByteBuffer;

                if (BytesAllocated == 1)
                {
                    data = new SwapByteBuffer(data, 2);
                }
                buffer.Buffers.Add(data);

                NumberOfFrames++;
            }
        public void ForEachByteUnderLeakDetectionShouldNotThrowException()
        {
            CompositeByteBuffer buf = (CompositeByteBuffer)NewBuffer(8);

            Assert.True(buf is SimpleLeakAwareCompositeByteBuffer);
            CompositeByteBuffer comp = (CompositeByteBuffer)NewBuffer(8);

            Assert.True(comp is SimpleLeakAwareCompositeByteBuffer);

            IByteBuffer inner = comp.Allocator.DirectBuffer(1).WriteByte(0);

            comp.AddComponent(true, inner);
            buf.AddComponent(true, comp);

            Assert.Equal(-1, buf.ForEachByte(new AlwaysByteProcessor()));
            Assert.True(buf.Release());
        }