diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java index 36073ed1bc..49edaf9440 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java @@ -184,25 +184,21 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http public void channelActive(ChannelHandlerContext ctx) throws Exception { } public void channelInactive(ChannelHandlerContext ctx) throws Exception { - try { - 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; - } - }); - } - } finally { - try { - encoder().close(); - } finally { - decoder().close(); - } + // 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; + } + }); } } diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/StreamBufferingEncoder.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/StreamBufferingEncoder.java index 7da5cf7229..7ed8e860c2 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/StreamBufferingEncoder.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/StreamBufferingEncoder.java @@ -20,9 +20,11 @@ import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR; import static io.netty.handler.codec.http2.Http2Exception.connectionError; import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPromise; +import io.netty.util.ByteString; import io.netty.util.ReferenceCountUtil; import java.util.ArrayDeque; @@ -43,7 +45,7 @@ import java.util.TreeMap; *
* If a {@code GOAWAY} frame is received from the remote endpoint, all buffered writes for streams * with an ID less than the specified {@code lastStreamId} will immediately fail with a - * {@link StreamBufferingEncoder.GoAwayException}. + * {@link Http2GoAwayException}. *
*This implementation makes the buffering mostly transparent and is expected to be used as a
* drop-in decorator of {@link DefaultHttp2ConnectionEncoder}.
@@ -51,17 +53,28 @@ import java.util.TreeMap;
*/
public class StreamBufferingEncoder extends DecoratingHttp2ConnectionEncoder {
+ /**
+ * Thrown if buffered streams are terminated due to this encoder being closed.
+ */
+ public static final class Http2ChannelClosedException extends Http2Exception {
+ private static final long serialVersionUID = 4768543442094476971L;
+
+ public Http2ChannelClosedException() {
+ super(Http2Error.REFUSED_STREAM, "Connection closed");
+ }
+ }
+
/**
* Thrown by {@link StreamBufferingEncoder} if buffered streams are terminated due to
* receipt of a {@code GOAWAY}.
*/
- public static final class GoAwayException extends Http2Exception {
+ public static final class Http2GoAwayException extends Http2Exception {
private static final long serialVersionUID = 1326785622777291198L;
private final int lastStreamId;
private final long errorCode;
- private final ByteBuf debugData;
+ private final ByteString debugData;
- public GoAwayException(int lastStreamId, long errorCode, ByteBuf debugData) {
+ public Http2GoAwayException(int lastStreamId, long errorCode, ByteString debugData) {
super(Http2Error.STREAM_CLOSED);
this.lastStreamId = lastStreamId;
this.errorCode = errorCode;
@@ -76,7 +89,7 @@ public class StreamBufferingEncoder extends DecoratingHttp2ConnectionEncoder {
return errorCode;
}
- public ByteBuf debugData() {
+ public ByteString debugData() {
return debugData;
}
}
@@ -87,6 +100,7 @@ public class StreamBufferingEncoder extends DecoratingHttp2ConnectionEncoder {
*/
private final TreeMap