use of org.xnio.ChannelListener in project undertow by undertow-io.
the class ServerSentEventHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/event-stream; charset=UTF-8");
exchange.setPersistent(false);
final StreamSinkChannel sink = exchange.getResponseChannel();
if (!sink.flush()) {
sink.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {
@Override
public void handleEvent(StreamSinkChannel channel) {
handleConnect(channel, exchange);
}
}, new ChannelExceptionHandler<StreamSinkChannel>() {
@Override
public void handleException(StreamSinkChannel channel, IOException exception) {
IoUtils.safeClose(exchange.getConnection());
}
}));
sink.resumeWrites();
} else {
exchange.dispatch(exchange.getIoThread(), new Runnable() {
@Override
public void run() {
handleConnect(sink, exchange);
}
});
}
}
use of org.xnio.ChannelListener in project undertow by undertow-io.
the class HttpContinue method internalSendContinueResponse.
private static void internalSendContinueResponse(final HttpServerExchange exchange, final IoCallback callback) {
if (exchange.getAttachment(ALREADY_SENT) != null) {
callback.onComplete(exchange, null);
return;
}
HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
exchange.putAttachment(ALREADY_SENT, true);
newExchange.setStatusCode(StatusCodes.CONTINUE);
newExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, 0);
final StreamSinkChannel responseChannel = newExchange.getResponseChannel();
try {
responseChannel.shutdownWrites();
if (!responseChannel.flush()) {
responseChannel.getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() {
@Override
public void handleEvent(StreamSinkChannel channel) {
channel.suspendWrites();
exchange.dispatch(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
callback.onComplete(exchange, null);
}
});
}
}, new ChannelExceptionHandler<Channel>() {
@Override
public void handleException(Channel channel, final IOException e) {
exchange.dispatch(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
callback.onException(exchange, null, e);
}
});
}
}));
responseChannel.resumeWrites();
exchange.dispatch();
} else {
callback.onComplete(exchange, null);
}
} catch (IOException e) {
callback.onException(exchange, null, e);
}
}
use of org.xnio.ChannelListener in project undertow by undertow-io.
the class BufferedTextMessage method read.
public void read(final StreamSourceFrameChannel channel, final WebSocketCallback<BufferedTextMessage> callback) {
PooledByteBuffer pooled = channel.getWebSocketChannel().getBufferPool().allocate();
final ByteBuffer buffer = pooled.getBuffer();
try {
try {
for (; ; ) {
int res = channel.read(buffer);
if (res == -1) {
this.complete = true;
buffer.flip();
data.write(buffer);
callback.complete(channel.getWebSocketChannel(), this);
return;
} else if (res == 0) {
buffer.flip();
if (buffer.hasRemaining()) {
data.write(buffer);
if (!bufferFullMessage) {
callback.complete(channel.getWebSocketChannel(), this);
}
}
channel.getReadSetter().set(new ChannelListener<StreamSourceFrameChannel>() {
@Override
public void handleEvent(StreamSourceFrameChannel channel) {
if (complete) {
return;
}
PooledByteBuffer pooled = channel.getWebSocketChannel().getBufferPool().allocate();
final ByteBuffer buffer = pooled.getBuffer();
try {
try {
for (; ; ) {
int res = channel.read(buffer);
if (res == -1) {
checkMaxSize(channel, res);
buffer.flip();
data.write(buffer);
complete = true;
callback.complete(channel.getWebSocketChannel(), BufferedTextMessage.this);
return;
} else if (res == 0) {
buffer.flip();
if (buffer.hasRemaining()) {
data.write(buffer);
if (!bufferFullMessage) {
callback.complete(channel.getWebSocketChannel(), BufferedTextMessage.this);
}
}
return;
}
if (!buffer.hasRemaining()) {
buffer.flip();
data.write(buffer);
buffer.clear();
if (!bufferFullMessage) {
callback.complete(channel.getWebSocketChannel(), BufferedTextMessage.this);
}
}
}
} catch (IOException e) {
callback.onError(channel.getWebSocketChannel(), BufferedTextMessage.this, e);
}
} finally {
pooled.close();
}
}
});
channel.resumeReads();
return;
}
checkMaxSize(channel, res);
if (!buffer.hasRemaining()) {
buffer.flip();
data.write(buffer);
buffer.clear();
if (!bufferFullMessage) {
callback.complete(channel.getWebSocketChannel(), this);
}
}
}
} catch (IOException e) {
callback.onError(channel.getWebSocketChannel(), this, e);
}
} finally {
pooled.close();
}
}
use of org.xnio.ChannelListener in project undertow by undertow-io.
the class WriteTimeoutTestCase method testWriteTimeout.
@Test
public void testWriteTimeout() throws IOException, InterruptedException {
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final StreamSinkChannel response = exchange.getResponseChannel();
try {
response.setOption(Options.WRITE_TIMEOUT, 10);
} catch (IOException e) {
throw new RuntimeException(e);
}
//1mb
final int capacity = 1 * 1024 * 1024;
final ByteBuffer originalBuffer = ByteBuffer.allocateDirect(capacity);
for (int i = 0; i < capacity; ++i) {
originalBuffer.put((byte) '*');
}
originalBuffer.flip();
response.getWriteSetter().set(new ChannelListener<Channel>() {
private ByteBuffer buffer = originalBuffer.duplicate();
int count = 0;
@Override
public void handleEvent(final Channel channel) {
do {
try {
int res = response.write(buffer);
if (res == 0) {
return;
}
} catch (IOException e) {
exception = e;
errorLatch.countDown();
}
if (!buffer.hasRemaining()) {
count++;
buffer = originalBuffer.duplicate();
}
} while (count < 1000);
exchange.endExchange();
}
});
response.wakeupWrites();
}
});
final TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
try {
HttpResponse result = client.execute(get);
InputStream content = result.getEntity().getContent();
byte[] buffer = new byte[512];
int r = 0;
while ((r = content.read(buffer)) > 0) {
Thread.sleep(200);
if (exception != null) {
Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
return;
}
}
Assert.fail("Write did not time out");
} catch (IOException e) {
if (errorLatch.await(5, TimeUnit.SECONDS)) {
Assert.assertEquals(WriteTimeoutException.class, exception.getClass());
} else {
Assert.fail("Write did not time out");
}
}
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.xnio.ChannelListener in project undertow by undertow-io.
the class DefaultServer method wrapOpenListener.
private static ChannelListener<StreamConnection> wrapOpenListener(final ChannelListener<StreamConnection> listener) {
if (!single) {
return listener;
}
return new ChannelListener<StreamConnection>() {
@Override
public void handleEvent(StreamConnection channel) {
channel.getSinkChannel().setConduit(new SingleByteStreamSinkConduit(channel.getSinkChannel().getConduit(), 10000));
channel.getSourceChannel().setConduit(new SingleByteStreamSourceConduit(channel.getSourceChannel().getConduit(), 10000));
listener.handleEvent(channel);
}
};
}
Aggregations