io.netty.handler.timeout.IdleState Java Examples

The following examples show how to use io.netty.handler.timeout.IdleState. 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: 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 #2
Source File: IdleStateTrigger.java    From netty-pubsub 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();
        switch (state){
            case READER_IDLE:
            	 //�����¼�����������
                //System.out.println("����������\t"+ DateUtils.getCurrentDateTime());
                //ctx.channel().writeAndFlushֱ�Ӵ�outβ�����һ��handler��ջ
                //ctx.writeAndFlush �ӵ�ǰ����һ����β�˳�ջ
                ChannelFuture channelFuture = ctx.channel().writeAndFlush(HEARTBEATE.clone());
                channelFuture.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
                break;
        }
    }else{
        super.userEventTriggered(ctx, evt);
    }
}
 
Example #3
Source File: NettyClientHandlerDemo5.java    From java-study with Apache License 2.0 6 votes vote down vote up
@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());  
            }  
        }  
    }  
}
 
Example #4
Source File: NettyRemotingClient.java    From rocketmq 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 event = (IdleStateEvent)evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #5
Source File: NettyRemotingClient.java    From dts 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 evnet = (IdleStateEvent)evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this.putNettyEvent(
                    new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #6
Source File: ServerConnectionStateNotifier.java    From openAGV 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) {
        if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE
                && connectionEventListener != null) {
            connectionEventListener.onIdle();
        }
    } else if (evt instanceof ConnectionAssociatedEvent) {
        key = ((ConnectionAssociatedEvent) evt).getKey();
        LOG.debug("Connection associated to key: '{}'", key);
        if (null != clientEntries && null != key && null != clientEntries.get(key)) {
            connectionEventListener = clientEntries.get(key).getConnectionEventListener();
            if (null != connectionEventListener) {
                connectionEventListener.onConnect();
            }
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #7
Source File: NettyRemotingServer.java    From dts 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 evnet = (IdleStateEvent)evt;
        if (evnet.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this.putNettyEvent(
                    new NettyEvent(NettyEventType.IDLE, remoteAddress.toString(), ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #8
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 #9
Source File: NettyRemotingClient.java    From rocketmq-read 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 event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #10
Source File: NettyRemotingServer.java    From rocketmq-read 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 event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #11
Source File: NoConnectIdleHandler.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(final @NotNull ChannelHandlerContext ctx, final @NotNull Object evt) {

    if (evt instanceof IdleStateEvent) {

        if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
            if (log.isDebugEnabled()) {

                log.debug("Client with IP {} disconnected. The client was idle for too long without sending a MQTT CONNECT packet",
                        ChannelUtils.getChannelIP(ctx.channel()).or("UNKNOWN"));
            }
            eventLog.clientWasDisconnected(ctx.channel(), "No CONNECT sent in time");
            ctx.close();
            return;
        }
    }
    ctx.fireUserEventTriggered(evt);
}
 
Example #12
Source File: NoTlsHandshakeIdleHandler.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {

    if (evt instanceof IdleStateEvent) {

        if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
            if (log.isDebugEnabled()) {

                log.debug("Client with IP {} disconnected. The client was idle for too long without finishing the TLS handshake",
                        ChannelUtils.getChannelIP(ctx.channel()).or("UNKNOWN"));
            }
            eventLog.clientWasDisconnected(ctx.channel(), "TLS handshake not finished in time");
            ctx.close();
            return;
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #13
Source File: KeepAliveIdleHandler.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt) throws Exception {

    if (evt instanceof IdleStateEvent) {

        if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
            if (log.isDebugEnabled()) {

                log.debug("Client with IP {} disconnected. The client was idle for too long without sending a MQTT control packet",
                        ChannelUtils.getChannelIP(ctx.channel()).or("UNKNOWN"));
            }
            eventLog.clientWasDisconnected(ctx.channel(), "Client was idle for too long");
            ctx.close();
            return;
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #14
Source File: NettyRemotingServer.java    From DDMQ 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 event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY SERVER PIPELINE: IDLE exception [{}]", remoteAddress);
            RemotingUtil.closeChannel(ctx.channel());
            if (NettyRemotingServer.this.channelEventListener != null) {
                NettyRemotingServer.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #15
Source File: NettyRemotingClient.java    From DDMQ 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 event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.ALL_IDLE)) {
            final String remoteAddress = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
            log.warn("NETTY CLIENT PIPELINE: IDLE exception [{}]", remoteAddress);
            closeChannel(ctx.channel());
            if (NettyRemotingClient.this.channelEventListener != null) {
                NettyRemotingClient.this
                    .putNettyEvent(new NettyEvent(NettyEventType.IDLE, remoteAddress, ctx.channel()));
            }
        }
    }

    ctx.fireUserEventTriggered(evt);
}
 
Example #16
Source File: HeartbeatServerHandler.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("WRITER_IDLE");
        } else if (e.state() == IdleState.READER_IDLE) {
        	NettyLog.info("READER_IDLE");
            //ctx.channel().close();
        } else if (e.state() == IdleState.ALL_IDLE) {
        	NettyLog.info("ALL_IDLE");
        	//
        	ctx.close();
        	return ;
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #17
Source File: ClientSocket.java    From OrionAlpha with GNU General Public License v3.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) {
            if (user != null && migrateState == MigrateState.WaitCenterMigrateOutResult) {
                // If a user has been migrating for over 30 seconds, dc and prepare a
                // HandleUserMigrateTimeout on the center JVM.
                if (System.currentTimeMillis() - migrateOut > 30000) {
                    channelInactive(ctx);
                }
            // If there's no active user and they aren't migrating, then they have no
            // excuse as to why they have no read operations for 20 seconds straight.
            } else {
                channelInactive(ctx);
            }
        // Handle the AliveAck on the client-end if this ever occurs.
        // If the user hits over a 15-second writer timeout -> disconnect.
        } else if (e.state() == IdleState.WRITER_IDLE) {
            channel.writeAndFlush(onAliveReq((int) (System.currentTimeMillis() / 1000)));
        }
    }
}
 
Example #18
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 #19
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 #20
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 #21
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 #22
Source File: NettyServerHandler.java    From springBoot-study with Apache License 2.0 6 votes vote down vote up
/**
 * 超时处理 如果5秒没有接受客户端的心跳,就触发; 如果超过两次,则直接关闭;
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object obj) throws Exception {
	if (obj instanceof IdleStateEvent) {
		IdleStateEvent event = (IdleStateEvent) obj;
		if (IdleState.READER_IDLE.equals(event.state())) { // 如果读通道处于空闲状态,说明没有接收到心跳命令
			System.out.println("已经5秒没有接收到客户端的信息了");
			if (idle_count > 1) {
				System.out.println("关闭这个不活跃的channel");
				ctx.channel().close();
			}
			idle_count++;
		}
	} else {
		super.userEventTriggered(ctx, obj);
	}
}
 
Example #23
Source File: MqttCommandInvocation.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    String clientId = NettyAttrManager.getAttrClientId(ctx.channel());
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.READER_IDLE)) {
            // keepalive的1.5倍时间内没有收到client端写操作 触发inactive并关闭连接
            LOG.info("READER_IDLE: {}, start close channel...", clientId);
            ctx.fireChannelInactive();
            ctx.close().addListener(CLOSE_ON_FAILURE);
        } else if (event.state().equals(IdleState.WRITER_IDLE)) {
            //未进行写操作
            LOG.info("WRITER_IDLE: {}, start close channel...", clientId);
        } else if (event.state().equals(IdleState.ALL_IDLE)) {
            //未进行读写
            LOG.info("ALL_IDLE: {}, start close channel...", clientId);
            ctx.fireChannelInactive();
            ctx.close().addListener(CLOSE_ON_FAILURE);
        }
    }
}
 
Example #24
Source File: ClientHandler.java    From LuckyFrameClient with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
        throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.READER_IDLE)) {
            //��������,���ֳ�����
            JSONObject json = new JSONObject();
            json.put("method", "ping");
            json.put("hostName", NETTY_HOST);
            //ctx.channel().writeAndFlush(json.toString() + "$_").sync();
            sendMessage(json.toString());
            //log.info("�������ͳɹ�!");
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example #25
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 #26
Source File: NettyServerHandler.java    From netty-learning-example with Apache License 2.0 6 votes vote down vote up
/**
 * 超时处理 如果5秒没有接受客户端的心跳,就触发; 如果超过两次,则直接关闭;
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object obj) throws Exception {
    if (obj instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) obj;
        // 如果读通道处于空闲状态,说明没有接收到心跳命令
        if (IdleState.READER_IDLE.equals(event.state())) {
            log.info("已经5秒没有接收到客户端的信息了");
            if (idle_count.get() > 1) {
                log.info("关闭这个不活跃的channel");
                ctx.channel().close();
            }
            idle_count.getAndIncrement();
        }
    } else {
        super.userEventTriggered(ctx, obj);
    }
}
 
Example #27
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 #28
Source File: IdleTimeoutInitializer.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
@Override
public void init(Channel channel) {
    if (timeoutMs > 0) {
        LOGGER.debug("Channel idle timeout is {}ms.", timeoutMs);
        channel.pipeline().addLast(new IdleStateHandler(0, 0, timeoutMs, TimeUnit.MILLISECONDS) {
            @Override
            protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) {
                if (evt.state() == IdleState.ALL_IDLE) {
                    // Some protocols may have their own grace period to shutdown the channel
                    // and we don't want the idle timeout to fire again during this process.
                    ctx.pipeline().remove(this);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Closing channel {} after {}ms of inactivity.", ctx.channel(), timeoutMs);
                    }
                    // Fire the event through the pipeline so protocols can prepare for the close event.
                    ctx.fireUserEventTriggered(evt);
                    ctx.close();
                }
            }
        });
    }
}
 
Example #29
Source File: ServerHandler.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state().equals(IdleState.READER_IDLE)) {
            // 空闲40s之后触发 (心跳包丢失)
            if (counter >= 3) {
                // 连续丢失3个心跳包 (断开连接)
                ctx.channel().close().sync();
                log.error("已与" + ctx.channel().remoteAddress() + "断开连接");
            } else {
                counter++;
                log.debug(ctx.channel().remoteAddress() + "丢失了第 " + counter + " 个心跳包");
            }
        }

    }
}
 
Example #30
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();
        }
    }
}