3 #include "base/TextException.h"
6 CBDATA_CLASS_INIT(BodyPipe
);
8 // BodySink is a BodyConsumer class which just consume and drops
9 // data from a BodyPipe
10 class BodySink
: public BodyConsumer
14 BodySink():AsyncJob("BodySink"), done(false) {}
15 virtual ~BodySink() {}
17 virtual void noteMoreBodyDataAvailable(BodyPipe::Pointer bp
) {
18 size_t contentSize
= bp
->buf().contentSize();
19 bp
->consume(contentSize
);
21 virtual void noteBodyProductionEnded(BodyPipe::Pointer bp
) {
22 stopConsumingFrom(bp
);
25 virtual void noteBodyProducerAborted(BodyPipe::Pointer bp
) {
26 stopConsumingFrom(bp
);
29 bool doneAll() const {return done
&& AsyncJob::doneAll();}
30 CBDATA_CLASS2(BodySink
);
33 CBDATA_CLASS_INIT(BodySink
);
35 // The BodyProducerDialer is an AsyncCall class which used to schedule BodyProducer calls.
36 // In addition to a normal AsyncCall checks if the BodyProducer is still the producer of
37 // the BodyPipe passed as argument
38 class BodyProducerDialer
: public UnaryMemFunT
<BodyProducer
, BodyPipe::Pointer
>
41 typedef UnaryMemFunT
<BodyProducer
, BodyPipe::Pointer
> Parent
;
43 BodyProducerDialer(BodyProducer
*aProducer
, Parent::Method aHandler
,
44 BodyPipe::Pointer bp
): Parent(aProducer
, aHandler
, bp
) {}
46 virtual bool canDial(AsyncCall
&call
);
49 // The BodyConsumerDialer is an AsyncCall class which used to schedule BodyConsumer calls.
50 // In addition to a normal AsyncCall checks if the BodyConsumer is still the reciptient
51 // of the BodyPipe passed as argument
52 class BodyConsumerDialer
: public UnaryMemFunT
<BodyConsumer
, BodyPipe::Pointer
>
55 typedef UnaryMemFunT
<BodyConsumer
, BodyPipe::Pointer
> Parent
;
57 BodyConsumerDialer(BodyConsumer
*aConsumer
, Parent::Method aHandler
,
58 BodyPipe::Pointer bp
): Parent(aConsumer
, aHandler
, bp
) {}
60 virtual bool canDial(AsyncCall
&call
);
64 BodyProducerDialer::canDial(AsyncCall
&call
)
66 if (!Parent::canDial(call
))
69 BodyProducer
*producer
= object
;
70 BodyPipe::Pointer pipe
= arg1
;
71 if (!pipe
->stillProducing(producer
)) {
72 debugs(call
.debugSection
, call
.debugLevel
, HERE
<< producer
<<
73 " no longer producing for " << pipe
->status());
74 return call
.cancel("no longer producing");
81 BodyConsumerDialer::canDial(AsyncCall
&call
)
83 if (!Parent::canDial(call
))
86 BodyConsumer
*consumer
= object
;
87 BodyPipe::Pointer pipe
= arg1
;
88 if (!pipe
->stillConsuming(consumer
)) {
89 debugs(call
.debugSection
, call
.debugLevel
, HERE
<< consumer
<<
90 " no longer consuming from " << pipe
->status());
91 return call
.cancel("no longer consuming");
100 // inform the pipe that we are done and clear the Pointer
101 void BodyProducer::stopProducingFor(RefCount
<BodyPipe
> &pipe
, bool atEof
)
103 debugs(91,7, HERE
<< this << " will not produce for " << pipe
<<
104 "; atEof: " << atEof
);
105 assert(pipe
!= NULL
); // be strict: the caller state may depend on this
106 pipe
->clearProducer(atEof
);
114 // inform the pipe that we are done and clear the Pointer
115 void BodyConsumer::stopConsumingFrom(RefCount
<BodyPipe
> &pipe
)
117 debugs(91,7, HERE
<< this << " will not consume from " << pipe
);
118 assert(pipe
!= NULL
); // be strict: the caller state may depend on this
119 pipe
->clearConsumer();
126 BodyPipe::BodyPipe(Producer
*aProducer
): theBodySize(-1),
127 theProducer(aProducer
), theConsumer(0),
128 thePutSize(0), theGetSize(0),
129 mustAutoConsume(false), isCheckedOut(false)
131 // TODO: teach MemBuf to start with zero minSize
132 // TODO: limit maxSize by theBodySize, when known?
133 theBuf
.init(2*1024, MaxCapacity
);
134 debugs(91,7, HERE
<< "created BodyPipe" << status());
137 BodyPipe::~BodyPipe()
139 debugs(91,7, HERE
<< "destroying BodyPipe" << status());
140 assert(!theProducer
);
141 assert(!theConsumer
);
145 void BodyPipe::setBodySize(uint64_t aBodySize
)
147 assert(!bodySizeKnown());
148 assert(aBodySize
>= 0);
149 assert(thePutSize
<= aBodySize
);
151 // If this assert fails, we need to add code to check for eof and inform
152 // the consumer about the eof condition via scheduleBodyEndNotification,
153 // because just setting a body size limit may trigger the eof condition.
154 assert(!theConsumer
);
156 theBodySize
= aBodySize
;
157 debugs(91,7, HERE
<< "set body size" << status());
160 uint64_t BodyPipe::bodySize() const
162 assert(bodySizeKnown());
163 return static_cast<uint64_t>(theBodySize
);
166 bool BodyPipe::expectMoreAfter(uint64_t offset
) const
168 assert(theGetSize
<= offset
);
169 return offset
< thePutSize
|| // buffer has more now or
170 (!productionEnded() && mayNeedMoreData()); // buffer will have more
173 bool BodyPipe::exhausted() const
175 return !expectMoreAfter(theGetSize
);
178 uint64_t BodyPipe::unproducedSize() const
180 return bodySize() - thePutSize
; // bodySize() asserts that size is known
184 BodyPipe::clearProducer(bool atEof
)
187 debugs(91,7, HERE
<< "clearing BodyPipe producer" << status());
190 if (!bodySizeKnown())
191 theBodySize
= thePutSize
;
192 else if (bodySize() != thePutSize
)
193 debugs(91,3, HERE
<< "aborting on premature eof" << status());
195 // asserta that we can detect the abort if the consumer joins later
196 assert(!bodySizeKnown() || bodySize() != thePutSize
);
198 scheduleBodyEndNotification();
203 BodyPipe::putMoreData(const char *aBuffer
, size_t size
)
206 size
= min((uint64_t)size
, unproducedSize());
208 const size_t spaceSize
= static_cast<size_t>(theBuf
.potentialSpaceSize());
209 if ((size
= min(size
, spaceSize
))) {
210 theBuf
.append(aBuffer
, size
);
218 BodyPipe::setConsumerIfNotLate(Consumer
*aConsumer
)
220 assert(!theConsumer
);
223 // TODO: convert this into an exception and remove IfNotLate suffix
224 // If there is something consumed already, we are in an auto-consuming mode
225 // and it is too late to attach a real consumer to the pipe.
226 if (theGetSize
> 0) {
227 assert(mustAutoConsume
);
231 theConsumer
= aConsumer
;
232 debugs(91,7, HERE
<< "set consumer" << status());
233 if (theBuf
.hasContent())
234 scheduleBodyDataNotification();
236 scheduleBodyEndNotification();
241 // When BodyPipe consumer is gone, all events for that consumer must not
242 // reach the new consumer (if any). Otherwise, the calls may go out of order
243 // (if _some_ calls are dropped due to the ultimate destination being
244 // temporary NULL). The code keeps track of the number of outstanding
245 // events and skips that number if consumer leaves. TODO: when AscyncCall
246 // support is improved, should we just schedule calls directly to consumer?
248 BodyPipe::clearConsumer()
251 debugs(91,7, HERE
<< "clearing consumer" << status());
253 if (consumedSize() && !exhausted()) {
254 AsyncCall::Pointer call
= asyncCall(91, 7,
255 "BodyProducer::noteBodyConsumerAborted",
256 BodyProducerDialer(theProducer
,
257 &BodyProducer::noteBodyConsumerAborted
, this));
258 ScheduleCallHere(call
);
264 BodyPipe::getMoreData(MemBuf
&aMemBuffer
)
266 if (!theBuf
.hasContent())
267 return 0; // did not touch the possibly uninitialized buf
269 if (aMemBuffer
.isNull())
271 const size_t size
= min(theBuf
.contentSize(), aMemBuffer
.potentialSpaceSize());
272 aMemBuffer
.append(theBuf
.content(), size
);
273 theBuf
.consume(size
);
275 return size
; // cannot be zero if we called buf.init above
279 BodyPipe::consume(size_t size
)
281 theBuf
.consume(size
);
285 // In the AutoConsumption mode the consumer has gone but the producer continues
286 // producing data. We are using a BodySink BodyConsumer which just discards the produced data.
288 BodyPipe::enableAutoConsumption()
290 mustAutoConsume
= true;
291 debugs(91,5, HERE
<< "enabled auto consumption" << status());
292 if (!theConsumer
&& theBuf
.hasContent())
293 startAutoConsumption();
296 // start auto consumption by creating body sink
298 BodyPipe::startAutoConsumption()
300 Must(mustAutoConsume
);
302 theConsumer
= new BodySink
;
303 debugs(91,7, HERE
<< "starting auto consumption" << status());
304 scheduleBodyDataNotification();
310 assert(!isCheckedOut
);
316 BodyPipe::checkIn(Checkout
&checkout
)
318 assert(isCheckedOut
);
319 isCheckedOut
= false;
320 const size_t currentSize
= theBuf
.contentSize();
321 if (checkout
.checkedOutSize
> currentSize
)
322 postConsume(checkout
.checkedOutSize
- currentSize
);
323 else if (checkout
.checkedOutSize
< currentSize
)
324 postAppend(currentSize
- checkout
.checkedOutSize
);
328 BodyPipe::undoCheckOut(Checkout
&checkout
)
330 assert(isCheckedOut
);
331 const size_t currentSize
= theBuf
.contentSize();
332 // We can only undo if size did not change, and even that carries
333 // some risk. If this becomes a problem, the code checking out
334 // raw buffers should always check them in (possibly unchanged)
335 // instead of relying on the automated undo mechanism of Checkout.
336 // The code can always use a temporary buffer to accomplish that.
337 assert(checkout
.checkedOutSize
== currentSize
);
340 // TODO: Optimize: inform consumer/producer about more data/space only if
341 // they used the data/space since we notified them last time.
344 BodyPipe::postConsume(size_t size
)
346 assert(!isCheckedOut
);
348 debugs(91,7, HERE
<< "consumed " << size
<< " bytes" << status());
349 if (mayNeedMoreData()) {
350 AsyncCall::Pointer call
= asyncCall(91, 7,
351 "BodyProducer::noteMoreBodySpaceAvailable",
352 BodyProducerDialer(theProducer
,
353 &BodyProducer::noteMoreBodySpaceAvailable
, this));
354 ScheduleCallHere(call
);
359 BodyPipe::postAppend(size_t size
)
361 assert(!isCheckedOut
);
363 debugs(91,7, HERE
<< "added " << size
<< " bytes" << status());
365 if (mustAutoConsume
&& !theConsumer
&& size
> 0)
366 startAutoConsumption();
368 // We should not consume here even if mustAutoConsume because the
369 // caller may not be ready for the data to be consumed during this call.
370 scheduleBodyDataNotification();
372 if (!mayNeedMoreData())
373 clearProducer(true); // reached end-of-body
378 BodyPipe::scheduleBodyDataNotification()
381 AsyncCall::Pointer call
= asyncCall(91, 7,
382 "BodyConsumer::noteMoreBodyDataAvailable",
383 BodyConsumerDialer(theConsumer
,
384 &BodyConsumer::noteMoreBodyDataAvailable
, this));
385 ScheduleCallHere(call
);
390 BodyPipe::scheduleBodyEndNotification()
393 if (bodySizeKnown() && bodySize() == thePutSize
) {
394 AsyncCall::Pointer call
= asyncCall(91, 7,
395 "BodyConsumer::noteBodyProductionEnded",
396 BodyConsumerDialer(theConsumer
,
397 &BodyConsumer::noteBodyProductionEnded
, this));
398 ScheduleCallHere(call
);
400 AsyncCall::Pointer call
= asyncCall(91, 7,
401 "BodyConsumer::noteBodyProducerAborted",
402 BodyConsumerDialer(theConsumer
,
403 &BodyConsumer::noteBodyProducerAborted
, this));
404 ScheduleCallHere(call
);
409 // a short temporary string describing buffer status for debugging
410 const char *BodyPipe::status() const
412 static MemBuf outputBuffer
;
413 outputBuffer
.reset();
415 outputBuffer
.append(" [", 2);
417 outputBuffer
.Printf("%"PRIu64
"<=%"PRIu64
, theGetSize
, thePutSize
);
418 if (theBodySize
>= 0)
419 outputBuffer
.Printf("<=%"PRId64
, theBodySize
);
421 outputBuffer
.append("<=?", 3);
423 outputBuffer
.Printf(" %d+%d", (int)theBuf
.contentSize(), (int)theBuf
.spaceSize());
425 outputBuffer
.Printf(" pipe%p", this);
427 outputBuffer
.Printf(" prod%p", theProducer
);
429 outputBuffer
.Printf(" cons%p", theConsumer
);
432 outputBuffer
.append(" A", 2);
434 outputBuffer
.append(" L", 2); // Locked
436 outputBuffer
.append("]", 1);
438 outputBuffer
.terminate();
440 return outputBuffer
.content();
444 /* BodyPipeCheckout */
446 BodyPipeCheckout::BodyPipeCheckout(BodyPipe
&aPipe
): pipe(aPipe
),
447 buf(aPipe
.checkOut()), offset(aPipe
.consumedSize()),
448 checkedOutSize(buf
.contentSize()), checkedIn(false)
452 BodyPipeCheckout::~BodyPipeCheckout()
455 pipe
.undoCheckOut(*this);
459 BodyPipeCheckout::checkIn()
467 BodyPipeCheckout::BodyPipeCheckout(const BodyPipeCheckout
&c
): pipe(c
.pipe
),
468 buf(c
.buf
), offset(c
.offset
), checkedOutSize(c
.checkedOutSize
),
469 checkedIn(c
.checkedIn
)
471 assert(false); // prevent copying
475 BodyPipeCheckout::operator =(const BodyPipeCheckout
&)
477 assert(false); // prevent assignment