Search in sources :

Example 16 with IdleStateHandler

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

the class MqttHeartBeatBroker method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.channel(NioServerSocketChannel.class);
        b.childHandler(new ChannelInitializer<SocketChannel>() {

            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast("encoder", MqttEncoder.INSTANCE);
                ch.pipeline().addLast("decoder", new MqttDecoder());
                ch.pipeline().addLast("heartBeatHandler", new IdleStateHandler(45, 0, 0, TimeUnit.SECONDS));
                ch.pipeline().addLast("handler", MqttHeartBeatBrokerHandler.INSTANCE);
            }
        });
        ChannelFuture f = b.bind(1883).sync();
        System.out.println("Broker initiated...");
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) MqttDecoder(io.netty.handler.codec.mqtt.MqttDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 17 with IdleStateHandler

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

the class FlowControlHandlerTest method testSwallowedReadComplete.

@Test
public void testSwallowedReadComplete() throws Exception {
    final long delayMillis = 100;
    final Queue<IdleStateEvent> userEvents = new LinkedBlockingQueue<IdleStateEvent>();
    final EmbeddedChannel channel = new EmbeddedChannel(false, false, new FlowControlHandler(), new IdleStateHandler(delayMillis, 0, 0, MILLISECONDS), new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.fireChannelActive();
            ctx.read();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ctx.fireChannelRead(msg);
            ctx.read();
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
            ctx.fireChannelReadComplete();
            ctx.read();
        }

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
            if (evt instanceof IdleStateEvent) {
                userEvents.add((IdleStateEvent) evt);
            }
            ctx.fireUserEventTriggered(evt);
        }
    });
    channel.config().setAutoRead(false);
    assertFalse(channel.config().isAutoRead());
    channel.register();
    // Reset read timeout by some message
    assertTrue(channel.writeInbound(Unpooled.EMPTY_BUFFER));
    channel.flushInbound();
    assertEquals(Unpooled.EMPTY_BUFFER, channel.readInbound());
    // Emulate 'no more messages in NIO channel' on the next read attempt.
    channel.flushInbound();
    assertNull(channel.readInbound());
    Thread.sleep(delayMillis + 20L);
    channel.runPendingTasks();
    assertEquals(IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT, userEvents.poll());
    assertFalse(channel.finish());
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 18 with IdleStateHandler

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

the class HttpServerWorker method configureHttp1OrH2C.

private void configureHttp1OrH2C(ChannelPipeline pipeline) {
    if (logEnabled) {
        pipeline.addLast("logging", new LoggingHandler(options.getActivityLogDataFormat()));
    }
    if (HttpServerImpl.USE_FLASH_POLICY_HANDLER) {
        pipeline.addLast("flashpolicy", new FlashPolicyHandler());
    }
    pipeline.addLast("httpDecoder", new VertxHttpRequestDecoder(options));
    pipeline.addLast("httpEncoder", new VertxHttpResponseEncoder());
    if (options.isDecompressionSupported()) {
        pipeline.addLast("inflater", new HttpContentDecompressor(false));
    }
    if (options.isCompressionSupported()) {
        pipeline.addLast("deflater", new HttpChunkContentCompressor(options.getCompressionLevel()));
    }
    if (sslHelper.isSSL() || options.isCompressionSupported()) {
        // 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()));
    }
    if (disableH2C) {
        configureHttp1(pipeline);
    } else {
        pipeline.addLast("h2c", new Http1xUpgradeToH2CHandler(this, options.isCompressionSupported(), options.isDecompressionSupported()));
    }
}
Also used : FlashPolicyHandler(io.vertx.core.http.impl.cgbystrom.FlashPolicyHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpContentDecompressor(io.netty.handler.codec.http.HttpContentDecompressor)

Example 19 with IdleStateHandler

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

the class HttpServerWorker method configureHttp2.

void configureHttp2(ChannelPipeline pipeline) {
    if (!server.requestAccept()) {
        // That should send an HTTP/2 go away
        pipeline.channel().close();
        return;
    }
    int idleTimeout = options.getIdleTimeout();
    int readIdleTimeout = options.getReadIdleTimeout();
    int writeIdleTimeout = options.getWriteIdleTimeout();
    if (idleTimeout > 0 || readIdleTimeout > 0 || writeIdleTimeout > 0) {
        pipeline.addBefore("handler", "idle", new IdleStateHandler(readIdleTimeout, writeIdleTimeout, idleTimeout, options.getIdleTimeoutUnit()));
    }
}
Also used : IdleStateHandler(io.netty.handler.timeout.IdleStateHandler)

Example 20 with IdleStateHandler

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

the class HttpChannelConnector method applyHttp1xConnectionOptions.

private void applyHttp1xConnectionOptions(ChannelPipeline pipeline) {
    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()));
    }
    if (options.getLogActivity()) {
        pipeline.addLast("logging", new LoggingHandler(options.getActivityLogDataFormat()));
    }
    pipeline.addLast("codec", new HttpClientCodec(options.getMaxInitialLineLength(), options.getMaxHeaderSize(), options.getMaxChunkSize(), false, !HttpHeaders.DISABLE_HTTP_HEADERS_VALIDATION, options.getDecoderInitialBufferSize()));
    if (options.isTryUseCompression()) {
        pipeline.addLast("inflater", new HttpContentDecompressor(false));
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler)

Aggregations

IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)70 ChannelPipeline (io.netty.channel.ChannelPipeline)35 SocketChannel (io.netty.channel.socket.SocketChannel)18 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)14 LoggingHandler (io.netty.handler.logging.LoggingHandler)14 SslHandler (io.netty.handler.ssl.SslHandler)14 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)12 ChannelFuture (io.netty.channel.ChannelFuture)10 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)10 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 EventLoopGroup (io.netty.channel.EventLoopGroup)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)7 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 Channel (io.netty.channel.Channel)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 SslContext (io.netty.handler.ssl.SslContext)6 InetSocketAddress (java.net.InetSocketAddress)6