Search in sources :

Example 1 with CloseException

use of org.eclipse.jetty.websocket.api.CloseException in project jetty.project by eclipse.

the class AbstractEventDriver method incomingFrame.

@Override
public void incomingFrame(Frame frame) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("incomingFrame({})", frame);
    }
    try {
        onFrame(frame);
        byte opcode = frame.getOpCode();
        switch(opcode) {
            case OpCode.CLOSE:
                {
                    boolean validate = true;
                    CloseFrame closeframe = (CloseFrame) frame;
                    CloseInfo close = new CloseInfo(closeframe, validate);
                    // process handshake
                    session.getConnection().getIOState().onCloseRemote(close);
                    return;
                }
            case OpCode.PING:
                {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("PING: {}", BufferUtil.toDetailString(frame.getPayload()));
                    }
                    ByteBuffer pongBuf;
                    if (frame.hasPayload()) {
                        pongBuf = ByteBuffer.allocate(frame.getPayload().remaining());
                        BufferUtil.put(frame.getPayload().slice(), pongBuf);
                        BufferUtil.flipToFlush(pongBuf, 0);
                    } else {
                        pongBuf = ByteBuffer.allocate(0);
                    }
                    onPing(frame.getPayload());
                    session.getRemote().sendPong(pongBuf);
                    break;
                }
            case OpCode.PONG:
                {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("PONG: {}", BufferUtil.toDetailString(frame.getPayload()));
                    }
                    onPong(frame.getPayload());
                    break;
                }
            case OpCode.BINARY:
                {
                    onBinaryFrame(frame.getPayload(), frame.isFin());
                    return;
                }
            case OpCode.TEXT:
                {
                    onTextFrame(frame.getPayload(), frame.isFin());
                    return;
                }
            case OpCode.CONTINUATION:
                {
                    onContinuationFrame(frame.getPayload(), frame.isFin());
                    return;
                }
            default:
                {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Unhandled OpCode: {}", opcode);
                }
        }
    } catch (NotUtf8Exception e) {
        terminateConnection(StatusCode.BAD_PAYLOAD, e.getMessage());
    } catch (CloseException e) {
        terminateConnection(e.getStatusCode(), e.getMessage());
    } catch (Throwable t) {
        unhandled(t);
    }
}
Also used : CloseException(org.eclipse.jetty.websocket.api.CloseException) CloseFrame(org.eclipse.jetty.websocket.common.frames.CloseFrame) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) ByteBuffer(java.nio.ByteBuffer) NotUtf8Exception(org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception)

Example 2 with CloseException

use of org.eclipse.jetty.websocket.api.CloseException in project jetty.project by eclipse.

the class WebSocketSession method open.

/**
     * Open/Activate the session
     */
public void open() {
    if (LOG_OPEN.isDebugEnabled())
        LOG_OPEN.debug("[{}] {}.open()", policy.getBehavior(), this.getClass().getSimpleName());
    if (remote != null) {
        // already opened
        return;
    }
    try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(classLoader)) {
        // Upgrade success
        connection.getIOState().onConnected();
        // Connect remote
        remote = remoteEndpointFactory.newRemoteEndpoint(connection, outgoingHandler, getBatchMode());
        if (LOG_OPEN.isDebugEnabled())
            LOG_OPEN.debug("[{}] {}.open() remote={}", policy.getBehavior(), this.getClass().getSimpleName(), remote);
        // Open WebSocket
        websocket.openSession(this);
        // Open connection
        connection.getIOState().onOpened();
        if (LOG.isDebugEnabled()) {
            LOG.debug("open -> {}", dump());
        }
        if (openFuture != null) {
            openFuture.complete(this);
        }
    } catch (CloseException ce) {
        LOG.warn(ce);
        close(ce.getStatusCode(), ce.getMessage());
    } catch (Throwable t) {
        LOG.warn(t);
        // Exception on end-user WS-Endpoint.
        // Fast-fail & close connection with reason.
        int statusCode = StatusCode.SERVER_ERROR;
        if (policy.getBehavior() == WebSocketBehavior.CLIENT) {
            statusCode = StatusCode.POLICY_VIOLATION;
        }
        close(statusCode, t.getMessage());
    }
}
Also used : CloseException(org.eclipse.jetty.websocket.api.CloseException) ThreadClassLoaderScope(org.eclipse.jetty.util.thread.ThreadClassLoaderScope)

Example 3 with CloseException

use of org.eclipse.jetty.websocket.api.CloseException in project jetty.project by eclipse.

the class AbstractWebSocketConnection method readParse.

private ReadMode readParse(ByteBuffer buffer) {
    EndPoint endPoint = getEndPoint();
    try {
        // Process the content from the Endpoint next
        while (// TODO: should this honor the LogicalConnection.suspend() ?
        true) {
            int filled = endPoint.fill(buffer);
            if (filled < 0) {
                LOG.debug("read - EOF Reached (remote: {})", getRemoteAddress());
                ioState.onReadFailure(new EOFException("Remote Read EOF"));
                return ReadMode.EOF;
            } else if (filled == 0) {
                // Done reading, wait for next onFillable
                return ReadMode.PARSE;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Filled {} bytes - {}", filled, BufferUtil.toDetailString(buffer));
            }
            parser.parse(buffer);
        }
    } catch (IOException e) {
        LOG.warn(e);
        close(StatusCode.PROTOCOL, e.getMessage());
        return ReadMode.DISCARD;
    } catch (CloseException e) {
        LOG.debug(e);
        close(e.getStatusCode(), e.getMessage());
        return ReadMode.DISCARD;
    } catch (Throwable t) {
        LOG.warn(t);
        close(StatusCode.ABNORMAL, t.getMessage());
        // TODO: should probably only switch to discard if a non-ws-endpoint error
        return ReadMode.DISCARD;
    }
}
Also used : CloseException(org.eclipse.jetty.websocket.api.CloseException) EOFException(java.io.EOFException) EndPoint(org.eclipse.jetty.io.EndPoint) IOException(java.io.IOException) EndPoint(org.eclipse.jetty.io.EndPoint)

Aggregations

CloseException (org.eclipse.jetty.websocket.api.CloseException)3 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 EndPoint (org.eclipse.jetty.io.EndPoint)1 NotUtf8Exception (org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception)1 ThreadClassLoaderScope (org.eclipse.jetty.util.thread.ThreadClassLoaderScope)1 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)1 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)1