Class WsRemoteEndpointImplServer
java.lang.Object
org.apache.tomcat.websocket.WsRemoteEndpointImplBase
org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer
- All Implemented Interfaces:
RemoteEndpoint
This is the server side
RemoteEndpoint implementation - i.e. what the server uses to send
data to the client.-
Nested Class Summary
Nested classes/interfaces inherited from interface RemoteEndpoint
RemoteEndpoint.Async, RemoteEndpoint.Basic -
Field Summary
Fields inherited from class WsRemoteEndpointImplBase
messagePartInProgress -
Constructor Summary
ConstructorsConstructorDescriptionWsRemoteEndpointImplServer(SocketWrapperBase<?> socketWrapper, UpgradeInfo upgradeInfo, WsServerContainer serverContainer, WebConnection connection) Constructs a server-side remote endpoint. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanacquireMessagePartInProgressSemaphore(byte opCode, long timeoutExpiry) Acquire the semaphore that allows a message part to be written.protected voiddoClose()Performs the actual close operation on the underlying connection.protected voiddoWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... buffers) Writes data to the underlying connection.protected ReentrantLockgetLock()Returns the lock used for thread-safe operations.protected longReturns the timeout expiry time in milliseconds.protected final booleanisMasked()Checks if frames should be masked.protected voidonTimeout(boolean useDispatch) Handles a write timeout event.voidonWritePossible(boolean useDispatch) Called when the underlying socket is ready for writing.protected voidsetTransformation(Transformation transformation) Sets the transformation for this endpoint.protected voidupdateStats(long payloadLength) Hook for updating server side statistics.Methods inherited from class WsRemoteEndpointImplBase
close, flushBatch, getBatchingAllowed, getSendStream, getSendTimeout, getSendWriter, getSession, sendBytes, sendBytesByCompletion, sendBytesByFuture, sendObject, sendObjectByCompletion, sendObjectByFuture, sendPartialBytes, sendPartialString, sendPing, sendPong, sendString, sendStringByCompletion, sendStringByFuture, setBatchingAllowed, setEncoders, setSendTimeout, setSession
-
Constructor Details
-
WsRemoteEndpointImplServer
public WsRemoteEndpointImplServer(SocketWrapperBase<?> socketWrapper, UpgradeInfo upgradeInfo, WsServerContainer serverContainer, WebConnection connection) Constructs a server-side remote endpoint.- Parameters:
socketWrapper- the socket wrapperupgradeInfo- the upgrade informationserverContainer- the server containerconnection- the web connection
-
-
Method Details
-
isMasked
protected final boolean isMasked()Description copied from class:WsRemoteEndpointImplBaseChecks if frames should be masked.- Specified by:
isMaskedin classWsRemoteEndpointImplBase- Returns:
- true if masking is required
-
acquireMessagePartInProgressSemaphore
protected boolean acquireMessagePartInProgressSemaphore(byte opCode, long timeoutExpiry) throws InterruptedException 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 deadlock may occur in the following scenario:
- Application thread writes message using non-blocking
- Write does not complete (write logic holds message pending lock)
- Socket is added to poller (or equivalent) for write
- Client sends close message
- Container processes received close message and tries to send close message in response
- Container holds socket lock and is blocked waiting for message pending lock
- Poller fires write possible event for socket
- Container tries to process write possible event but is blocked waiting for socket lock
- Processing of the WebSocket connection is deadlocked until the original message write times out
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.
- Overrides:
acquireMessagePartInProgressSemaphorein classWsRemoteEndpointImplBase- 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
-
doWrite
Description copied from class:WsRemoteEndpointImplBaseWrites data to the underlying connection.- Specified by:
doWritein classWsRemoteEndpointImplBase- Parameters:
handler- the send handlerblockingWriteTimeoutExpiry- the timeout expiry timebuffers- the data buffers to write
-
updateStats
protected void updateStats(long payloadLength) Description copied from class:WsRemoteEndpointImplBaseHook 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).- Overrides:
updateStatsin classWsRemoteEndpointImplBase- Parameters:
payloadLength- Size of message payload
-
onWritePossible
public void onWritePossible(boolean useDispatch) Called when the underlying socket is ready for writing.- Parameters:
useDispatch- whether to use a dispatch for callback
-
doClose
protected void doClose()Description copied from class:WsRemoteEndpointImplBasePerforms the actual close operation on the underlying connection.- Specified by:
doClosein classWsRemoteEndpointImplBase
-
getTimeoutExpiry
protected long getTimeoutExpiry()Returns the timeout expiry time in milliseconds.- Returns:
- the timeout expiry time
-
onTimeout
protected void onTimeout(boolean useDispatch) Handles a write timeout event.- Parameters:
useDispatch- whether to use a dispatch for callback
-
setTransformation
Description copied from class:WsRemoteEndpointImplBaseSets the transformation for this endpoint.- Overrides:
setTransformationin classWsRemoteEndpointImplBase- Parameters:
transformation- the transformation to use
-
getLock
Description copied from class:WsRemoteEndpointImplBaseReturns the lock used for thread-safe operations.- Specified by:
getLockin classWsRemoteEndpointImplBase- Returns:
- the reentrant lock
-