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(); }
private static void TestAddContentFromByteBuf0(bool composite) { DiskFileUpload f1 = new DiskFileUpload("file3", "file3", "application/json", null, null, 0); try { byte[] bytes = new byte[4096]; (new SafeRandom()).NextBytes(bytes); IByteBuffer buffer; if (composite) { buffer = Unpooled.CompositeBuffer() .AddComponent(true, Unpooled.WrappedBuffer(bytes, 0, bytes.Length / 2)) .AddComponent(true, Unpooled.WrappedBuffer(bytes, bytes.Length / 2, bytes.Length / 2)); } else { buffer = Unpooled.WrappedBuffer(bytes); } f1.AddContent(buffer, true); IByteBuffer buf = f1.GetByteBuffer(); Assert.Equal(0, buf.ReaderIndex); Assert.Equal(buf.WriterIndex, bytes.Length); Assert.True(ByteBufferUtil.GetBytes(buf).SequenceEqual(bytes)); } finally { //release the ByteBuf f1.Delete(); } }
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(); }
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(); }
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); }
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 TestCompositeBufferEncoder() { string s = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz12345678"; IByteBuffer src1 = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(s.Substring(0, 10))); IByteBuffer src2 = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes(s.Substring(10))); var src = Unpooled.CompositeBuffer(); src.AddComponents(true, src1, src2); IByteBuffer expectedEncoded = Unpooled.CopiedBuffer(Encoding.ASCII.GetBytes("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejEyMzQ1\nNjc4")); this.TestEncode(src, expectedEncoded); }
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 ReadWithEmptyCompositeBuffer() { IByteBuffer buf = Unpooled.CompositeBuffer(); int n = 65; for (int i = 0; i < n; i++) { buf.WriteByte(1); Assert.Equal(1, buf.ReadByte()); } buf.Release(); }
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(); }
private IByteBuffer CaptureWrites() { var captor = new ArgumentCaptor <IByteBuffer>(); this.ctx.Verify(x => x.WriteAsync(It.Is <IByteBuffer>(t => captor.Capture(t)), It.IsAny <IPromise>()), Times.AtLeastOnce()); var composite = (CompositeByteBuffer)this.ReleaseLater(Unpooled.CompositeBuffer()); foreach (var item in captor.GetAllValues()) { var buf = this.ReleaseLater((IByteBuffer)item.Retain()); composite.AddComponent(true, buf); } return(composite); }
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 ToStringDoesNotThrowIndexOutOfBounds() { var buffer = Unpooled.CompositeBuffer(); try { byte[] bytes = TextEncodings.UTF8NoBOM.GetBytes("1234"); buffer.AddComponent(Unpooled.Buffer(bytes.Length).WriteBytes(bytes)); buffer.AddComponent(Unpooled.Buffer(bytes.Length).WriteBytes(bytes)); Assert.Equal("1234", buffer.ToString(bytes.Length, bytes.Length, Encoding.UTF8)); } finally { buffer.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 AddComponentWithLeakAwareByteBuf() { NoopResourceLeakTracker tracker = new NoopResourceLeakTracker(); var buffer = Unpooled.WrappedBuffer(Encoding.ASCII.GetBytes("hello world")).Slice(6, 5); var leakAwareBuf = Wrap(buffer, tracker); var composite = Unpooled.CompositeBuffer(); composite.AddComponent(true, leakAwareBuf); byte[] result = new byte[5]; IByteBuffer bb = composite[0]; //System.out.println(bb); bb.ReadBytes(result); Assert.Equal(Encoding.ASCII.GetBytes("world"), result); composite.Release(); }
public void WriteUsAsciiCompositeWrapped() { string usAscii = "NettyRocks"; var buf = Unpooled.Buffer(16); buf.WriteBytes(Encoding.ASCII.GetBytes(usAscii)); var buf2 = new WrappedCompositeByteBuffer(Unpooled.CompositeBuffer().AddComponent( Unpooled.Buffer(8)).AddComponent(Unpooled.Buffer(24))); // write some byte so we start AddComponent with an offset. buf2.WriteByte(1); ByteBufferUtil.WriteAscii(buf2, usAscii); // Skip the previously written byte. Assert.Equal(buf, buf2.SkipBytes(1)); buf.Release(); buf2.Release(); }
public void WriteUtf8CompositeWrapped() { string utf8 = "Some UTF-8 like äÄ∏ŒŒ"; var buf = Unpooled.Buffer(16); buf.WriteBytes(TextEncodings.UTF8NoBOM.GetBytes(utf8)); var buf2 = new WrappedCompositeByteBuffer(Unpooled.CompositeBuffer().AddComponent( Unpooled.Buffer(8)).AddComponent(Unpooled.Buffer(24))); // write some byte so we start writing with an offset. buf2.WriteByte(1); ByteBufferUtil.WriteUtf8(buf2, utf8); // Skip the previously written byte. Assert.Equal(buf, buf2.SkipBytes(1)); buf.Release(); buf2.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(); }
public override void AddContent(IByteBuffer buffer, bool last) { if (buffer != null) { long localsize = buffer.ReadableBytes; this.CheckSize(this.Size + localsize); if (this.DefinedSize > 0 && this.DefinedSize < this.Size + localsize) { throw new IOException($"Out of size: {(this.Size + localsize)} > {this.DefinedSize}"); } this.Size += localsize; if (this.byteBuf == null) { this.byteBuf = buffer; } else if (this.byteBuf is CompositeByteBuffer buf) { buf.AddComponent(true, buffer); buf.SetWriterIndex((int)this.Size); } else { CompositeByteBuffer compositeBuffer = Unpooled.CompositeBuffer(int.MaxValue); compositeBuffer.AddComponents(true, this.byteBuf, buffer); compositeBuffer.SetWriterIndex((int)this.Size); this.byteBuf = compositeBuffer; } } if (last) { this.SetCompleted(); } else { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } } }
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 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 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(); }
void TestHttpResponseAndFrameInSameBuffer(bool codec) { string url = "ws://localhost:9999/ws"; WebSocketClientHandshaker shaker = this.NewHandshaker(new Uri(url)); var handshaker = new Handshaker(shaker); // use randomBytes helper from utils to check that it functions properly byte[] data = WebSocketUtil.RandomBytes(24); // Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these // to test the actual handshaker. var factory = new WebSocketServerHandshakerFactory(url, null, false); IFullHttpRequest request = shaker.NewHandshakeRequest(); WebSocketServerHandshaker socketServerHandshaker = factory.NewHandshaker(request); request.Release(); var websocketChannel = new EmbeddedChannel(socketServerHandshaker.NewWebSocketEncoder(), socketServerHandshaker.NewWebsocketDecoder()); Assert.True(websocketChannel.WriteOutbound(new BinaryWebSocketFrame(Unpooled.WrappedBuffer(data)))); byte[] bytes = Encoding.ASCII.GetBytes("HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n"); CompositeByteBuffer compositeByteBuf = Unpooled.CompositeBuffer(); compositeByteBuf.AddComponent(true, Unpooled.WrappedBuffer(bytes)); for (; ;) { var frameBytes = websocketChannel.ReadOutbound <IByteBuffer>(); if (frameBytes == null) { break; } compositeByteBuf.AddComponent(true, frameBytes); } var ch = new EmbeddedChannel(new HttpObjectAggregator(int.MaxValue), new Handler(handshaker)); if (codec) { ch.Pipeline.AddFirst(new HttpClientCodec()); } else { ch.Pipeline.AddFirst(new HttpRequestEncoder(), new HttpResponseDecoder()); } // We need to first write the request as HttpClientCodec will fail if we receive a response before a request // was written. shaker.HandshakeAsync(ch).Wait(); for (; ;) { // Just consume the bytes, we are not interested in these. var buf = ch.ReadOutbound <IByteBuffer>(); if (buf == null) { break; } buf.Release(); } Assert.True(ch.WriteInbound(compositeByteBuf)); Assert.True(ch.Finish()); var frame = ch.ReadInbound <BinaryWebSocketFrame>(); IByteBuffer expect = Unpooled.WrappedBuffer(data); try { Assert.Equal(expect, frame.Content); Assert.True(frame.IsFinalFragment); Assert.Equal(0, frame.Rsv); } finally { expect.Release(); frame.Release(); } }
private async Task TestGatheringWrite0( ServerBootstrap sb, Bootstrap cb, byte[] data, bool composite, bool autoRead) { sb.ChildOption(ChannelOption.AutoRead, autoRead); cb.Option(ChannelOption.AutoRead, autoRead); IPromise serverDonePromise = new DefaultPromise(); TestServerHandler sh = new TestServerHandler(autoRead, serverDonePromise, data.Length); TestHandler ch = new TestHandler(autoRead); cb.Handler(ch); sb.ChildHandler(sh); IChannel sc = await sb.BindAsync(); IChannel cc = await cb.ConnectAsync(sc.LocalAddress); for (int i = 0; i < data.Length;) { int length = Math.Min(s_random.Next(1024 * 8), data.Length - i); if (composite && i % 2 == 0) { int firstBufLength = length / 2; CompositeByteBuffer comp = Unpooled.CompositeBuffer(); comp.AddComponent(true, Unpooled.WrappedBuffer(data, i, firstBufLength)) .AddComponent(true, Unpooled.WrappedBuffer(data, i + firstBufLength, length - firstBufLength)); cc.WriteAsync(comp).Ignore(); } else { cc.WriteAsync(Unpooled.WrappedBuffer(data, i, length)).Ignore(); } i += length; } var cf = cc.WriteAndFlushAsync(Unpooled.Empty); Assert.NotEqual(cc.VoidPromise().Task, cf); try { Assert.True(cf.Wait(60000)); await cf; } catch (Exception) { throw; } await serverDonePromise.Task; await sh._channel.CloseAsync(); await ch._channel.CloseAsync(); await sc.CloseAsync(); if (sh._exception.Value != null && !(sh._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc && chexc.InnerException is OperationException) || sh._exception.Value is OperationException)) { throw sh._exception.Value; } if (sh._exception.Value != null) { throw sh._exception.Value; } if (ch._exception.Value != null && !(ch._exception.Value is SocketException || (sh._exception.Value is ChannelException chexc1 && chexc1.InnerException is OperationException) || sh._exception.Value is OperationException)) { throw ch._exception.Value; } if (ch._exception.Value != null) { throw ch._exception.Value; } IByteBuffer expected = Unpooled.WrappedBuffer(data); Assert.Equal(expected, sh._received); expected.Release(); sh._received.Release(); }