Search in sources :

Example 41 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 42 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project netty-socketio by mrniko.

the class WebSocketTransport method handshake.

private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String path, FullHttpRequest req) {
    final Channel channel = ctx.channel();
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, configuration.getMaxFramePayloadLength());
    WebSocketServerHandshaker handshaker = factory.newHandshaker(req);
    if (handshaker != null) {
        ChannelFuture f = handshaker.handshake(channel, req);
        f.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    log.error("Can't handshake " + sessionId, future.cause());
                    return;
                }
                channel.pipeline().addBefore(SocketIOChannelInitializer.WEB_SOCKET_TRANSPORT, SocketIOChannelInitializer.WEB_SOCKET_AGGREGATOR, new WebSocketFrameAggregator(configuration.getMaxFramePayloadLength()));
                connectClient(channel, sessionId);
            }
        });
    } else {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) Channel(io.netty.channel.Channel) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 43 with ChannelFutureListener

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

the class DataServerWriteHandler method replySuccess.

/**
   * Writes a response to signify the success of the block write. Also resets the channel.
   *
   * @param channel the channel
   */
private void replySuccess(Channel channel) {
    channel.writeAndFlush(RPCProtoMessage.createOkResponse(null)).addListeners(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE, new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            reset();
        }
    });
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
        channel.read();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException)

Example 44 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project intellij-community by JetBrains.

the class WebSocketHandshakeHandler method handleWebSocketRequest.

private void handleWebSocketRequest(@NotNull final ChannelHandlerContext context, @NotNull FullHttpRequest request, @NotNull final QueryStringDecoder uriDecoder) {
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory("ws://" + request.headers().getAsString(HttpHeaderNames.HOST) + uriDecoder.path(), null, false, NettyUtil.MAX_CONTENT_LENGTH);
    WebSocketServerHandshaker handshaker = factory.newHandshaker(request);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(context.channel());
        return;
    }
    if (!context.channel().isOpen()) {
        return;
    }
    final Client client = new WebSocketClient(context.channel(), handshaker);
    context.channel().attr(ClientManagerKt.getCLIENT()).set(client);
    handshaker.handshake(context.channel(), request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ClientManager clientManager = WebSocketHandshakeHandler.this.clientManager.getValue();
                clientManager.addClient(client);
                MessageChannelHandler messageChannelHandler = new MessageChannelHandler(clientManager, getMessageServer());
                BuiltInServer.replaceDefaultHandler(context, messageChannelHandler);
                ChannelHandlerContext messageChannelHandlerContext = context.pipeline().context(messageChannelHandler);
                context.pipeline().addBefore(messageChannelHandlerContext.name(), "webSocketFrameAggregator", new WebSocketFrameAggregator(NettyUtil.MAX_CONTENT_LENGTH));
                messageChannelHandlerContext.channel().attr(ClientManagerKt.getCLIENT()).set(client);
                connected(client, uriDecoder.parameters());
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 45 with ChannelFutureListener

use of io.netty.channel.ChannelFutureListener in project aerospike-client-java by aerospike.

the class NettyCommand method writeByteBuffer.

private void writeByteBuffer() {
    ByteBuf byteBuffer = PooledByteBufAllocator.DEFAULT.directBuffer(command.dataOffset);
    byteBuffer.clear();
    byteBuffer.writeBytes(command.dataBuffer, 0, command.dataOffset);
    ChannelFuture cf = conn.channel.writeAndFlush(byteBuffer);
    cf.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (state == AsyncCommand.COMMAND_WRITE) {
                state = AsyncCommand.COMMAND_READ_HEADER;
                commandSentCounter++;
            } else {
                state = AsyncCommand.AUTH_READ_HEADER;
            }
            command.dataOffset = 0;
            // Socket timeout applies only to read events.
            // Reset event received because we are switching from a write to a read state.
            // This handles case where write succeeds and read event does not occur.  If we didn't reset,
            // the socket timeout would go through two iterations (double the timeout) because a write
            // event occurred in the first timeout period.
            eventReceived = false;
            conn.channel.config().setAutoRead(true);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Aggregations

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