Search in sources :

Example 66 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SocketDataReadInitialStateTest method testAutoReadOffNoDataReadUntilReadCalled.

public void testAutoReadOffNoDataReadUntilReadCalled(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    Channel serverChannel = null;
    Channel clientChannel = null;
    final int sleepMs = 100;
    try {
        sb.option(AUTO_READ, false);
        sb.childOption(AUTO_READ, false);
        cb.option(AUTO_READ, false);
        final CountDownLatch serverReadyLatch = new CountDownLatch(1);
        final CountDownLatch acceptorReadLatch = new CountDownLatch(1);
        final CountDownLatch serverReadLatch = new CountDownLatch(1);
        final CountDownLatch clientReadLatch = new CountDownLatch(1);
        final AtomicReference<Channel> serverConnectedChannelRef = new AtomicReference<Channel>();
        sb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        acceptorReadLatch.countDown();
                        ctx.fireChannelRead(msg);
                    }
                });
            }
        });
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                serverConnectedChannelRef.set(ch);
                ch.pipeline().addLast(new SimpleChannelInboundHandler<ByteBuf>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
                        ctx.writeAndFlush(msg.retainedDuplicate());
                        serverReadLatch.countDown();
                    }
                });
                serverReadyLatch.countDown();
            }
        });
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new SimpleChannelInboundHandler<Object>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
                        clientReadLatch.countDown();
                    }
                });
            }
        });
        serverChannel = sb.bind().sync().channel();
        clientChannel = cb.connect(serverChannel.localAddress()).sync().channel();
        clientChannel.writeAndFlush(clientChannel.alloc().buffer().writeZero(1)).syncUninterruptibly();
        // The acceptor shouldn't read any data until we call read() below, but give it some time to see if it will.
        Thread.sleep(sleepMs);
        assertEquals(1, acceptorReadLatch.getCount());
        serverChannel.read();
        serverReadyLatch.await();
        Channel serverConnectedChannel = serverConnectedChannelRef.get();
        assertNotNull(serverConnectedChannel);
        // Allow some amount of time for the server peer to receive the message (which isn't expected to happen
        // until we call read() below).
        Thread.sleep(sleepMs);
        assertEquals(1, serverReadLatch.getCount());
        serverConnectedChannel.read();
        serverReadLatch.await();
        // Allow some amount of time for the client to read the echo.
        Thread.sleep(sleepMs);
        assertEquals(1, clientReadLatch.getCount());
        clientChannel.read();
        clientReadLatch.await();
    } finally {
        if (serverChannel != null) {
            serverChannel.close().sync();
        }
        if (clientChannel != null) {
            clientChannel.close().sync();
        }
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 67 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SocketHalfClosedTest method testHalfClosureOnlyOneEventWhenAutoRead.

public void testHalfClosureOnlyOneEventWhenAutoRead(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    Channel serverChannel = null;
    try {
        cb.option(ChannelOption.ALLOW_HALF_CLOSURE, true).option(ChannelOption.AUTO_READ, true);
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ((DuplexChannel) ctx).shutdownOutput();
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        });
        final AtomicInteger shutdownEventReceivedCounter = new AtomicInteger();
        final AtomicInteger shutdownReadCompleteEventReceivedCounter = new AtomicInteger();
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) {
                        if (evt == ChannelInputShutdownEvent.INSTANCE) {
                            shutdownEventReceivedCounter.incrementAndGet();
                        } else if (evt == ChannelInputShutdownReadComplete.INSTANCE) {
                            shutdownReadCompleteEventReceivedCounter.incrementAndGet();
                            ctx.executor().schedule(new Runnable() {

                                @Override
                                public void run() {
                                    ctx.close();
                                }
                            }, 100, MILLISECONDS);
                        }
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        });
        serverChannel = sb.bind().sync().channel();
        Channel clientChannel = cb.connect(serverChannel.localAddress()).sync().channel();
        clientChannel.closeFuture().await();
        assertEquals(1, shutdownEventReceivedCounter.get());
        assertEquals(1, shutdownReadCompleteEventReceivedCounter.get());
    } finally {
        if (serverChannel != null) {
            serverChannel.close().sync();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DuplexChannel(io.netty.channel.socket.DuplexChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DuplexChannel(io.netty.channel.socket.DuplexChannel) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 68 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SocketHalfClosedTest method testAllDataReadClosure.

private static void testAllDataReadClosure(final boolean autoRead, final boolean allowHalfClosed, ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final int totalServerBytesWritten = 1024 * 16;
    final int numReadsPerReadLoop = 2;
    final CountDownLatch serverInitializedLatch = new CountDownLatch(1);
    final CountDownLatch clientReadAllDataLatch = new CountDownLatch(1);
    final CountDownLatch clientHalfClosedLatch = new CountDownLatch(1);
    final AtomicInteger clientReadCompletes = new AtomicInteger();
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        cb.option(ChannelOption.ALLOW_HALF_CLOSURE, allowHalfClosed).option(ChannelOption.AUTO_READ, autoRead).option(ChannelOption.RCVBUF_ALLOCATOR, new TestNumReadsRecvByteBufAllocator(numReadsPerReadLoop));
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        ByteBuf buf = ctx.alloc().buffer(totalServerBytesWritten);
                        buf.writerIndex(buf.capacity());
                        ctx.writeAndFlush(buf).addListener(ChannelFutureListener.CLOSE);
                        serverInitializedLatch.countDown();
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        });
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    private int bytesRead;

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        ByteBuf buf = (ByteBuf) msg;
                        bytesRead += buf.readableBytes();
                        buf.release();
                    }

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt == ChannelInputShutdownEvent.INSTANCE && allowHalfClosed) {
                            clientHalfClosedLatch.countDown();
                        } else if (evt == ChannelInputShutdownReadComplete.INSTANCE) {
                            ctx.close();
                        }
                    }

                    @Override
                    public void channelInactive(ChannelHandlerContext ctx) {
                        if (!allowHalfClosed) {
                            clientHalfClosedLatch.countDown();
                        }
                    }

                    @Override
                    public void channelReadComplete(ChannelHandlerContext ctx) {
                        clientReadCompletes.incrementAndGet();
                        if (bytesRead == totalServerBytesWritten) {
                            clientReadAllDataLatch.countDown();
                        }
                        if (!autoRead) {
                            ctx.read();
                        }
                    }

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        });
        serverChannel = sb.bind().sync().channel();
        clientChannel = cb.connect(serverChannel.localAddress()).sync().channel();
        clientChannel.read();
        serverInitializedLatch.await();
        clientReadAllDataLatch.await();
        clientHalfClosedLatch.await();
        assertTrue(totalServerBytesWritten / numReadsPerReadLoop + 10 > clientReadCompletes.get(), "too many read complete events: " + clientReadCompletes.get());
    } finally {
        if (clientChannel != null) {
            clientChannel.close().sync();
        }
        if (serverChannel != null) {
            serverChannel.close().sync();
        }
    }
}
Also used : DuplexChannel(io.netty.channel.socket.DuplexChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 69 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class SocketRstTest method testSoLingerZeroCausesOnlyRstOnClose.

public void testSoLingerZeroCausesOnlyRstOnClose(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    final AtomicReference<Channel> serverChannelRef = new AtomicReference<Channel>();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    // SO_LINGER=0 means that we must send ONLY a RST when closing (not a FIN + RST).
    sb.childOption(ChannelOption.SO_LINGER, 0);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            serverChannelRef.compareAndSet(null, ch);
            latch.countDown();
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                    throwableRef.compareAndSet(null, cause);
                }

                @Override
                public void channelInactive(ChannelHandlerContext ctx) {
                    latch2.countDown();
                }
            });
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect(sc.localAddress()).sync().channel();
    // Wait for the server to get setup.
    latch.await();
    // The server has SO_LINGER=0 and so it must send a RST when close is called.
    serverChannelRef.get().close();
    // Wait for the client to get channelInactive.
    latch2.await();
    // Verify the client received a RST.
    Throwable cause = throwableRef.get();
    assertTrue(cause instanceof IOException, "actual [type, message]: [" + cause.getClass() + ", " + cause.getMessage() + "]");
    assertRstOnCloseException((IOException) cause, cc);
}
Also used : Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 70 with ChannelInboundHandlerAdapter

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter in project netty by netty.

the class NettyBlockHoundIntegrationTest method testHandshake.

private static void testHandshake(SslContext sslClientCtx, SslHandler clientSslHandler, SslHandler serverSslHandler) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    Channel sc = null;
    Channel cc = null;
    try {
        sc = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).childHandler(serverSslHandler).bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
        ChannelFuture future = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(clientSslHandler).addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt instanceof SslHandshakeCompletionEvent && ((SslHandshakeCompletionEvent) evt).cause() != null) {
                            ((SslHandshakeCompletionEvent) evt).cause().printStackTrace();
                        }
                        ctx.fireUserEventTriggered(evt);
                    }
                });
            }
        }).connect(sc.localAddress());
        cc = future.syncUninterruptibly().channel();
        clientSslHandler.handshakeFuture().await().sync();
        serverSslHandler.handshakeFuture().await().sync();
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)248 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)192 Channel (io.netty.channel.Channel)132 Bootstrap (io.netty.bootstrap.Bootstrap)109 Test (org.junit.jupiter.api.Test)102 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)99 ChannelFuture (io.netty.channel.ChannelFuture)71 CountDownLatch (java.util.concurrent.CountDownLatch)70 InetSocketAddress (java.net.InetSocketAddress)66 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)54 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)53 EventLoopGroup (io.netty.channel.EventLoopGroup)52 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)51 ByteBuf (io.netty.buffer.ByteBuf)47 AtomicReference (java.util.concurrent.atomic.AtomicReference)47 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)46 ClosedChannelException (java.nio.channels.ClosedChannelException)46 LocalServerChannel (io.netty.channel.local.LocalServerChannel)44 LocalChannel (io.netty.channel.local.LocalChannel)42 SocketChannel (io.netty.channel.socket.SocketChannel)39