Search in sources :

Example 21 with BinaryFrame

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

the class JsrAsyncRemote method sendBinary.

@Override
public void sendBinary(ByteBuffer data, SendHandler handler) {
    assertMessageNotNull(data);
    assertSendHandlerNotNull(handler);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendBinary({},{})", BufferUtil.toDetailString(data), handler);
    }
    WebSocketFrame frame = new BinaryFrame().setPayload(data).setFin(true);
    jettyRemote.uncheckedSendFrame(frame, new SendHandlerWriteCallback(handler));
}
Also used : SendHandlerWriteCallback(org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame)

Example 22 with BinaryFrame

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

the class WebSocketServletRFCTest method testBinaryAggregate.

/**
     * Test that aggregation of binary frames into a single message occurs
     * @throws Exception on test failure
     */
@Test
public void testBinaryAggregate() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // Generate binary frames
        byte[] buf1 = new byte[128];
        byte[] buf2 = new byte[128];
        byte[] buf3 = new byte[128];
        Arrays.fill(buf1, (byte) 0xAA);
        Arrays.fill(buf2, (byte) 0xBB);
        Arrays.fill(buf3, (byte) 0xCC);
        WebSocketFrame bin;
        bin = new BinaryFrame().setPayload(buf1).setFin(false);
        // write buf1 (fin=false)
        client.write(bin);
        bin = new ContinuationFrame().setPayload(buf2).setFin(false);
        // write buf2 (fin=false)
        client.write(bin);
        bin = new ContinuationFrame().setPayload(buf3).setFin(true);
        // write buf3 (fin=true)
        client.write(bin);
        // Read frame echo'd back (hopefully a single binary frame)
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        Frame binmsg = frames.poll();
        int expectedSize = buf1.length + buf2.length + buf3.length;
        Assert.assertThat("BinaryFrame.payloadLength", binmsg.getPayloadLength(), is(expectedSize));
        int aaCount = 0;
        int bbCount = 0;
        int ccCount = 0;
        ByteBuffer echod = binmsg.getPayload();
        while (echod.remaining() >= 1) {
            byte b = echod.get();
            switch(b) {
                case (byte) 0xAA:
                    aaCount++;
                    break;
                case (byte) 0xBB:
                    bbCount++;
                    break;
                case (byte) 0xCC:
                    ccCount++;
                    break;
                default:
                    Assert.fail(String.format("Encountered invalid byte 0x%02X", (byte) (0xFF & b)));
            }
        }
        Assert.assertThat("Echoed data count for 0xAA", aaCount, is(buf1.length));
        Assert.assertThat("Echoed data count for 0xBB", bbCount, is(buf2.length));
        Assert.assertThat("Echoed data count for 0xCC", ccCount, is(buf3.length));
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 23 with BinaryFrame

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

the class TestABCase9 method testCase9_2_3.

/**
     * Echo 1MB binary message (1 frame)
     * @throws Exception on test failure
     */
@Test
@Stress("High I/O use")
public void testCase9_2_3() throws Exception {
    byte[] data = new byte[1 * MBYTE];
    Arrays.fill(data, (byte) 0x23);
    ByteBuffer buf = ByteBuffer.wrap(data);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new BinaryFrame().setPayload(buf));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new BinaryFrame().setPayload(clone(buf)));
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect, 4, TimeUnit.SECONDS);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test) Stress(org.eclipse.jetty.toolchain.test.annotation.Stress)

Example 24 with BinaryFrame

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

the class TestABCase9 method testCase9_2_5.

/**
     * Echo 8MB binary message (1 frame)
     * @throws Exception on test failure
     */
@Test
@Stress("High I/O use")
public void testCase9_2_5() throws Exception {
    byte[] data = new byte[8 * MBYTE];
    Arrays.fill(data, (byte) 0x25);
    ByteBuffer buf = ByteBuffer.wrap(data);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new BinaryFrame().setPayload(buf));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new BinaryFrame().setPayload(clone(buf)));
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect, 16, TimeUnit.SECONDS);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test) Stress(org.eclipse.jetty.toolchain.test.annotation.Stress)

Example 25 with BinaryFrame

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

the class TestABCase9 method testCase9_2_6.

/**
     * Echo 16MB binary message (1 frame)
     * @throws Exception on test failure
     */
@Test
@Stress("High I/O use")
public void testCase9_2_6() throws Exception {
    byte[] data = new byte[16 * MBYTE];
    Arrays.fill(data, (byte) 0x26);
    ByteBuffer buf = ByteBuffer.wrap(data);
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new BinaryFrame().setPayload(buf));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    List<WebSocketFrame> expect = new ArrayList<>();
    expect.add(new BinaryFrame().setPayload(clone(buf)));
    expect.add(new CloseInfo(StatusCode.NORMAL).asFrame());
    try (Fuzzer fuzzer = new Fuzzer(this)) {
        fuzzer.connect();
        fuzzer.setSendMode(Fuzzer.SendMode.BULK);
        fuzzer.send(send);
        fuzzer.expect(expect, 32, TimeUnit.SECONDS);
    }
}
Also used : Fuzzer(org.eclipse.jetty.websocket.common.test.Fuzzer) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) ArrayList(java.util.ArrayList) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test) Stress(org.eclipse.jetty.toolchain.test.annotation.Stress)

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