Search in sources :

Example 46 with ChannelFutureListener

use of org.apache.flink.shaded.netty4.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 47 with ChannelFutureListener

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

the class DynamicAddressConnectHandler method connect.

@Override
public final void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
    final SocketAddress remote;
    final SocketAddress local;
    try {
        remote = remoteAddress(remoteAddress, localAddress);
        local = localAddress(remoteAddress, localAddress);
    } catch (Exception e) {
        promise.setFailure(e);
        return;
    }
    ctx.connect(remote, local, promise).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                // We only remove this handler from the pipeline once the connect was successful as otherwise
                // the user may try to connect again.
                future.channel().pipeline().remove(DynamicAddressConnectHandler.this);
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketAddress(java.net.SocketAddress) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 48 with ChannelFutureListener

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

the class WebSocketClientHandshaker method handshake.

/**
 * Begins the opening handshake
 *
 * @param channel
 *            Channel
 * @param promise
 *            the {@link ChannelPromise} to be notified when the opening handshake is sent
 */
public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {
    ChannelPipeline pipeline = channel.pipeline();
    HttpResponseDecoder decoder = pipeline.get(HttpResponseDecoder.class);
    if (decoder == null) {
        HttpClientCodec codec = pipeline.get(HttpClientCodec.class);
        if (codec == null) {
            promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpResponseDecoder or HttpClientCodec"));
            return promise;
        }
    }
    FullHttpRequest request = newHandshakeRequest();
    channel.writeAndFlush(request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ChannelPipeline p = future.channel().pipeline();
                ChannelHandlerContext ctx = p.context(HttpRequestEncoder.class);
                if (ctx == null) {
                    ctx = p.context(HttpClientCodec.class);
                }
                if (ctx == null) {
                    promise.setFailure(new IllegalStateException("ChannelPipeline does not contain " + "an HttpRequestEncoder or HttpClientCodec"));
                    return;
                }
                p.addAfter(ctx.name(), "ws-encoder", newWebSocketEncoder());
                promise.setSuccess();
            } else {
                promise.setFailure(future.cause());
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequestEncoder(io.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 49 with ChannelFutureListener

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

the class WebSocketClientHandshaker method close0.

private ChannelFuture close0(final ChannelOutboundInvoker invoker, final Channel channel, CloseWebSocketFrame frame, ChannelPromise promise) {
    invoker.writeAndFlush(frame, promise);
    final long forceCloseTimeoutMillis = this.forceCloseTimeoutMillis;
    final WebSocketClientHandshaker handshaker = this;
    if (forceCloseTimeoutMillis <= 0 || !channel.isActive() || forceCloseInit != 0) {
        return promise;
    }
    promise.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            // Also, close might be called twice from different threads.
            if (future.isSuccess() && channel.isActive() && FORCE_CLOSE_INIT_UPDATER.compareAndSet(handshaker, 0, 1)) {
                final Future<?> forceCloseFuture = channel.eventLoop().schedule(new Runnable() {

                    @Override
                    public void run() {
                        if (channel.isActive()) {
                            invoker.close();
                            forceCloseComplete = true;
                        }
                    }
                }, forceCloseTimeoutMillis, TimeUnit.MILLISECONDS);
                channel.closeFuture().addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        forceCloseFuture.cancel(false);
                    }
                });
            }
        }
    });
    return promise;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Future(java.util.concurrent.Future) ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 50 with ChannelFutureListener

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

the class WebSocketClientProtocolHandshakeHandler method channelActive.

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    handshaker.handshake(ctx.channel()).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (!future.isSuccess()) {
                handshakePromise.tryFailure(future.cause());
                ctx.fireExceptionCaught(future.cause());
            } else {
                ctx.fireUserEventTriggered(WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_ISSUED);
            }
        }
    });
    applyHandshakeTimeout();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Aggregations

ChannelFutureListener (io.netty.channel.ChannelFutureListener)223 ChannelFuture (io.netty.channel.ChannelFuture)208 Channel (io.netty.channel.Channel)70 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)57 ByteBuf (io.netty.buffer.ByteBuf)49 Bootstrap (io.netty.bootstrap.Bootstrap)43 Test (org.junit.jupiter.api.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)36 IOException (java.io.IOException)35 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)33 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)31 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)31 InetSocketAddress (java.net.InetSocketAddress)27 ClosedChannelException (java.nio.channels.ClosedChannelException)25 ChannelPromise (io.netty.channel.ChannelPromise)21 Logger (org.slf4j.Logger)21 LoggerFactory (org.slf4j.LoggerFactory)21 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)20 EventLoopGroup (io.netty.channel.EventLoopGroup)18 List (java.util.List)17