Search in sources :

Example 26 with BinaryFrame

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

Example 27 with BinaryFrame

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

the class EventDriverTest method testAnnotated_Frames.

@Test
public void testAnnotated_Frames() throws Exception {
    AnnotatedFramesSocket socket = new AnnotatedFramesSocket();
    EventDriver driver = wrap(socket);
    try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container, testname, driver)) {
        conn.open();
        driver.incomingFrame(new PingFrame().setPayload("PING"));
        driver.incomingFrame(new TextFrame().setPayload("Text Me"));
        driver.incomingFrame(new BinaryFrame().setPayload("Hello Bin"));
        driver.incomingFrame(new CloseInfo(StatusCode.SHUTDOWN, "testcase").asFrame());
        socket.capture.assertEventCount(6);
        socket.capture.pop().assertEventStartsWith("onConnect(");
        socket.capture.pop().assertEventStartsWith("onFrame(PING[");
        socket.capture.pop().assertEventStartsWith("onFrame(TEXT[");
        socket.capture.pop().assertEventStartsWith("onFrame(BINARY[");
        socket.capture.pop().assertEventStartsWith("onFrame(CLOSE[");
        socket.capture.pop().assertEventStartsWith("onClose(1001,");
    }
}
Also used : AnnotatedFramesSocket(examples.AnnotatedFramesSocket) LocalWebSocketSession(org.eclipse.jetty.websocket.common.io.LocalWebSocketSession) CloseableLocalWebSocketSession(org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) PingFrame(org.eclipse.jetty.websocket.common.frames.PingFrame) CloseableLocalWebSocketSession(org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 28 with BinaryFrame

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

the class GeneratorTest method testWindowedGenerateWithMasking.

@Test
public void testWindowedGenerateWithMasking() {
    // A decent sized frame, with masking
    byte[] payload = new byte[10240];
    Arrays.fill(payload, (byte) 0x55);
    byte[] mask = new byte[] { 0x2A, (byte) 0xF0, 0x0F, 0x00 };
    WebSocketFrame frame = new BinaryFrame().setPayload(payload);
    // masking!
    frame.setMask(mask);
    // Generate
    int windowSize = 2929;
    WindowHelper helper = new WindowHelper(windowSize);
    ByteBuffer completeBuffer = helper.generateWindowed(frame);
    // Validate
    int expectedHeaderSize = 8;
    int expectedSize = payload.length + expectedHeaderSize;
    int expectedParts = 1;
    helper.assertTotalParts(expectedParts);
    helper.assertTotalBytes(payload.length + expectedHeaderSize);
    Assert.assertThat("Generated Buffer", completeBuffer.remaining(), is(expectedSize));
    // Parse complete buffer.
    WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
    Parser parser = new UnitParser(policy);
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuffer);
    // Assert validity of frame
    WebSocketFrame actual = capture.getFrames().poll();
    Assert.assertThat("Frame.opcode", actual.getOpCode(), is(OpCode.BINARY));
    Assert.assertThat("Frame.payloadLength", actual.getPayloadLength(), is(payload.length));
    // Validate payload contents for proper masking
    ByteBuffer actualData = actual.getPayload().slice();
    Assert.assertThat("Frame.payload.remaining", actualData.remaining(), is(payload.length));
    while (actualData.remaining() > 0) {
        Assert.assertThat("Actual.payload[" + actualData.position() + "]", actualData.get(), is((byte) 0x55));
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) ByteBuffer(java.nio.ByteBuffer) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) Test(org.junit.Test)

Example 29 with BinaryFrame

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

the class TestABCase1_2 method testGenerate127ByteBinaryCase1_2_4.

@Test
public void testGenerate127ByteBinaryCase1_2_4() {
    int length = 127;
    ByteBuffer bb = ByteBuffer.allocate(length);
    for (int i = 0; i < length; ++i) {
        bb.put("*".getBytes());
    }
    bb.flip();
    WebSocketFrame binaryFrame = new BinaryFrame().setPayload(bb);
    ByteBuffer actual = UnitGenerator.generate(binaryFrame);
    ByteBuffer expected = ByteBuffer.allocate(length + 5);
    expected.put(new byte[] { (byte) 0x82 });
    // no masking
    byte b = 0x00;
    b |= length & 0x7E;
    expected.put(b);
    //expected.put((byte)((length>>8) & 0xFF));
    //expected.put((byte)(length & 0xFF));
    expected.putShort((short) length);
    for (int i = 0; i < length; ++i) {
        expected.put("*".getBytes());
    }
    BufferUtil.flipToFlush(expected, 0);
    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
Also used : BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with BinaryFrame

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

the class TestABCase1_2 method testGenerateEmptyBinaryCase1_2_1.

@Test
public void testGenerateEmptyBinaryCase1_2_1() {
    WebSocketFrame binaryFrame = new BinaryFrame().setPayload(new byte[] {});
    ByteBuffer actual = UnitGenerator.generate(binaryFrame);
    ByteBuffer expected = ByteBuffer.allocate(5);
    expected.put(new byte[] { (byte) 0x82, (byte) 0x00 });
    BufferUtil.flipToFlush(expected, 0);
    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
}
Also used : BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)33 Test (org.junit.Test)29 ByteBuffer (java.nio.ByteBuffer)25 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)25 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)16 ArrayList (java.util.ArrayList)15 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)15 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)5 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)4 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)4 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)3 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)2 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)2 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)2 AnnotatedFramesSocket (examples.AnnotatedFramesSocket)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Random (java.util.Random)1 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)1 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)1