Search in sources :

Example 56 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project vert.x by eclipse.

the class HttpServerWorker method handle.

@Override
public void handle(Channel ch) {
    if (HAProxyMessageCompletionHandler.canUseProxyProtocol(options.isUseProxyProtocol())) {
        IdleStateHandler idle;
        io.netty.util.concurrent.Promise<Channel> p = ch.eventLoop().newPromise();
        ch.pipeline().addLast(new HAProxyMessageDecoder());
        if (options.getProxyProtocolTimeout() > 0) {
            ch.pipeline().addLast("idle", idle = new IdleStateHandler(0, 0, options.getProxyProtocolTimeout(), options.getProxyProtocolTimeoutUnit()));
        } else {
            idle = null;
        }
        ch.pipeline().addLast(new HAProxyMessageCompletionHandler(p));
        p.addListener((GenericFutureListener<Future<Channel>>) future -> {
            if (future.isSuccess()) {
                if (idle != null) {
                    ch.pipeline().remove(idle);
                }
                configurePipeline(future.getNow());
            } else {
                handleException(future.cause());
            }
        });
    } else {
        configurePipeline(ch);
    }
}
Also used : HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) SniHandler(io.netty.handler.ssl.SniHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) FlashPolicyHandler(io.vertx.core.http.impl.cgbystrom.FlashPolicyHandler) ContextInternal(io.vertx.core.impl.ContextInternal) SslHandshakeCompletionHandler(io.vertx.core.net.impl.SslHandshakeCompletionHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) Supplier(java.util.function.Supplier) Unpooled(io.netty.buffer.Unpooled) HttpServerMetrics(io.vertx.core.spi.metrics.HttpServerMetrics) IdleState(io.netty.handler.timeout.IdleState) io.netty.channel(io.netty.channel) VertxHandler(io.vertx.core.net.impl.VertxHandler) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) VertxInternal(io.vertx.core.impl.VertxInternal) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) SSLHelper(io.vertx.core.net.impl.SSLHelper) StandardCharsets(java.nio.charset.StandardCharsets) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) EventLoopContext(io.vertx.core.impl.EventLoopContext) SslHandler(io.netty.handler.ssl.SslHandler) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor) Future(io.netty.util.concurrent.Future) Handler(io.vertx.core.Handler) HAProxyMessageCompletionHandler(io.vertx.core.net.impl.HAProxyMessageCompletionHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) Future(io.netty.util.concurrent.Future)

Example 57 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project vert.x by eclipse.

the class HttpServerWorker method configurePipeline.

private void configurePipeline(Channel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslHelper.isSSL()) {
        if (options.isSni()) {
            SniHandler sniHandler = new SniHandler(sslHelper.serverNameMapper(vertx));
            pipeline.addLast(sniHandler);
        } else {
            SslHandler handler = new SslHandler(sslHelper.createEngine(vertx));
            handler.setHandshakeTimeout(sslHelper.getSslHandshakeTimeout(), sslHelper.getSslHandshakeTimeoutUnit());
            pipeline.addLast("ssl", handler);
        }
        ChannelPromise p = ch.newPromise();
        pipeline.addLast("handshaker", new SslHandshakeCompletionHandler(p));
        p.addListener(future -> {
            if (future.isSuccess()) {
                if (options.isUseAlpn()) {
                    SslHandler sslHandler = pipeline.get(SslHandler.class);
                    String protocol = sslHandler.applicationProtocol();
                    if ("h2".equals(protocol)) {
                        handleHttp2(ch);
                    } else {
                        handleHttp1(ch);
                    }
                } else {
                    handleHttp1(ch);
                }
            } else {
                handleException(future.cause());
            }
        });
    } else {
        if (disableH2C) {
            handleHttp1(ch);
        } else {
            IdleStateHandler idle;
            int idleTimeout = options.getIdleTimeout();
            int readIdleTimeout = options.getReadIdleTimeout();
            int writeIdleTimeout = options.getWriteIdleTimeout();
            if (idleTimeout > 0 || readIdleTimeout > 0 || writeIdleTimeout > 0) {
                pipeline.addLast("idle", idle = new IdleStateHandler(readIdleTimeout, writeIdleTimeout, idleTimeout, options.getIdleTimeoutUnit()));
            } else {
                idle = null;
            }
            // Handler that detects whether the HTTP/2 connection preface or just process the request
            // with the HTTP 1.x pipeline to support H2C with prior knowledge, i.e a client that connects
            // and uses HTTP/2 in clear text directly without an HTTP upgrade.
            pipeline.addLast(new Http1xOrH2CHandler() {

                @Override
                protected void configure(ChannelHandlerContext ctx, boolean h2c) {
                    if (idle != null) {
                        // It will be re-added but this way we don't need to pay attention to order
                        pipeline.remove(idle);
                    }
                    if (h2c) {
                        handleHttp2(ctx.channel());
                    } else {
                        handleHttp1(ch);
                    }
                }

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                    if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state() == IdleState.ALL_IDLE) {
                        ctx.close();
                    }
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    super.exceptionCaught(ctx, cause);
                    handleException(cause);
                }
            });
        }
    }
}
Also used : SniHandler(io.netty.handler.ssl.SniHandler) SslHandler(io.netty.handler.ssl.SslHandler) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) SslHandshakeCompletionHandler(io.vertx.core.net.impl.SslHandshakeCompletionHandler)

Example 58 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project vert.x by eclipse.

the class NetServerImpl method initChannel.

protected void initChannel(ChannelPipeline pipeline, boolean ssl) {
    if (options.getLogActivity()) {
        pipeline.addLast("logging", new LoggingHandler(options.getActivityLogDataFormat()));
    }
    if (ssl) {
        // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used.
        // For large file / sendfile support
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    }
    int idleTimeout = options.getIdleTimeout();
    int readIdleTimeout = options.getReadIdleTimeout();
    int writeIdleTimeout = options.getWriteIdleTimeout();
    if (idleTimeout > 0 || readIdleTimeout > 0 || writeIdleTimeout > 0) {
        pipeline.addLast("idle", new IdleStateHandler(readIdleTimeout, writeIdleTimeout, idleTimeout, options.getIdleTimeoutUnit()));
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler)

Example 59 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project Glowstone by GlowstoneMC.

the class GlowChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel c) {
    MessageHandler handler = new MessageHandler(connectionManager);
    CodecsHandler codecs = new CodecsHandler(connectionManager.getProtocolProvider().handshake);
    FramingHandler framing = new FramingHandler();
    try {
        c.config().setOption(ChannelOption.IP_TOS, 0x18);
    } catch (ChannelException e) {
        // Not supported on all OSs, like Windows XP and lesser
        GlowServer.logger.warning("Your OS does not support type of service.");
    }
    c.pipeline().addLast("idle_timeout", new IdleStateHandler(READ_IDLE_TIMEOUT, WRITE_IDLE_TIMEOUT, 0)).addLast("legacy_ping", new LegacyPingHandler(connectionManager)).addLast("encryption", NoopHandler.INSTANCE).addLast("framing", framing).addLast("compression", NoopHandler.INSTANCE).addLast("codecs", codecs).addLast("handler", handler);
}
Also used : LegacyPingHandler(net.glowstone.net.handler.legacyping.LegacyPingHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelException(io.netty.channel.ChannelException)

Example 60 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project dubbo by alibaba.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetIdleEventHandler());
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)61 ChannelPipeline (io.netty.channel.ChannelPipeline)29 SocketChannel (io.netty.channel.socket.SocketChannel)16 LoggingHandler (io.netty.handler.logging.LoggingHandler)15 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)12 SslHandler (io.netty.handler.ssl.SslHandler)11 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)11 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)10 EventLoopGroup (io.netty.channel.EventLoopGroup)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)8 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)8 Bootstrap (io.netty.bootstrap.Bootstrap)7 ChannelFuture (io.netty.channel.ChannelFuture)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)6 InetSocketAddress (java.net.InetSocketAddress)6 StringDecoder (io.netty.handler.codec.string.StringDecoder)5 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)5 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)5 ThreadFactory (java.util.concurrent.ThreadFactory)5