Example usage for io.netty.channel ChannelFuture addListeners

List of usage examples for io.netty.channel ChannelFuture addListeners

Introduction

In this page you can find the example usage for io.netty.channel ChannelFuture addListeners.

Prototype

@Override
    ChannelFuture addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners);

Source Link

Usage

From source file:com.feihong.newzxclient.tcp.NettyClient.java

License:Apache License

@SuppressWarnings("unchecked")
public void connect() throws Exception {
    mGroup = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(mGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override/*  w w w .j  ava 2  s .c  o  m*/
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast("decoder", new IntLengthDecoder());
                    ch.pipeline().addLast(new NettyClientHandler());
                }
            });

    ChannelFuture future = b.connect(host, port).sync();
    future.addListeners(new ChannelFutureListener() {
        public void operationComplete(final ChannelFuture future) throws Exception {
            requestLogin();
        }
    });

    mChannel = future.channel();

    mHandler = mChannel.pipeline().get(NettyClientHandler.class);

}

From source file:com.github.mrstampy.gameboot.netty.NettyConnectionRegistry.java

License:Open Source License

private void sendMessage(Comparable<?> key, String message, Channel channel,
        ChannelFutureListener... listeners) {
    if (channel == null || !channel.isActive()) {
        log.warn("Cannot send {} to {}", message, channel);
        return;// w  ww  .  j a  va2  s  . c  o  m
    }

    ChannelFutureListener[] all = utils.prependArray(f -> log((ChannelFuture) f, key), listeners);
    ChannelFuture f = channel.writeAndFlush(message);
    f.addListeners(all);
}

From source file:com.github.mrstampy.gameboot.netty.NettyConnectionRegistry.java

License:Open Source License

private void sendMessage(Comparable<?> key, byte[] message, Channel channel,
        ChannelFutureListener... listeners) {
    if (channel == null || !channel.isActive()) {
        log.warn("Cannot send {} to {}", message, channel);
        return;//from  w w  w.  ja  v a2s  .  c  om
    }

    ChannelFutureListener[] all = utils.prependArray(f -> log((ChannelFuture) f, key), listeners);
    ChannelFuture f = channel.writeAndFlush(message);
    f.addListeners(all);
}

From source file:org.acmsl.katas.antlr4netty.InterpreterServer.java

License:Open Source License

/**
 * Wraps given {@link ChannelFuture} to ensure the event loops
 * shut down gracefully./*from  w  w w . j  av a 2  s  . c o  m*/
 * @param target the original channel future.
 * @param bossGroup the boss group.
 * @param workerGroup the worker group.
 * @return the wrapped future.
 */
@NotNull
protected ChannelFuture wrap(@NotNull final ChannelFuture target, @NotNull final NioEventLoopGroup bossGroup,
        @NotNull final NioEventLoopGroup workerGroup) {
    return new ChannelFuture() {
        @Override
        public Channel channel() {
            return target.channel();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture addListener(
                @NotNull final GenericFutureListener<? extends Future<? super Void>> listener) {
            return target.addListener(listener);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture addListeners(
                @NotNull final GenericFutureListener<? extends Future<? super Void>>... listeners) {
            return target.addListeners(listeners);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture removeListener(
                @NotNull final GenericFutureListener<? extends Future<? super Void>> listener) {
            return target.removeListener(listener);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture removeListeners(
                @NotNull final GenericFutureListener<? extends Future<? super Void>>... listeners) {
            return target.removeListeners(listeners);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture sync() throws InterruptedException {
            ChannelFuture result = null;

            try {
                result = target.sync();
            } finally {
                workerGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
            }

            return result;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture syncUninterruptibly() {
            return target.syncUninterruptibly();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture await() throws InterruptedException {
            return target.await();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public ChannelFuture awaitUninterruptibly() {
            return target.awaitUninterruptibly();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isSuccess() {
            return target.isSuccess();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isCancellable() {
            return target.isCancellable();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Throwable cause() {
            return target.cause();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean await(final long timeout, @NotNull final TimeUnit unit) throws InterruptedException {
            return target.await(timeout, unit);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean await(final long timeoutMillis) throws InterruptedException {
            return target.await(timeoutMillis);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean awaitUninterruptibly(final long timeout, @NotNull final TimeUnit unit) {
            return target.awaitUninterruptibly(timeout, unit);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean awaitUninterruptibly(final long timeoutMillis) {
            return target.awaitUninterruptibly(timeoutMillis);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Void getNow() {
            return target.getNow();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean cancel(final boolean mayInterruptIfRunning) {
            return target.cancel(mayInterruptIfRunning);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isCancelled() {
            return target.isCancelled();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isDone() {
            return target.isDone();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Void get() throws InterruptedException, ExecutionException {
            return target.get();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Void get(final long timeout, @NotNull final TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            return target.get(timeout, unit);
        }
    };
}

From source file:org.diorite.impl.connection.CoreNetworkManager.java

License:Open Source License

private void sendPacket(final Packet<?> packet,
        final GenericFutureListener<? extends Future<? super Void>>[] listeners) {
    if (this.closed) {
        this.handleClosed();
        return;//from  ww  w.  j  a v a 2 s  .c o m
    }
    if ((packet instanceof PacketPlayClientboundKeepAlive)
            || (packet instanceof PacketPlayServerboundKeepAlive)) {
        this.sentAlive = System.currentTimeMillis();
    }
    final EnumProtocol ep1 = EnumProtocol.getByPacketClass(packet);
    final EnumProtocol ep2 = this.channel.attr(this.core.getConnectionHandler().getProtocolKey()).get();
    if (ep2 != ep1) {
        this.channel.config().setAutoRead(false);
    }
    if (this.channel.eventLoop().inEventLoop()) {
        if (ep1 != ep2) {
            this.setProtocol(ep1);
        }
        final ChannelFuture channelfuture = this.channel.writeAndFlush(packet);
        if (listeners != null) {
            channelfuture.addListeners(listeners);
        }
        channelfuture.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    } else {
        this.channel.eventLoop().execute(() -> {
            if (ep1 != ep2) {
                this.setProtocol(ep1);
            }
            final ChannelFuture channelFuture = this.channel.writeAndFlush(packet);
            if (listeners != null) {
                channelFuture.addListeners(listeners);
            }
            channelFuture.addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        });
    }
}