@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()); } } };
@Override public void close() throws IOException { channel.close(); }
/** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public ChannelListener.Setter<? extends SslConnection> getCloseSetter() { return (ChannelListener.Setter<? extends SslConnection>) super.getCloseSetter(); } }
/** * Resets the channel to its original state, effectively disabling all current conduit * wrappers. The current state is lost. */ public void clearChannel() { channel.getSinkChannel().setConduit(originalSinkConduit); channel.getSourceChannel().setConduit(originalSourceConduit); } /**
@Override public void resumeWrites() { state |= FLAG_WRITES_RESUMED; if(anyAreSet(state, FLAG_WRITE_REQUIRES_READ)) { delegate.getSourceChannel().resumeReads(); } else { delegate.getSinkChannel().resumeWrites(); } }
AddressWrappedConnection(StreamConnection delegate, SocketAddress source, SocketAddress dest) { super(delegate.getIoThread()); this.delegate = delegate; this.source = source; this.dest = dest; setSinkConduit(delegate.getSinkChannel().getConduit()); setSourceConduit(delegate.getSourceChannel().getConduit()); }
@Override public void handleEvent(final StreamConnection channel) { if (UndertowLogger.REQUEST_LOGGER.isTraceEnabled()) { UndertowLogger.REQUEST_LOGGER.tracef("Opened connection with %s", channel.getPeerAddress()); Integer readTimeout = channel.getOption(Options.READ_TIMEOUT); Integer idle = undertowOptions.get(UndertowOptions.IDLE_TIMEOUT); if(idle != null) { IdleTimeoutConduit conduit = new IdleTimeoutConduit(channel); channel.getSourceChannel().setConduit(conduit); channel.getSinkChannel().setConduit(conduit); channel.getSourceChannel().setConduit(new ReadTimeoutStreamSourceConduit(channel.getSourceChannel().getConduit(), channel, this)); Integer writeTimeout = channel.getOption(Options.WRITE_TIMEOUT); if (writeTimeout != null && writeTimeout > 0) { channel.getSinkChannel().setConduit(new WriteTimeoutStreamSinkConduit(channel.getSinkChannel().getConduit(), channel, this)); IoUtils.safeClose(channel); UndertowLogger.REQUEST_IO_LOGGER.ioException(e); channel.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(channel.getSinkChannel().getConduit(), connectorStatistics.sentAccumulator())); channel.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(channel.getSourceChannel().getConduit(), connectorStatistics.receivedAccumulator())); connectorStatistics.incrementConnectionCount(); channel.getSourceChannel().setReadListener(readListener); readListener.handleEvent(channel.getSourceChannel());
log.debugf("exchange complete in connection to %s", getPeerAddress()); connection.getSinkChannel().setConduit(originalSinkConduit); connection.getSourceChannel().setConduit(pushBackStreamSourceConduit); connection.getSinkChannel().suspendWrites(); connection.getSinkChannel().setWriteListener(null); pendingResponse = null; this.state |= CLOSED; safeClose(connection); } else if (anyAreSet(state, UPGRADE_REQUESTED)) { connection.getSourceChannel().suspendReads(); currentRequest = null; pendingResponse = null; if (next == null) { connection.getSourceChannel().setReadListener(clientReadListener); connection.getSourceChannel().resumeReads(); } else { initiateRequest(next);
private void handleCPing() { state = new AjpRequestParseState(); final StreamConnection underlyingChannel = connection.getChannel(); underlyingChannel.getSourceChannel().suspendReads(); final ByteBuffer buffer = ByteBuffer.wrap(CPONG); int res; try { do { res = underlyingChannel.getSinkChannel().write(buffer); if (res == 0) { underlyingChannel.getSinkChannel().setWriteListener(new ChannelListener<ConduitStreamSinkChannel>() { @Override public void handleEvent(ConduitStreamSinkChannel channel) { underlyingChannel.getSinkChannel().resumeWrites(); return; AjpReadListener.this.handleEvent(underlyingChannel.getSourceChannel()); } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); safeClose(connection);
HttpClientConnection(final StreamConnection connection, final OptionMap options, final ByteBufferPool bufferPool) { if(options.get(UndertowOptions.ENABLE_STATISTICS, false)) { clientStatistics = new ClientStatisticsImpl(); connection.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(connection.getSinkChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { this.pushBackStreamSourceConduit = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit()); this.connection.getSourceChannel().setConduit(pushBackStreamSourceConduit); this.bufferPool = bufferPool; this.originalSinkConduit = connection.getSinkChannel().getConduit(); connection.getCloseSetter().set(new ChannelListener<StreamConnection>() { connection.getSourceChannel().setReadListener(clientReadListener); connection.getSourceChannel().resumeReads();
@Override public void handleEvent(Channel c) { try { if (flushPipelinedData()) { channel.getSinkChannel().setWriteListener(null); channel.getSinkChannel().suspendWrites(); connection.restoreChannel(oldState); connection.getReadListener().exchangeComplete(exchange); } } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); IoUtils.safeClose(channel); } catch (Throwable t) { UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(channel); } } });
@Override public void handleEvent(ConduitStreamSinkChannel channel) { int res; do { try { res = channel.write(buffer); if (res == 0) { return; } } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); safeClose(connection); } } while (buffer.hasRemaining()); channel.suspendWrites(); AjpReadListener.this.handleEvent(underlyingChannel.getSourceChannel()); } });
doHandshake(); } catch (IOException e) { UndertowLogger.REQUEST_LOGGER.ioException(e); IoUtils.safeClose(delegate); } catch (Throwable t) { UndertowLogger.REQUEST_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(delegate); final ChannelListener<? super ConduitStreamSinkChannel> writeListener = connection.getSinkChannel().getWriteListener(); if (writeListener == null) { suspendWrites(); delegate.getSinkChannel().suspendWrites();
@Override protected void notifyWriteClosed() { IoUtils.safeClose(delegate.getSinkChannel()); }
@Override protected void notifyReadClosed() { IoUtils.safeClose(delegate.getSourceChannel()); }
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); } } }
private void runWriteListener() { try { delegate.getIoThread().execute(new Runnable() { @Override public void run() { writeReadyHandler.writeReady(); } }); } catch (Throwable e) { //will only happen on shutdown IoUtils.safeClose(connection, delegate); UndertowLogger.REQUEST_IO_LOGGER.debugf(e, "Failed to queue read listener invocation"); } }
private void runReadListener(final boolean resumeInListener) { try { if(readListenerInvocationCount++ == MAX_READ_LISTENER_INVOCATIONS) { UndertowLogger.REQUEST_LOGGER.sslReadLoopDetected(this); IoUtils.safeClose(connection, delegate); close(); return; } if(resumeInListener) { delegate.getIoThread().execute(runReadListenerAndResumeCommand); } else { delegate.getIoThread().execute(runReadListenerCommand); } } catch (Throwable e) { //will only happen on shutdown IoUtils.safeClose(connection, delegate); UndertowLogger.REQUEST_IO_LOGGER.debugf(e, "Failed to queue read listener invocation"); } }
/** * Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state. * <p> * The underlying read side will be forcibly closed. * * @param cause The possibly null cause */ @SuppressWarnings({"unchecked", "rawtypes"}) protected void markReadsBroken(Throwable cause) { if (readsBrokenUpdater.compareAndSet(this, 0, 1)) { if(UndertowLogger.REQUEST_IO_LOGGER.isDebugEnabled()) { UndertowLogger.REQUEST_IO_LOGGER.debugf(new ClosedChannelException(), "Marking reads broken on channel %s", this); } if(receiver != null) { receiver.markStreamBroken(); } for(AbstractFramedStreamSourceChannel<C, R, S> r : new ArrayList<>(getReceivers())) { r.markStreamBroken(); } handleBrokenSourceChannel(cause); safeClose(channel.getSourceChannel()); closeSubChannels(); } }
private static Http2ClientConnection createHttp2Channel(StreamConnection connection, ByteBufferPool bufferPool, OptionMap options, String defaultHost) { final ClientStatisticsImpl clientStatistics; //first we set up statistics, if required if (options.get(UndertowOptions.ENABLE_STATISTICS, false)) { clientStatistics = new ClientStatisticsImpl(); connection.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(connection.getSinkChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.written += bytes; } })); connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.read += bytes; } })); } else { clientStatistics = null; } Http2Channel http2Channel = new Http2Channel(connection, null, bufferPool, null, true, false, options); return new Http2ClientConnection(http2Channel, false, defaultHost, clientStatistics, true); }