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

The following examples show how to use io.netty.handler.timeout.IdleState#READER_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: WireKeepAlive.java    From besu with Apache License 2.0 6 votes vote down vote up
@Override
public void userEventTriggered(final ChannelHandlerContext ctx, final Object evt)
    throws IOException {
  if (!(evt instanceof IdleStateEvent
      && ((IdleStateEvent) evt).state() == IdleState.READER_IDLE)) {
    // We only care about idling of incoming data from our peer
    return;
  }

  if (waitingForPong.get()) {
    // We are still waiting for a response from our last pong, disconnect with timeout error
    LOG.debug("Wire PONG never received, disconnecting from peer.");
    connection.disconnect(DisconnectMessage.DisconnectReason.TIMEOUT);
    return;
  }

  try {
    LOG.debug("Idle connection detected, sending Wire PING to peer.");
    connection.send(null, PingMessage.get());
    waitingForPong.set(true);
  } catch (final PeerConnection.PeerNotConnected ignored) {
    LOG.trace("PING not sent because peer is already disconnected");
  }
}
 
Example 2
Source File: IdleConnectionHandler.java    From Kepler with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object event) {
    if (event instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) event;

        if (e.state() == IdleState.READER_IDLE) {
            Player player = ctx.channel().attr(Player.PLAYER_KEY).get();

            if (player.isPingOK()) {
                player.setPingOK(false);
                player.send(new PING());
            } else {
                if (player.isLoggedIn()) {
                    logger.info("Player {} has timed out", player.getDetails().getName());
                } else {
                    logger.info("Connection {} has timed out", player.getNetwork().getConnectionId());
                }

                player.kickFromServer();
            }
        }
    }
}
 
Example 3
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 4
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 5
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 6
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 7
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 8
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 9
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 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: MoquetteIdleTimeoutHandler.java    From cassandana 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) {
        IdleState e = ((IdleStateEvent) evt).state();
        if (e == IdleState.READER_IDLE) {
            LOG.info("Firing channel inactive event. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
            // fire a close that then fire channelInactive to trigger publish of Will
            ctx.close().addListener(CLOSE_ON_FAILURE);
        }
    } else {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Firing Netty event CId = {}, eventClass = {}", NettyUtils.clientID(ctx.channel()),
                      evt.getClass().getName());
        }
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 12
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 13
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 14
Source File: NettyServerHandlerDemo5.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 {  
    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);  
    }  
}
 
Example 15
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 16
Source File: HeartBeatHandler.java    From iot-dc 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) {
            loss_connect_time++;
            if (loss_connect_time > 2) {
                ctx.channel().close();
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
 
Example 17
Source File: RtpFinalHandler.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(@Nullable ChannelHandlerContext ctx, @Nullable Object evt) throws Exception {
   if (evt instanceof IdleStateEvent) {
      IdleStateEvent e = (IdleStateEvent)evt;
      if (e.state() == IdleState.READER_IDLE) {
         log.warn("connection idle for too long, terminating");
         registry.remove(ctx);
         if (ctx != null) {
            ctx.close();
         }
      }
   }

   super.userEventTriggered(ctx, evt);
}
 
Example 18
Source File: ClientConnectionDropNotifier.java    From openAGV 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) {
        if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
            connectionEventListener.onIdle();
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
 
Example 19
Source File: UptimeClientHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
    if (!(evt instanceof IdleStateEvent)) {
        return;
    }

    IdleStateEvent e = (IdleStateEvent) evt;
    if (e.state() == IdleState.READER_IDLE) {
        // The connection was OK but there was no traffic for last period.
        println("Disconnecting due to no inbound traffic");
        ctx.close();
    }
}
 
Example 20
Source File: TCPServerHandler.java    From jt808-server with Apache License 2.0 5 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) {
            Session session = this.sessionManager.removeBySessionId(Session.buildId(ctx.channel()));
            logger.logEvent("服务器主动断开连接", session);
            ctx.close();
        }
    }
}