BodyPipe.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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:
55  void stopConsumingFrom(RefCount<BodyPipe> &);
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 
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 
147  void scheduleBodyDataNotification();
148  void scheduleBodyEndNotification();
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 startAutoConsumptionIfNeeded();
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 
168  bool isCheckedOut; // to keep track of checkout violations
169 };
170 
171 #endif /* SQUID_BODY_PIPE_H */
172 
const MemBuf & buf() const
Definition: BodyPipe.h:137
bool abortedConsumption
called BodyProducer::noteBodyConsumerAborted
Definition: BodyPipe.h:167
bool stillConsuming(const Consumer::Pointer &consumer) const
Definition: BodyPipe.h:132
uint64_t thePutSize
Definition: BodyPipe.h:161
BodyPipe & thePipe
Definition: BodyPipe.h:73
#define MEMPROXY_CLASS(CLASS)
CbcPointer< BodyProducer > Pointer
Definition: BodyPipe.h:25
friend class BodyPipeCheckout
Definition: BodyPipe.h:102
BodyConsumer Consumer
Definition: BodyPipe.h:97
BodyProducer()
Definition: BodyPipe.h:27
uint64_t theGetSize
Definition: BodyPipe.h:162
bool bodySizeKnown() const
Definition: BodyPipe.h:109
uint64_t consumedSize() const
Definition: BodyPipe.h:111
friend class BodyPipe
Definition: BodyPipe.h:64
BodyProducer Producer
Definition: BodyPipe.h:96
bool mustAutoConsume
keep theBuf empty when producing without consumer
Definition: BodyPipe.h:166
virtual ~BodyProducer()
Definition: BodyPipe.h:28
virtual const char * status() const
internal cleanup; do not call directly
Definition: AsyncJob.cc:159
virtual void noteMoreBodySpaceAvailable(RefCount< BodyPipe > bp)=0
void checkIn(Checkout &checkout)
Definition: BodyPipe.cc:351
BodyPipeCheckout Checkout
Definition: BodyPipe.h:98
uint64_t producedSize() const
Definition: BodyPipe.h:112
const size_t checkedOutSize
Definition: BodyPipe.h:78
bool isCheckedOut
Definition: BodyPipe.h:168
RefCount< BodyPipe > Pointer
Definition: BodyPipe.h:95
Producer::Pointer theProducer
Definition: BodyPipe.h:158
bool stillProducing(const Producer::Pointer &producer) const
Definition: BodyPipe.h:121
void const char * buf
Definition: stub_helper.cc:16
CbcPointer< BodyConsumer > Pointer
Definition: BodyPipe.h:45
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
int64_t theBodySize
Definition: BodyPipe.h:157
bool mayNeedMoreData() const
Definition: BodyPipe.h:118
virtual void noteBodyConsumerAborted(RefCount< BodyPipe > bp)=0
virtual ~BodyConsumer()
Definition: BodyPipe.h:48
int size
Definition: ModDevPoll.cc:77
bool needsMoreData() const
Definition: BodyPipe.h:119
bool productionEnded() const
Definition: BodyPipe.h:113
MemBuf & buf
Definition: BodyPipe.h:74
Consumer::Pointer theConsumer
Definition: BodyPipe.h:159

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors