Java Code Examples for io.netty.handler.timeout.IdleState#ALL_IDLE

The following examples show how to use io.netty.handler.timeout.IdleState#ALL_IDLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MatchServerHandler.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //超时事件
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idleEvent = (IdleStateEvent) evt;
        //读
        if (idleEvent.state() == IdleState.READER_IDLE) {
            LinkMgr.closeOnFlush(ctx.channel(), ConnState.CLOSE_HEARTBEAT_EXPIRE);
        }
        //写
        else if (idleEvent.state() == IdleState.WRITER_IDLE) {

        }
        //全部
        else if (idleEvent.state() == IdleState.ALL_IDLE) {

        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example 2
Source File: HeartbeatServerHandler.java    From nuls-v2 with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    if (evt instanceof IdleStateEvent) {
        SocketChannel channel = (SocketChannel) ctx.channel();
        String nodeId = IpUtil.getNodeId(channel.remoteAddress());
        LoggerUtil.COMMON_LOG.info("{}====userEventTriggered  IdleStateEvent==", nodeId);
        IdleStateEvent event = (IdleStateEvent) evt;
        String type = "";
        if (event.state() == IdleState.READER_IDLE) {
            type = "read idle";
        } else if (event.state() == IdleState.WRITER_IDLE) {
            type = "write idle";
        } else if (event.state() == IdleState.ALL_IDLE) {
            type = "all idle";
        }
        LoggerUtil.COMMON_LOG.info("localInfo: " + channel.localAddress().getHostString() + ":" + channel.localAddress().getPort());
        LoggerUtil.COMMON_LOG.info("remoteInfo: " + channel.remoteAddress().getHostString() + ":" + channel.remoteAddress().getPort());
        ctx.channel().close();
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 3
Source File: HeartbeatHandler.java    From pampas with Apache License 2.0 6 votes vote down vote up
@Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        ctx.fireUserEventTriggered(evt);
        if (evt instanceof IdleStateEvent) {  // 2
            IdleStateEvent event = (IdleStateEvent) evt;
            String type = "";
            if (event.state() == IdleState.READER_IDLE) {
                type = "read idle";
            } else if (event.state() == IdleState.WRITER_IDLE) {
                type = "write idle";
            } else if (event.state() == IdleState.ALL_IDLE) {
                type = "all idle";
            }

//            ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate()).addListener(
//                    ChannelFutureListener.CLOSE_ON_FAILURE);
//            log.info("{}超时类型:{}", ctx.channel().remoteAddress(), type);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
 
Example 4
Source File: SoaLinkStateHandler.java    From dapeng-soa with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;

        if (e.state() == IdleState.READER_IDLE) {
            ctx.close();
            LOGGER.info(getClass().getName() + "::读超时,关闭连接:" + ctx.channel());

        } else if (e.state() == IdleState.WRITER_IDLE) {
            ctx.writeAndFlush(ctx.alloc().buffer(1).writeInt(0));

            if (LOGGER.isDebugEnabled())
                LOGGER.debug(getClass().getName() + "::写超时,发送心跳包:" + ctx.channel());

        } else if (e.state() == IdleState.ALL_IDLE) {

            if (LOGGER.isDebugEnabled())
                LOGGER.debug(getClass().getName() + "::读写都超时,发送心跳包:" + ctx.channel());
        }
    }

}
 
Example 5
Source File: NettyClientMessageHandler.java    From Lottor with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
    //心跳配置
    if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.READER_IDLE) {
            SpringBeanUtils.getInstance().getBean(NettyClientService.class).doConnect();
        } else if (event.state() == IdleState.WRITER_IDLE) {
            //表示已经多久没有发送数据了
            HEART_BEAT.setAction(NettyMessageActionEnum.HEART.getCode());
            HEART_BEAT.setMetaInfo(modelNameService.findClientMetaInfo());
            HEART_BEAT.setSerialProtocol(this.txConfig.getNettySerializer());
            TxTransactionGroup group = new TxTransactionGroup();
            group.setSource(modelNameService.findModelName());
            HEART_BEAT.setTxTransactionGroup(group);
            ctx.writeAndFlush(HEART_BEAT);
            LogUtil.debug(LOGGER, "发送【心跳】事件到Lottor Server【{}】", () -> ctx.channel().remoteAddress().toString().substring(1));
        } else if (event.state() == IdleState.ALL_IDLE) {
            //表示已经多久既没有收到也没有发送数据了
            SpringBeanUtils.getInstance().getBean(NettyClientService.class).doConnect();
        }
    }
}
 
Example 6
Source File: MqttTransportHandler.java    From netty-learning-example with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
        if (idleStateEvent.state() == IdleState.ALL_IDLE) {
            Channel channel = ctx.channel();
            String clientId = (String) channel.attr(AttributeKey.valueOf("clientId")).get();
            // 发送遗嘱消息
            if (this.protocolProcess.getGrozaSessionStoreService().containsKey(clientId)) {
                SessionStore sessionStore = this.protocolProcess.getGrozaSessionStoreService().get(clientId);
                if (sessionStore.getWillMessage() != null) {
                    this.protocolProcess.publish().processPublish(ctx.channel(), sessionStore.getWillMessage());
                }
            }
            ctx.close();
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 7
Source File: BattleServerHandler.java    From Almost-Famous with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //超时事件
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idleEvent = (IdleStateEvent) evt;
        //读
        if (idleEvent.state() == IdleState.READER_IDLE) {
            LinkMgr.closeOnFlush(ctx.channel(), ConnState.CLOSE_HEARTBEAT_EXPIRE);
        }
        //写
        else if (idleEvent.state() == IdleState.WRITER_IDLE) {

        }
        //全部
        else if (idleEvent.state() == IdleState.ALL_IDLE) {

        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example 8
Source File: ClientByte2MessageInboundHandler.java    From jt809-tcp-server with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleState state = ((IdleStateEvent) evt).state();
        if (state == IdleState.READER_IDLE) {

        }else if(state == IdleState.WRITER_IDLE){
            log.warn("发送心跳包到服务端 : {}",ctx.channel().remoteAddress().toString());
            this.sendTestMsgToServer(ctx);
        }else if(state == IdleState.ALL_IDLE){
            log.warn("发送心跳包到服务端 : {}",ctx.channel().remoteAddress().toString());
            this.sendTestMsgToServer(ctx);
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 9
Source File: BaseHandler.java    From jt808-netty with MIT License 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        //此实例项目只设置了读取超时时间,可以通过state分别做处理,一般服务端在这里关闭连接节省资源,客户端发送心跳维持连接
        IdleState state = ((IdleStateEvent) evt).state();
        if (state == IdleState.READER_IDLE) {
            log.warn("客户端{}读取超时,关闭连接", ctx.channel().remoteAddress());
            ctx.close();
        } else if (state == IdleState.WRITER_IDLE) {
            log.warn("客户端{}写入超时", ctx.channel().remoteAddress());
        }else if(state == IdleState.ALL_IDLE){
            log.warn("客户端{}读取写入超时", ctx.channel().remoteAddress());
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 10
Source File: HeartbeatClientHandler.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.WRITER_IDLE) {
        	NettyLog.info("client WRITER_IDLE");
        } else if (e.state() == IdleState.READER_IDLE) {
        	TranDataProtoUtil.writeAndFlushTranData(ctx, TranDataProtoUtil.getPingInstance());
        	
        	NettyLog.info("client READER_IDLE");
        	return;
        	
        } else if (e.state() == IdleState.ALL_IDLE) {
        	NettyLog.info("client ALL_IDLE");
        	ctx.close();
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example 11
Source File: HttpRequester.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.ALL_IDLE) {
      	  ctx.close();
      	  logger.debug("Closing channel. Idle for >= [{}] seconds", IDLE_TIMEOUT_SECONDS);
        }
    }
}
 
Example 12
Source File: TcpHandler.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
        if (idleStateEvent.state() == IdleState.ALL_IDLE) {
            String channelId = ctx.channel().id().asShortText();
            log.info("channel idle too long, close channel: {}", channelId);
            ctx.close();
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 13
Source File: NettyHttpServer.java    From krpc with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //debugLog("userEventTriggered, evt="+evt);
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.ALL_IDLE) {
            String connId = getConnId(ctx);
            ctx.close();
            log.error("connection timeout, connId={}", connId);
        }
    }
}
 
Example 14
Source File: RPCServerChannelIdleHandler.java    From rpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object event) throws Exception {
    if (event instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) event;
        if (e.state() == IdleState.ALL_IDLE) {
            // if no read and write for period time, close current channel
            LOG.debug("channel={} ip={} is idle for period time, close now.",
                    ctx.channel(), ctx.channel().remoteAddress());
            ctx.close();
        } else {
            LOG.debug("idle on channel[{}]:{}", e.state(), ctx.channel());
        }
    }
}
 
Example 15
Source File: NetEventNettyEntrance.java    From PeonyFramwork with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleState state = ((IdleStateEvent) evt).state();
        if (state == IdleState.ALL_IDLE) {
            ctx.writeAndFlush(pingEvent);
            log.info("send ping to {}", ctx.channel());
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 16
Source File: RpcServerChannelIdleHandler.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object event) throws Exception {
    if (event instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) event;
        if (e.state() == IdleState.ALL_IDLE) {
            // if no read and write for period time, close current channel
            LOG.debug("channel={} ip={} is idle for period time, close now.",
                    ctx.channel(), ctx.channel().remoteAddress());
            ctx.close();
        } else {
            LOG.debug("idle on channel[{}]:{}", e.state(), ctx.channel());
        }
    }
}
 
Example 17
Source File: NettyClient.java    From krpc with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //debugLog("userEventTriggered called, evt="+evt);
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.ALL_IDLE) {
            Channel ch = ctx.channel();
            ByteBuf encoded = ctx.alloc().buffer(32);
            codec.getReqHeartBeat(encoded);
            ch.writeAndFlush(encoded);
        }
    }
}
 
Example 18
Source File: MqttServerHandler.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
	if (evt instanceof IdleStateEvent) {
		IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
		if (idleStateEvent.state() == IdleState.ALL_IDLE) {
			this.protocolProcess.processWillMessage(ctx.channel());
			ctx.close();
		}
	} else {
		super.userEventTriggered(ctx, evt);
	}
}
 
Example 19
Source File: HeartBeatHandler.java    From momo-cloud-permission with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    // 判断evt是否是IdleStateEvent(用于触发用户事件,包含 读空闲/写空闲/读写空闲 )
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;        // 强制类型转换
        String type = "";
        if (event.state() == IdleState.READER_IDLE) {
            log.info("进入读空闲...");
            type = "read idle";
        } else if (event.state() == IdleState.WRITER_IDLE) {
            log.info("进入写空闲...");
            type = "write idle";
        } else if (event.state() == IdleState.ALL_IDLE) {
            type = "all idle";
        }

        if (unRecPingTimes >= MAX_UN_REC_PING_TIMES) {
            // 连续超过N次未收到client的ping消息,那么关闭该通道,等待client重连
            ctx.channel().close();
        } else {
            // 失败计数器加1

            unRecPingTimes++;
        }
        log.debug(ctx.channel().remoteAddress() + "超时类型:" + type);
    } else {
        super.userEventTriggered(ctx, evt);
    }

}
 
Example 20
Source File: NettyServer.java    From krpc with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    //debugLog("userEventTriggered, evt="+evt);
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.ALL_IDLE) {
            String connId = getConnId(ctx);
            log.error("connection timeout, connId={}", connId);
            ctx.close();
        }
    }
}