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();
        }
        public void DuplicateEmpty()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer();

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

            buf.Release();
        }
Esempio n. 3
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);
        }
        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 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);
        }
        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();
        }
        public void CompositeHeapBufferWithCapacity()
        {
            IByteBufferAllocator allocator = this.NewAllocator();
            CompositeByteBuffer  buffer    = allocator.CompositeHeapBuffer(8);

            try
            {
                this.AssertCompositeByteBuffer(buffer, 8);
            }
            finally
            {
                buffer.Release();
            }
        }
Esempio n. 9
0
        public void CompositeDirectBuffer(bool preferDirect)
        {
            IByteBufferAllocator allocator = this.NewAllocator(preferDirect);
            CompositeByteBuffer  buffer    = allocator.CompositeDirectBuffer();

            try
            {
                this.AssertCompositeByteBuffer(buffer, this.DefaultMaxComponents);
            }
            finally
            {
                buffer.Release();
            }
        }
Esempio n. 10
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();
        }
        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();
        }
        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();
        }
        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());
        }
        public void AddEmptyBufferInMiddle()
        {
            CompositeByteBuffer cbuf = Unpooled.CompositeBuffer();
            IByteBuffer         buf1 = Unpooled.Buffer().WriteByte(1);

            cbuf.AddComponent(true, buf1);
            cbuf.AddComponent(true, Unpooled.Empty);
            IByteBuffer buf3 = Unpooled.Buffer().WriteByte(2);

            cbuf.AddComponent(true, buf3);

            Assert.Equal(2, cbuf.ReadableBytes);
            Assert.Equal((byte)1, cbuf.ReadByte());
            Assert.Equal((byte)2, cbuf.ReadByte());

            Assert.Same(Unpooled.Empty, cbuf.InternalComponent(1));
            Assert.NotSame(Unpooled.Empty, cbuf.InternalComponentAtOffset(1));
            cbuf.Release();
        }
Esempio n. 16
0
            public IByteBuffer Cumulate(IByteBufferAllocator alloc, IByteBuffer cumulation, IByteBuffer input)
            {
                if (!cumulation.IsReadable())
                {
                    _ = cumulation.Release();
                    return(input);
                }
                CompositeByteBuffer composite = null;

                try
                {
                    composite = cumulation as CompositeByteBuffer;
                    if (composite is object && 0u >= (uint)(cumulation.ReferenceCount - 1))
                    {
                        // Writer index must equal capacity if we are going to "write"
                        // new components to the end
                        if (composite.WriterIndex != composite.Capacity)
                        {
                            _ = composite.AdjustCapacity(composite.WriterIndex);
                        }
                    }
                    else
                    {
                        composite = alloc.CompositeBuffer(int.MaxValue).AddFlattenedComponents(true, cumulation);
                    }
                    _     = composite.AddFlattenedComponents(true, input);
                    input = null;
                    return(composite);
                }
                finally
                {
                    if (input is object)
                    {
                        // We must release if the ownership was not transferred as otherwise it may produce a leak
                        _ = input.Release();
                        // Also release any new buffer allocated if we're not returning it
                        if (composite is object && composite != cumulation)
                        {
                            _ = composite.Release();
                        }
                    }
                }
            }
        public void AddEmptyBuffersRelease()
        {
            CompositeByteBuffer cbuf = Unpooled.CompositeBuffer();
            IByteBuffer         buf  = Unpooled.Buffer();
            IByteBuffer         buf2 = Unpooled.Buffer().WriteInt(1);
            IByteBuffer         buf3 = Unpooled.Buffer();

            Assert.Equal(1, buf.ReferenceCount);
            Assert.Equal(1, buf2.ReferenceCount);
            Assert.Equal(1, buf3.ReferenceCount);

            cbuf.AddComponents(buf, buf2, buf3);
            Assert.Equal(1, buf.ReferenceCount);
            Assert.Equal(1, buf2.ReferenceCount);
            Assert.Equal(1, buf3.ReferenceCount);

            cbuf.Release();
            Assert.Equal(0, buf.ReferenceCount);
            Assert.Equal(0, buf2.ReferenceCount);
            Assert.Equal(0, buf3.ReferenceCount);
        }
        public void DiscardSomeReadBytes()
        {
            CompositeByteBuffer cbuf = Unpooled.CompositeBuffer();
            int len = 8 * 4;

            for (int i = 0; i < len; i += 4)
            {
                IByteBuffer buf = Unpooled.Buffer().WriteInt(i);
                cbuf.AdjustCapacity(cbuf.WriterIndex);
                cbuf.AddComponent(buf).SetWriterIndex(i + 4);
            }
            cbuf.WriteByte(1);

            var me = new byte[len];

            cbuf.ReadBytes(me);
            cbuf.ReadByte();

            cbuf.DiscardSomeReadBytes();
            cbuf.Release();
        }
        public void CompositeToSingleBuffer()
        {
            CompositeByteBuffer buf = Unpooled.CompositeBuffer(3);

            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 1, 2, 3 }));
            Assert.Equal(1, buf.NumComponents);

            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 4 }));
            Assert.Equal(2, buf.NumComponents);

            buf.AddComponent(Unpooled.WrappedBuffer(new byte[] { 5, 6 }));
            Assert.Equal(3, buf.NumComponents);

            // NOTE: hard-coding 6 here, since it seems like addComponent doesn't bump the writer index.
            // I'm unsure as to whether or not this is correct behavior
            ArraySegment <byte> nioBuffer = buf.GetIoBuffer(0, 6);

            Assert.Equal(6, nioBuffer.Count);
            Assert.True(nioBuffer.Array.SequenceEqual(new byte[] { 1, 2, 3, 4, 5, 6 }));

            buf.Release();
        }
        public void ReferenceCounts2()
        {
            IByteBuffer c1 = Unpooled.Buffer().WriteByte(1);
            var         c2 = (IByteBuffer)Unpooled.Buffer().WriteByte(2).Retain();
            var         c3 = (IByteBuffer)Unpooled.Buffer().WriteByte(3).Retain(2);

            CompositeByteBuffer bufA = Unpooled.CompositeBuffer();

            bufA.AddComponents(c1, c2, c3).SetWriterIndex(3);

            CompositeByteBuffer bufB = Unpooled.CompositeBuffer();

            bufB.AddComponents((IByteBuffer)bufA);

            // Ensure that bufA.refCnt() did not change.
            Assert.Equal(1, bufA.ReferenceCount);

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

            // This should decrease bufA.refCnt().
            bufB.Release();
            Assert.Equal(0, bufB.ReferenceCount);

            // Ensure bufA.refCnt() changed.
            Assert.Equal(0, bufA.ReferenceCount);

            // Ensure that c[123]'s refCnt also changed due to the deallocation of bufA.
            Assert.Equal(0, c1.ReferenceCount);
            Assert.Equal(1, c2.ReferenceCount);
            Assert.Equal(2, c3.ReferenceCount);

            c3.Release(2);
            c2.Release();
        }
Esempio n. 21
0
        protected internal override void Decode(IChannelHandlerContext ctx, WebSocketFrame msg, List <object> output)
        {
            if (this.decoder == null)
            {
                if (!(msg is TextWebSocketFrame) && !(msg is BinaryWebSocketFrame))
                {
                    throw new CodecException($"unexpected initial frame type: {msg.GetType().Name}");
                }

                this.decoder = new EmbeddedChannel(ZlibCodecFactory.NewZlibDecoder(ZlibWrapper.None));
            }

            bool readable = msg.Content.IsReadable();

            this.decoder.WriteInbound(msg.Content.Retain());
            if (this.AppendFrameTail(msg))
            {
                this.decoder.WriteInbound(Unpooled.WrappedBuffer(FrameTail));
            }

            CompositeByteBuffer compositeUncompressedContent = ctx.Allocator.CompositeDirectBuffer();

            for (;;)
            {
                var partUncompressedContent = this.decoder.ReadInbound <IByteBuffer>();
                if (partUncompressedContent == null)
                {
                    break;
                }

                if (!partUncompressedContent.IsReadable())
                {
                    partUncompressedContent.Release();
                    continue;
                }

                compositeUncompressedContent.AddComponent(true, partUncompressedContent);
            }

            // Correctly handle empty frames
            // See https://github.com/netty/netty/issues/4348
            if (readable && compositeUncompressedContent.NumComponents <= 0)
            {
                compositeUncompressedContent.Release();
                throw new CodecException("cannot read uncompressed buffer");
            }

            if (msg.IsFinalFragment && this.noContext)
            {
                this.Cleanup();
            }

            WebSocketFrame outMsg;

            if (msg is TextWebSocketFrame)
            {
                outMsg = new TextWebSocketFrame(msg.IsFinalFragment, this.NewRsv(msg), compositeUncompressedContent);
            }
            else if (msg is BinaryWebSocketFrame)
            {
                outMsg = new BinaryWebSocketFrame(msg.IsFinalFragment, this.NewRsv(msg), compositeUncompressedContent);
            }
            else if (msg is ContinuationWebSocketFrame)
            {
                outMsg = new ContinuationWebSocketFrame(msg.IsFinalFragment, this.NewRsv(msg), compositeUncompressedContent);
            }
            else
            {
                throw new CodecException($"unexpected frame type: {msg.GetType().Name}");
            }

            output.Add(outMsg);
        }
Esempio n. 22
0
        protected internal override void Encode(IChannelHandlerContext ctx, WebSocketFrame msg, List <object> output)
        {
            if (this.encoder == null)
            {
                this.encoder = new EmbeddedChannel(
                    ZlibCodecFactory.NewZlibEncoder(
                        ZlibWrapper.None,
                        this.compressionLevel,
                        this.windowSize,
                        8));
            }

            this.encoder.WriteOutbound(msg.Content.Retain());

            CompositeByteBuffer fullCompressedContent = ctx.Allocator.CompositeBuffer();

            for (;;)
            {
                var partCompressedContent = this.encoder.ReadOutbound <IByteBuffer>();
                if (partCompressedContent == null)
                {
                    break;
                }

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

                fullCompressedContent.AddComponent(true, partCompressedContent);
            }

            if (fullCompressedContent.NumComponents <= 0)
            {
                fullCompressedContent.Release();
                throw new CodecException("cannot read compressed buffer");
            }

            if (msg.IsFinalFragment && this.noContext)
            {
                this.Cleanup();
            }

            IByteBuffer compressedContent;

            if (this.RemoveFrameTail(msg))
            {
                int realLength = fullCompressedContent.ReadableBytes - FrameTail.Length;
                compressedContent = fullCompressedContent.Slice(0, realLength);
            }
            else
            {
                compressedContent = fullCompressedContent;
            }

            WebSocketFrame outMsg;

            if (msg is TextWebSocketFrame)
            {
                outMsg = new TextWebSocketFrame(msg.IsFinalFragment, this.Rsv(msg), compressedContent);
            }
            else if (msg is BinaryWebSocketFrame)
            {
                outMsg = new BinaryWebSocketFrame(msg.IsFinalFragment, this.Rsv(msg), compressedContent);
            }
            else if (msg is ContinuationWebSocketFrame)
            {
                outMsg = new ContinuationWebSocketFrame(msg.IsFinalFragment, this.Rsv(msg), compressedContent);
            }
            else
            {
                throw new CodecException($"unexpected frame type: {msg.GetType().Name}");
            }

            output.Add(outMsg);
        }