Search in sources :

Example 1 with RuntimeIOException

use of org.eclipse.jetty.io.RuntimeIOException 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 RuntimeIOException

use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.

the class MyEchoSocket method onWebSocketText.

@Override
public void onWebSocketText(String message) {
    if (isNotConnected()) {
        return;
    }
    try {
        // echo the data back
        RemoteEndpoint remote = getRemote();
        remote.sendString(message);
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Example 3 with RuntimeIOException

use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.

the class JettyEchoSocket method onWebSocketBinary.

@Override
public void onWebSocketBinary(byte[] payload, int offset, int len) {
    if (isNotConnected())
        return;
    try {
        RemoteEndpoint remote = getRemote();
        remote.sendBytes(BufferUtil.toBuffer(payload, offset, len), null);
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
    } catch (IOException x) {
        throw new RuntimeIOException(x);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Example 4 with RuntimeIOException

use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.

the class JettyEchoSocket method onWebSocketText.

@Override
public void onWebSocketText(String message) {
    if (isNotConnected())
        return;
    try {
        RemoteEndpoint remote = getRemote();
        remote.sendString(message, null);
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
    } catch (IOException x) {
        throw new RuntimeIOException(x);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Example 5 with RuntimeIOException

use of org.eclipse.jetty.io.RuntimeIOException in project jetty.project by eclipse.

the class CloseTest method testClientAbruptlyClosesConnection.

@Test
public void testClientAbruptlyClosesConnection() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    final AtomicReference<Session> sessionRef = new AtomicReference<>();
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            try {
                sessionRef.set(stream.getSession());
                MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
                // Reply with HEADERS.
                stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                closeLatch.await(5, TimeUnit.SECONDS);
                return null;
            } catch (InterruptedException x) {
                return null;
            }
        }
    });
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            output.write(BufferUtil.toArray(buffer));
        }
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                try {
                    // Close the connection just after
                    // receiving the response headers.
                    client.close();
                    closeLatch.countDown();
                } catch (IOException x) {
                    throw new RuntimeIOException(x);
                }
            }
        }, 4096, 8192);
        parseResponse(client, parser);
        // We need to give some time to the server to receive and process the TCP FIN.
        Thread.sleep(1000);
        Session session = sessionRef.get();
        Assert.assertTrue(session.isClosed());
        Assert.assertTrue(((HTTP2Session) session).isDisconnected());
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)9 IOException (java.io.IOException)8 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)3 Test (org.junit.Test)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ReadListener (javax.servlet.ReadListener)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpFields (org.eclipse.jetty.http.HttpFields)1 MetaData (org.eclipse.jetty.http.MetaData)1 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)1 Session (org.eclipse.jetty.http2.api.Session)1