Class Draft_6455


  • public class Draft_6455
    extends Draft
    Implementation for the RFC 6455 websocket protocol This is the recommended class for your websocket connection
    • Field Detail

      • SEC_WEB_SOCKET_KEY

        private static final java.lang.String SEC_WEB_SOCKET_KEY
        Handshake specific field for the key
        See Also:
        Constant Field Values
      • SEC_WEB_SOCKET_PROTOCOL

        private static final java.lang.String SEC_WEB_SOCKET_PROTOCOL
        Handshake specific field for the protocol
        See Also:
        Constant Field Values
      • SEC_WEB_SOCKET_EXTENSIONS

        private static final java.lang.String SEC_WEB_SOCKET_EXTENSIONS
        Handshake specific field for the extension
        See Also:
        Constant Field Values
      • SEC_WEB_SOCKET_ACCEPT

        private static final java.lang.String SEC_WEB_SOCKET_ACCEPT
        Handshake specific field for the accept
        See Also:
        Constant Field Values
      • UPGRADE

        private static final java.lang.String UPGRADE
        Handshake specific field for the upgrade
        See Also:
        Constant Field Values
      • CONNECTION

        private static final java.lang.String CONNECTION
        Handshake specific field for the connection
        See Also:
        Constant Field Values
      • log

        private final org.slf4j.Logger log
        Logger instance
        Since:
        1.4.0
      • extension

        private IExtension extension
        Attribute for the used extension in this draft
      • knownExtensions

        private java.util.List<IExtension> knownExtensions
        Attribute for all available extension in this draft
      • protocol

        private IProtocol protocol
        Attribute for the used protocol in this draft
      • knownProtocols

        private java.util.List<IProtocol> knownProtocols
        Attribute for all available protocols in this draft
      • currentContinuousFrame

        private Framedata currentContinuousFrame
        Attribute for the current continuous frame
      • byteBufferList

        private final java.util.List<java.nio.ByteBuffer> byteBufferList
        Attribute for the payload of the current continuous frame
      • incompleteframe

        private java.nio.ByteBuffer incompleteframe
        Attribute for the current incomplete frame
      • reuseableRandom

        private final java.security.SecureRandom reuseableRandom
        Attribute for the reusable random instance
      • maxFrameSize

        private int maxFrameSize
        Attribute for the maximum allowed size of a frame
        Since:
        1.4.0
    • Constructor Detail

      • Draft_6455

        public Draft_6455()
        Constructor for the websocket protocol specified by RFC 6455 with default extensions
        Since:
        1.3.5
      • Draft_6455

        public Draft_6455​(IExtension inputExtension)
        Constructor for the websocket protocol specified by RFC 6455 with custom extensions
        Parameters:
        inputExtension - the extension which should be used for this draft
        Since:
        1.3.5
      • Draft_6455

        public Draft_6455​(java.util.List<IExtension> inputExtensions)
        Constructor for the websocket protocol specified by RFC 6455 with custom extensions
        Parameters:
        inputExtensions - the extensions which should be used for this draft
        Since:
        1.3.5
      • Draft_6455

        public Draft_6455​(java.util.List<IExtension> inputExtensions,
                          java.util.List<IProtocol> inputProtocols)
        Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
        Parameters:
        inputExtensions - the extensions which should be used for this draft
        inputProtocols - the protocols which should be used for this draft
        Since:
        1.3.7
      • Draft_6455

        public Draft_6455​(java.util.List<IExtension> inputExtensions,
                          int inputMaxFrameSize)
        Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
        Parameters:
        inputExtensions - the extensions which should be used for this draft
        inputMaxFrameSize - the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)
        Since:
        1.4.0
      • Draft_6455

        public Draft_6455​(java.util.List<IExtension> inputExtensions,
                          java.util.List<IProtocol> inputProtocols,
                          int inputMaxFrameSize)
        Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocols
        Parameters:
        inputExtensions - the extensions which should be used for this draft
        inputProtocols - the protocols which should be used for this draft
        inputMaxFrameSize - the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)
        Since:
        1.4.0
    • Method Detail

      • containsRequestedProtocol

        private HandshakeState containsRequestedProtocol​(java.lang.String requestedProtocol)
        Check if the requested protocol is part of this draft
        Parameters:
        requestedProtocol - the requested protocol
        Returns:
        MATCHED if it is matched, otherwise NOT_MATCHED
      • getExtension

        public IExtension getExtension()
        Getter for the extension which is used by this draft
        Returns:
        the extension which is used or null, if handshake is not yet done
      • getKnownExtensions

        public java.util.List<IExtension> getKnownExtensions()
        Getter for all available extensions for this draft
        Returns:
        the extensions which are enabled for this draft
      • getProtocol

        public IProtocol getProtocol()
        Getter for the protocol which is used by this draft
        Returns:
        the protocol which is used or null, if handshake is not yet done or no valid protocols
        Since:
        1.3.7
      • getMaxFrameSize

        public int getMaxFrameSize()
        Getter for the maximum allowed payload size which is used by this draft
        Returns:
        the size, which is allowed for the payload
        Since:
        1.4.0
      • getKnownProtocols

        public java.util.List<IProtocol> getKnownProtocols()
        Getter for all available protocols for this draft
        Returns:
        the protocols which are enabled for this draft
        Since:
        1.3.7
      • copyInstance

        public Draft copyInstance()
        Description copied from class: Draft
        Drafts must only be by one websocket at all. To prevent drafts to be used more than once the Websocket implementation should call this method in order to create a new usable version of a given draft instance.
        The copy can be safely used in conjunction with a new websocket connection.
        Specified by:
        copyInstance in class Draft
        Returns:
        a copy of the draft
      • createByteBufferFromFramedata

        private java.nio.ByteBuffer createByteBufferFromFramedata​(Framedata framedata)
      • translateSingleFramePayloadLength

        private Draft_6455.TranslatedPayloadMetaData translateSingleFramePayloadLength​(java.nio.ByteBuffer buffer,
                                                                                       Opcode optcode,
                                                                                       int oldPayloadlength,
                                                                                       int maxpacketsize,
                                                                                       int oldRealpacketsize)
                                                                                throws InvalidFrameException,
                                                                                       IncompleteException,
                                                                                       LimitExceededException
        Translate the buffer depending when it has an extended payload length (126 or 127)
        Parameters:
        buffer - the buffer to read from
        optcode - the decoded optcode
        oldPayloadlength - the old payload length
        maxpacketsize - the max packet size allowed
        oldRealpacketsize - the real packet size
        Returns:
        the new payload data containing new payload length and new packet size
        Throws:
        InvalidFrameException - thrown if a control frame has an invalid length
        IncompleteException - if the maxpacketsize is smaller than the realpackagesize
        LimitExceededException - if the payload length is to big
      • translateSingleFrameCheckLengthLimit

        private void translateSingleFrameCheckLengthLimit​(long length)
                                                   throws LimitExceededException
        Check if the frame size exceeds the allowed limit
        Parameters:
        length - the current payload length
        Throws:
        LimitExceededException - if the payload length is to big
      • translateSingleFrameCheckPacketSize

        private void translateSingleFrameCheckPacketSize​(int maxpacketsize,
                                                         int realpacketsize)
                                                  throws IncompleteException
        Check if the max packet size is smaller than the real packet size
        Parameters:
        maxpacketsize - the max packet size
        realpacketsize - the real packet size
        Throws:
        IncompleteException - if the maxpacketsize is smaller than the realpackagesize
      • getRSVByte

        private byte getRSVByte​(int rsv)
        Get a byte that can set RSV bits when OR(|)'d. 0 1 2 3 4 5 6 7 +-+-+-+-+-------+ |F|R|R|R| opcode| |I|S|S|S| (4) | |N|V|V|V| | | |1|2|3| |
        Parameters:
        rsv - Can only be {0, 1, 2, 3}
        Returns:
        byte that represents which RSV bit is set.
      • getMaskByte

        private byte getMaskByte​(boolean mask)
        Get the mask byte if existing
        Parameters:
        mask - is mask active or not
        Returns:
        -128 for true, 0 for false
      • getSizeBytes

        private int getSizeBytes​(java.nio.ByteBuffer mes)
        Get the size bytes for the byte buffer
        Parameters:
        mes - the current buffer
        Returns:
        the size bytes
      • createFrames

        public java.util.List<Framedata> createFrames​(java.nio.ByteBuffer binary,
                                                      boolean mask)
        Specified by:
        createFrames in class Draft
      • createFrames

        public java.util.List<Framedata> createFrames​(java.lang.String text,
                                                      boolean mask)
        Specified by:
        createFrames in class Draft
      • reset

        public void reset()
        Specified by:
        reset in class Draft
      • getServerTime

        private java.lang.String getServerTime()
        Generate a date for for the date-header
        Returns:
        the server time
      • generateFinalKey

        private java.lang.String generateFinalKey​(java.lang.String in)
        Generate a final key from a input string
        Parameters:
        in - the input string
        Returns:
        a final key
      • toByteArray

        private byte[] toByteArray​(long val,
                                   int bytecount)
      • fromOpcode

        private byte fromOpcode​(Opcode opcode)
      • processFrameContinuousAndNonFin

        private void processFrameContinuousAndNonFin​(WebSocketImpl webSocketImpl,
                                                     Framedata frame,
                                                     Opcode curop)
                                              throws InvalidDataException
        Process the frame if it is a continuous frame or the fin bit is not set
        Parameters:
        webSocketImpl - the websocket implementation to use
        frame - the current frame
        curop - the current Opcode
        Throws:
        InvalidDataException - if there is a protocol error
      • processFrameBinary

        private void processFrameBinary​(WebSocketImpl webSocketImpl,
                                        Framedata frame)
        Process the frame if it is a binary frame
        Parameters:
        webSocketImpl - the websocket impl
        frame - the frame
      • logRuntimeException

        private void logRuntimeException​(WebSocketImpl webSocketImpl,
                                         java.lang.RuntimeException e)
        Log the runtime exception to the specific WebSocketImpl
        Parameters:
        webSocketImpl - the implementation of the websocket
        e - the runtime exception
      • processFrameClosing

        private void processFrameClosing​(WebSocketImpl webSocketImpl,
                                         Framedata frame)
        Process the frame if it is a closing frame
        Parameters:
        webSocketImpl - the websocket impl
        frame - the frame
      • clearBufferList

        private void clearBufferList()
        Clear the current bytebuffer list
      • addToBufferList

        private void addToBufferList​(java.nio.ByteBuffer payloadData)
        Add a payload to the current bytebuffer list
        Parameters:
        payloadData - the new payload
      • checkBufferLimit

        private void checkBufferLimit()
                               throws LimitExceededException
        Check the current size of the buffer and throw an exception if the size is bigger than the max allowed frame size
        Throws:
        LimitExceededException - if the current size is bigger than the allowed size
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class Draft
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • getPayloadFromByteBufferList

        private java.nio.ByteBuffer getPayloadFromByteBufferList()
                                                          throws LimitExceededException
        Method to generate a full bytebuffer out of all the fragmented frame payload
        Returns:
        a bytebuffer containing all the data
        Throws:
        LimitExceededException - will be thrown when the totalSize is bigger then Integer.MAX_VALUE due to not being able to allocate more
      • getByteBufferListSize

        private long getByteBufferListSize()
        Get the current size of the resulting bytebuffer in the bytebuffer list
        Returns:
        the size as long (to not get an integer overflow)