Deframer for GRPC frames. This class is not thread-safe. Unless otherwise stated, all calls to public methods should be made in the deframing thread.
| 40 | * made in the deframing thread. |
| 41 | */ |
| 42 | @NotThreadSafe |
| 43 | public class MessageDeframer implements Closeable, Deframer { |
| 44 | private static final int HEADER_LENGTH = 5; |
| 45 | private static final int COMPRESSED_FLAG_MASK = 1; |
| 46 | private static final int RESERVED_MASK = 0xFE; |
| 47 | private static final int MAX_BUFFER_SIZE = 1024 * 1024 * 2; |
| 48 | |
| 49 | /** |
| 50 | * A listener of deframing events. These methods will be invoked from the deframing thread. |
| 51 | */ |
| 52 | public interface Listener { |
| 53 | |
| 54 | /** |
| 55 | * Called when the given number of bytes has been read from the input source of the deframer. |
| 56 | * This is typically used to indicate to the underlying transport that more data can be |
| 57 | * accepted. |
| 58 | * |
| 59 | * @param numBytes the number of bytes read from the deframer's input source. |
| 60 | */ |
| 61 | void bytesRead(int numBytes); |
| 62 | |
| 63 | /** |
| 64 | * Called to deliver the next complete message. |
| 65 | * |
| 66 | * @param producer single message producer wrapping the message. |
| 67 | */ |
| 68 | void messagesAvailable(StreamListener.MessageProducer producer); |
| 69 | |
| 70 | /** |
| 71 | * Called when the deframer closes. |
| 72 | * |
| 73 | * @param hasPartialMessage whether the deframer contained an incomplete message at closing. |
| 74 | */ |
| 75 | void deframerClosed(boolean hasPartialMessage); |
| 76 | |
| 77 | /** |
| 78 | * Called when a {@link #deframe(ReadableBuffer)} operation failed. |
| 79 | * |
| 80 | * @param cause the actual failure |
| 81 | */ |
| 82 | void deframeFailed(Throwable cause); |
| 83 | } |
| 84 | |
| 85 | private enum State { |
| 86 | HEADER, BODY |
| 87 | } |
| 88 | |
| 89 | private Listener listener; |
| 90 | private int maxInboundMessageSize; |
| 91 | private final StatsTraceContext statsTraceCtx; |
| 92 | private final TransportTracer transportTracer; |
| 93 | private Decompressor decompressor; |
| 94 | private GzipInflatingBuffer fullStreamDecompressor; |
| 95 | private byte[] inflatedBuffer; |
| 96 | private int inflatedIndex; |
| 97 | private State state = State.HEADER; |
| 98 | private int requiredLength = HEADER_LENGTH; |
| 99 | private boolean compressedFlag; |
nothing calls this directly
no outgoing calls
no test coverage detected