]> git.ipfire.org Git - thirdparty/squid.git/blame - src/CommCalls.h
SourceFormat: enforcement
[thirdparty/squid.git] / src / CommCalls.h
CommitLineData
b0469965 1
2/*
262a0e14 3 * $Id$
b0469965 4 */
5
6#ifndef SQUID_COMMCALLS_H
7#define SQUID_COMMCALLS_H
8
9#include "comm.h"
10#include "ConnectionDetail.h"
3ff65596 11#include "DnsLookupDetails.h"
882255af
AR
12#include "base/AsyncCall.h"
13#include "base/AsyncJobCalls.h"
b0469965 14
15/* CommCalls implement AsyncCall interface for comm_* callbacks.
16 * The classes cover two call dialer kinds:
17 * - A C-style call using a function pointer (depricated);
18 * - A C++-style call to an AsyncJob child.
19 * and three comm_* callback kinds:
20 * - accept (IOACB),
21 * - connect (CNCB),
22 * - I/O (IOCB).
23 */
24
26ac0430
AJ
25/*
26 * TODO: When there are no function-pointer-based callbacks left, all
27 * this complexity can be removed. Jobs that need comm services will just
28 * implement CommReader, CommWriter, etc. interfaces and receive calls
29 * using general (not comm-specific) AsyncCall code. For now, we have to
30 * allow the caller to create a callback that comm can modify to set
31 * parameters, which is not trivial when the caller type/kind is not
32 * known to comm and there are many kinds of parameters.
33 */
b0469965 34
35
36/* Comm*CbParams classes below handle callback parameters */
37
38// Maintains parameters common to all comm callbacks
26ac0430
AJ
39class CommCommonCbParams
40{
b0469965 41public:
42 CommCommonCbParams(void *aData);
43 CommCommonCbParams(const CommCommonCbParams &params);
44 ~CommCommonCbParams();
45
4c3ba68d
AR
46 /// adjust using the current Comm state; returns false to cancel the call
47 // not virtual because callers know dialer type
26ac0430 48 bool syncWithComm() { return true; }
82ec8dfc 49
b0469965 50 void print(std::ostream &os) const;
51
52public:
53 void *data; // cbdata-protected
54 int fd;
55 int xerrno;
56 comm_err_t flag;
57
58private:
59 // should not be needed and not yet implemented
26ac0430 60 CommCommonCbParams &operator =(const CommCommonCbParams &params);
b0469965 61};
62
63// accept parameters
26ac0430
AJ
64class CommAcceptCbParams: public CommCommonCbParams
65{
b0469965 66public:
67 CommAcceptCbParams(void *aData);
68
69 void print(std::ostream &os) const;
70
71public:
72 ConnectionDetail details;
73 int nfd; // TODO: rename to fdNew or somesuch
74};
75
76// connect parameters
26ac0430
AJ
77class CommConnectCbParams: public CommCommonCbParams
78{
b0469965 79public:
80 CommConnectCbParams(void *aData);
4c3ba68d
AR
81
82 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
3ff65596
AR
83
84 void print(std::ostream &os) const;
85
86public:
87 DnsLookupDetails dns;
b0469965 88};
89
90// read/write (I/O) parameters
26ac0430
AJ
91class CommIoCbParams: public CommCommonCbParams
92{
b0469965 93public:
94 CommIoCbParams(void *aData);
95
96 void print(std::ostream &os) const;
4c3ba68d 97 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
b0469965 98
99public:
100 char *buf;
101 size_t size;
102};
103
104// close parameters
26ac0430
AJ
105class CommCloseCbParams: public CommCommonCbParams
106{
b0469965 107public:
108 CommCloseCbParams(void *aData);
109};
110
26ac0430
AJ
111class CommTimeoutCbParams: public CommCommonCbParams
112{
b0469965 113public:
114 CommTimeoutCbParams(void *aData);
115};
116
117// Interface to expose comm callback parameters of all comm dialers.
118// GetCommParams() uses this interface to access comm parameters.
119template <class Params_>
26ac0430
AJ
120class CommDialerParamsT
121{
b0469965 122public:
123 typedef Params_ Params;
124 CommDialerParamsT(const Params &io): params(io) {}
125
126public:
127 Params params;
128};
129
130// Get comm params of an async comm call
131template <class Params>
26ac0430
AJ
132Params &GetCommParams(AsyncCall::Pointer &call)
133{
134 typedef CommDialerParamsT<Params> DialerParams;
b0469965 135 DialerParams *dp = dynamic_cast<DialerParams*>(call->getDialer());
136 assert(dp);
137 return dp->params;
138}
139
140
141// All job dialers with comm parameters are merged into one since they
142// all have exactly one callback argument and differ in Params type only
143template <class C, class Params_>
144class CommCbMemFunT: public JobDialer, public CommDialerParamsT<Params_>
145{
146public:
147 typedef Params_ Params;
148 typedef void (C::*Method)(const Params &io);
149
150 CommCbMemFunT(C *obj, Method meth): JobDialer(obj),
26ac0430 151 CommDialerParamsT<Params>(obj), object(obj), method(meth) {}
b0469965 152
26ac0430
AJ
153 virtual bool canDial(AsyncCall &c) {
154 return JobDialer::canDial(c) &&
155 this->params.syncWithComm();
156 }
4c3ba68d 157
b0469965 158 virtual void print(std::ostream &os) const {
26ac0430
AJ
159 os << '(';
160 this->params.print(os);
161 os << ')';
162 }
b0469965 163
164public:
26ac0430 165 C *object;
b0469965 166 Method method;
167
168protected:
4c3ba68d 169 virtual void doDial() { (object->*method)(this->params); }
b0469965 170};
171
172
173// accept (IOACB) dialer
174class CommAcceptCbPtrFun: public CallDialer,
e1381638 175 public CommDialerParamsT<CommAcceptCbParams>
b0469965 176{
177public:
178 typedef CommAcceptCbParams Params;
179
180 CommAcceptCbPtrFun(IOACB *aHandler, const CommAcceptCbParams &aParams);
181 void dial();
182
183 virtual void print(std::ostream &os) const;
184
185public:
186 IOACB *handler;
187};
188
189// connect (CNCB) dialer
190class CommConnectCbPtrFun: public CallDialer,
e1381638 191 public CommDialerParamsT<CommConnectCbParams>
b0469965 192{
193public:
194 typedef CommConnectCbParams Params;
195
196 CommConnectCbPtrFun(CNCB *aHandler, const Params &aParams);
197 void dial();
198
199 virtual void print(std::ostream &os) const;
200
201public:
202 CNCB *handler;
203};
204
205
206// read/write (IOCB) dialer
207class CommIoCbPtrFun: public CallDialer,
e1381638 208 public CommDialerParamsT<CommIoCbParams>
b0469965 209{
210public:
211 typedef CommIoCbParams Params;
212
213 CommIoCbPtrFun(IOCB *aHandler, const Params &aParams);
214 void dial();
215
216 virtual void print(std::ostream &os) const;
217
218public:
219 IOCB *handler;
220};
221
222
223// close (PF) dialer
224class CommCloseCbPtrFun: public CallDialer,
e1381638 225 public CommDialerParamsT<CommCloseCbParams>
b0469965 226{
227public:
228 typedef CommCloseCbParams Params;
229
230 CommCloseCbPtrFun(PF *aHandler, const Params &aParams);
231 void dial();
232
233 virtual void print(std::ostream &os) const;
234
235public:
236 PF *handler;
237};
238
239class CommTimeoutCbPtrFun:public CallDialer,
e1381638 240 public CommDialerParamsT<CommTimeoutCbParams>
b0469965 241{
242public:
243 typedef CommTimeoutCbParams Params;
244
245 CommTimeoutCbPtrFun(PF *aHandler, const Params &aParams);
246 void dial();
247
248 virtual void print(std::ostream &os) const;
249
250public:
251 PF *handler;
252};
253
254// AsyncCall to comm handlers implemented as global functions.
255// The dialer is one of the Comm*CbPtrFunT above
256// TODO: Get rid of this class by moving canFire() to canDial() method
257// of dialers.
258template <class Dialer>
26ac0430
AJ
259class CommCbFunPtrCallT: public AsyncCall
260{
b0469965 261public:
262 typedef typename Dialer::Params Params;
263
264 inline CommCbFunPtrCallT(int debugSection, int debugLevel,
26ac0430 265 const char *callName, const Dialer &aDialer);
b0469965 266
267 virtual CallDialer* getDialer() { return &dialer; }
268
269public:
270 Dialer dialer;
271
272protected:
273 inline virtual bool canFire();
274 inline virtual void fire();
275};
276
277// Conveninece wrapper: It is often easier to call a templated function than
278// to create a templated class.
279template <class Dialer>
280inline
281CommCbFunPtrCallT<Dialer> *commCbCall(int debugSection, int debugLevel,
26ac0430 282 const char *callName, const Dialer &dialer)
b0469965 283{
284 return new CommCbFunPtrCallT<Dialer>(debugSection, debugLevel, callName,
26ac0430 285 dialer);
b0469965 286}
287
288/* inlined implementation of templated methods */
289
290/* CommCbFunPtrCallT */
291
292template <class Dialer>
293CommCbFunPtrCallT<Dialer>::CommCbFunPtrCallT(int debugSection, int debugLevel,
26ac0430 294 const char *callName, const Dialer &aDialer):
b0469965 295 AsyncCall(debugSection, debugLevel, callName),
296 dialer(aDialer)
297{
298}
299
300
301template <class Dialer>
302bool
303CommCbFunPtrCallT<Dialer>::canFire()
304{
305 if (!AsyncCall::canFire())
306 return false;
307
308 if (!cbdataReferenceValid(dialer.params.data))
309 return cancel("callee gone");
310
4c3ba68d
AR
311 if (!dialer.params.syncWithComm())
312 return cancel("out of sync w/comm");
313
b0469965 314 return true;
315}
316
317template <class Dialer>
318void
319CommCbFunPtrCallT<Dialer>::fire()
320{
321 dialer.dial();
322}
323
324#endif /* SQUID_COMMCALLS_H */