Class WsFrameBase

  • Direct Known Subclasses:
    WsFrameClient, WsFrameServer

    public abstract class WsFrameBase
    extends java.lang.Object
    Takes the ServletInputStream, processes the WebSocket frames it contains and extracts the messages. WebSocket Pings received will be responded to automatically without any action required by the application.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  WsFrameBase.ReadState
      WAITING - not suspended Server case: waiting for a notification that data is ready to be read from the socket, the socket is registered to the poller Client case: data has been read from the socket and is waiting for data to be processed PROCESSING - not suspended Server case: reading from the socket and processing the data Client case: processing the data if such has already been read and more data will be read from the socket SUSPENDING_WAIT - suspended, a call to suspend() was made while in WAITING state.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.nio.ByteBuffer inputBuffer
      The input buffer for reading data.
      protected WsSession wsSession
      The WebSocket session for this frame.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected static long byteArrayToLong​(byte[] b, int start, int len)
      Converts a byte array segment to a long value.
      protected void changeReadState​(WsFrameBase.ReadState newState)
      Changes the read state to the new state.
      protected boolean changeReadState​(WsFrameBase.ReadState oldState, WsFrameBase.ReadState newState)
      Atomically changes the read state if it matches the expected old state.
      protected abstract Log getLog()
      Returns the log instance for this frame.
      protected WsFrameBase.ReadState getReadState()
      Returns the current read state.
      protected Transformation getTransformation()
      Returns the transformation applied to this frame.
      protected abstract boolean isMasked()
      Returns whether frames from this peer must be masked.
      protected boolean isOpen()
      Returns whether the connection is open.
      protected boolean isSuspended()
      Returns whether this frame is currently suspended.
      protected void processInputBuffer()
      Processes the input buffer, reading and handling WebSocket frames.
      void resume()
      Resumes processing of this frame after suspension.
      protected abstract void resumeProcessing()
      This method will be invoked when the read operation is resumed.
      protected void sendMessageBinary​(java.nio.ByteBuffer msg, boolean last)
      Sends a binary message to the remote endpoint.
      protected void sendMessageText​(boolean last)
      Sends a text message to the remote endpoint.
      void suspend()
      Suspends processing of this frame.
      protected void updateStats​(long payloadLength)
      Hook for updating server side statistics.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • wsSession

        protected final WsSession wsSession
        The WebSocket session for this frame.
      • inputBuffer

        protected final java.nio.ByteBuffer inputBuffer
        The input buffer for reading data.
    • Constructor Detail

      • WsFrameBase

        public WsFrameBase​(WsSession wsSession,
                           Transformation transformation)
        Constructs a new WsFrameBase.
        Parameters:
        wsSession - The WebSocket session
        transformation - The transformation to apply
    • Method Detail

      • processInputBuffer

        protected void processInputBuffer()
                                   throws java.io.IOException
        Processes the input buffer, reading and handling WebSocket frames.
        Throws:
        java.io.IOException - If an I/O error occurs
      • isMasked

        protected abstract boolean isMasked()
        Returns whether frames from this peer must be masked.
        Returns:
        true if frames must be masked
      • getLog

        protected abstract Log getLog()
        Returns the log instance for this frame.
        Returns:
        the log instance
      • updateStats

        protected void updateStats​(long payloadLength)
        Hook for updating server side statistics. Called on every frame received.
        Parameters:
        payloadLength - Size of message payload
      • sendMessageText

        protected void sendMessageText​(boolean last)
                                throws WsIOException
        Sends a text message to the remote endpoint.
        Parameters:
        last - Whether this is the last fragment of the message
        Throws:
        WsIOException - If a WebSocket I/O error occurs
      • sendMessageBinary

        protected void sendMessageBinary​(java.nio.ByteBuffer msg,
                                         boolean last)
                                  throws WsIOException
        Sends a binary message to the remote endpoint.
        Parameters:
        msg - The message data
        last - Whether this is the last fragment of the message
        Throws:
        WsIOException - If a WebSocket I/O error occurs
      • byteArrayToLong

        protected static long byteArrayToLong​(byte[] b,
                                              int start,
                                              int len)
                                       throws java.io.IOException
        Converts a byte array segment to a long value.
        Parameters:
        b - The byte array
        start - The start index
        len - The length
        Returns:
        the long value
        Throws:
        java.io.IOException - If the length exceeds 8 bytes
      • isOpen

        protected boolean isOpen()
        Returns whether the connection is open.
        Returns:
        true if open
      • getTransformation

        protected Transformation getTransformation()
        Returns the transformation applied to this frame.
        Returns:
        the transformation
      • suspend

        public void suspend()
        Suspends processing of this frame.
      • resume

        public void resume()
        Resumes processing of this frame after suspension.
      • isSuspended

        protected boolean isSuspended()
        Returns whether this frame is currently suspended.
        Returns:
        true if suspended
      • getReadState

        protected WsFrameBase.ReadState getReadState()
        Returns the current read state.
        Returns:
        the read state
      • changeReadState

        protected void changeReadState​(WsFrameBase.ReadState newState)
        Changes the read state to the new state.
        Parameters:
        newState - The new read state
      • changeReadState

        protected boolean changeReadState​(WsFrameBase.ReadState oldState,
                                          WsFrameBase.ReadState newState)
        Atomically changes the read state if it matches the expected old state.
        Parameters:
        oldState - The expected old state
        newState - The new state
        Returns:
        true if the state was changed
      • resumeProcessing

        protected abstract void resumeProcessing()
        This method will be invoked when the read operation is resumed. Since suspend of the read operation can be invoked at any time, when implementing this method one should consider that there might still be data remaining into the internal buffers that needs to be processed before reading again from the socket.