Search in sources :

Example 11 with CloseReason

use of jakarta.websocket.CloseReason in project spring-framework by spring-projects.

the class StandardWebSocketHandlerAdapterTests method onClose.

@Test
public void onClose() throws Throwable {
    this.adapter.onClose(this.session, new CloseReason(CloseCodes.NORMAL_CLOSURE, "reason"));
    verify(this.webSocketHandler).afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL.withReason("reason"));
}
Also used : CloseReason(jakarta.websocket.CloseReason) Test(org.junit.jupiter.api.Test)

Example 12 with CloseReason

use of jakarta.websocket.CloseReason in project tomcat by apache.

the class WsSession method checkExpiration.

protected void checkExpiration() {
    // Local copies to ensure consistent behaviour during method execution
    long timeout = maxIdleTimeout;
    long timeoutRead = getMaxIdleTimeoutRead();
    long timeoutWrite = getMaxIdleTimeoutWrite();
    long currentTime = System.currentTimeMillis();
    String key = null;
    if (timeoutRead > 0 && (currentTime - lastActiveRead) > timeoutRead) {
        key = "wsSession.timeoutRead";
    } else if (timeoutWrite > 0 && (currentTime - lastActiveWrite) > timeoutWrite) {
        key = "wsSession.timeoutWrite";
    } else if (timeout > 0 && (currentTime - lastActiveRead) > timeout && (currentTime - lastActiveWrite) > timeout) {
        key = "wsSession.timeout";
    }
    if (key != null) {
        String msg = sm.getString(key, getId());
        if (log.isDebugEnabled()) {
            log.debug(msg);
        }
        doClose(new CloseReason(CloseCodes.GOING_AWAY, msg), new CloseReason(CloseCodes.CLOSED_ABNORMALLY, msg));
    }
}
Also used : CloseReason(jakarta.websocket.CloseReason)

Example 13 with CloseReason

use of jakarta.websocket.CloseReason in project tomcat by apache.

the class WsWebSocketContainer method destroy.

/**
 * Cleans up the resources still in use by WebSocket sessions created from
 * this container. This includes closing sessions and cancelling
 * {@link Future}s associated with blocking read/writes.
 */
public void destroy() {
    CloseReason cr = new CloseReason(CloseCodes.GOING_AWAY, sm.getString("wsWebSocketContainer.shutdown"));
    for (WsSession session : sessions.keySet()) {
        try {
            session.close(cr);
        } catch (IOException ioe) {
            log.debug(sm.getString("wsWebSocketContainer.sessionCloseFail", session.getId()), ioe);
        }
    }
    // registered with it
    if (asynchronousChannelGroup != null) {
        synchronized (asynchronousChannelGroupLock) {
            if (asynchronousChannelGroup != null) {
                AsyncChannelGroupUtil.unregister();
                asynchronousChannelGroup = null;
            }
        }
    }
}
Also used : CloseReason(jakarta.websocket.CloseReason) IOException(java.io.IOException)

Example 14 with CloseReason

use of jakarta.websocket.CloseReason in project tomcat by apache.

the class WsRemoteEndpointImplBase method sendMessageBlock.

private void sendMessageBlock(byte opCode, ByteBuffer payload, boolean last, long timeoutExpiry) throws IOException {
    wsSession.updateLastActiveWrite();
    BlockingSendHandler bsh = new BlockingSendHandler();
    List<MessagePart> messageParts = new ArrayList<>();
    messageParts.add(new MessagePart(last, 0, opCode, payload, bsh, bsh, timeoutExpiry));
    messageParts = transformation.sendMessagePart(messageParts);
    // return.
    if (messageParts.size() == 0) {
        return;
    }
    long timeout = timeoutExpiry - System.currentTimeMillis();
    try {
        if (!messagePartInProgress.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
            String msg = sm.getString("wsRemoteEndpoint.acquireTimeout");
            wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, msg), new CloseReason(CloseCodes.CLOSED_ABNORMALLY, msg), true);
            throw new SocketTimeoutException(msg);
        }
    } catch (InterruptedException e) {
        String msg = sm.getString("wsRemoteEndpoint.sendInterrupt");
        wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, msg), new CloseReason(CloseCodes.CLOSED_ABNORMALLY, msg), true);
        throw new IOException(msg, e);
    }
    for (MessagePart mp : messageParts) {
        try {
            writeMessagePart(mp);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            messagePartInProgress.release();
            wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, t.getMessage()), new CloseReason(CloseCodes.CLOSED_ABNORMALLY, t.getMessage()), true);
            throw t;
        }
        if (!bsh.getSendResult().isOK()) {
            messagePartInProgress.release();
            Throwable t = bsh.getSendResult().getException();
            wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, t.getMessage()), new CloseReason(CloseCodes.CLOSED_ABNORMALLY, t.getMessage()), true);
            throw new IOException(t);
        }
        // The BlockingSendHandler doesn't call end message so update the
        // flags.
        fragmented = nextFragmented;
        text = nextText;
    }
    if (payload != null) {
        payload.clear();
    }
    endMessage(null, null);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) CloseReason(jakarta.websocket.CloseReason) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 15 with CloseReason

use of jakarta.websocket.CloseReason in project tomcat by apache.

the class WsFrameBase method processRemainingHeader.

/**
 * @return <code>true</code> if sufficient data was present to complete the
 *         processing of the header
 */
private boolean processRemainingHeader() throws IOException {
    // Ignore the 2 bytes already read. 4 for the mask
    int headerLength;
    if (isMasked()) {
        headerLength = 4;
    } else {
        headerLength = 0;
    }
    // Add additional bytes depending on length
    if (payloadLength == 126) {
        headerLength += 2;
    } else if (payloadLength == 127) {
        headerLength += 8;
    }
    if (inputBuffer.remaining() < headerLength) {
        return false;
    }
    // Calculate new payload length if necessary
    if (payloadLength == 126) {
        payloadLength = byteArrayToLong(inputBuffer.array(), inputBuffer.arrayOffset() + inputBuffer.position(), 2);
        inputBuffer.position(inputBuffer.position() + 2);
    } else if (payloadLength == 127) {
        payloadLength = byteArrayToLong(inputBuffer.array(), inputBuffer.arrayOffset() + inputBuffer.position(), 8);
        // the resulting payload length will be negative so test for that.
        if (payloadLength < 0) {
            throw new WsIOException(new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.payloadMsbInvalid")));
        }
        inputBuffer.position(inputBuffer.position() + 8);
    }
    if (Util.isControl(opCode)) {
        if (payloadLength > 125) {
            throw new WsIOException(new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.controlPayloadTooBig", Long.valueOf(payloadLength))));
        }
        if (!fin) {
            throw new WsIOException(new CloseReason(CloseCodes.PROTOCOL_ERROR, sm.getString("wsFrame.controlNoFin")));
        }
    }
    if (isMasked()) {
        inputBuffer.get(mask, 0, 4);
    }
    state = State.DATA;
    return true;
}
Also used : CloseReason(jakarta.websocket.CloseReason)

Aggregations

CloseReason (jakarta.websocket.CloseReason)15 IOException (java.io.IOException)6 MessageHandler (jakarta.websocket.MessageHandler)2 ByteBuffer (java.nio.ByteBuffer)2 CoderResult (java.nio.charset.CoderResult)2 Cookie (jakarta.servlet.http.Cookie)1 Endpoint (jakarta.websocket.Endpoint)1 PongMessage (jakarta.websocket.PongMessage)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1 Buffer (java.nio.Buffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 WsIOException (org.apache.tomcat.websocket.WsIOException)1 JSR356WebSocket (org.atmosphere.container.version.JSR356WebSocket)1 AtmosphereRequestImpl (org.atmosphere.cpr.AtmosphereRequestImpl)1