Search in sources :

Example 1 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo 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 CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class IOState method onDisconnected.

public void onDisconnected() {
    ConnectionState event = null;
    synchronized (this) {
        if (this.state == ConnectionState.CLOSED) {
            // already closed
            return;
        }
        CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, "Disconnected");
        this.cleanClose = false;
        this.state = ConnectionState.CLOSED;
        this.closeInfo = close;
        this.inputAvailable = false;
        this.outputAvailable = false;
        this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
        event = this.state;
    }
    notifyStateListeners(event);
}
Also used : ConnectionState(org.eclipse.jetty.websocket.common.ConnectionState) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 3 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class IOState method onWriteFailure.

/**
     * The local endpoint has reached a write failure.
     * <p>
     * A low level I/O failure, or even a jetty side EndPoint close (from idle timeout) are a few scenarios
     * @param t the throwable that caused the write failure
     */
public void onWriteFailure(Throwable t) {
    ConnectionState event = null;
    synchronized (this) {
        if (this.state == ConnectionState.CLOSED) {
            // already closed
            return;
        }
        // Build out Close Reason
        String reason = "WebSocket Write Failure";
        if (t instanceof EOFException) {
            reason = "WebSocket Write EOF";
            Throwable cause = t.getCause();
            if ((cause != null) && (StringUtil.isNotBlank(cause.getMessage()))) {
                reason = "EOF: " + cause.getMessage();
            }
        } else {
            if (StringUtil.isNotBlank(t.getMessage())) {
                reason = t.getMessage();
            }
        }
        CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, reason);
        finalClose.compareAndSet(null, close);
        this.cleanClose = false;
        this.state = ConnectionState.CLOSED;
        this.inputAvailable = false;
        this.outputAvailable = false;
        this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
        event = this.state;
    }
    notifyStateListeners(event);
}
Also used : EOFException(java.io.EOFException) ConnectionState(org.eclipse.jetty.websocket.common.ConnectionState) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 4 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class IOState method onReadFailure.

/**
     * The local endpoint has reached a read failure.
     * <p>
     * This could be a normal result after a proper close handshake, or even a premature close due to a connection disconnect.
     * @param t the read failure
     */
public void onReadFailure(Throwable t) {
    ConnectionState event = null;
    synchronized (this) {
        if (this.state == ConnectionState.CLOSED) {
            // already closed
            return;
        }
        // Build out Close Reason
        String reason = "WebSocket Read Failure";
        if (t instanceof EOFException) {
            reason = "WebSocket Read EOF";
            Throwable cause = t.getCause();
            if ((cause != null) && (StringUtil.isNotBlank(cause.getMessage()))) {
                reason = "EOF: " + cause.getMessage();
            }
        } else {
            if (StringUtil.isNotBlank(t.getMessage())) {
                reason = t.getMessage();
            }
        }
        CloseInfo close = new CloseInfo(StatusCode.ABNORMAL, reason);
        finalClose.compareAndSet(null, close);
        this.cleanClose = false;
        this.state = ConnectionState.CLOSED;
        this.closeInfo = close;
        this.inputAvailable = false;
        this.outputAvailable = false;
        this.closeHandshakeSource = CloseHandshakeSource.ABNORMAL;
        event = this.state;
    }
    notifyStateListeners(event);
}
Also used : EOFException(java.io.EOFException) ConnectionState(org.eclipse.jetty.websocket.common.ConnectionState) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 5 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class TestABCase5 method testCase5_13.

/**
     * Send continuation+!fin, then text+fin (framewise)
     * @throws Exception on test failure
     */
@Test
public void testCase5_13() throws Exception {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new ContinuationFrame().setPayload("sorry").setFin(false));
    send.add(new TextFrame().setPayload("hello, world"));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new CloseInfo(StatusCode.PROTOCOL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this);
        StacklessLogging supress = new StacklessLogging(Parser.class)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.PER_FRAME);
        fuzzer.sendAndIgnoreBrokenPipe(send);
        fuzzer.expect(expect);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) ArrayList(java.util.ArrayList) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Aggregations

CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)151 Test (org.junit.Test)129 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)118 ArrayList (java.util.ArrayList)104 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)104 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)68 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)55 ByteBuffer (java.nio.ByteBuffer)48 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)30 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)27 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)19 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)17 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)14 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)10 CloseableLocalWebSocketSession (org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession)8 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)8 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)5 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4