Search in sources :

Example 1 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project transporter by wang4ever.

the class AbstractChannelMessageHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        logger.warn("连接空闲. {}, client={}", event.state(), this.client.asText());
        switch(event.state()) {
            case // 读超时事件:可能客户端设备No-run或断线, 为节省资源则close.
            READER_IDLE:
                this.closeClient();
                break;
            case // 写超时事件:可能此连接上无业务同时也未收到心跳, 为节省资源则close.
            WRITER_IDLE:
                this.closeClient();
                break;
            case // 读&写:异常情况(可能客户端设备No-run或断线), 为节省资源则close.
            ALL_IDLE:
                this.closeClient();
                break;
            default:
                throw new UnsupportedOperationException("不支持的空闲检测类型.");
        }
    }
    super.userEventTriggered(ctx, evt);
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent)

Example 2 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project transporter by wang4ever.

the class AbstractMessageHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        getLogger().warn("连接空闲. " + ctx.channel() + ", " + event.state());
        switch(event.state()) {
            case // 读超时事件:可能此通道无业务或断线, 发送心跳检查一下.
            READER_IDLE:
                ctx.channel().writeAndFlush(new ActiveMessage());
                break;
            case // 写超时事件:异常情况(连心跳都未发送,不易触发), 直接close重连.
            WRITER_IDLE:
                // 重连
                this.reconnect(ctx);
                break;
            case // 读&写事件:异常情况(连心跳都未发送,不易触发), 直接close重连.
            ALL_IDLE:
                // 重连
                this.reconnect(ctx);
                break;
            default:
                throw new UnsupportedOperationException("不支持的空闲检测类型.");
        }
    }
    super.userEventTriggered(ctx, evt);
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) ActiveMessage(io.transport.sdk.protocol.message.internal.ActiveMessage)

Example 3 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project lightconf by haifeiWu.

the class ClientHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        switch(e.state()) {
            case WRITER_IDLE:
                PingMsg pingMsg = new PingMsg();
                ctx.writeAndFlush(pingMsg);
                break;
            default:
                break;
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent)

Example 4 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project pancm_project by xuwujing.

the class NettyClientHandlerDemo5 method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    System.out.println("循环触发时间:" + MyTools.getNowTime(""));
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        System.out.println("event.state():" + event.state() + ",IdleState.READER_IDLE:" + IdleState.READER_IDLE);
        if (event.state() == IdleState.WRITER_IDLE) {
            System.out.println("TRY_TIMES:" + TRY_TIMES);
            if (currentTime <= TRY_TIMES) {
                System.out.println("currentTime:" + currentTime);
                currentTime++;
                ctx.channel().writeAndFlush(HEARTBEAT_SEQUENCE.duplicate());
            }
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent)

Example 5 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project pancm_project by xuwujing.

the class NettyServerHandlerDemo5 method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        System.out.println("event.state():" + event.state() + ",IdleState.READER_IDLE:" + IdleState.READER_IDLE);
        if (event.state() == IdleState.READER_IDLE) {
            loss_connect_time++;
            System.out.println("5 秒没有接收到客户端的信息了");
            if (loss_connect_time > 2) {
                System.out.println("关闭这个不活跃的channel");
                ctx.channel().close();
            }
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent)

Aggregations

IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)28 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 Channel (io.netty.channel.Channel)3 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 SslHandshakeCompletionEvent (io.netty.handler.ssl.SslHandshakeCompletionEvent)3 CompleteEvent (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 SslHandler (io.netty.handler.ssl.SslHandler)2 JSONObject (com.alibaba.fastjson.JSONObject)1 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)1 TokenMessage (com.bonree.brfs.common.net.tcp.TokenMessage)1 CompleteReason (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason)1 HttpRequestReadTimeoutEvent (com.netflix.netty.common.HttpRequestReadTimeoutEvent)1 OutboundException (com.netflix.zuul.exception.OutboundException)1 ZuulException (com.netflix.zuul.exception.ZuulException)1 RequestCancelledEvent (com.netflix.zuul.netty.RequestCancelledEvent)1 Request (info.xiancloud.nettyhttpserver.http.bean.Request)1