Search in sources :

Example 1 with DuplexChannel

use of io.netty.channel.socket.DuplexChannel 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 2 with DuplexChannel

use of io.netty.channel.socket.DuplexChannel in project vert.x by eclipse.

the class Http2Test method testDiscardConnectionWhenChannelBecomesInactive.

@Test
public void testDiscardConnectionWhenChannelBecomesInactive() throws Exception {
    AtomicInteger count = new AtomicInteger();
    server.requestHandler(req -> {
        if (count.getAndIncrement() == 0) {
            Http2ServerConnection a = (Http2ServerConnection) req.connection();
            DuplexChannel channel = (DuplexChannel) a.channel();
            channel.shutdown();
        } else {
            req.response().end();
        }
    });
    startServer(testAddress);
    AtomicInteger closed = new AtomicInteger();
    client.connectionHandler(conn -> conn.closeHandler(v -> closed.incrementAndGet()));
    client.request(requestOptions).onComplete(onSuccess(req -> {
        req.send(onFailure(err -> {
        }));
    }));
    AsyncTestBase.assertWaitUntil(() -> closed.get() == 1);
    client.request(requestOptions).compose(HttpClientRequest::send).onComplete(onSuccess(resp -> {
        testComplete();
    }));
    await();
}
Also used : java.util(java.util) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionBase(io.vertx.core.net.impl.ConnectionBase) Context(io.vertx.core.Context) PfxOptions(io.vertx.core.net.PfxOptions) BooleanSupplier(java.util.function.BooleanSupplier) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) ChannelPromise(io.netty.channel.ChannelPromise) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DuplexChannel(io.netty.channel.socket.DuplexChannel) Http2ServerConnection(io.vertx.core.http.impl.Http2ServerConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Promise(io.vertx.core.Promise) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Test(org.junit.Test) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Ignore(org.junit.Ignore) AsyncTestBase(io.vertx.test.core.AsyncTestBase) Cert(io.vertx.test.tls.Cert) Http2CodecUtil(io.netty.handler.codec.http2.Http2CodecUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DuplexChannel(io.netty.channel.socket.DuplexChannel) Http2ServerConnection(io.vertx.core.http.impl.Http2ServerConnection) Test(org.junit.Test)

Example 3 with DuplexChannel

use of io.netty.channel.socket.DuplexChannel in project netty by netty.

the class SocketHalfClosedTest method testAllDataReadAfterHalfClosure.

private static void testAllDataReadAfterHalfClosure(final boolean autoRead, 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, true).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(new ChannelFutureListener() {

                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                ((DuplexChannel) future.channel()).shutdownOutput();
                            }
                        });
                        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) {
                            clientHalfClosedLatch.countDown();
                        } else if (evt == ChannelInputShutdownReadComplete.INSTANCE) {
                            ctx.close();
                        }
                    }

                    @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 : ChannelFuture(io.netty.channel.ChannelFuture) 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) ChannelFutureListener(io.netty.channel.ChannelFutureListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

Channel (io.netty.channel.Channel)3 DuplexChannel (io.netty.channel.socket.DuplexChannel)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelPromise (io.netty.channel.ChannelPromise)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 Http2CodecUtil (io.netty.handler.codec.http2.Http2CodecUtil)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 Buffer (io.vertx.core.buffer.Buffer)1 Http2ServerConnection (io.vertx.core.http.impl.Http2ServerConnection)1 OpenSSLEngineOptions (io.vertx.core.net.OpenSSLEngineOptions)1 PfxOptions (io.vertx.core.net.PfxOptions)1