예제 #1
0
    @Override
    public void run(Timeout timeout) throws Exception {
      if (timeout.isCancelled()) {
        return;
      }

      if (!ctx.getChannel().isOpen()) {
        return;
      }

      State state = (State) ctx.getAttachment();
      long currentTime = System.currentTimeMillis();
      long nextDelay = timeoutMillis - (currentTime - state.lastReadTime);
      if (nextDelay <= 0) {
        // Read timed out - set a new timeout and notify the callback.
        state.timeout = timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS);
        try {
          // FIXME This should be called from an I/O thread.
          //       To be fixed in Netty 4.
          readTimedOut(ctx);
        } catch (Throwable t) {
          fireExceptionCaught(ctx, t);
        }
      } else {
        // Read occurred before the timeout - set a new timeout with shorter delay.
        state.timeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS);
      }
    }
예제 #2
0
 private void destroy(ChannelHandlerContext ctx) {
   State state = (State) ctx.getAttachment();
   if (state.timeout != null) {
     state.timeout.cancel();
     state.timeout = null;
   }
 }
예제 #3
0
 private void initialize(ChannelHandlerContext ctx) {
   State state = new State();
   ctx.setAttachment(state);
   if (timeoutMillis > 0) {
     state.timeout =
         timer.newTimeout(new ReadTimeoutTask(ctx), timeoutMillis, TimeUnit.MILLISECONDS);
   }
 }
예제 #4
0
 @Override
 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
   State state = (State) ctx.getAttachment();
   state.lastReadTime = System.currentTimeMillis();
   ctx.sendUpstream(e);
 }