Package org.apache.tomcat.websocket
Class WsFrameBase
- java.lang.Object
-
- org.apache.tomcat.websocket.WsFrameBase
-
- Direct Known Subclasses:
WsFrameClient,WsFrameServer
public abstract class WsFrameBase extends java.lang.ObjectTakes 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 classWsFrameBase.ReadStateWAITING - 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.ByteBufferinputBufferThe input buffer for reading data.protected WsSessionwsSessionThe WebSocket session for this frame.
-
Constructor Summary
Constructors Constructor Description WsFrameBase(WsSession wsSession, Transformation transformation)Constructs a new WsFrameBase.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected static longbyteArrayToLong(byte[] b, int start, int len)Converts a byte array segment to a long value.protected voidchangeReadState(WsFrameBase.ReadState newState)Changes the read state to the new state.protected booleanchangeReadState(WsFrameBase.ReadState oldState, WsFrameBase.ReadState newState)Atomically changes the read state if it matches the expected old state.protected abstract LoggetLog()Returns the log instance for this frame.protected WsFrameBase.ReadStategetReadState()Returns the current read state.protected TransformationgetTransformation()Returns the transformation applied to this frame.protected abstract booleanisMasked()Returns whether frames from this peer must be masked.protected booleanisOpen()Returns whether the connection is open.protected booleanisSuspended()Returns whether this frame is currently suspended.protected voidprocessInputBuffer()Processes the input buffer, reading and handling WebSocket frames.voidresume()Resumes processing of this frame after suspension.protected abstract voidresumeProcessing()This method will be invoked when the read operation is resumed.protected voidsendMessageBinary(java.nio.ByteBuffer msg, boolean last)Sends a binary message to the remote endpoint.protected voidsendMessageText(boolean last)Sends a text message to the remote endpoint.voidsuspend()Suspends processing of this frame.protected voidupdateStats(long payloadLength)Hook for updating server side statistics.
-
-
-
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 sessiontransformation- The transformation to apply
-
-
Method Detail
-
processInputBuffer
protected void processInputBuffer() throws java.io.IOExceptionProcesses 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:
trueif 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 WsIOExceptionSends 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 WsIOExceptionSends a binary message to the remote endpoint.- Parameters:
msg- The message datalast- 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.IOExceptionConverts a byte array segment to a long value.- Parameters:
b- The byte arraystart- The start indexlen- 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:
trueif 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:
trueif 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 statenewState- The new state- Returns:
trueif 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.
-
-