/** * Generate a new instance of an {@link EmbeddedChannel} capable of compressing data * @param ctx the context. * @param wrapper Defines what type of encoder should be used */ private EmbeddedChannel newCompressionChannel(final ChannelHandlerContext ctx, ZlibWrapper wrapper) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibEncoder(wrapper, compressionLevel, windowBits, memLevel)); }
private void decode(ByteBuf in, List<Object> out) { // call retain here as it will call release after its written to the channel decoder.writeInbound(in.retain()); fetchDecoderOutput(out); }
private void fetchEncoderOutput(List<Object> out) { for (;;) { ByteBuf buf = encoder.readOutbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } out.add(new DefaultHttpContent(buf)); } }
@Override public final ChannelFuture close() { return close(newPromise()); }
/** * Mark this {@link Channel} as finished. Any further try to write data to it will fail. * * @param releaseAll if {@code true} all pending message in the inbound and outbound buffer are released. * @return bufferReadable returns {@code true} if any of the used buffers has something left to read */ private boolean finish(boolean releaseAll) { close(); try { checkException(); return isNotEmpty(inboundMessages) || isNotEmpty(outboundMessages); } finally { if (releaseAll) { releaseAll(inboundMessages); releaseAll(outboundMessages); } } }
private ChannelFuture flushInbound(boolean recordException, ChannelPromise promise) { if (checkOpen(recordException)) { pipeline().fireChannelReadComplete(); runPendingTasks(); } return checkException(promise); }
private void testClientToServer(Message msg) { EmbeddedChannel clientChannel = new EmbeddedChannel(new FileRegionEncoder(), MessageEncoder.INSTANCE); clientChannel.writeOutbound(msg); EmbeddedChannel serverChannel = new EmbeddedChannel( NettyUtils.createFrameDecoder(), MessageDecoder.INSTANCE); while (!clientChannel.outboundMessages().isEmpty()) { serverChannel.writeOneInbound(clientChannel.readOutbound()); } assertEquals(1, serverChannel.inboundMessages().size()); assertEquals(msg, serverChannel.readInbound()); }
@Test public void shouldDecodeMaxSizeChunk() { byte[] message = new byte[0xFFFF]; ByteBuf input = buffer(); input.writeShort( message.length ); input.writeBytes( message ); assertTrue( channel.writeInbound( input ) ); assertTrue( channel.finish() ); assertEquals( 1, channel.inboundMessages().size() ); assertByteBufEquals( wrappedBuffer( message ), channel.readInbound() ); } }
@Test public void shouldHandleFragmentedMessage() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{(byte) 0x17, 0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 1, 0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 0} ) ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 1 ), channel.readOutbound() ); thrown.expect( NoSuchElementException.class ); channel.pipeline().remove( ProtocolHandshaker.class ); assertTrue( channel.isActive() ); verify( protocol ).install(); }
@Benchmark public void vertx() { GET.setIndex(readerIndex, writeIndex); vertxChannel.writeInbound(GET); ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll(); consume(result); }
/** * Called for each outbound message. * * @see #doWrite(ChannelOutboundBuffer) */ protected void handleOutboundMessage(Object msg) { outboundMessages().add(msg); }
@Test public void shouldDecodeMultipleConsecutiveMessages() { channel.writeInbound( wrappedBuffer( new byte[]{1, 2, 3} ) ); channel.writeInbound( wrappedBuffer( new byte[0] ) ); channel.writeInbound( wrappedBuffer( new byte[]{4, 5} ) ); channel.writeInbound( wrappedBuffer( new byte[]{6} ) ); channel.writeInbound( wrappedBuffer( new byte[0] ) ); channel.writeInbound( wrappedBuffer( new byte[]{7, 8} ) ); channel.writeInbound( wrappedBuffer( new byte[]{9, 10} ) ); channel.writeInbound( wrappedBuffer( new byte[0] ) ); assertEquals( 3, channel.inboundMessages().size() ); assertByteBufEquals( wrappedBuffer( new byte[]{1, 2, 3} ), channel.readInbound() ); assertByteBufEquals( wrappedBuffer( new byte[]{4, 5, 6} ), channel.readInbound() ); assertByteBufEquals( wrappedBuffer( new byte[]{7, 8, 9, 10} ), channel.readInbound() ); }
@Override protected EmbeddedChannel newContentDecoder(String contentEncoding) throws Exception { if (GZIP.contentEqualsIgnoreCase(contentEncoding) || X_GZIP.contentEqualsIgnoreCase(contentEncoding)) { return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP)); } if (DEFLATE.contentEqualsIgnoreCase(contentEncoding) || X_DEFLATE.contentEqualsIgnoreCase(contentEncoding)) { final ZlibWrapper wrapper = strict ? ZlibWrapper.ZLIB : ZlibWrapper.ZLIB_OR_NONE; // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibDecoder(wrapper)); } // 'identity' or unsupported return null; } }
@Test public void shouldFlushNothingWhenEmpty() throws Exception { out.flush(); assertEquals( 0, channel.outboundMessages().size() ); }
throw new CodecException("unexpected initial frame type: " + msg.getClass().getName()); decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE)); boolean readable = msg.content().isReadable(); decoder.writeInbound(msg.content().retain()); if (appendFrameTail(msg)) { decoder.writeInbound(Unpooled.wrappedBuffer(FRAME_TAIL)); CompositeByteBuf compositeUncompressedContent = ctx.alloc().compositeBuffer(); for (;;) { ByteBuf partUncompressedContent = decoder.readInbound(); if (partUncompressedContent == null) { break; if (!partUncompressedContent.isReadable()) { partUncompressedContent.release(); continue;
@Test public void shouldRejectIfHttp() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When FullHttpRequest request = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "http://hello_world:10000" ); request.headers().setInt( HttpHeaderNames.CONTENT_LENGTH, 0 ); channel.writeInbound( request ); // Then assertEquals( 0, channel.outboundMessages().size() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); logProvider.assertExactly( AssertableLogProvider.inLog( ProtocolHandshaker.class ).warn( "Unsupported connection type: 'HTTP'. Bolt protocol only operates over a TCP connection or WebSocket." ) ); }
@Test(timeout = 60000) public void testDiscardReadWhenStopped() throws Exception { assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Buffering)); handlerRule.channel.close(); // Should remove handler. handlerRule.channel.runPendingTasks(); assertThat("Unexpected handler state.", handlerRule.handler.getCurrentState(), is(State.Stopped)); final ByteBuf msg = Unpooled.buffer().writeBytes("Hello".getBytes()); handlerRule.handler.channelRead(Mockito.mock(ChannelHandlerContext.class), msg); assertThat("Message not released when stopped.", msg.refCnt(), is(0)); }
List<Object> out) throws Exception { if (encoder == null) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder( ZlibWrapper.NONE, compressionLevel, windowSize, 8)); encoder.writeOutbound(msg.content().retain()); CompositeByteBuf fullCompressedContent = ctx.alloc().compositeBuffer(); for (;;) { ByteBuf partCompressedContent = encoder.readOutbound(); if (partCompressedContent == null) { break; if (!partCompressedContent.isReadable()) { partCompressedContent.release(); continue;
private void unpack( byte[] input ) throws IOException { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( input ) ); channel.finishAndReleaseAll(); }
@Override public void evaluate() throws Throwable { data = Unpooled.buffer().writeBytes("Hello".getBytes()); channel = new EmbeddedChannel(new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof SourceEvent) { SourceEvent sourceEvent = (SourceEvent) evt; sourceEvent.subscriber.onNext(data); sourceEvent.subscriber.onCompleted(); } super.userEventTriggered(ctx, evt); } }); base.evaluate(); } };