private void changeToDemandState(State oldState) { if (changeState(oldState, State.DEMAND)) { // Protect from infinite recursion in Undertow, where we can't check if data // is available, so all we can do is to try to read. // Generally, no need to check if we just came out of readAndPublish()... if (!oldState.equals(State.READING)) { checkOnDataAvailable(); } } }
<T> void cancel(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); } else { publisher.state.get().cancel(publisher); } }
private boolean changeState(State oldState, State newState) { boolean result = this.state.compareAndSet(oldState, newState); if (result && rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + oldState + " -> " + newState); } return result; }
@Override <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, READING)) { try { boolean demandAvailable = publisher.readAndPublish(); if (demandAvailable) { publisher.changeToDemandState(READING); } else { publisher.readingPaused(); if (publisher.changeState(READING, NO_DEMAND)) { // Demand may have arrived since readAndPublish returned long r = publisher.demand; if (r > 0) { publisher.changeToDemandState(NO_DEMAND); } } } } catch (IOException ex) { publisher.onError(ex); } } // Else, either competing onDataAvailable (request vs container), or concurrent completion } },
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Publishing data read"); } subscriber.onNext(data); } else { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "No more data to read"); } return true; } } return false; }
private void changeToDemandState(State oldState) { if (changeState(oldState, State.DEMAND)) { // Protect from infinite recursion in Undertow, where we can't check if data // is available, so all we can do is to try to read. // Generally, no need to check if we just came out of readAndPublish()... if (!oldState.equals(State.READING)) { checkOnDataAvailable(); } } }
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (logger.isTraceEnabled()) { logger.trace("Data item read, publishing.."); } subscriber.onNext(data); } else { if (logger.isTraceEnabled()) { logger.trace("No more data to read"); } return true; } } return false; }
@Override <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, READING)) { try { boolean demandAvailable = publisher.readAndPublish(); if (demandAvailable) { publisher.changeToDemandState(READING); } else { publisher.readingPaused(); if (publisher.changeState(READING, NO_DEMAND)) { // Demand may have arrived since readAndPublish returned long r = publisher.demand; if (r > 0) { publisher.changeToDemandState(NO_DEMAND); } } } } catch (IOException ex) { publisher.onError(ex); } } // Else, either competing onDataAvailable (request vs container), or concurrent completion } },
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Publishing data read"); } subscriber.onNext(data); } else { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "No more data to read"); } return true; } } return false; }
@Override <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, READING)) { try { boolean demandAvailable = publisher.readAndPublish(); if (demandAvailable) { publisher.changeToDemandState(READING); } else { publisher.readingPaused(); if (publisher.changeState(READING, NO_DEMAND)) { // Demand may have arrived since readAndPublish returned long r = publisher.demand; if (r > 0) { publisher.changeToDemandState(NO_DEMAND); } } } } catch (IOException ex) { publisher.onError(ex); } } // Else, either competing onDataAvailable (request vs container), or concurrent completion } },
/** * Sub-classes can call this to delegate container error notifications. */ public final void onError(Throwable ex) { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Connection error: " + ex); } this.state.get().onError(this, ex); }
private void changeToDemandState(State oldState) { if (changeState(oldState, State.DEMAND)) { // Protect from infinite recursion in Undertow, where we can't check if data // is available, so all we can do is to try to read. // Generally, no need to check if we just came out of readAndPublish()... if (!oldState.equals(State.READING)) { checkOnDataAvailable(); } } }
/** * Read and publish data one at a time until there is no more data, no more * demand, or perhaps we completed in the mean time. * @return {@code true} if there is more demand; {@code false} if there is * no more demand or we have completed. */ private boolean readAndPublish() throws IOException { long r; while ((r = this.demand) > 0 && !this.state.get().equals(State.COMPLETED)) { T data = read(); if (data != null) { if (r != Long.MAX_VALUE) { DEMAND_FIELD_UPDATER.addAndGet(this, -1L); } Subscriber<? super T> subscriber = this.subscriber; Assert.state(subscriber != null, "No subscriber"); if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "Publishing data read"); } subscriber.onNext(data); } else { if (rsReadLogger.isTraceEnabled()) { rsReadLogger.trace(getLogPrefix() + "No more data to read"); } return true; } } return false; }
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable t) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); Subscriber<? super T> s = publisher.subscriber; if (s != null) { s.onError(t); } } else { publisher.state.get().onError(publisher, t); } } }
@Override <T> void onDataAvailable(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, READING)) { try { boolean demandAvailable = publisher.readAndPublish(); if (demandAvailable) { publisher.changeToDemandState(READING); } else { publisher.readingPaused(); if (publisher.changeState(READING, NO_DEMAND)) { // Demand may have arrived since readAndPublish returned long r = publisher.demand; if (r > 0) { publisher.changeToDemandState(NO_DEMAND); } } } } catch (IOException ex) { publisher.onError(ex); } } // Else, either competing onDataAvailable (request vs container), or concurrent completion } },
/** * Invoked when reading is possible, either in the same thread after a check * via {@link #checkOnDataAvailable()}, or as a callback from the underlying * container. */ public final void onDataAvailable() { rsReadLogger.trace(getLogPrefix() + "onDataAvailable"); this.state.get().onDataAvailable(this); }
private void changeToDemandState(State oldState) { if (changeState(oldState, State.DEMAND)) { // Protect from infinite recursion in Undertow, where we can't check if data // is available, so all we can do is to try to read. // Generally, no need to check if we just came out of readAndPublish()... if (!oldState.equals(State.READING)) { checkOnDataAvailable(); } } }
<T> void cancel(AbstractListenerReadPublisher<T> publisher) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); } else { publisher.state.get().cancel(publisher); } }
/** * Sub-classes can call this method to delegate a contain notification when * all data has been read. */ public void onAllDataRead() { rsReadLogger.trace(getLogPrefix() + "onAllDataRead"); this.state.get().onAllDataRead(this); }
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable t) { if (publisher.changeState(this, COMPLETED)) { publisher.discardData(); Subscriber<? super T> s = publisher.subscriber; if (s != null) { s.onError(t); } } else { publisher.state.get().onError(publisher, t); } } }