Search in sources :

Example 1 with DataFrame

use of org.eclipse.jetty.websocket.common.frames.DataFrame in project jetty.project by eclipse.

the class WebSocketRemoteEndpoint method sendPartialString.

@Override
public void sendPartialString(String fragment, boolean isLast) throws IOException {
    boolean first = lockMsg(MsgType.PARTIAL_TEXT);
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("sendPartialString({}, {})", fragment, isLast);
        }
        DataFrame frame = first ? new TextFrame() : new ContinuationFrame();
        frame.setPayload(BufferUtil.toBuffer(fragment, StandardCharsets.UTF_8));
        frame.setFin(isLast);
        blockingWrite(frame);
    } finally {
        if (isLast)
            unlockMsg(MsgType.PARTIAL_TEXT);
    }
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) DataFrame(org.eclipse.jetty.websocket.common.frames.DataFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame)

Example 2 with DataFrame

use of org.eclipse.jetty.websocket.common.frames.DataFrame in project jetty.project by eclipse.

the class CompressExtension method forwardIncoming.

protected void forwardIncoming(Frame frame, ByteAccumulator accumulator) {
    DataFrame newFrame = new DataFrame(frame);
    // Unset RSV1 since it's not compressed anymore.
    newFrame.setRsv1(false);
    ByteBuffer buffer = getBufferPool().acquire(accumulator.getLength(), false);
    try {
        BufferUtil.flipToFill(buffer);
        accumulator.transferTo(buffer);
        newFrame.setPayload(buffer);
        nextIncomingFrame(newFrame);
    } finally {
        getBufferPool().release(buffer);
    }
}
Also used : DataFrame(org.eclipse.jetty.websocket.common.frames.DataFrame) ByteBuffer(java.nio.ByteBuffer)

Example 3 with DataFrame

use of org.eclipse.jetty.websocket.common.frames.DataFrame in project jetty.project by eclipse.

the class ParserTest method testParseCase6_2_3.

/**
     * Similar to the server side 6.2.3 testcase. Lots of small 1 byte UTF8 Text frames, representing 1 overall text message.
     */
@Test
public void testParseCase6_2_3() {
    String utf8 = "Hello-습@쎟쎤쎼쎠쎡-UTF-8!!";
    byte[] msg = StringUtil.getUtf8Bytes(utf8);
    List<WebSocketFrame> send = new ArrayList<>();
    int textCount = 0;
    int continuationCount = 0;
    int len = msg.length;
    boolean continuation = false;
    byte[] mini;
    for (int i = 0; i < len; i++) {
        DataFrame frame = null;
        if (continuation) {
            frame = new ContinuationFrame();
            continuationCount++;
        } else {
            frame = new TextFrame();
            textCount++;
        }
        mini = new byte[1];
        mini[0] = msg[i];
        frame.setPayload(ByteBuffer.wrap(mini));
        boolean isLast = (i >= (len - 1));
        frame.setFin(isLast);
        send.add(frame);
        continuation = true;
    }
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuf);
    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, textCount);
    capture.assertHasFrame(OpCode.CONTINUATION, continuationCount);
    capture.assertHasFrame(OpCode.CLOSE, 1);
}
Also used : ArrayList(java.util.ArrayList) DataFrame(org.eclipse.jetty.websocket.common.frames.DataFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) Test(org.junit.Test)

Example 4 with DataFrame

use of org.eclipse.jetty.websocket.common.frames.DataFrame in project jetty.project by eclipse.

the class TestABCase6 method fragmentText.

/**
     * Split a message byte array into a series of fragments (frames + continuations) of 1 byte message contents each.
     * @param frames the frames
     * @param msg the message
     */
protected void fragmentText(List<WebSocketFrame> frames, byte[] msg) {
    int len = msg.length;
    boolean continuation = false;
    byte[] mini;
    for (int i = 0; i < len; i++) {
        DataFrame frame = null;
        if (continuation) {
            frame = new ContinuationFrame();
        } else {
            frame = new TextFrame();
        }
        mini = new byte[1];
        mini[0] = msg[i];
        frame.setPayload(ByteBuffer.wrap(mini));
        boolean isLast = (i >= (len - 1));
        frame.setFin(isLast);
        frames.add(frame);
        continuation = true;
    }
}
Also used : TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) DataFrame(org.eclipse.jetty.websocket.common.frames.DataFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame)

Example 5 with DataFrame

use of org.eclipse.jetty.websocket.common.frames.DataFrame in project jetty.project by eclipse.

the class WebSocketRemoteEndpoint method sendPartialBytes.

@Override
public void sendPartialBytes(ByteBuffer fragment, boolean isLast) throws IOException {
    boolean first = lockMsg(MsgType.PARTIAL_BINARY);
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("sendPartialBytes({}, {})", BufferUtil.toDetailString(fragment), isLast);
        }
        DataFrame frame = first ? new BinaryFrame() : new ContinuationFrame();
        frame.setPayload(fragment);
        frame.setFin(isLast);
        blockingWrite(frame);
    } finally {
        if (isLast)
            unlockMsg(MsgType.PARTIAL_BINARY);
    }
}
Also used : BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) DataFrame(org.eclipse.jetty.websocket.common.frames.DataFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame)

Aggregations

DataFrame (org.eclipse.jetty.websocket.common.frames.DataFrame)5 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)4 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)1 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)1 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)1 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)1 Test (org.junit.Test)1