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());
}
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);
}
}
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);
}
}
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);
}
}
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());
}
}
Aggregations