private void flushChannel(StreamSinkChannel stream) throws IOException { stream.shutdownWrites(); if (!stream.flush()) { stream.getWriteSetter().set(ChannelListeners.flushingChannelListener(null, writeExceptionHandler())); stream.resumeWrites(); } }
@Override public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) { if(log.isDebugEnabled()) { log.debugf("Upgraded request %s to for exchange %s", result.getRequest(), exchange); } StreamConnection clientChannel = null; try { clientChannel = result.getConnection().performUpgrade(); final ClosingExceptionHandler handler = new ClosingExceptionHandler(streamConnection, clientChannel); Transfer.initiateTransfer(clientChannel.getSourceChannel(), streamConnection.getSinkChannel(), ChannelListeners.closingChannelListener(), ChannelListeners.writeShutdownChannelListener(ChannelListeners.<StreamSinkChannel>flushingChannelListener(ChannelListeners.closingChannelListener(), ChannelListeners.closingChannelExceptionHandler()), ChannelListeners.closingChannelExceptionHandler()), handler, handler, result.getConnection().getBufferPool()); Transfer.initiateTransfer(streamConnection.getSourceChannel(), clientChannel.getSinkChannel(), ChannelListeners.closingChannelListener(), ChannelListeners.writeShutdownChannelListener(ChannelListeners.<StreamSinkChannel>flushingChannelListener(ChannelListeners.closingChannelListener(), ChannelListeners.closingChannelExceptionHandler()), ChannelListeners.closingChannelExceptionHandler()), handler, handler, result.getConnection().getBufferPool()); } catch (IOException e) { IoUtils.safeClose(streamConnection, clientChannel); } } });
public IoFuture<SslConnection> openSslConnection(final XnioIoThread ioThread, final InetSocketAddress bindAddress, final InetSocketAddress destination, final ChannelListener<? super SslConnection> openListener, final ChannelListener<? super BoundChannel> bindListener, final OptionMap optionMap) { final FutureResult<SslConnection> futureResult = new FutureResult<>(ioThread); final IoFuture<StreamConnection> connection = ioThread.openStreamConnection(bindAddress, destination, new ChannelListener<StreamConnection>() { public void handleEvent(final StreamConnection connection) { } catch (IOException e) { if (futureResult.setException(e)) { IoUtils.safeClose(connection); if (! futureResult.setResult(wrappedConnection)) { IoUtils.safeClose(connection); } else { ChannelListeners.invokeChannelListener(wrappedConnection, openListener);
return delegatingChannelListener(delegate); result = channel.transferFrom(source, p, cnt); } catch (IOException e) { invokeChannelExceptionHandler(channel, exceptionHandler, e); return; } while (cnt > 0L); invokeChannelListener(channel, delegate); return; } finally {
public void handleEvent(final T channel) { try { long count = this.count; try { long res; for (;;) { res = Channels.drain(channel, count); if (res == -1 || res == count) { this.count = 0L; invokeChannelListener(channel, finishListener); return; } else if (res == 0) { return; } else if (count < Long.MAX_VALUE) { // MAX_VALUE means drain to EOF count -= res; } } } finally { this.count = count; } } catch (IOException e) { this.count = 0L; if (exceptionHandler != null) { invokeChannelExceptionHandler(channel, exceptionHandler, e); } else { IoUtils.safeShutdownReads(channel); } } }
boolean free = true; try { final ByteBuffer buffer = allocated.getResource(); long transferred; for(;;) { try { transferred = source.transferTo(count, buffer, sink); } catch (IOException e) { invokeChannelExceptionHandler(source, readExceptionHandler, e); return; sink.suspendWrites(); } else { sink.wakeupWrites(); sink.suspendWrites(); invokeChannelExceptionHandler(source, readExceptionHandler, new EOFException()); invokeChannelExceptionHandler(sink, writeExceptionHandler, e); return; source.getReadSetter().set(listener); sink.getWriteSetter().set(listener); sink.getWriteSetter().set(listener);
if (UndertowLogger.CLIENT_LOGGER.isDebugEnabled()) { safeClose(connection); buffer.clear(); try { res = channel.read(buffer); if (!channel.isReadResumed()) { channel.getReadSetter().set(this); if (anyAreSet(HttpClientConnection.this.state, UPGRADE_REQUESTED)) { if ((connectionString == null || !Headers.UPGRADE.equalToString(connectionString)) && !response.getResponseHeaders().contains(Headers.UPGRADE)) { if(!currentRequest.getRequest().getMethod().equals(Methods.CONNECT) || response.getResponseCode() != 200) { //make sure it was not actually a connect request } else { prepareResponseChannel(response, currentRequest); channel.getReadSetter().set(null); if(HttpContinue.requiresContinueResponse(currentRequest.getRequest().getRequestHeaders())) { HttpClientConnection.this.state |= CLOSE_REQ; ConduitStreamSinkChannel sinkChannel = HttpClientConnection.this.connection.getSinkChannel(); sinkChannel.shutdownWrites(); if(!sinkChannel.flush()) { sinkChannel.setWriteListener(ChannelListeners.flushingChannelListener(null, null)); sinkChannel.resumeWrites();
if (contentLength != null) { if (!"0".equals(contentLength)) { future.setException(new IOException("Upgrade responses must have a content length of zero.")); return; future.setException(new IOException("Upgrade responses cannot have a transfer coding")); return; safeClose(connection); future.setException(e); return; future.setResult(connection); ChannelListeners.invokeChannelListener(connection, openListener);
@Override public void run() { handle = null; if (expireTime == -1) { return; } long current = System.currentTimeMillis(); if (current < expireTime) { //timeout has been bumped, re-schedule handle = WorkerUtils.executeAfter(connection.getIoThread(),timeoutCommand, (expireTime - current) + FUZZ_FACTOR, TimeUnit.MILLISECONDS); return; } UndertowLogger.REQUEST_LOGGER.tracef("Timing out channel %s due to inactivity", connection.getSourceChannel()); IoUtils.safeClose(connection); if (connection.getSourceChannel().isReadResumed()) { ChannelListeners.invokeChannelListener(connection.getSourceChannel(), connection.getSourceChannel().getReadListener()); } if (connection.getSinkChannel().isWriteResumed()) { ChannelListeners.invokeChannelListener(connection.getSinkChannel(), connection.getSinkChannel().getWriteListener()); } } };
public void handleEvent(final StreamConnection channel) { final AssembledConnectedStreamChannel assembledChannel = new AssembledConnectedStreamChannel(channel, channel.getSourceChannel(), channel.getSinkChannel()); if (!futureResult.setResult(assembledChannel)) { safeClose(assembledChannel); } else { ChannelListeners.invokeChannelListener(assembledChannel, openListener); } } }
for(;;) { try { read = source.read(buffer); buffer.flip(); } catch (IOException e) { ChannelListeners.invokeChannelExceptionHandler(source, readExceptionHandler, e); return; final int res; try { res = sink.write(buffer); } catch (IOException e) { ChannelListeners.invokeChannelExceptionHandler(sink, writeExceptionHandler, e); return; sink.getWriteSetter().set(listener); source.getReadSetter().set(listener); source.resumeReads(); sink.resumeWrites();
@Override public void readReady() { if(anyAreSet(state, FLAG_WRITE_REQUIRES_READ) && anyAreSet(state, FLAG_WRITES_RESUMED | FLAG_READS_RESUMED) && !anyAreSet(state, FLAG_ENGINE_INBOUND_SHUTDOWN)) { try { invokingReadListenerHandshake = true; doHandshake(); } catch (IOException e) { UndertowLogger.REQUEST_LOGGER.ioException(e); IoUtils.safeClose(delegate); } catch (Throwable t) { UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(delegate); } finally { invokingReadListenerHandshake = false; if(!anyAreSet(state, FLAG_READS_RESUMED) && !allAreSet(state, FLAG_WRITE_REQUIRES_READ | FLAG_WRITES_RESUMED)) { delegate.getSourceChannel().suspendReads(); initialUnwrapped = unwrappedData.getBuffer().remaining(); ChannelListeners.invokeChannelListener(connection.getSourceChannel(), readListener); if(anyAreSet(state, FLAG_DATA_TO_UNWRAP) && initialDataToUnwrap == dataToUnwrap.getBuffer().remaining()) { noProgress = true;
private void flushUpgradeChannel() { try { if(!connection.getSinkChannel().flush()) { connection.getSinkChannel().getWriteSetter().set(ChannelListeners.flushingChannelListener(new ChannelListener<StreamSinkChannel>() { @Override public void handleEvent(StreamSinkChannel channel) { channel.suspendWrites(); new UpgradeResultListener().handleEvent(connection.getSourceChannel()); } }, new ChannelExceptionHandler<StreamSinkChannel>() { @Override public void handleException(StreamSinkChannel channel, IOException exception) { safeClose(channel); future.setException(exception); } })); connection.getSinkChannel().resumeWrites(); return; } } catch (IOException e) { safeClose(connection); future.setException(e); return; } new UpgradeResultListener().handleEvent(connection.getSourceChannel()); }
channel.getSourceChannel().setReadListener(HttpReadListener.this); channel.getSourceChannel().resumeReads(); requestStateUpdater.set(this, 0); } else { while (true) { if (connection.getOriginalSourceConduit().isReadShutdown() || connection.getOriginalSinkConduit().isWriteShutdown()) { channel.getSourceChannel().suspendReads(); channel.getSinkChannel().suspendWrites(); IoUtils.safeClose(connection); return; } else { channel.getSourceChannel().suspendReads(); channel.getSinkChannel().suspendWrites(); IoUtils.safeClose(connection); return; } else if (requestStateUpdater.compareAndSet(this, 1, 2)) { if (!connection.getChannel().getSinkChannel().flush()) { connection.getChannel().getSinkChannel().setWriteListener(ChannelListeners.flushingChannelListener(new ChannelListener<ConduitStreamSinkChannel>() { @Override public void handleEvent(ConduitStreamSinkChannel conduitStreamSinkChannel) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); IoUtils.safeClose(connection); } catch (Throwable t) { UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(connection);
final int res; try { res = sink.write(buffer); } catch (IOException e) { pooledBuffer.close(); pooledBuffer = null; done = true; ChannelListeners.invokeChannelExceptionHandler(sink, writeExceptionHandler, e); return; read = source.read(buffer); buffer.flip(); } catch (IOException e) { pooledBuffer = null; done = true; ChannelListeners.invokeChannelExceptionHandler(source, readExceptionHandler, e); return; sink.suspendWrites(); } else if(!sink.isWriteResumed()) { sink.resumeWrites(); source.suspendReads(); } else if(!source.isReadResumed()){ source.resumeReads();
@Override public void writeReady() { if(anyAreSet(state, FLAG_READ_REQUIRES_WRITE)) { if(anyAreSet(state, FLAG_READS_RESUMED)) { readReadyHandler.readReady(); } else { doHandshake(); } catch (IOException e) { UndertowLogger.REQUEST_LOGGER.ioException(e); IoUtils.safeClose(delegate); } catch (Throwable t) { UndertowLogger.REQUEST_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(delegate); if (anyAreSet(state, FLAG_WRITES_RESUMED)) { if(delegateHandler == null) { final ChannelListener<? super ConduitStreamSinkChannel> writeListener = connection.getSinkChannel().getWriteListener(); suspendWrites(); } else { ChannelListeners.invokeChannelListener(connection.getSinkChannel(), writeListener); if(!anyAreSet(state, FLAG_WRITES_RESUMED | FLAG_READ_REQUIRES_WRITE)) { delegate.getSinkChannel().suspendWrites();
private void readFailed(final IOException e) { try { source.suspendReads(); sink.suspendWrites(); invokeChannelExceptionHandler(source, readExceptionHandler, e); } finally { pooledBuffer.free(); } }
connection.getSinkChannel().shutdownWrites(); if (!connection.getSinkChannel().flush()) { connection.getSinkChannel().setWriteListener(ChannelListeners.flushingChannelListener(new ChannelListener<ConduitStreamSinkChannel>() { @Override public void handleEvent(ConduitStreamSinkChannel channel) { UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e); } else { UndertowLogger.REQUEST_IO_LOGGER.ioException(new IOException(e)); IoUtils.safeClose(connection); IoUtils.safeClose(additional);
responseChannel.getWriteSetter().set(ChannelListeners.flushingChannelListener( new ChannelListener<StreamSinkChannel>() { @Override } else { if (anyAreClear(state, FLAG_RESPONSE_TERMINATED)) { UndertowLogger.ROOT_LOGGER.responseWasNotTerminated(connection, this); IoUtils.safeClose(connection); UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) t); } else { UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(connection);
@Override public void handleEvent(StreamSinkChannel channel) { if(timerKey != null) { timerKey.remove(); } for (ChannelListener<ServerSentEventConnection> listener : closeTasks) { ChannelListeners.invokeChannelListener(ServerSentEventConnection.this, listener); } IoUtils.safeClose(ServerSentEventConnection.this); } });