@Override
 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
   for (Integer streamId : spdySession.activeStreams().keySet()) {
     removeStream(streamId, ctx.newSucceededFuture());
   }
   ctx.fireChannelInactive();
 }
 private synchronized ChannelFuture sendGoAwayFrame(
     ChannelHandlerContext ctx, SpdySessionStatus status) {
   if (!sentGoAwayFrame) {
     sentGoAwayFrame = true;
     SpdyGoAwayFrame spdyGoAwayFrame = new DefaultSpdyGoAwayFrame(lastGoodStreamId, status);
     return ctx.writeAndFlush(spdyGoAwayFrame);
   } else {
     return ctx.newSucceededFuture();
   }
 }
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
      // Connection has terminated, close the encoder and decoder.
      encoder().close();
      decoder().close();

      final Http2Connection connection = connection();
      // Check if there are streams to avoid the overhead of creating the ChannelFuture.
      if (connection.numActiveStreams() > 0) {
        final ChannelFuture future = ctx.newSucceededFuture();
        connection.forEachActiveStream(
            new Http2StreamVisitor() {
              @Override
              public boolean visit(Http2Stream stream) throws Http2Exception {
                closeStream(stream, future);
                return true;
              }
            });
      }
    }
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof SpdyDataFrame) {

      /*
       * SPDY Data frame processing requirements:
       *
       * If an endpoint receives a data frame for a Stream-ID which is not open
       * and the endpoint has not sent a GOAWAY frame, it must issue a stream error
       * with the error code INVALID_STREAM for the Stream-ID.
       *
       * If an endpoint which created the stream receives a data frame before receiving
       * a SYN_REPLY on that stream, it is a protocol error, and the recipient must
       * issue a stream error with the getStatus code PROTOCOL_ERROR for the Stream-ID.
       *
       * If an endpoint receives multiple data frames for invalid Stream-IDs,
       * it may close the session.
       *
       * If an endpoint refuses a stream it must ignore any data frames for that stream.
       *
       * If an endpoint receives a data frame after the stream is half-closed from the
       * sender, it must send a RST_STREAM frame with the getStatus STREAM_ALREADY_CLOSED.
       *
       * If an endpoint receives a data frame after the stream is closed, it must send
       * a RST_STREAM frame with the getStatus PROTOCOL_ERROR.
       */
      SpdyDataFrame spdyDataFrame = (SpdyDataFrame) msg;
      int streamId = spdyDataFrame.streamId();

      int deltaWindowSize = -1 * spdyDataFrame.content().readableBytes();
      int newSessionWindowSize =
          spdySession.updateReceiveWindowSize(SPDY_SESSION_STREAM_ID, deltaWindowSize);

      // Check if session window size is reduced beyond allowable lower bound
      if (newSessionWindowSize < 0) {
        issueSessionError(ctx, SpdySessionStatus.PROTOCOL_ERROR);
        return;
      }

      // Send a WINDOW_UPDATE frame if less than half the session window size remains
      if (newSessionWindowSize <= initialSessionReceiveWindowSize / 2) {
        int sessionDeltaWindowSize = initialSessionReceiveWindowSize - newSessionWindowSize;
        spdySession.updateReceiveWindowSize(SPDY_SESSION_STREAM_ID, sessionDeltaWindowSize);
        SpdyWindowUpdateFrame spdyWindowUpdateFrame =
            new DefaultSpdyWindowUpdateFrame(SPDY_SESSION_STREAM_ID, sessionDeltaWindowSize);
        ctx.writeAndFlush(spdyWindowUpdateFrame);
      }

      // Check if we received a data frame for a Stream-ID which is not open

      if (!spdySession.isActiveStream(streamId)) {
        spdyDataFrame.release();
        if (streamId <= lastGoodStreamId) {
          issueStreamError(ctx, streamId, SpdyStreamStatus.PROTOCOL_ERROR);
        } else if (!sentGoAwayFrame) {
          issueStreamError(ctx, streamId, SpdyStreamStatus.INVALID_STREAM);
        }
        return;
      }

      // Check if we received a data frame for a stream which is half-closed

      if (spdySession.isRemoteSideClosed(streamId)) {
        spdyDataFrame.release();
        issueStreamError(ctx, streamId, SpdyStreamStatus.STREAM_ALREADY_CLOSED);
        return;
      }

      // Check if we received a data frame before receiving a SYN_REPLY
      if (!isRemoteInitiatedId(streamId) && !spdySession.hasReceivedReply(streamId)) {
        spdyDataFrame.release();
        issueStreamError(ctx, streamId, SpdyStreamStatus.PROTOCOL_ERROR);
        return;
      }

      /*
       * SPDY Data frame flow control processing requirements:
       *
       * Recipient should not send a WINDOW_UPDATE frame as it consumes the last data frame.
       */

      // Update receive window size
      int newWindowSize = spdySession.updateReceiveWindowSize(streamId, deltaWindowSize);

      // Window size can become negative if we sent a SETTINGS frame that reduces the
      // size of the transfer window after the peer has written data frames.
      // The value is bounded by the length that SETTINGS frame decrease the window.
      // This difference is stored for the session when writing the SETTINGS frame
      // and is cleared once we send a WINDOW_UPDATE frame.
      if (newWindowSize < spdySession.getReceiveWindowSizeLowerBound(streamId)) {
        spdyDataFrame.release();
        issueStreamError(ctx, streamId, SpdyStreamStatus.FLOW_CONTROL_ERROR);
        return;
      }

      // Window size became negative due to sender writing frame before receiving SETTINGS
      // Send data frames upstream in initialReceiveWindowSize chunks
      if (newWindowSize < 0) {
        while (spdyDataFrame.content().readableBytes() > initialReceiveWindowSize) {
          SpdyDataFrame partialDataFrame =
              new DefaultSpdyDataFrame(
                  streamId, spdyDataFrame.content().readSlice(initialReceiveWindowSize).retain());
          ctx.writeAndFlush(partialDataFrame);
        }
      }

      // Send a WINDOW_UPDATE frame if less than half the stream window size remains
      if (newWindowSize <= initialReceiveWindowSize / 2 && !spdyDataFrame.isLast()) {
        int streamDeltaWindowSize = initialReceiveWindowSize - newWindowSize;
        spdySession.updateReceiveWindowSize(streamId, streamDeltaWindowSize);
        SpdyWindowUpdateFrame spdyWindowUpdateFrame =
            new DefaultSpdyWindowUpdateFrame(streamId, streamDeltaWindowSize);
        ctx.writeAndFlush(spdyWindowUpdateFrame);
      }

      // Close the remote side of the stream if this is the last frame
      if (spdyDataFrame.isLast()) {
        halfCloseStream(streamId, true, ctx.newSucceededFuture());
      }

    } else if (msg instanceof SpdySynStreamFrame) {

      /*
       * SPDY SYN_STREAM frame processing requirements:
       *
       * If an endpoint receives a SYN_STREAM with a Stream-ID that is less than
       * any previously received SYN_STREAM, it must issue a session error with
       * the getStatus PROTOCOL_ERROR.
       *
       * If an endpoint receives multiple SYN_STREAM frames with the same active
       * Stream-ID, it must issue a stream error with the getStatus code PROTOCOL_ERROR.
       *
       * The recipient can reject a stream by sending a stream error with the
       * getStatus code REFUSED_STREAM.
       */

      SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg;
      int streamId = spdySynStreamFrame.streamId();

      // Check if we received a valid SYN_STREAM frame
      if (spdySynStreamFrame.isInvalid()
          || !isRemoteInitiatedId(streamId)
          || spdySession.isActiveStream(streamId)) {
        issueStreamError(ctx, streamId, SpdyStreamStatus.PROTOCOL_ERROR);
        return;
      }

      // Stream-IDs must be monotonically increasing
      if (streamId <= lastGoodStreamId) {
        issueSessionError(ctx, SpdySessionStatus.PROTOCOL_ERROR);
        return;
      }

      // Try to accept the stream
      byte priority = spdySynStreamFrame.priority();
      boolean remoteSideClosed = spdySynStreamFrame.isLast();
      boolean localSideClosed = spdySynStreamFrame.isUnidirectional();
      if (!acceptStream(streamId, priority, remoteSideClosed, localSideClosed)) {
        issueStreamError(ctx, streamId, SpdyStreamStatus.REFUSED_STREAM);
        return;
      }

    } else if (msg instanceof SpdySynReplyFrame) {

      /*
       * SPDY SYN_REPLY frame processing requirements:
       *
       * If an endpoint receives multiple SYN_REPLY frames for the same active Stream-ID
       * it must issue a stream error with the getStatus code STREAM_IN_USE.
       */

      SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg;
      int streamId = spdySynReplyFrame.streamId();

      // Check if we received a valid SYN_REPLY frame
      if (spdySynReplyFrame.isInvalid()
          || isRemoteInitiatedId(streamId)
          || spdySession.isRemoteSideClosed(streamId)) {
        issueStreamError(ctx, streamId, SpdyStreamStatus.INVALID_STREAM);
        return;
      }

      // Check if we have received multiple frames for the same Stream-ID
      if (spdySession.hasReceivedReply(streamId)) {
        issueStreamError(ctx, streamId, SpdyStreamStatus.STREAM_IN_USE);
        return;
      }

      spdySession.receivedReply(streamId);

      // Close the remote side of the stream if this is the last frame
      if (spdySynReplyFrame.isLast()) {
        halfCloseStream(streamId, true, ctx.newSucceededFuture());
      }

    } else if (msg instanceof SpdyRstStreamFrame) {

      /*
       * SPDY RST_STREAM frame processing requirements:
       *
       * After receiving a RST_STREAM on a stream, the receiver must not send
       * additional frames on that stream.
       *
       * An endpoint must not send a RST_STREAM in response to a RST_STREAM.
       */

      SpdyRstStreamFrame spdyRstStreamFrame = (SpdyRstStreamFrame) msg;
      removeStream(spdyRstStreamFrame.streamId(), ctx.newSucceededFuture());

    } else if (msg instanceof SpdySettingsFrame) {

      SpdySettingsFrame spdySettingsFrame = (SpdySettingsFrame) msg;

      int settingsMinorVersion =
          spdySettingsFrame.getValue(SpdySettingsFrame.SETTINGS_MINOR_VERSION);
      if (settingsMinorVersion >= 0 && settingsMinorVersion != minorVersion) {
        // Settings frame had the wrong minor version
        issueSessionError(ctx, SpdySessionStatus.PROTOCOL_ERROR);
        return;
      }

      int newConcurrentStreams =
          spdySettingsFrame.getValue(SpdySettingsFrame.SETTINGS_MAX_CONCURRENT_STREAMS);
      if (newConcurrentStreams >= 0) {
        remoteConcurrentStreams = newConcurrentStreams;
      }

      // Persistence flag are inconsistent with the use of SETTINGS to communicate
      // the initial window size. Remove flags from the sender requesting that the
      // value be persisted. Remove values that the sender indicates are persisted.
      if (spdySettingsFrame.isPersisted(SpdySettingsFrame.SETTINGS_INITIAL_WINDOW_SIZE)) {
        spdySettingsFrame.removeValue(SpdySettingsFrame.SETTINGS_INITIAL_WINDOW_SIZE);
      }
      spdySettingsFrame.setPersistValue(SpdySettingsFrame.SETTINGS_INITIAL_WINDOW_SIZE, false);

      int newInitialWindowSize =
          spdySettingsFrame.getValue(SpdySettingsFrame.SETTINGS_INITIAL_WINDOW_SIZE);
      if (newInitialWindowSize >= 0) {
        updateInitialSendWindowSize(newInitialWindowSize);
      }

    } else if (msg instanceof SpdyPingFrame) {

      /*
       * SPDY PING frame processing requirements:
       *
       * Receivers of a PING frame should send an identical frame to the sender
       * as soon as possible.
       *
       * Receivers of a PING frame must ignore frames that it did not initiate
       */

      SpdyPingFrame spdyPingFrame = (SpdyPingFrame) msg;

      if (isRemoteInitiatedId(spdyPingFrame.id())) {
        ctx.writeAndFlush(spdyPingFrame);
        return;
      }

      // Note: only checks that there are outstanding pings since uniqueness is not enforced
      if (pings.get() == 0) {
        return;
      }
      pings.getAndDecrement();

    } else if (msg instanceof SpdyGoAwayFrame) {

      receivedGoAwayFrame = true;

    } else if (msg instanceof SpdyHeadersFrame) {

      SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg;
      int streamId = spdyHeadersFrame.streamId();

      // Check if we received a valid HEADERS frame
      if (spdyHeadersFrame.isInvalid()) {
        issueStreamError(ctx, streamId, SpdyStreamStatus.PROTOCOL_ERROR);
        return;
      }

      if (spdySession.isRemoteSideClosed(streamId)) {
        issueStreamError(ctx, streamId, SpdyStreamStatus.INVALID_STREAM);
        return;
      }

      // Close the remote side of the stream if this is the last frame
      if (spdyHeadersFrame.isLast()) {
        halfCloseStream(streamId, true, ctx.newSucceededFuture());
      }

    } else if (msg instanceof SpdyWindowUpdateFrame) {

      /*
       * SPDY WINDOW_UPDATE frame processing requirements:
       *
       * Receivers of a WINDOW_UPDATE that cause the window size to exceed 2^31
       * must send a RST_STREAM with the getStatus code FLOW_CONTROL_ERROR.
       *
       * Sender should ignore all WINDOW_UPDATE frames associated with a stream
       * after sending the last frame for the stream.
       */

      SpdyWindowUpdateFrame spdyWindowUpdateFrame = (SpdyWindowUpdateFrame) msg;
      int streamId = spdyWindowUpdateFrame.streamId();
      int deltaWindowSize = spdyWindowUpdateFrame.deltaWindowSize();

      // Ignore frames for half-closed streams
      if (streamId != SPDY_SESSION_STREAM_ID && spdySession.isLocalSideClosed(streamId)) {
        return;
      }

      // Check for numerical overflow
      if (spdySession.getSendWindowSize(streamId) > Integer.MAX_VALUE - deltaWindowSize) {
        if (streamId == SPDY_SESSION_STREAM_ID) {
          issueSessionError(ctx, SpdySessionStatus.PROTOCOL_ERROR);
        } else {
          issueStreamError(ctx, streamId, SpdyStreamStatus.FLOW_CONTROL_ERROR);
        }
        return;
      }

      updateSendWindowSize(ctx, streamId, deltaWindowSize);
    }

    ctx.fireChannelRead(msg);
  }