Class MessageDeframer

  • All Implemented Interfaces:
    Deframer, java.io.Closeable, java.lang.AutoCloseable

    @NotThreadSafe
    public class MessageDeframer
    extends java.lang.Object
    implements java.io.Closeable, Deframer
    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.

    • Field Detail

      • maxInboundMessageSize

        private int maxInboundMessageSize
      • inflatedBuffer

        private byte[] inflatedBuffer
      • inflatedIndex

        private int inflatedIndex
      • requiredLength

        private int requiredLength
      • compressedFlag

        private boolean compressedFlag
      • pendingDeliveries

        private long pendingDeliveries
      • inDelivery

        private boolean inDelivery
      • currentMessageSeqNo

        private int currentMessageSeqNo
      • inboundBodyWireSize

        private int inboundBodyWireSize
      • closeWhenComplete

        private boolean closeWhenComplete
      • stopDelivery

        private volatile boolean stopDelivery
    • Constructor Detail

      • MessageDeframer

        public MessageDeframer​(MessageDeframer.Listener listener,
                               Decompressor decompressor,
                               int maxMessageSize,
                               StatsTraceContext statsTraceCtx,
                               TransportTracer transportTracer)
        Create a deframer.
        Parameters:
        listener - listener for deframer events.
        decompressor - the compression used if a compressed frame is encountered, with NONE meaning unsupported
        maxMessageSize - the maximum allowed size for received messages.
    • Method Detail

      • setDecompressor

        public void setDecompressor​(Decompressor decompressor)
        Description copied from interface: Deframer
        Sets the decompressor available to use. The message encoding for the stream comes later in time, and thus will not be available at the time of construction. This should only be set once, since the compression codec cannot change after the headers have been sent.
        Specified by:
        setDecompressor in interface Deframer
        Parameters:
        decompressor - the decompressing wrapper.
      • request

        public void request​(int numMessages)
        Description copied from interface: Deframer
        Requests up to the given number of messages from the call. No additional messages will be delivered.

        If Deframer.close() has been called, this method will have no effect.

        Specified by:
        request in interface Deframer
        Parameters:
        numMessages - the requested number of messages to be delivered to the listener.
      • deframe

        public void deframe​(ReadableBuffer data)
        Description copied from interface: Deframer
        Adds the given data to this deframer and attempts delivery to the listener.
        Specified by:
        deframe in interface Deframer
        Parameters:
        data - the raw data read from the remote endpoint. Must be non-null.
      • closeWhenComplete

        public void closeWhenComplete()
        Description copied from interface: Deframer
        Close when any messages currently queued have been requested and delivered.
        Specified by:
        closeWhenComplete in interface Deframer
      • stopDelivery

        void stopDelivery()
        Sets a flag to interrupt delivery of any currently queued messages. This may be invoked outside of the deframing thread, and must be followed by a call to close() in the deframing thread. Without a subsequent call to close(), the deframer may hang waiting for additional messages before noticing that the stopDelivery flag has been set.
      • hasPendingDeliveries

        boolean hasPendingDeliveries()
      • close

        public void close()
        Description copied from interface: Deframer
        Closes this deframer and frees any resources. After this method is called, additional calls will have no effect.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface Deframer
      • isClosed

        public boolean isClosed()
        Indicates whether or not this deframer has been closed.
      • isClosedOrScheduledToClose

        private boolean isClosedOrScheduledToClose()
        Returns true if this deframer has already been closed or scheduled to close.
      • isStalled

        private boolean isStalled()
      • deliver

        private void deliver()
        Reads and delivers as many messages to the listener as possible.
      • readRequiredBytes

        private boolean readRequiredBytes()
        Attempts to read the required bytes into nextFrame.
        Returns:
        true if all of the required bytes have been read.
      • processHeader

        private void processHeader()
        Processes the GRPC compression header which is composed of the compression flag and the outer frame length.
      • processBody

        private void processBody()
        Processes the GRPC message body, which depending on frame header flags may be compressed.
      • getUncompressedBody

        private java.io.InputStream getUncompressedBody()
      • getCompressedBody

        private java.io.InputStream getCompressedBody()