Search in sources :

Example 1 with BatchMode

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

the class DeflateFrameExtensionTest method testCompressAndDecompressBigPayload.

@Test
public void testCompressAndDecompressBigPayload() throws Exception {
    byte[] input = new byte[1024 * 1024];
    // Make them not compressible.
    new Random().nextBytes(input);
    int maxMessageSize = (1024 * 1024) + 8192;
    DeflateFrameExtension clientExtension = new DeflateFrameExtension();
    clientExtension.setBufferPool(bufferPool);
    clientExtension.setPolicy(WebSocketPolicy.newClientPolicy());
    clientExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    clientExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    clientExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    final DeflateFrameExtension serverExtension = new DeflateFrameExtension();
    serverExtension.setBufferPool(bufferPool);
    serverExtension.setPolicy(WebSocketPolicy.newServerPolicy());
    serverExtension.getPolicy().setMaxBinaryMessageSize(maxMessageSize);
    serverExtension.getPolicy().setMaxBinaryMessageBufferSize(maxMessageSize);
    serverExtension.setConfig(ExtensionConfig.parse("deflate-frame"));
    // Chain the next element to decompress.
    clientExtension.setNextOutgoingFrames(new OutgoingFrames() {

        @Override
        public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode) {
            LOG.debug("outgoingFrame({})", frame);
            serverExtension.incomingFrame(frame);
            callback.writeSuccess();
        }
    });
    final ByteArrayOutputStream result = new ByteArrayOutputStream(input.length);
    serverExtension.setNextIncomingFrames(new IncomingFrames() {

        @Override
        public void incomingFrame(Frame frame) {
            LOG.debug("incomingFrame({})", frame);
            try {
                result.write(BufferUtil.toArray(frame.getPayload()));
            } catch (IOException x) {
                throw new RuntimeIOException(x);
            }
        }

        @Override
        public void incomingError(Throwable t) {
        }
    });
    BinaryFrame frame = new BinaryFrame();
    frame.setPayload(input);
    frame.setFin(true);
    clientExtension.outgoingFrame(frame, null, BatchMode.OFF);
    Assert.assertArrayEquals(input, result.toByteArray());
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) 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) IncomingFrames(org.eclipse.jetty.websocket.api.extensions.IncomingFrames) WriteCallback(org.eclipse.jetty.websocket.api.WriteCallback) BatchMode(org.eclipse.jetty.websocket.api.BatchMode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) Random(java.util.Random) OutgoingFrames(org.eclipse.jetty.websocket.api.extensions.OutgoingFrames) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 2 with BatchMode

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

the class WebSocketRemoteEndpoint method uncheckedSendFrame.

public void uncheckedSendFrame(WebSocketFrame frame, WriteCallback callback) {
    try {
        BatchMode batchMode = BatchMode.OFF;
        if (frame.isDataFrame())
            batchMode = getBatchMode();
        connection.getIOState().assertOutputOpen();
        outgoing.outgoingFrame(frame, callback, batchMode);
    } catch (IOException e) {
        callback.writeFailed(e);
    }
}
Also used : BatchMode(org.eclipse.jetty.websocket.api.BatchMode) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 BatchMode (org.eclipse.jetty.websocket.api.BatchMode)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Random (java.util.Random)1 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)1 WriteCallback (org.eclipse.jetty.websocket.api.WriteCallback)1 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)1 IncomingFrames (org.eclipse.jetty.websocket.api.extensions.IncomingFrames)1 OutgoingFrames (org.eclipse.jetty.websocket.api.extensions.OutgoingFrames)1 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)1 AbstractExtensionTest (org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest)1 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)1 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)1 Test (org.junit.Test)1