Search in sources :

Example 1 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class AsyncWebSocketHttpServerExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    final PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
    final ByteBuffer buffer = pooled.getBuffer();
    final StreamSourceChannel channel = exchange.getRequestChannel();
    int res;
    for (; ; ) {
        try {
            res = channel.read(buffer);
            if (res == -1) {
                return new FinishedIoFuture<>(data.toByteArray());
            } else if (res == 0) {
                //callback
                final FutureResult<byte[]> future = new FutureResult<>();
                channel.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {

                    @Override
                    public void handleEvent(final StreamSourceChannel channel) {
                        int res;
                        try {
                            res = channel.read(buffer);
                            if (res == -1) {
                                future.setResult(data.toByteArray());
                                channel.suspendReads();
                                return;
                            } else if (res == 0) {
                                return;
                            } else {
                                buffer.flip();
                                while (buffer.hasRemaining()) {
                                    data.write(buffer.get());
                                }
                                buffer.clear();
                            }
                        } catch (IOException e) {
                            future.setException(e);
                        }
                    }
                });
                channel.resumeReads();
                return future.getIoFuture();
            } else {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    data.write(buffer.get());
                }
                buffer.clear();
            }
        } catch (IOException e) {
            final FutureResult<byte[]> future = new FutureResult<>();
            future.setException(e);
            return future.getIoFuture();
        }
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) FinishedIoFuture(org.xnio.FinishedIoFuture) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 2 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class AsyncWebSocketHttpServerExchange method sendData.

@Override
public IoFuture<Void> sendData(final ByteBuffer data) {
    if (sender == null) {
        this.sender = exchange.getResponseSender();
    }
    final FutureResult<Void> future = new FutureResult<>();
    sender.send(data, new IoCallback() {

        @Override
        public void onComplete(final HttpServerExchange exchange, final Sender sender) {
            future.setResult(null);
        }

        @Override
        public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) {
            UndertowLogger.REQUEST_IO_LOGGER.ioException(exception);
            future.setException(exception);
        }
    });
    return future.getIoFuture();
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Sender(io.undertow.io.Sender) FutureResult(org.xnio.FutureResult) IoCallback(io.undertow.io.IoCallback) IOException(java.io.IOException)

Example 3 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class BlockingWebSocketHttpServerExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[1024];
        int r;
        while ((r = in.read(buf)) != -1) {
            data.write(buf, 0, r);
        }
        return new FinishedIoFuture<>(data.toByteArray());
    } catch (IOException e) {
        final FutureResult<byte[]> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
Also used : FinishedIoFuture(org.xnio.FinishedIoFuture) FutureResult(org.xnio.FutureResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class AbstractWebSocketServerTest method testBinary.

@Test
public void testBinary() throws Exception {
    if (getVersion() == WebSocketVersion.V00) {
        // ignore 00 tests for now
        return;
    }
    final AtomicBoolean connected = new AtomicBoolean(false);
    DefaultServer.setRootHandler(new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
            connected.set(true);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
                    final Pooled<ByteBuffer[]> data = message.getData();
                    WebSockets.sendBinary(data.getResource(), channel, new WebSocketCallback<Void>() {

                        @Override
                        public void complete(WebSocketChannel channel, Void context) {
                            data.close();
                        }

                        @Override
                        public void onError(WebSocketChannel channel, Void context, Throwable throwable) {
                            data.close();
                        }
                    });
                }
            });
            channel.resumeReceives();
        }
    }));
    final FutureResult latch = new FutureResult();
    final byte[] payload = "payload".getBytes();
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) Pooled(org.xnio.Pooled) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) WebSocketCallback(io.undertow.websockets.core.WebSocketCallback) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) FrameChecker(io.undertow.websockets.utils.FrameChecker) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Test(org.junit.Test)

Example 5 with FutureResult

use of org.xnio.FutureResult in project undertow by undertow-io.

the class DynamicEndpointTest method testDynamicAnnotatedEndpoint.

@Test
public void testDynamicAnnotatedEndpoint() throws Exception {
    final byte[] payload = "hello".getBytes();
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/dynamicEchoEndpoint?annotated=true"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "opened:true /dynamicEchoEndpoint hello".getBytes(), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) URI(java.net.URI) Test(org.junit.Test)

Aggregations

FutureResult (org.xnio.FutureResult)33 URI (java.net.URI)28 Test (org.junit.Test)28 FrameChecker (io.undertow.websockets.utils.FrameChecker)27 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)15 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)14 UndertowSession (io.undertow.websockets.jsr.UndertowSession)14 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)14 IOException (java.io.IOException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 Endpoint (javax.websocket.Endpoint)14 EndpointConfig (javax.websocket.EndpointConfig)14 Session (javax.websocket.Session)14 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)14 MessageHandler (javax.websocket.MessageHandler)11 ByteBuffer (java.nio.ByteBuffer)9 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)7 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)5