From e1c78b471b772b68e6762ed24642dc5db6773222 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 14 Jul 2013 23:38:02 +0200 Subject: [PATCH] Use the configured ByteBufAllocator in SpdyFrameEncoder --- .../handler/codec/spdy/SpdyFrameEncoder.java | 152 ++++++++++-------- .../codec/spdy/SpdyHeaderBlockEncoder.java | 3 +- .../spdy/SpdyHeaderBlockJZlibEncoder.java | 11 +- .../codec/spdy/SpdyHeaderBlockRawEncoder.java | 7 +- .../spdy/SpdyHeaderBlockZlibEncoder.java | 11 +- 5 files changed, 99 insertions(+), 85 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameEncoder.java index 1dc28f97c4..e2a9f062ff 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameEncoder.java @@ -16,13 +16,11 @@ package io.netty.handler.codec.spdy; import io.netty.buffer.ByteBuf; -import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.UnsupportedMessageTypeException; -import io.netty.util.CharsetUtil; import java.util.Set; @@ -90,66 +88,74 @@ public class SpdyFrameEncoder extends MessageToByteEncoder { } else if (msg instanceof SpdySynStreamFrame) { SpdySynStreamFrame spdySynStreamFrame = (SpdySynStreamFrame) msg; - ByteBuf data = headerBlockEncoder.encode(spdySynStreamFrame); - byte flags = spdySynStreamFrame.isLast() ? SPDY_FLAG_FIN : 0; - if (spdySynStreamFrame.isUnidirectional()) { - flags |= SPDY_FLAG_UNIDIRECTIONAL; - } - int headerBlockLength = data.readableBytes(); - int length; - if (version < 3) { - length = headerBlockLength == 0 ? 12 : 10 + headerBlockLength; - } else { - length = 10 + headerBlockLength; - } - out.ensureWritable(SPDY_HEADER_SIZE + length); - out.writeShort(version | 0x8000); - out.writeShort(SPDY_SYN_STREAM_FRAME); - out.writeByte(flags); - out.writeMedium(length); - out.writeInt(spdySynStreamFrame.getStreamId()); - out.writeInt(spdySynStreamFrame.getAssociatedToStreamId()); - if (version < 3) { - // Restrict priorities for SPDY/2 to between 0 and 3 - byte priority = spdySynStreamFrame.getPriority(); - if (priority > 3) { - priority = 3; + ByteBuf data = headerBlockEncoder.encode(ctx, spdySynStreamFrame); + try { + byte flags = spdySynStreamFrame.isLast() ? SPDY_FLAG_FIN : 0; + if (spdySynStreamFrame.isUnidirectional()) { + flags |= SPDY_FLAG_UNIDIRECTIONAL; } - out.writeShort((priority & 0xFF) << 14); - } else { - out.writeShort((spdySynStreamFrame.getPriority() & 0xFF) << 13); + int headerBlockLength = data.readableBytes(); + int length; + if (version < 3) { + length = headerBlockLength == 0 ? 12 : 10 + headerBlockLength; + } else { + length = 10 + headerBlockLength; + } + out.ensureWritable(SPDY_HEADER_SIZE + length); + out.writeShort(version | 0x8000); + out.writeShort(SPDY_SYN_STREAM_FRAME); + out.writeByte(flags); + out.writeMedium(length); + out.writeInt(spdySynStreamFrame.getStreamId()); + out.writeInt(spdySynStreamFrame.getAssociatedToStreamId()); + if (version < 3) { + // Restrict priorities for SPDY/2 to between 0 and 3 + byte priority = spdySynStreamFrame.getPriority(); + if (priority > 3) { + priority = 3; + } + out.writeShort((priority & 0xFF) << 14); + } else { + out.writeShort((spdySynStreamFrame.getPriority() & 0xFF) << 13); + } + if (version < 3 && data.readableBytes() == 0) { + out.writeShort(0); + } + out.writeBytes(data, data.readerIndex(), headerBlockLength); + } finally { + data.release(); } - if (version < 3 && data.readableBytes() == 0) { - out.writeShort(0); - } - out.writeBytes(data, data.readerIndex(), headerBlockLength); } else if (msg instanceof SpdySynReplyFrame) { SpdySynReplyFrame spdySynReplyFrame = (SpdySynReplyFrame) msg; - ByteBuf data = headerBlockEncoder.encode(spdySynReplyFrame); - byte flags = spdySynReplyFrame.isLast() ? SPDY_FLAG_FIN : 0; - int headerBlockLength = data.readableBytes(); - int length; - if (version < 3) { - length = headerBlockLength == 0 ? 8 : 6 + headerBlockLength; - } else { - length = 4 + headerBlockLength; - } - out.ensureWritable(SPDY_HEADER_SIZE + length); - out.writeShort(version | 0x8000); - out.writeShort(SPDY_SYN_REPLY_FRAME); - out.writeByte(flags); - out.writeMedium(length); - out.writeInt(spdySynReplyFrame.getStreamId()); - if (version < 3) { - if (headerBlockLength == 0) { - out.writeInt(0); + ByteBuf data = headerBlockEncoder.encode(ctx, spdySynReplyFrame); + try { + byte flags = spdySynReplyFrame.isLast() ? SPDY_FLAG_FIN : 0; + int headerBlockLength = data.readableBytes(); + int length; + if (version < 3) { + length = headerBlockLength == 0 ? 8 : 6 + headerBlockLength; } else { - out.writeShort(0); + length = 4 + headerBlockLength; } + out.ensureWritable(SPDY_HEADER_SIZE + length); + out.writeShort(version | 0x8000); + out.writeShort(SPDY_SYN_REPLY_FRAME); + out.writeByte(flags); + out.writeMedium(length); + out.writeInt(spdySynReplyFrame.getStreamId()); + if (version < 3) { + if (headerBlockLength == 0) { + out.writeInt(0); + } else { + out.writeShort(0); + } + } + out.writeBytes(data, data.readerIndex(), headerBlockLength); + } finally { + data.release(); } - out.writeBytes(data, data.readerIndex(), headerBlockLength); } else if (msg instanceof SpdyRstStreamFrame) { @@ -224,25 +230,29 @@ public class SpdyFrameEncoder extends MessageToByteEncoder { } else if (msg instanceof SpdyHeadersFrame) { SpdyHeadersFrame spdyHeadersFrame = (SpdyHeadersFrame) msg; - ByteBuf data = headerBlockEncoder.encode(spdyHeadersFrame); - byte flags = spdyHeadersFrame.isLast() ? SPDY_FLAG_FIN : 0; - int headerBlockLength = data.readableBytes(); - int length; - if (version < 3) { - length = headerBlockLength == 0 ? 4 : 6 + headerBlockLength; - } else { - length = 4 + headerBlockLength; + ByteBuf data = headerBlockEncoder.encode(ctx, spdyHeadersFrame); + try { + byte flags = spdyHeadersFrame.isLast() ? SPDY_FLAG_FIN : 0; + int headerBlockLength = data.readableBytes(); + int length; + if (version < 3) { + length = headerBlockLength == 0 ? 4 : 6 + headerBlockLength; + } else { + length = 4 + headerBlockLength; + } + out.ensureWritable(SPDY_HEADER_SIZE + length); + out.writeShort(version | 0x8000); + out.writeShort(SPDY_HEADERS_FRAME); + out.writeByte(flags); + out.writeMedium(length); + out.writeInt(spdyHeadersFrame.getStreamId()); + if (version < 3 && headerBlockLength != 0) { + out.writeShort(0); + } + out.writeBytes(data, data.readerIndex(), headerBlockLength); + } finally { + data.release(); } - out.ensureWritable(SPDY_HEADER_SIZE + length); - out.writeShort(version | 0x8000); - out.writeShort(SPDY_HEADERS_FRAME); - out.writeByte(flags); - out.writeMedium(length); - out.writeInt(spdyHeadersFrame.getStreamId()); - if (version < 3 && headerBlockLength != 0) { - out.writeShort(0); - } - out.writeBytes(data, data.readerIndex(), headerBlockLength); } else if (msg instanceof SpdyWindowUpdateFrame) { diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockEncoder.java index 398b26fb49..56afcb8d2f 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockEncoder.java @@ -16,6 +16,7 @@ package io.netty.handler.codec.spdy; import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; import io.netty.util.internal.PlatformDependent; abstract class SpdyHeaderBlockEncoder { @@ -32,6 +33,6 @@ abstract class SpdyHeaderBlockEncoder { } } - abstract ByteBuf encode(SpdyHeadersFrame frame) throws Exception; + abstract ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception; abstract void end(); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockJZlibEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockJZlibEncoder.java index 6fc11888ba..912b7d828c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockJZlibEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockJZlibEncoder.java @@ -15,14 +15,15 @@ */ package io.netty.handler.codec.spdy; -import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; - import com.jcraft.jzlib.Deflater; import com.jcraft.jzlib.JZlib; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.compression.CompressionException; +import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; + class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder { private final Deflater z = new Deflater(); @@ -97,7 +98,7 @@ class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder { } @Override - public synchronized ByteBuf encode(SpdyHeadersFrame frame) throws Exception { + public synchronized ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception { if (frame == null) { throw new IllegalArgumentException("frame"); } @@ -106,12 +107,12 @@ class SpdyHeaderBlockJZlibEncoder extends SpdyHeaderBlockRawEncoder { return Unpooled.EMPTY_BUFFER; } - ByteBuf decompressed = super.encode(frame); + ByteBuf decompressed = super.encode(ctx, frame); if (decompressed.readableBytes() == 0) { return Unpooled.EMPTY_BUFFER; } - ByteBuf compressed = Unpooled.buffer(); + ByteBuf compressed = ctx.alloc().buffer(); setInput(decompressed); encode(compressed); return compressed; diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockRawEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockRawEncoder.java index b439a30a61..512de8de4f 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockRawEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockRawEncoder.java @@ -15,10 +15,11 @@ */ package io.netty.handler.codec.spdy; -import java.util.Set; - import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; + +import java.util.Set; import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; @@ -51,7 +52,7 @@ public class SpdyHeaderBlockRawEncoder extends SpdyHeaderBlockEncoder { } @Override - public ByteBuf encode(SpdyHeadersFrame frame) throws Exception { + public ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception { Set names = frame.headers().names(); int numHeaders = names.size(); if (numHeaders == 0) { diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibEncoder.java index ba097acf68..b6971a4e62 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibEncoder.java @@ -15,13 +15,14 @@ */ package io.netty.handler.codec.spdy; -import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; - import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; import java.util.zip.Deflater; +import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; + class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder { private final byte[] out = new byte[8192]; @@ -58,7 +59,7 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder { } @Override - public synchronized ByteBuf encode(SpdyHeadersFrame frame) throws Exception { + public synchronized ByteBuf encode(ChannelHandlerContext ctx, SpdyHeadersFrame frame) throws Exception { if (frame == null) { throw new IllegalArgumentException("frame"); } @@ -67,12 +68,12 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder { return Unpooled.EMPTY_BUFFER; } - ByteBuf decompressed = super.encode(frame); + ByteBuf decompressed = super.encode(ctx, frame); if (decompressed.readableBytes() == 0) { return Unpooled.EMPTY_BUFFER; } - ByteBuf compressed = Unpooled.buffer(); + ByteBuf compressed = ctx.alloc().buffer(); setInput(decompressed); encode(compressed); return compressed;