下面列出了怎么用 io.netty.handler.codec.http2.DefaultHttp2FrameWriter 的API类实例代码及写法,或者点击链接到github查看源代码。
protected ByteBuf grpcDataFrame(int streamId, boolean endStream, byte[] content) {
final ByteBuf compressionFrame = Unpooled.buffer(content.length);
MessageFramer framer = new MessageFramer(
new MessageFramer.Sink() {
@Override
public void deliverFrame(
WritableBuffer frame, boolean endOfStream, boolean flush, int numMessages) {
if (frame != null) {
ByteBuf bytebuf = ((NettyWritableBuffer) frame).bytebuf();
compressionFrame.writeBytes(bytebuf);
}
}
},
new NettyWritableBufferAllocator(ByteBufAllocator.DEFAULT),
StatsTraceContext.NOOP);
framer.writePayload(new ByteArrayInputStream(content));
framer.flush();
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeData(ctx, streamId, compressionFrame, 0, endStream,
newPromise());
return captureWrite(ctx);
}
protected ByteBuf grpcDataFrame(int streamId, boolean endStream, byte[] content) {
final ByteBuf compressionFrame = Unpooled.buffer(content.length);
MessageFramer framer = new MessageFramer(
new MessageFramer.Sink() {
@Override
public void deliverFrame(
WritableBuffer frame, boolean endOfStream, boolean flush, int numMessages) {
if (frame != null) {
ByteBuf bytebuf = ((NettyWritableBuffer) frame).bytebuf();
compressionFrame.writeBytes(bytebuf);
}
}
},
new NettyWritableBufferAllocator(ByteBufAllocator.DEFAULT),
StatsTraceContext.NOOP);
framer.writePayload(new ByteArrayInputStream(content));
framer.flush();
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeData(ctx, streamId, compressionFrame, 0, endStream,
newPromise());
return captureWrite(ctx);
}
static NettyClientHandler newHandler(
ClientTransportLifecycleManager lifecycleManager,
@Nullable KeepAliveManager keepAliveManager,
int flowControlWindow,
int maxHeaderListSize,
Supplier<Stopwatch> stopwatchFactory,
Runnable tooManyPingsRunnable,
TransportTracer transportTracer,
Attributes eagAttributes,
String authority) {
Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
Http2HeadersDecoder headersDecoder = new GrpcHttp2ClientHeadersDecoder(maxHeaderListSize);
Http2FrameReader frameReader = new DefaultHttp2FrameReader(headersDecoder);
Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
Http2Connection connection = new DefaultHttp2Connection(false);
WeightedFairQueueByteDistributor dist = new WeightedFairQueueByteDistributor(connection);
dist.allocationQuantum(16 * 1024); // Make benchmarks fast again.
DefaultHttp2RemoteFlowController controller =
new DefaultHttp2RemoteFlowController(connection, dist);
connection.remote().flowController(controller);
return newHandler(
connection,
frameReader,
frameWriter,
lifecycleManager,
keepAliveManager,
flowControlWindow,
maxHeaderListSize,
stopwatchFactory,
tooManyPingsRunnable,
transportTracer,
eagAttributes,
authority);
}
/**
* Must be called by subclasses to initialize the handler and channel.
*/
protected final void initChannel(Http2HeadersDecoder headersDecoder) throws Exception {
content = Unpooled.copiedBuffer("hello world", UTF_8);
frameWriter = mock(Http2FrameWriter.class, delegatesTo(new DefaultHttp2FrameWriter()));
frameReader = new DefaultHttp2FrameReader(headersDecoder);
channel = new FakeClockSupportedChanel();
handler = newHandler();
channel.pipeline().addLast(handler);
ctx = channel.pipeline().context(handler);
writeQueue = initWriteQueue();
}
protected final ByteBuf dataFrame(int streamId, boolean endStream, ByteBuf content) {
// Need to retain the content since the frameWriter releases it.
content.retain();
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeData(ctx, streamId, content, 0, endStream, newPromise());
return captureWrite(ctx);
}
private CapturingGrpcHttp2ConnectionHandler capturingGrpcHandler() {
// Netty Boilerplate. We don't really need any of this, but there is a tight coupling
// between a Http2ConnectionHandler and its dependencies.
Http2Connection connection = new DefaultHttp2Connection(true);
Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
Http2FrameReader frameReader = new DefaultHttp2FrameReader(false);
DefaultHttp2ConnectionEncoder encoder =
new DefaultHttp2ConnectionEncoder(connection, frameWriter);
DefaultHttp2ConnectionDecoder decoder =
new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader);
return new CapturingGrpcHttp2ConnectionHandler(decoder, encoder, new Http2Settings());
}
static FakeGrpcHttp2ConnectionHandler newHandler() {
DefaultHttp2Connection conn = new DefaultHttp2Connection(/*server=*/ false);
DefaultHttp2ConnectionEncoder encoder =
new DefaultHttp2ConnectionEncoder(conn, new DefaultHttp2FrameWriter());
DefaultHttp2ConnectionDecoder decoder =
new DefaultHttp2ConnectionDecoder(conn, encoder, new DefaultHttp2FrameReader());
Http2Settings settings = new Http2Settings();
return new FakeGrpcHttp2ConnectionHandler(
/*channelUnused=*/ null, decoder, encoder, settings);
}
static NettyClientHandler newHandler(
ClientTransportLifecycleManager lifecycleManager,
@Nullable KeepAliveManager keepAliveManager,
boolean autoFlowControl,
int flowControlWindow,
int maxHeaderListSize,
Supplier<Stopwatch> stopwatchFactory,
Runnable tooManyPingsRunnable,
TransportTracer transportTracer,
Attributes eagAttributes,
String authority) {
Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
Http2HeadersDecoder headersDecoder = new GrpcHttp2ClientHeadersDecoder(maxHeaderListSize);
Http2FrameReader frameReader = new DefaultHttp2FrameReader(headersDecoder);
Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
Http2Connection connection = new DefaultHttp2Connection(false);
WeightedFairQueueByteDistributor dist = new WeightedFairQueueByteDistributor(connection);
dist.allocationQuantum(16 * 1024); // Make benchmarks fast again.
DefaultHttp2RemoteFlowController controller =
new DefaultHttp2RemoteFlowController(connection, dist);
connection.remote().flowController(controller);
return newHandler(
connection,
frameReader,
frameWriter,
lifecycleManager,
keepAliveManager,
autoFlowControl,
flowControlWindow,
maxHeaderListSize,
stopwatchFactory,
tooManyPingsRunnable,
transportTracer,
eagAttributes,
authority);
}
/**
* Must be called by subclasses to initialize the handler and channel.
*/
protected final void initChannel(Http2HeadersDecoder headersDecoder) throws Exception {
content = Unpooled.copiedBuffer("hello world", UTF_8);
frameWriter = mock(Http2FrameWriter.class, delegatesTo(new DefaultHttp2FrameWriter()));
frameReader = new DefaultHttp2FrameReader(headersDecoder);
channel = new FakeClockSupportedChanel();
handler = newHandler();
channel.pipeline().addLast(handler);
ctx = channel.pipeline().context(handler);
writeQueue = initWriteQueue();
}
protected final ByteBuf dataFrame(int streamId, boolean endStream, ByteBuf content) {
// Need to retain the content since the frameWriter releases it.
content.retain();
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeData(ctx, streamId, content, 0, endStream, newPromise());
return captureWrite(ctx);
}
private static FakeGrpcHttp2ConnectionHandler newHandler(boolean noop) {
DefaultHttp2Connection conn = new DefaultHttp2Connection(/*server=*/ false);
DefaultHttp2ConnectionEncoder encoder =
new DefaultHttp2ConnectionEncoder(conn, new DefaultHttp2FrameWriter());
DefaultHttp2ConnectionDecoder decoder =
new DefaultHttp2ConnectionDecoder(conn, encoder, new DefaultHttp2FrameReader());
Http2Settings settings = new Http2Settings();
return new FakeGrpcHttp2ConnectionHandler(
/*channelUnused=*/ null, decoder, encoder, settings, noop);
}
private CapturingGrpcHttp2ConnectionHandler capturingGrpcHandler() {
// Netty Boilerplate. We don't really need any of this, but there is a tight coupling
// between an Http2ConnectionHandler and its dependencies.
Http2Connection connection = new DefaultHttp2Connection(true);
Http2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
Http2FrameReader frameReader = new DefaultHttp2FrameReader(false);
DefaultHttp2ConnectionEncoder encoder =
new DefaultHttp2ConnectionEncoder(connection, frameWriter);
DefaultHttp2ConnectionDecoder decoder =
new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader);
return new CapturingGrpcHttp2ConnectionHandler(decoder, encoder, new Http2Settings());
}
static NettyServerHandler newHandler(
ServerTransportListener transportListener,
ChannelPromise channelUnused,
List<ServerStreamTracer.Factory> streamTracerFactories,
TransportTracer transportTracer,
int maxStreams,
int flowControlWindow,
int maxHeaderListSize,
int maxMessageSize,
long keepAliveTimeInNanos,
long keepAliveTimeoutInNanos,
long maxConnectionIdleInNanos,
long maxConnectionAgeInNanos,
long maxConnectionAgeGraceInNanos,
boolean permitKeepAliveWithoutCalls,
long permitKeepAliveTimeInNanos) {
Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
Http2FrameLogger frameLogger = new Http2FrameLogger(LogLevel.DEBUG, NettyServerHandler.class);
Http2HeadersDecoder headersDecoder = new GrpcHttp2ServerHeadersDecoder(maxHeaderListSize);
Http2FrameReader frameReader = new Http2InboundFrameLogger(
new DefaultHttp2FrameReader(headersDecoder), frameLogger);
Http2FrameWriter frameWriter =
new Http2OutboundFrameLogger(new DefaultHttp2FrameWriter(), frameLogger);
return newHandler(
channelUnused,
frameReader,
frameWriter,
transportListener,
streamTracerFactories,
transportTracer,
maxStreams,
flowControlWindow,
maxHeaderListSize,
maxMessageSize,
keepAliveTimeInNanos,
keepAliveTimeoutInNanos,
maxConnectionIdleInNanos,
maxConnectionAgeInNanos,
maxConnectionAgeGraceInNanos,
permitKeepAliveWithoutCalls,
permitKeepAliveTimeInNanos);
}
protected final ByteBuf pingFrame(boolean ack, long payload) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writePing(ctx, ack, payload, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf headersFrame(int streamId, Http2Headers headers) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeHeaders(ctx, streamId, headers, 0, false, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf goAwayFrame(int lastStreamId, int errorCode, ByteBuf data) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeGoAway(ctx, lastStreamId, errorCode, data, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf rstStreamFrame(int streamId, int errorCode) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeRstStream(ctx, streamId, errorCode, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf serializeSettings(Http2Settings settings) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeSettings(ctx, settings, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf windowUpdate(int streamId, int delta) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeWindowUpdate(ctx, 0, delta, newPromise());
return captureWrite(ctx);
}
private static Http2FrameWriter frameWriter() {
return new Http2OutboundFrameLogger(new DefaultHttp2FrameWriter(), logger);
}
public HelloWorldHttp2Handler() {
this(new DefaultHttp2Connection(true), new Http2InboundFrameLogger(
new DefaultHttp2FrameReader(), logger),
new Http2OutboundFrameLogger(new DefaultHttp2FrameWriter(), logger),
new SimpleHttp2FrameListener());
}
static NettyServerHandler newHandler(
ServerTransportListener transportListener,
ChannelPromise channelUnused,
List<? extends ServerStreamTracer.Factory> streamTracerFactories,
TransportTracer transportTracer,
int maxStreams,
boolean autoFlowControl,
int flowControlWindow,
int maxHeaderListSize,
int maxMessageSize,
long keepAliveTimeInNanos,
long keepAliveTimeoutInNanos,
long maxConnectionIdleInNanos,
long maxConnectionAgeInNanos,
long maxConnectionAgeGraceInNanos,
boolean permitKeepAliveWithoutCalls,
long permitKeepAliveTimeInNanos) {
Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive: %s",
maxHeaderListSize);
Http2FrameLogger frameLogger = new Http2FrameLogger(LogLevel.DEBUG, NettyServerHandler.class);
Http2HeadersDecoder headersDecoder = new GrpcHttp2ServerHeadersDecoder(maxHeaderListSize);
Http2FrameReader frameReader = new Http2InboundFrameLogger(
new DefaultHttp2FrameReader(headersDecoder), frameLogger);
Http2FrameWriter frameWriter =
new Http2OutboundFrameLogger(new DefaultHttp2FrameWriter(), frameLogger);
return newHandler(
channelUnused,
frameReader,
frameWriter,
transportListener,
streamTracerFactories,
transportTracer,
maxStreams,
autoFlowControl,
flowControlWindow,
maxHeaderListSize,
maxMessageSize,
keepAliveTimeInNanos,
keepAliveTimeoutInNanos,
maxConnectionIdleInNanos,
maxConnectionAgeInNanos,
maxConnectionAgeGraceInNanos,
permitKeepAliveWithoutCalls,
permitKeepAliveTimeInNanos);
}
protected final ByteBuf pingFrame(boolean ack, long payload) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writePing(ctx, ack, payload, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf headersFrame(int streamId, Http2Headers headers) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeHeaders(ctx, streamId, headers, 0, false, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf goAwayFrame(int lastStreamId, int errorCode, ByteBuf data) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeGoAway(ctx, lastStreamId, errorCode, data, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf rstStreamFrame(int streamId, int errorCode) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeRstStream(ctx, streamId, errorCode, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf serializeSettings(Http2Settings settings) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeSettings(ctx, settings, newPromise());
return captureWrite(ctx);
}
protected final ByteBuf windowUpdate(int streamId, int delta) {
ChannelHandlerContext ctx = newMockContext();
new DefaultHttp2FrameWriter().writeWindowUpdate(ctx, 0, delta, newPromise());
return captureWrite(ctx);
}