Top Description Inners Fields Constructors Methods
org.apache.tomcat.websocket.server

public Class WsRemoteEndpointImplServer

extends WsRemoteEndpointImplBase
Class Inheritance
Imports
java.io.EOFException, .IOException, java.net.SocketTimeoutException, java.nio.ByteBuffer, java.nio.channels.CompletionHandler, java.util.concurrent.RejectedExecutionException, .TimeUnit, java.util.concurrent.locks.Lock, .ReentrantLock, jakarta.servlet.http.WebConnection, jakarta.websocket.SendHandler, .SendResult, org.apache.coyote.http11.upgrade.UpgradeInfo, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.net.SocketWrapperBase, .SocketWrapperBase.BlockingMode, org.apache.tomcat.util.res.StringManager, org.apache.tomcat.websocket.Constants, .Transformation, .WsRemoteEndpointImplBase

This is the server side jakarta.websocket.RemoteEndpoint implementation - i.e. what the server uses to send data to the client.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private volatile ByteBuffer[]
private final WebConnection
private volatile SendHandler
private final Log
private static final StringManager
private final SocketWrapperBase<?>
private volatile long
private final UpgradeInfo
private final WsWriteTimeout
Inherited from org.apache.tomcat.websocket.WsRemoteEndpointImplBase:
messagePartInProgressSENDRESULT_OK

Constructor Summary

AccessConstructor and Description
public
WsRemoteEndpointImplServer(SocketWrapperBase<?> socketWrapper, UpgradeInfo upgradeInfo, WsServerContainer serverContainer, WebConnection connection)

Method Summary

Modifier and TypeMethod and Description
protected boolean
acquireMessagePartInProgressSemaphore(byte
The OPCODE for the message to be written
opCode
,
long
The time when the attempt to acquire the semaphore should expire
timeoutExpiry
)

Overrides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.acquireMessagePartInProgressSemaphore.

Acquire the semaphore that allows a message part to be written.

pack-priv void
clearHandler(Throwable
The throwable associated with any error that occurred
t
,
boolean
Should SendHandler#onResult(SendResult) be called from a new thread, keeping in mind the requirements of jakarta.websocket.RemoteEndpoint.Async
useDispatch
)

protected void
protected void
doWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... buffers)

Implements abstract org.apache.tomcat.websocket.WsRemoteEndpointImplBase.doWrite.

protected Lock
protected long
protected final boolean
protected void
onTimeout(boolean useDispatch)

public void
onWritePossible(boolean useDispatch)

protected void
protected void
updateStats(long
Size of message payload
payloadLength
)

Overrides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.updateStats.

Hook for updating server side statistics.

Inherited from org.apache.tomcat.websocket.WsRemoteEndpointImplBase:
closeflushBatchgetBatchingAllowedgetSendStreamgetSendTimeoutgetSendWritersendBytessendBytesByCompletionsendBytesByFuturesendObjectsendObjectByCompletionsendObjectByFuturesendPartialBytessendPartialStringsendPingsendPongsendStringsendStringByCompletionsendStringByFuturesetBatchingAllowedsetEncoderssetSendTimeoutsetSession

Field Detail

buffersback to summary
private volatile ByteBuffer[] buffers
connectionback to summary
private final WebConnection connection
handlerback to summary
private volatile SendHandler handler
logback to summary
private final Log log

Hides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.log.

smback to summary
private static final StringManager sm

Hides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sm.

socketWrapperback to summary
private final SocketWrapperBase<?> socketWrapper
timeoutExpiryback to summary
private volatile long timeoutExpiry
upgradeInfoback to summary
private final UpgradeInfo upgradeInfo
wsWriteTimeoutback to summary
private final WsWriteTimeout wsWriteTimeout

Constructor Detail

WsRemoteEndpointImplServerback to summary
public WsRemoteEndpointImplServer(SocketWrapperBase<?> socketWrapper, UpgradeInfo upgradeInfo, WsServerContainer serverContainer, WebConnection connection)

Method Detail

acquireMessagePartInProgressSemaphoreback to summary
protected boolean acquireMessagePartInProgressSemaphore(byte opCode, long timeoutExpiry) throws InterruptedException

Overrides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.acquireMessagePartInProgressSemaphore.

Doc from org.apache.tomcat.websocket.WsRemoteEndpointImplBase.acquireMessagePartInProgressSemaphore.

Acquire the semaphore that allows a message part to be written.

The close message is a special case. It needs to be blocking else implementing the clean-up that follows the sending of the close message gets a lot more complicated. On the server, this creates additional complications as a dead-lock may occur in the following scenario:

  1. Application thread writes message using non-blocking
  2. Write does not complete (write logic holds message pending lock)
  3. Socket is added to poller (or equivalent) for write
  4. Client sends close message
  5. Container processes received close message and tries to send close message in response
  6. Container holds socket lock and is blocked waiting for message pending lock
  7. Poller fires write possible event for socket
  8. Container tries to process write possible event but is blocked waiting for socket lock
  9. Processing of the WebSocket connection is dead-locked until the original message write times out
The purpose of this method is to break the above dead-lock. It does this by returning control of the processor to the socket wrapper and releasing the socket lock while waiting for the pending message write to complete. Normally, that would be a terrible idea as it creates the possibility that the processor is returned to the pool more than once under various error conditions. In this instance it is safe because these are upgrade processors (isUpgrade() returns true) and upgrade processors are never pooled.

Todo

Despite the complications it creates, it would be worth exploring the possibility of processing a received close frame in a non-blocking manner.

Parameters
opCode:byte

The OPCODE for the message to be written

timeoutExpiry:long

The time when the attempt to acquire the semaphore should expire

Returns:boolean

true if the semaphore is obtained, otherwise false.

Annotations
@Override
Exceptions
InterruptedException:
If the wait for the semaphore is interrupted
clearHandlerback to summary
pack-priv void clearHandler(Throwable t, boolean useDispatch)
Parameters
t:Throwable

The throwable associated with any error that occurred

useDispatch:boolean

Should SendHandler#onResult(SendResult) be called from a new thread, keeping in mind the requirements of jakarta.websocket.RemoteEndpoint.Async

doCloseback to summary
protected void doClose()

Implements abstract org.apache.tomcat.websocket.WsRemoteEndpointImplBase.doClose.

Annotations
@Override
doWriteback to summary
protected void doWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... buffers)

Implements abstract org.apache.tomcat.websocket.WsRemoteEndpointImplBase.doWrite.

Annotations
@Override
getLockback to summary
protected Lock getLock()

Implements abstract org.apache.tomcat.websocket.WsRemoteEndpointImplBase.getLock.

Annotations
@Override
getTimeoutExpiryback to summary
protected long getTimeoutExpiry()

Hides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.getTimeoutExpiry.

isMaskedback to summary
protected final boolean isMasked()

Implements abstract org.apache.tomcat.websocket.WsRemoteEndpointImplBase.isMasked.

Annotations
@Override
onTimeoutback to summary
protected void onTimeout(boolean useDispatch)
onWritePossibleback to summary
public void onWritePossible(boolean useDispatch)
setTransformationback to summary
protected void setTransformation(Transformation transformation)

Overrides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.setTransformation.

Annotations
@Override
updateStatsback to summary
protected void updateStats(long payloadLength)

Overrides org.apache.tomcat.websocket.WsRemoteEndpointImplBase.updateStats.

Doc from org.apache.tomcat.websocket.WsRemoteEndpointImplBase.updateStats.

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:long

Size of message payload

Annotations
@Override
org.apache.tomcat.websocket.server back to summary

private Class WsRemoteEndpointImplServer.OnResultRunnable

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.OnResultRunnable
All Implemented Interfaces
java.lang.Runnable

Field Summary

Modifier and TypeField and Description
private final SendHandler
private final Throwable
t

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

shback to summary
private final SendHandler sh
tback to summary
private final Throwable t

Constructor Detail

OnResultRunnableback to summary
private OnResultRunnable(SendHandler sh, Throwable t)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override