BodyPipe.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
16class BodyPipe;
17
22class BodyProducer: virtual public AsyncJob
23{
24public:
26
27 BodyProducer():AsyncJob("BodyProducer") {}
28 ~BodyProducer() override {}
29
32
33protected:
34 void stopProducingFor(RefCount<BodyPipe> &, bool atEof);
35};
36
42class BodyConsumer: virtual public AsyncJob
43{
44public:
46
47 BodyConsumer():AsyncJob("BodyConsumer") {}
48 ~BodyConsumer() override {}
49
53
54protected:
56};
57
62{
63public:
64 friend class BodyPipe;
65
66public:
67 BodyPipeCheckout(BodyPipe &); // checks out
68 ~BodyPipeCheckout(); // aborts checkout unless checkedIn
69
70 void checkIn();
71
72public:
75 const uint64_t offset; // of current content, relative to the body start
76
77protected:
78 const size_t checkedOutSize;
80
81private:
82 BodyPipeCheckout(const BodyPipeCheckout &); // prevent copying
83 BodyPipeCheckout &operator =(const BodyPipeCheckout &); // prevent assignment
84};
85
91{
93
94public:
99
100 enum { MaxCapacity = 64*1024 };
101
102 friend class BodyPipeCheckout;
103
104public:
105 BodyPipe(Producer *aProducer);
106 ~BodyPipe() override; // 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
136
137 const MemBuf &buf() const { return theBuf; }
138
139 const char *status() const; // for debugging only
140
141protected:
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
155
156private:
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
#define RefCountable
The locking interface for use on Reference-Counted classes.
Definition: Lock.h:66
int size
Definition: ModDevPoll.cc:75
virtual void noteBodyProductionEnded(RefCount< BodyPipe > bp)=0
CbcPointer< BodyConsumer > Pointer
Definition: BodyPipe.h:45
void stopConsumingFrom(RefCount< BodyPipe > &)
Definition: BodyPipe.cc:118
~BodyConsumer() override
Definition: BodyPipe.h:48
virtual void noteMoreBodyDataAvailable(RefCount< BodyPipe > bp)=0
virtual void noteBodyProducerAborted(RefCount< BodyPipe > bp)=0
BodyConsumer()
Definition: BodyPipe.h:47
const size_t checkedOutSize
Definition: BodyPipe.h:78
const uint64_t offset
Definition: BodyPipe.h:75
BodyPipe & thePipe
Definition: BodyPipe.h:73
BodyPipeCheckout & operator=(const BodyPipeCheckout &)
Definition: BodyPipe.cc:516
BodyPipeCheckout(BodyPipe &)
Definition: BodyPipe.cc:483
MemBuf & buf
Definition: BodyPipe.h:74
bool stillConsuming(const Consumer::Pointer &consumer) const
Definition: BodyPipe.h:132
void scheduleBodyEndNotification()
Definition: BodyPipe.cc:426
void checkIn(Checkout &checkout)
Definition: BodyPipe.cc:352
void scheduleBodyDataNotification()
Definition: BodyPipe.cc:414
~BodyPipe() override
Definition: BodyPipe.cc:139
uint64_t producedSize() const
Definition: BodyPipe.h:112
void expectNoConsumption()
there will be no more setConsumer() calls
Definition: BodyPipe.cc:267
size_t putMoreData(const char *buf, size_t size)
Definition: BodyPipe.cc:213
BodyPipeCheckout Checkout
Definition: BodyPipe.h:98
size_t getMoreData(MemBuf &buf)
Definition: BodyPipe.cc:294
const MemBuf & buf() const
Definition: BodyPipe.h:137
bool needsMoreData() const
Definition: BodyPipe.h:119
uint64_t thePutSize
Definition: BodyPipe.h:161
bool isCheckedOut
Definition: BodyPipe.h:168
void clearConsumer()
Definition: BodyPipe.cc:254
void clearProducer(bool atEof)
Definition: BodyPipe.cc:194
MemBuf & checkOut()
Definition: BodyPipe.cc:344
void postConsume(size_t size)
Definition: BodyPipe.cc:380
bool exhausted() const
Definition: BodyPipe.cc:174
bool bodySizeKnown() const
Definition: BodyPipe.h:109
bool mustAutoConsume
keep theBuf empty when producing without consumer
Definition: BodyPipe.h:166
bool abortedConsumption
called BodyProducer::noteBodyConsumerAborted
Definition: BodyPipe.h:167
uint64_t unproducedSize() const
Definition: BodyPipe.cc:179
void setBodySize(uint64_t aSize)
Definition: BodyPipe.cc:147
MEMPROXY_CLASS(BodyPipe)
void consume(size_t size)
Definition: BodyPipe.cc:309
uint64_t consumedSize() const
Definition: BodyPipe.h:111
Producer::Pointer theProducer
Definition: BodyPipe.h:158
bool setConsumerIfNotLate(const Consumer::Pointer &aConsumer)
Definition: BodyPipe.cc:228
uint64_t theGetSize
Definition: BodyPipe.h:162
void expectProductionEndAfter(uint64_t extraSize)
sets or checks body size
Definition: BodyPipe.cc:184
BodyConsumer Consumer
Definition: BodyPipe.h:97
const char * status() const
Definition: BodyPipe.cc:446
void postAppend(size_t size)
Definition: BodyPipe.cc:395
bool stillProducing(const Producer::Pointer &producer) const
Definition: BodyPipe.h:121
void startAutoConsumptionIfNeeded()
Definition: BodyPipe.cc:327
RefCount< BodyPipe > Pointer
Definition: BodyPipe.h:95
MemBuf theBuf
Definition: BodyPipe.h:164
bool productionEnded() const
Definition: BodyPipe.h:113
bool mayNeedMoreData() const
Definition: BodyPipe.h:118
uint64_t bodySize() const
Definition: BodyPipe.cc:161
void enableAutoConsumption()
start or continue consuming when producing without consumer
Definition: BodyPipe.cc:316
BodyProducer Producer
Definition: BodyPipe.h:96
@ MaxCapacity
Definition: BodyPipe.h:100
BodyPipe(Producer *aProducer)
Definition: BodyPipe.cc:128
int64_t theBodySize
Definition: BodyPipe.h:157
bool expectMoreAfter(uint64_t offset) const
Definition: BodyPipe.cc:167
void undoCheckOut(Checkout &checkout)
Definition: BodyPipe.cc:364
Consumer::Pointer theConsumer
Definition: BodyPipe.h:159
CbcPointer< BodyProducer > Pointer
Definition: BodyPipe.h:25
virtual void noteMoreBodySpaceAvailable(RefCount< BodyPipe > bp)=0
virtual void noteBodyConsumerAborted(RefCount< BodyPipe > bp)=0
void stopProducingFor(RefCount< BodyPipe > &, bool atEof)
Definition: BodyPipe.cc:107
BodyProducer()
Definition: BodyPipe.h:27
~BodyProducer() override
Definition: BodyPipe.h:28
Definition: MemBuf.h:24

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors