BodyPipe.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_BODY_PIPE_H
10 #define SQUID_BODY_PIPE_H
11 
12 #include "base/AsyncJob.h"
13 #include "base/CbcPointer.h"
14 #include "MemBuf.h"
15 
16 class BodyPipe;
17 
22 class BodyProducer: virtual public AsyncJob
23 {
24 public:
26 
27  BodyProducer():AsyncJob("BodyProducer") {}
28  virtual ~BodyProducer() {}
29 
31  virtual void noteBodyConsumerAborted(RefCount<BodyPipe> bp) = 0;
32 
33 protected:
34  void stopProducingFor(RefCount<BodyPipe> &, bool atEof);
35 };
36 
42 class BodyConsumer: virtual public AsyncJob
43 {
44 public:
46 
47  BodyConsumer():AsyncJob("BodyConsumer") {}
48  virtual ~BodyConsumer() {}
49 
50  virtual void noteMoreBodyDataAvailable(RefCount<BodyPipe> bp) = 0;
51  virtual void noteBodyProductionEnded(RefCount<BodyPipe> bp) = 0;
52  virtual void noteBodyProducerAborted(RefCount<BodyPipe> bp) = 0;
53 
54 protected:
56 };
57 
62 {
63 public:
64  friend class BodyPipe;
65 
66 public:
67  BodyPipeCheckout(BodyPipe &); // checks out
68  ~BodyPipeCheckout(); // aborts checkout unless checkedIn
69 
70  void checkIn();
71 
72 public:
75  const uint64_t offset; // of current content, relative to the body start
76 
77 protected:
78  const size_t checkedOutSize;
79  bool checkedIn;
80 
81 private:
82  BodyPipeCheckout(const BodyPipeCheckout &); // prevent copying
83  BodyPipeCheckout &operator =(const BodyPipeCheckout &); // prevent assignment
84 };
85 
90 class BodyPipe: public RefCountable
91 {
93 
94 public:
99 
100  enum { MaxCapacity = 64*1024 };
101 
102  friend class BodyPipeCheckout;
103 
104 public:
105  BodyPipe(Producer *aProducer);
106  ~BodyPipe(); // asserts that producer and consumer are cleared
107 
108  void setBodySize(uint64_t aSize); // set body size
109  bool bodySizeKnown() const { return theBodySize >= 0; }
110  uint64_t bodySize() const;
111  uint64_t consumedSize() const { return theGetSize; }
112  uint64_t producedSize() const { return thePutSize; }
113  bool productionEnded() const { return !theProducer; }
114 
115  // called by producers
116  void clearProducer(bool atEof); // aborts or sends eof
117  size_t putMoreData(const char *buf, size_t size);
118  bool mayNeedMoreData() const { return !bodySizeKnown() || needsMoreData(); }
119  bool needsMoreData() const { return bodySizeKnown() && unproducedSize() > 0; }
120  uint64_t unproducedSize() const; // size of still unproduced data
121  bool stillProducing(const Producer::Pointer &producer) const { return theProducer == producer; }
122  void expectProductionEndAfter(uint64_t extraSize);
123 
124  // called by consumers
125  bool setConsumerIfNotLate(const Consumer::Pointer &aConsumer);
126  void clearConsumer(); // aborts if still piping
127  void expectNoConsumption();
128  size_t getMoreData(MemBuf &buf);
129  void consume(size_t size);
130  bool expectMoreAfter(uint64_t offset) const;
131  bool exhausted() const; // saw eof/abort and all data consumed
132  bool stillConsuming(const Consumer::Pointer &consumer) const { return theConsumer == consumer; }
133 
134  // start or continue consuming when there is no consumer
135  void enableAutoConsumption();
136 
137  const MemBuf &buf() const { return theBuf; }
138 
139  const char *status() const; // for debugging only
140 
141 protected:
142  // lower-level interface used by Checkout
143  MemBuf &checkOut(); // obtain raw buffer
144  void checkIn(Checkout &checkout); // return updated raw buffer
145  void undoCheckOut(Checkout &checkout); // undo checkout efffect
146 
149 
150  // keep counters in sync and notify producer or consumer
151  void postConsume(size_t size);
152  void postAppend(size_t size);
153 
154  void startAutoConsumption(); // delayed start of enabled consumption
155 
156 private:
157  int64_t theBodySize; // expected total content length, if known
158  Producer::Pointer theProducer; // content producer, if any
159  Consumer::Pointer theConsumer; // content consumer, if any
160 
161  uint64_t thePutSize; // ever-increasing total
162  uint64_t theGetSize; // ever-increasing total
163 
164  MemBuf theBuf; // produced but not yet consumed content, if any
165 
166  bool mustAutoConsume; // consume when there is no consumer
168  bool isCheckedOut; // to keep track of checkout violations
169 };
170 
171 #endif /* SQUID_BODY_PIPE_H */
172 
~BodyPipe()
Definition: BodyPipe.cc:139
bool abortedConsumption
called BodyProducer::noteBodyConsumerAborted
Definition: BodyPipe.h:167
uint64_t thePutSize
Definition: BodyPipe.h:161
void expectProductionEndAfter(uint64_t extraSize)
sets or checks body size
Definition: BodyPipe.cc:184
void postConsume(size_t size)
Definition: BodyPipe.cc:375
BodyPipe & thePipe
Definition: BodyPipe.h:73
void enableAutoConsumption()
Definition: BodyPipe.cc:319
CbcPointer< BodyProducer > Pointer
Definition: BodyPipe.h:25
const char * status() const
Definition: BodyPipe.cc:440
void scheduleBodyDataNotification()
Definition: BodyPipe.cc:408
const MemBuf & buf() const
Definition: BodyPipe.h:137
BodyConsumer Consumer
Definition: BodyPipe.h:97
void undoCheckOut(Checkout &checkout)
Definition: BodyPipe.cc:359
bool exhausted() const
Definition: BodyPipe.cc:174
BodyProducer()
Definition: BodyPipe.h:27
uint64_t theGetSize
Definition: BodyPipe.h:162
void startAutoConsumption()
Definition: BodyPipe.cc:329
uint64_t bodySize() const
Definition: BodyPipe.cc:161
void setBodySize(uint64_t aSize)
Definition: BodyPipe.cc:147
void expectNoConsumption()
there will be no more setConsumer() calls
Definition: BodyPipe.cc:267
BodyProducer Producer
Definition: BodyPipe.h:96
bool mustAutoConsume
Definition: BodyPipe.h:166
virtual ~BodyProducer()
Definition: BodyPipe.h:28
MemBuf & checkOut()
Definition: BodyPipe.cc:339
size_t putMoreData(const char *buf, size_t size)
Definition: BodyPipe.cc:213
uint64_t consumedSize() const
Definition: BodyPipe.h:111
virtual void noteMoreBodySpaceAvailable(RefCount< BodyPipe > bp)=0
bool productionEnded() const
Definition: BodyPipe.h:113
uint64_t unproducedSize() const
Definition: BodyPipe.cc:179
void checkIn(Checkout &checkout)
Definition: BodyPipe.cc:347
void scheduleBodyEndNotification()
Definition: BodyPipe.cc:420
BodyPipeCheckout Checkout
Definition: BodyPipe.h:98
uint64_t producedSize() const
Definition: BodyPipe.h:112
const size_t checkedOutSize
Definition: BodyPipe.h:78
void clearProducer(bool atEof)
Definition: BodyPipe.cc:194
bool expectMoreAfter(uint64_t offset) const
Definition: BodyPipe.cc:167
bool isCheckedOut
Definition: BodyPipe.h:168
bool mayNeedMoreData() const
Definition: BodyPipe.h:118
RefCount< BodyPipe > Pointer
Definition: BodyPipe.h:95
Producer::Pointer theProducer
Definition: BodyPipe.h:158
bool setConsumerIfNotLate(const Consumer::Pointer &aConsumer)
Definition: BodyPipe.cc:228
size_t getMoreData(MemBuf &buf)
Definition: BodyPipe.cc:295
CbcPointer< BodyConsumer > Pointer
Definition: BodyPipe.h:45
virtual void noteBodyProducerAborted(RefCount< BodyPipe > bp)=0
bool stillProducing(const Producer::Pointer &producer) const
Definition: BodyPipe.h:121
void stopProducingFor(RefCount< BodyPipe > &, bool atEof)
Definition: BodyPipe.cc:107
BodyConsumer()
Definition: BodyPipe.h:47
const uint64_t offset
Definition: BodyPipe.h:75
MemBuf theBuf
Definition: BodyPipe.h:164
Definition: MemBuf.h:23
void stopConsumingFrom(RefCount< BodyPipe > &)
Definition: BodyPipe.cc:118
bool needsMoreData() const
Definition: BodyPipe.h:119
int64_t theBodySize
Definition: BodyPipe.h:157
virtual void noteBodyProductionEnded(RefCount< BodyPipe > bp)=0
MEMPROXY_CLASS(BodyPipe)
virtual void noteBodyConsumerAborted(RefCount< BodyPipe > bp)=0
void consume(size_t size)
Definition: BodyPipe.cc:310
BodyPipeCheckout & operator=(const BodyPipeCheckout &)
Definition: BodyPipe.cc:510
virtual ~BodyConsumer()
Definition: BodyPipe.h:48
virtual void noteMoreBodyDataAvailable(RefCount< BodyPipe > bp)=0
void postAppend(size_t size)
Definition: BodyPipe.cc:390
void clearConsumer()
Definition: BodyPipe.cc:254
int size
Definition: ModDevPoll.cc:77
BodyPipe(Producer *aProducer)
Definition: BodyPipe.cc:128
bool stillConsuming(const Consumer::Pointer &consumer) const
Definition: BodyPipe.h:132
MemBuf & buf
Definition: BodyPipe.h:74
BodyPipeCheckout(BodyPipe &)
Definition: BodyPipe.cc:477
bool bodySizeKnown() const
Definition: BodyPipe.h:109
Consumer::Pointer theConsumer
Definition: BodyPipe.h:159

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors