|
2 | 2 |
|
3 | 3 | import io.netty.buffer.ByteBuf; |
4 | 4 | import io.netty.buffer.ByteBufAllocator; |
| 5 | +import io.netty.buffer.CompositeByteBuf; |
5 | 6 | import io.netty.handler.codec.http2.Http2Connection; |
6 | 7 | import io.netty.handler.codec.http2.Http2Headers; |
7 | 8 | import io.netty.handler.codec.http2.Http2Stream; |
8 | 9 |
|
9 | 10 | public class Http2Request { |
10 | | - private int streamId; |
11 | | - private ByteBuf commulation; |
| 11 | + private final int streamId; |
| 12 | + private final String path; |
12 | 13 | private volatile Http2Headers headers; |
| 14 | + private CompositeByteBuf pending; |
13 | 15 | private Http2Stream http2Stream; |
14 | 16 | private Http2Connection.PropertyKey streamKey; |
15 | | - private String marshaller; |
16 | | - private ByteBufAllocator allocator; |
17 | | - private byte[] content; |
| 17 | + private int bytesToRead; |
| 18 | + private final ByteBufAllocator alloc; |
18 | 19 |
|
19 | | - public Http2Request(int streamId, Http2Stream http2Stream |
20 | | - , Http2Headers headers |
21 | | - //, AbstractHttp2CodecHandler http2CodecHandler |
22 | | - , Http2Connection.PropertyKey streamKey, String marshaller, ByteBufAllocator allocator) { |
| 20 | + public Http2Request(int streamId, String path, Http2Stream http2Stream, Http2Headers headers, |
| 21 | + Http2Connection.PropertyKey streamKey, ByteBufAllocator allocator) { |
23 | 22 | this.streamId = streamId; |
| 23 | + this.path = path; |
24 | 24 | this.http2Stream = http2Stream; |
25 | 25 | this.headers = headers; |
26 | | - //this.http2CodecHandler = http2CodecHandler; |
| 26 | + this.alloc=allocator; |
27 | 27 | this.streamKey = streamKey; |
28 | | - this.marshaller = marshaller; |
29 | | - this.allocator = allocator; |
30 | | - this.commulation = allocator.buffer(); |
| 28 | + this.pending = allocator.compositeBuffer(); |
31 | 29 | } |
32 | 30 |
|
33 | 31 | public Http2Headers getHeaders() { |
34 | 32 | return headers; |
35 | 33 | } |
36 | 34 |
|
37 | 35 | public ByteBuf getData() { |
38 | | - return commulation; |
| 36 | + return pending; |
39 | 37 | } |
40 | 38 |
|
41 | 39 | public int getStreamId() { |
42 | 40 | return streamId; |
43 | 41 | } |
44 | 42 |
|
45 | | - public byte[] content() { |
46 | | - if (content != null) { |
47 | | - return content; |
48 | | - } |
49 | | - this.content = new byte[commulation.readableBytes()]; |
50 | | - commulation.readBytes(content); |
51 | | - commulation.release(); |
52 | | - return content; |
| 43 | + public void appendData(ByteBuf data) { |
| 44 | + pending.addComponent(true, data); |
53 | 45 | } |
54 | 46 |
|
55 | | - public void cumulate(ByteBuf byteBuf) { |
56 | | - commulation = cumulate(allocator, commulation, byteBuf); |
57 | | - } |
| 47 | + public ByteBuf getAvailableTrunk(){ |
58 | 48 |
|
59 | | - public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) { |
60 | | - final ByteBuf buffer; |
61 | | - if (cumulation.writerIndex() > cumulation.maxCapacity() - in.readableBytes() |
62 | | - || cumulation.refCnt() > 1 || cumulation.isReadOnly()) { |
63 | | - // Expand cumulation (by replace it) when either there is not more room in the buffer |
64 | | - // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or |
65 | | - // duplicate().retain() or if its read-only. |
66 | | - // |
67 | | - // See: |
68 | | - // - https://github.com/netty/netty/issues/2327 |
69 | | - // - https://github.com/netty/netty/issues/1764 |
70 | | - buffer = expandCumulation(alloc, cumulation, in.readableBytes()); |
71 | | - } else { |
72 | | - buffer = cumulation; |
73 | | - } |
74 | | - buffer.writeBytes(in); |
75 | | - return buffer; |
76 | | - } |
| 49 | + int type = pending.readUnsignedByte(); |
| 50 | +// if ((type & RESERVED_MASK) != 0) { |
| 51 | +// throw Status.INTERNAL.withDescription( |
| 52 | +// "gRPC frame header malformed: reserved bits not zero") |
| 53 | +// .asRuntimeException(); |
| 54 | +// } |
| 55 | +// compressedFlag = (type & COMPRESSED_FLAG_MASK) != 0; |
77 | 56 |
|
78 | | - private ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) { |
79 | | - ByteBuf oldCumulation = cumulation; |
80 | | - cumulation = alloc.buffer(oldCumulation.readableBytes() + readable); |
81 | | - cumulation.writeBytes(oldCumulation); |
82 | | - oldCumulation.release(); |
83 | | - return cumulation; |
| 57 | + // Update the required length to include the length of the frame. |
| 58 | + this.bytesToRead = pending.readInt(); |
| 59 | +/* if (requiredLength < 0 || requiredLength > maxInboundMessageSize) { |
| 60 | + throw Status.RESOURCE_EXHAUSTED.withDescription( |
| 61 | + String.format("gRPC message exceeds maximum size %d: %d", |
| 62 | + maxInboundMessageSize, requiredLength)) |
| 63 | + .asRuntimeException(); |
| 64 | + }*/ |
| 65 | + return tryRead(); |
| 66 | + |
| 67 | + } |
| 68 | + private ByteBuf tryRead(){ |
| 69 | + if(bytesToRead>0&&pending.readableBytes()>=bytesToRead){ |
| 70 | + final ByteBuf ready = alloc.buffer(); |
| 71 | + pending.readBytes(ready,bytesToRead); |
| 72 | + return ready; |
| 73 | + } |
| 74 | + return null; |
84 | 75 | } |
85 | 76 | } |
0 commit comments