Class WsRemoteEndpointImplBase
java.lang.Object
org.apache.tomcat.websocket.WsRemoteEndpointImplBase
- All Implemented Interfaces:
RemoteEndpoint
- Direct Known Subclasses:
WsRemoteEndpointImplClient, WsRemoteEndpointImplServer
Base implementation of a WebSocket remote endpoint.
-
Nested Class Summary
Nested classes/interfaces inherited from interface RemoteEndpoint
RemoteEndpoint.Async, RemoteEndpoint.Basic -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final SemaphoreSemaphore to ensure only one message part is being processed at a time.protected static final StringManagerString manager for this class. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacquireMessagePartInProgressSemaphore(byte opCode, long timeoutExpiry) Acquire the semaphore that allows a message part to be written.final voidclose()Closes all encoders associated with this endpoint.protected abstract voiddoClose()Performs the actual close operation on the underlying connection.protected abstract voiddoWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... data) Writes data to the underlying connection.voidFlush any currently batched messages to the remote endpoint.booleanObtains the current batching status of the endpoint.protected abstract ReentrantLockgetLock()Returns the lock used for thread-safe operations.Returns an OutputStream for sending binary data.longReturns the send timeout in milliseconds.Returns a Writer for sending text data.protected WsSessionReturns the WebSocket session.protected abstract booleanisMasked()Checks if frames should be masked.voidsendBytes(ByteBuffer data) Sends binary data to the remote endpoint.voidsendBytesByCompletion(ByteBuffer data, SendHandler handler) Sends binary data asynchronously with a completion handler.sendBytesByFuture(ByteBuffer data) Sends binary data asynchronously, returning a Future for completion tracking.voidsendObject(Object obj) Sends an object to the remote endpoint using the appropriate encoder.voidsendObjectByCompletion(Object obj, SendHandler completion) Sends an object asynchronously with a completion handler.sendObjectByFuture(Object obj) Sends an object asynchronously, returning a Future for completion tracking.voidsendPartialBytes(ByteBuffer partialByte, boolean last) Sends a partial binary message fragment.voidsendPartialString(String fragment, boolean isLast) Sends a partial text message fragment.voidsendPing(ByteBuffer applicationData) Send a ping message blocking until the message has been sent.voidsendPong(ByteBuffer applicationData) Send a pong message blocking until the message has been sent.voidsendString(String text) Sends a text message to the remote endpoint.voidsendStringByCompletion(String text, SendHandler handler) Sends a text message asynchronously with a completion handler.sendStringByFuture(String text) Sends a text message asynchronously, returning a Future for completion tracking.voidsetBatchingAllowed(boolean batchingAllowed) Enable or disable the batching of outgoing messages for this endpoint.protected voidsetEncoders(EndpointConfig endpointConfig) Initializes the encoders for this endpoint from the given endpoint configuration.voidsetSendTimeout(long timeout) Sets the send timeout in milliseconds.protected voidsetSession(WsSession wsSession) Sets the WebSocket session for this endpoint.protected voidsetTransformation(Transformation transformation) Sets the transformation for this endpoint.protected voidupdateStats(long payloadLength) Hook for updating server side statistics.
-
Field Details
-
sm
String manager for this class. -
messagePartInProgress
Semaphore to ensure only one message part is being processed at a time.
-
-
Constructor Details
-
WsRemoteEndpointImplBase
public WsRemoteEndpointImplBase()Default constructor.
-
-
Method Details
-
setTransformation
Sets the transformation for this endpoint.- Parameters:
transformation- the transformation to use
-
getSendTimeout
public long getSendTimeout()Returns the send timeout in milliseconds.- Returns:
- the send timeout
-
setSendTimeout
public void setSendTimeout(long timeout) Sets the send timeout in milliseconds.- Parameters:
timeout- the send timeout
-
getSession
-
setBatchingAllowed
Description copied from interface:jakarta.websocket.RemoteEndpointEnable or disable the batching of outgoing messages for this endpoint. If batching is disabled when it was previously enabled then this method will block until any currently batched messages have been written.- Specified by:
setBatchingAllowedin interfaceRemoteEndpoint- Parameters:
batchingAllowed- New setting- Throws:
IOException- If changing the value resulted in a call toRemoteEndpoint.flushBatch()and that call threw anIOException.
-
getBatchingAllowed
public boolean getBatchingAllowed()Description copied from interface:jakarta.websocket.RemoteEndpointObtains the current batching status of the endpoint.- Specified by:
getBatchingAllowedin interfaceRemoteEndpoint- Returns:
trueif batching is enabled, otherwisefalse.
-
flushBatch
Description copied from interface:jakarta.websocket.RemoteEndpointFlush any currently batched messages to the remote endpoint. This method will block until the flush completes.- Specified by:
flushBatchin interfaceRemoteEndpoint- Throws:
IOException- If an I/O error occurs while flushing
-
sendBytes
Sends binary data to the remote endpoint.- Parameters:
data- the binary data- Throws:
IOException- if a send error occurs
-
sendBytesByFuture
Sends binary data asynchronously, returning a Future for completion tracking.- Parameters:
data- the binary data- Returns:
- a Future that completes when the send is done
-
sendBytesByCompletion
Sends binary data asynchronously with a completion handler.- Parameters:
data- the binary datahandler- the completion handler
-
sendPartialBytes
Sends a partial binary message fragment.- Parameters:
partialByte- the binary data fragmentlast- whether this is the last fragment- Throws:
IOException- if a send error occurs
-
sendPing
Description copied from interface:jakarta.websocket.RemoteEndpointSend a ping message blocking until the message has been sent. Note that if a message is in the process of being sent asynchronously, this method will block until that message and this ping has been sent.- Specified by:
sendPingin interfaceRemoteEndpoint- Parameters:
applicationData- The payload for the ping message- Throws:
IOException- If an I/O error occurs while sending the pingIllegalArgumentException- if the applicationData is too large for a control message (max 125 bytes)
-
sendPong
Description copied from interface:jakarta.websocket.RemoteEndpointSend a pong message blocking until the message has been sent. Note that if a message is in the process of being sent asynchronously, this method will block until that message and this pong has been sent.- Specified by:
sendPongin interfaceRemoteEndpoint- Parameters:
applicationData- The payload for the pong message- Throws:
IOException- If an I/O error occurs while sending the pongIllegalArgumentException- if the applicationData is too large for a control message (max 125 bytes)
-
sendString
Sends a text message to the remote endpoint.- Parameters:
text- the text data- Throws:
IOException- if a send error occurs
-
sendStringByFuture
-
sendStringByCompletion
Sends a text message asynchronously with a completion handler.- Parameters:
text- the text datahandler- the completion handler
-
sendPartialString
Sends a partial text message fragment.- Parameters:
fragment- the text fragmentisLast- whether this is the last fragment- Throws:
IOException- if a send error occurs
-
getSendStream
Returns an OutputStream for sending binary data.- Returns:
- the output stream
-
getSendWriter
-
acquireMessagePartInProgressSemaphore
protected boolean acquireMessagePartInProgressSemaphore(byte opCode, long timeoutExpiry) throws InterruptedException Acquire the semaphore that allows a message part to be written.- Parameters:
opCode- The OPCODE for the message to be writtentimeoutExpiry- The time when the attempt to acquire the semaphore should expire- Returns:
trueif the semaphore is obtained, otherwisefalse.- Throws:
InterruptedException- If the wait for the semaphore is interrupted
-
updateStats
protected void updateStats(long payloadLength) Hook for updating server side statistics. Called on every frame written (including when batching is enabled and the frames are buffered locally until the buffer is full or is flushed).- Parameters:
payloadLength- Size of message payload
-
sendObject
Sends an object to the remote endpoint using the appropriate encoder.- Parameters:
obj- the object to send- Throws:
IOException- if a send error occursEncodeException- if encoding fails
-
sendObjectByFuture
-
sendObjectByCompletion
Sends an object asynchronously with a completion handler.- Parameters:
obj- the object to sendcompletion- the completion handler
-
setSession
Sets the WebSocket session for this endpoint.- Parameters:
wsSession- the session
-
setEncoders
Initializes the encoders for this endpoint from the given endpoint configuration.- Parameters:
endpointConfig- the endpoint configuration- Throws:
DeploymentException- if an encoder cannot be initialized
-
close
public final void close()Closes all encoders associated with this endpoint. -
doWrite
protected abstract void doWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... data) Writes data to the underlying connection.- Parameters:
handler- the send handlerblockingWriteTimeoutExpiry- the timeout expiry timedata- the data buffers to write
-
isMasked
protected abstract boolean isMasked()Checks if frames should be masked.- Returns:
- true if masking is required
-
doClose
protected abstract void doClose()Performs the actual close operation on the underlying connection. -
getLock
Returns the lock used for thread-safe operations.- Returns:
- the reentrant lock
-