Package org.java_websocket.drafts
Class Draft_6455
- java.lang.Object
-
- org.java_websocket.drafts.Draft
-
- org.java_websocket.drafts.Draft_6455
-
public class Draft_6455 extends Draft
Implementation for the RFC 6455 websocket protocol This is the recommended class for your websocket connection
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
Draft_6455.TranslatedPayloadMetaData
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.nio.ByteBuffer>
byteBufferList
Attribute for the payload of the current continuous frameprivate static java.lang.String
CONNECTION
Handshake specific field for the connectionprivate Framedata
currentContinuousFrame
Attribute for the current continuous frameprivate IExtension
extension
Attribute for the used extension in this draftprivate java.nio.ByteBuffer
incompleteframe
Attribute for the current incomplete frameprivate java.util.List<IExtension>
knownExtensions
Attribute for all available extension in this draftprivate java.util.List<IProtocol>
knownProtocols
Attribute for all available protocols in this draftprivate org.slf4j.Logger
log
Logger instanceprivate int
maxFrameSize
Attribute for the maximum allowed size of a frameprivate IProtocol
protocol
Attribute for the used protocol in this draftprivate java.util.Random
reuseableRandom
Attribute for the reusable random instanceprivate static java.lang.String
SEC_WEB_SOCKET_ACCEPT
Handshake specific field for the acceptprivate static java.lang.String
SEC_WEB_SOCKET_EXTENSIONS
Handshake specific field for the extensionprivate static java.lang.String
SEC_WEB_SOCKET_KEY
Handshake specific field for the keyprivate static java.lang.String
SEC_WEB_SOCKET_PROTOCOL
Handshake specific field for the protocolprivate static java.lang.String
UPGRADE
Handshake specific field for the upgrade-
Fields inherited from class org.java_websocket.drafts.Draft
continuousFrameType, role
-
-
Constructor Summary
Constructors Constructor Description Draft_6455()
Constructor for the websocket protocol specified by RFC 6455 with default extensionsDraft_6455(java.util.List<IExtension> inputExtensions)
Constructor for the websocket protocol specified by RFC 6455 with custom extensionsDraft_6455(java.util.List<IExtension> inputExtensions, int inputMaxFrameSize)
Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocolsDraft_6455(java.util.List<IExtension> inputExtensions, java.util.List<IProtocol> inputProtocols)
Constructor for the websocket protocol specified by RFC 6455 with custom extensions and protocolsDraft_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 protocolsDraft_6455(IExtension inputExtension)
Constructor for the websocket protocol specified by RFC 6455 with custom extensions
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HandshakeState
acceptHandshakeAsClient(ClientHandshake request, ServerHandshake response)
HandshakeState
acceptHandshakeAsServer(ClientHandshake handshakedata)
private void
addToBufferList(java.nio.ByteBuffer payloadData)
Add a payload to the current bytebuffer listprivate void
checkBufferLimit()
Check the current size of the buffer and throw an exception if the size is bigger than the max allowed frame sizeprivate void
clearBufferList()
Clear the current bytebuffer listprivate HandshakeState
containsRequestedProtocol(java.lang.String requestedProtocol)
Check if the requested protocol is part of this draftDraft
copyInstance()
Drafts must only be by one websocket at all.java.nio.ByteBuffer
createBinaryFrame(Framedata framedata)
private java.nio.ByteBuffer
createByteBufferFromFramedata(Framedata framedata)
java.util.List<Framedata>
createFrames(java.lang.String text, boolean mask)
java.util.List<Framedata>
createFrames(java.nio.ByteBuffer binary, boolean mask)
boolean
equals(java.lang.Object o)
private byte
fromOpcode(Opcode opcode)
private java.lang.String
generateFinalKey(java.lang.String in)
Generate a final key from a input stringprivate long
getByteBufferListSize()
Get the current size of the resulting bytebuffer in the bytebuffer listCloseHandshakeType
getCloseHandshakeType()
IExtension
getExtension()
Getter for the extension which is used by this draftjava.util.List<IExtension>
getKnownExtensions()
Getter for all available extensions for this draftjava.util.List<IProtocol>
getKnownProtocols()
Getter for all available protocols for this draftprivate byte
getMaskByte(boolean mask)
Get the mask byte if existingint
getMaxFrameSize()
Getter for the maximum allowed payload size which is used by this draftprivate java.nio.ByteBuffer
getPayloadFromByteBufferList()
Method to generate a full bytebuffer out of all the fragmented frame payloadIProtocol
getProtocol()
Getter for the protocol which is used by this draftprivate byte
getRSVByte(int rsv)
Get a byte that can set RSV bits when OR(|)'d.private java.lang.String
getServerTime()
Generate a date for for the date-headerprivate int
getSizeBytes(java.nio.ByteBuffer mes)
Get the size bytes for the byte bufferint
hashCode()
private void
logRuntimeException(WebSocketImpl webSocketImpl, java.lang.RuntimeException e)
Log the runtime exception to the specific WebSocketImplClientHandshakeBuilder
postProcessHandshakeRequestAsClient(ClientHandshakeBuilder request)
HandshakeBuilder
postProcessHandshakeResponseAsServer(ClientHandshake request, ServerHandshakeBuilder response)
void
processFrame(WebSocketImpl webSocketImpl, Framedata frame)
Handle the frame specific to the draftprivate void
processFrameBinary(WebSocketImpl webSocketImpl, Framedata frame)
Process the frame if it is a binary frameprivate void
processFrameClosing(WebSocketImpl webSocketImpl, Framedata frame)
Process the frame if it is a closing frameprivate void
processFrameContinuousAndNonFin(WebSocketImpl webSocketImpl, Framedata frame, Opcode curop)
Process the frame if it is a continuous frame or the fin bit is not setprivate void
processFrameIsFin(WebSocketImpl webSocketImpl, Framedata frame)
Process the frame if it is the last frameprivate void
processFrameIsNotFin(Framedata frame)
Process the frame if it is not the last frameprivate void
processFrameText(WebSocketImpl webSocketImpl, Framedata frame)
Process the frame if it is a text framevoid
reset()
private byte[]
toByteArray(long val, int bytecount)
private Opcode
toOpcode(byte opcode)
java.lang.String
toString()
java.util.List<Framedata>
translateFrame(java.nio.ByteBuffer buffer)
private Framedata
translateSingleFrame(java.nio.ByteBuffer buffer)
private void
translateSingleFrameCheckLengthLimit(long length)
Check if the frame size exceeds the allowed limitprivate void
translateSingleFrameCheckPacketSize(int maxpacketsize, int realpacketsize)
Check if the max packet size is smaller than the real packet sizeprivate Draft_6455.TranslatedPayloadMetaData
translateSingleFramePayloadLength(java.nio.ByteBuffer buffer, Opcode optcode, int oldPayloadlength, int maxpacketsize, int oldRealpacketsize)
Translate the buffer depending when it has an extended payload length (126 or 127)-
Methods inherited from class org.java_websocket.drafts.Draft
basicAccept, checkAlloc, continuousFrame, createHandshake, createHandshake, createHandshake, createHandshake, getRole, readLine, readStringLine, readVersion, setParseMode, translateHandshake, translateHandshakeHttp
-
-
-
-
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.util.Random 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 draftinputProtocols
- 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 draftinputMaxFrameSize
- 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 draftinputProtocols
- the protocols which should be used for this draftinputMaxFrameSize
- the maximum allowed size of a frame (the real payload size, decoded frames can be bigger)- Since:
- 1.4.0
-
-
Method Detail
-
acceptHandshakeAsServer
public HandshakeState acceptHandshakeAsServer(ClientHandshake handshakedata) throws InvalidHandshakeException
- Specified by:
acceptHandshakeAsServer
in classDraft
- Throws:
InvalidHandshakeException
-
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
-
acceptHandshakeAsClient
public HandshakeState acceptHandshakeAsClient(ClientHandshake request, ServerHandshake response) throws InvalidHandshakeException
- Specified by:
acceptHandshakeAsClient
in classDraft
- Throws:
InvalidHandshakeException
-
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
-
postProcessHandshakeRequestAsClient
public ClientHandshakeBuilder postProcessHandshakeRequestAsClient(ClientHandshakeBuilder request)
- Specified by:
postProcessHandshakeRequestAsClient
in classDraft
-
postProcessHandshakeResponseAsServer
public HandshakeBuilder postProcessHandshakeResponseAsServer(ClientHandshake request, ServerHandshakeBuilder response) throws InvalidHandshakeException
- Specified by:
postProcessHandshakeResponseAsServer
in classDraft
- Throws:
InvalidHandshakeException
-
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 classDraft
- Returns:
- a copy of the draft
-
createBinaryFrame
public java.nio.ByteBuffer createBinaryFrame(Framedata framedata)
- Specified by:
createBinaryFrame
in classDraft
-
createByteBufferFromFramedata
private java.nio.ByteBuffer createByteBufferFromFramedata(Framedata framedata)
-
translateSingleFrame
private Framedata translateSingleFrame(java.nio.ByteBuffer buffer) throws IncompleteException, InvalidDataException
-
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 fromoptcode
- the decoded optcodeoldPayloadlength
- the old payload lengthmaxpacketsize
- the max packet size allowedoldRealpacketsize
- 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 lengthIncompleteException
- if the maxpacketsize is smaller than the realpackagesizeLimitExceededException
- 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 sizerealpacketsize
- 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
-
translateFrame
public java.util.List<Framedata> translateFrame(java.nio.ByteBuffer buffer) throws InvalidDataException
- Specified by:
translateFrame
in classDraft
- Throws:
InvalidDataException
-
createFrames
public java.util.List<Framedata> createFrames(java.nio.ByteBuffer binary, boolean mask)
- Specified by:
createFrames
in classDraft
-
createFrames
public java.util.List<Framedata> createFrames(java.lang.String text, boolean mask)
- Specified by:
createFrames
in classDraft
-
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)
-
toOpcode
private Opcode toOpcode(byte opcode) throws InvalidFrameException
- Throws:
InvalidFrameException
-
processFrame
public void processFrame(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException
Description copied from class:Draft
Handle the frame specific to the draft- Specified by:
processFrame
in classDraft
- Parameters:
webSocketImpl
- the websocketimpl used for this draftframe
- the frame which is supposed to be handled- Throws:
InvalidDataException
- will be thrown on invalid data
-
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 useframe
- the current framecurop
- 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 implframe
- 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 websockete
- the runtime exception
-
processFrameText
private void processFrameText(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException
Process the frame if it is a text frame- Parameters:
webSocketImpl
- the websocket implframe
- the frame- Throws:
InvalidDataException
-
processFrameIsFin
private void processFrameIsFin(WebSocketImpl webSocketImpl, Framedata frame) throws InvalidDataException
Process the frame if it is the last frame- Parameters:
webSocketImpl
- the websocket implframe
- the frame- Throws:
InvalidDataException
- if there is a protocol error
-
processFrameIsNotFin
private void processFrameIsNotFin(Framedata frame) throws InvalidDataException
Process the frame if it is not the last frame- Parameters:
frame
- the frame- Throws:
InvalidDataException
- if there is a protocol error
-
processFrameClosing
private void processFrameClosing(WebSocketImpl webSocketImpl, Framedata frame)
Process the frame if it is a closing frame- Parameters:
webSocketImpl
- the websocket implframe
- 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
-
getCloseHandshakeType
public CloseHandshakeType getCloseHandshakeType()
- Specified by:
getCloseHandshakeType
in classDraft
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.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)
-
-