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

The following examples show how to use io.netty.handler.timeout.IdleState#WRITER_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: HeartbeatHandler.java    From mantis 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) {
            logger.warn("Read idle, due to missed heartbeats, closing connection: " + ctx.channel().remoteAddress());
            ctx.close();
        } else if (e.state() == IdleState.WRITER_IDLE) {
            ByteBuffer buffer = ByteBuffer.allocate(4 + 1); // length plus one byte
            buffer.putInt(1); // length of op code
            buffer.put((byte) 6); // op code for heartbeat for legacy protocol
            ctx.channel().writeAndFlush(buffer.array());
        }
    }
    super.userEventTriggered(ctx, evt);
}
 
Example 2
Source File: EchoClientHandle.java    From netty-learning with MIT License 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.WRITER_IDLE){
            LOGGER.info("已经 10 秒没有发送信息!");
            //向客户端发送消息
            CustomProtocol customProtocol = new CustomProtocol(45678L,"ping") ;
            ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8)) ;
        }


    }

    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: SoaIdleHandler.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());

            //check times of write idle, close the channel if exceed specify times
            IdleConnectionManager.addChannel(ctx.channel());

        } else if (e.state() == IdleState.ALL_IDLE) {
            if(LOGGER.isDebugEnabled())
                LOGGER.debug(getClass().getName() + "::读写都超时,发送心跳包:" + ctx.channel());
        }
    }

}
 
Example 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: FtdHeartbeatHandler.java    From ftdc 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 event = (IdleStateEvent) evt;
   		if(event.state() == IdleState.READER_IDLE) {
   			//超过约定时间没有收到数据认为链路有问题
   			logger.warn("not recieve heartbeat , exceed {}", ApplicationRuntime.conf().getFtdcReaderIdle());
   			ctx.close();
   		}else if(event.state() == IdleState.WRITER_IDLE) {
   			//超过约定时间没有写数据发心跳保活
   			ctx.writeAndFlush(HeartBeat.getHeartBeat());
   		}
	}
}
 
Example 14
Source File: TransferHandler.java    From ratel 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 event = (IdleStateEvent) evt;  
		if (event.state() == IdleState.WRITER_IDLE) {  
			ChannelUtils.pushToServer(ctx.channel(), ServerEventCode.CODE_CLIENT_HEAD_BEAT, "heartbeat");
		}  
	}  
}
 
Example 15
Source File: StreamServerGroupHandler.java    From IpCamera with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
    if (evt == null || ctx == null) {
        return;
    }
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.WRITER_IDLE) {
            logger.debug("Stream server is going to close an idle channel.");
            ctx.close();
        }
    }
}
 
Example 16
Source File: IdleEventListener.java    From xian 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 event = (IdleStateEvent) evt;
        LOG.info(ctx.channel().remoteAddress() + "超时类型:" + event.state().name());
        if (event.state() == IdleState.WRITER_IDLE) {
            List<Request> timeoutRequests = ctx.channel().attr(ReqQueue.REQ_QUEUE).get().removeTimeout();
            for (Request timeoutRequest : timeoutRequests) {
                timeoutRequest.getChannelHandlerContext().writeAndFlush(buildTimeoutResponse(timeoutRequest));
            }
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 17
Source File: HeartBeatHandler.java    From Distributed-KV with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
	IdleState state = null;
	if (evt instanceof IdleStateEvent)
		state = ((IdleStateEvent) evt).state();
	//对于写空闲,即长时间未传送数据,将传输心跳包
	if (state == IdleState.WRITER_IDLE) {
		Log.logger.info("send heartbeat package");
		ctx.writeAndFlush(HEARTBEATPACKAGE.copy());
		return;
	}
	
	//当UserEventTriggered事件未处理完时执行
	ctx.fireUserEventTriggered(evt);
}
 
Example 18
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 19
Source File: StreamServerHandler.java    From IpCamera with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
    if (ctx == null) {
        return;
    }
    logger.trace("Stream server:{}.", evt);
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        if (e.state() == IdleState.WRITER_IDLE) {
            logger.debug("Stream server is going to close an idle channel.");
            ctx.close();
        }
    }
}
 
Example 20
Source File: BasicClient.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
  if (evt.state() == IdleState.WRITER_IDLE) {
    ctx.writeAndFlush(PING_MESSAGE).addListener(pingFailedHandler);
  }
}