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