Search in sources :

Example 26 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class Lz4FrameEncoder method close.

@Override
public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
    ChannelFuture f = finishEncode(ctx, ctx.newPromise());
    f.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            ctx.close(promise);
        }
    });
    if (!f.isDone()) {
        // Ensure the channel is closed even if the write operation completes in time.
        ctx.executor().schedule(new Runnable() {

            @Override
            public void run() {
                ctx.close(promise);
            }
        }, 10, // FIXME: Magic number
        TimeUnit.SECONDS);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) EncoderException(io.netty.handler.codec.EncoderException) LZ4Exception(net.jpountz.lz4.LZ4Exception)

Example 27 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class JdkZlibEncoder method close.

@Override
public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception {
    ChannelFuture f = finishEncode(ctx, ctx.newPromise());
    f.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            ctx.close(promise);
        }
    });
    if (!f.isDone()) {
        // Ensure the channel is closed even if the write operation completes in time.
        ctx.executor().schedule(new Runnable() {

            @Override
            public void run() {
                ctx.close(promise);
            }
        }, 10, // FIXME: Magic number
        TimeUnit.SECONDS);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 28 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class Bootstrap method doResolveAndConnect.

/**
     * @see #connect()
     */
private ChannelFuture doResolveAndConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();
    if (regFuture.isDone()) {
        if (!regFuture.isSuccess()) {
            return regFuture;
        }
        return doResolveAndConnect0(channel, remoteAddress, localAddress, channel.newPromise());
    } else {
        // Registration future is almost always fulfilled already, but just in case it's not.
        final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
        regFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                // Direclty obtain the cause and do a null check so we only need one volatile read in case of a
                // failure.
                Throwable cause = future.cause();
                if (cause != null) {
                    // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                    // IllegalStateException once we try to access the EventLoop of the Channel.
                    promise.setFailure(cause);
                } else {
                    // Registration was successful, so set the correct executor to use.
                    // See https://github.com/netty/netty/issues/2586
                    promise.registered();
                    doResolveAndConnect0(channel, remoteAddress, localAddress, promise);
                }
            }
        });
        return promise;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 29 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class AbstractBootstrap method doBind.

private ChannelFuture doBind(final SocketAddress localAddress) {
    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();
    if (regFuture.cause() != null) {
        return regFuture;
    }
    if (regFuture.isDone()) {
        // At this point we know that the registration was complete and successful.
        ChannelPromise promise = channel.newPromise();
        doBind0(regFuture, channel, localAddress, promise);
        return promise;
    } else {
        // Registration future is almost always fulfilled already, but just in case it's not.
        final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
        regFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Throwable cause = future.cause();
                if (cause != null) {
                    // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                    // IllegalStateException once we try to access the EventLoop of the Channel.
                    promise.setFailure(cause);
                } else {
                    // Registration was successful, so set the correct executor to use.
                    // See https://github.com/netty/netty/issues/2586
                    promise.registered();
                    doBind0(regFuture, channel, localAddress, promise);
                }
            }
        });
        return promise;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 30 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty by netty.

the class Http2ConnectionHandler method checkCloseConnection.

/**
     * Closes the connection if the graceful shutdown process has completed.
     * @param future Represents the status that will be passed to the {@link #closeListener}.
     */
private void checkCloseConnection(ChannelFuture future) {
    // once this operation completes.
    if (closeListener != null && isGracefulShutdownComplete()) {
        ChannelFutureListener closeListener = this.closeListener;
        // This method could be called multiple times
        // and we don't want to notify the closeListener multiple times.
        this.closeListener = null;
        try {
            closeListener.operationComplete(future);
        } catch (Exception e) {
            throw new IllegalStateException("Close listener threw an unexpected exception", e);
        }
    }
}
Also used : ChannelFutureListener(io.netty.channel.ChannelFutureListener) StreamException(io.netty.handler.codec.http2.Http2Exception.StreamException) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) CompositeStreamException(io.netty.handler.codec.http2.Http2Exception.CompositeStreamException)

Aggregations

ChannelFutureListener (io.netty.channel.ChannelFutureListener)90 ChannelFuture (io.netty.channel.ChannelFuture)85 Channel (io.netty.channel.Channel)27 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)24 Bootstrap (io.netty.bootstrap.Bootstrap)19 Test (org.junit.Test)19 ByteBuf (io.netty.buffer.ByteBuf)18 ClosedChannelException (java.nio.channels.ClosedChannelException)17 CountDownLatch (java.util.concurrent.CountDownLatch)16 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)14 IOException (java.io.IOException)12 ChannelPromise (io.netty.channel.ChannelPromise)11 ConnectException (java.net.ConnectException)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)10 InetSocketAddress (java.net.InetSocketAddress)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 AbstractChannel (io.netty.channel.AbstractChannel)6 EventLoopGroup (io.netty.channel.EventLoopGroup)6 ChannelPipeline (io.netty.channel.ChannelPipeline)4