6 #ifndef SQUID_COMMCALLS_H
7 #define SQUID_COMMCALLS_H
10 #include "ConnectionDetail.h"
11 #include "DnsLookupDetails.h"
12 #include "base/AsyncCall.h"
13 #include "base/AsyncJobCalls.h"
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:
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.
36 /* Comm*CbParams classes below handle callback parameters */
38 // Maintains parameters common to all comm callbacks
39 class CommCommonCbParams
42 CommCommonCbParams(void *aData
);
43 CommCommonCbParams(const CommCommonCbParams
¶ms
);
44 ~CommCommonCbParams();
46 /// adjust using the current Comm state; returns false to cancel the call
47 // not virtual because callers know dialer type
48 bool syncWithComm() { return true; }
50 void print(std::ostream
&os
) const;
53 void *data
; // cbdata-protected
59 // should not be needed and not yet implemented
60 CommCommonCbParams
&operator =(const CommCommonCbParams
¶ms
);
64 class CommAcceptCbParams
: public CommCommonCbParams
67 CommAcceptCbParams(void *aData
);
69 void print(std::ostream
&os
) const;
72 ConnectionDetail details
;
73 int nfd
; // TODO: rename to fdNew or somesuch
77 class CommConnectCbParams
: public CommCommonCbParams
80 CommConnectCbParams(void *aData
);
82 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
84 void print(std::ostream
&os
) const;
90 // read/write (I/O) parameters
91 class CommIoCbParams
: public CommCommonCbParams
94 CommIoCbParams(void *aData
);
96 void print(std::ostream
&os
) const;
97 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
105 class CommCloseCbParams
: public CommCommonCbParams
108 CommCloseCbParams(void *aData
);
111 class CommTimeoutCbParams
: public CommCommonCbParams
114 CommTimeoutCbParams(void *aData
);
117 // Interface to expose comm callback parameters of all comm dialers.
118 // GetCommParams() uses this interface to access comm parameters.
119 template <class Params_
>
120 class CommDialerParamsT
123 typedef Params_ Params
;
124 CommDialerParamsT(const Params
&io
): params(io
) {}
130 // Get comm params of an async comm call
131 template <class Params
>
132 Params
&GetCommParams(AsyncCall::Pointer
&call
)
134 typedef CommDialerParamsT
<Params
> DialerParams
;
135 DialerParams
*dp
= dynamic_cast<DialerParams
*>(call
->getDialer());
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
143 template <class C
, class Params_
>
144 class CommCbMemFunT
: public JobDialer
<C
>, public CommDialerParamsT
<Params_
>
147 typedef Params_ Params
;
148 typedef void (C::*Method
)(const Params
&io
);
150 CommCbMemFunT(const CbcPointer
<C
> &job
, Method meth
): JobDialer
<C
>(job
),
151 CommDialerParamsT
<Params_
>(job
.get()),
154 virtual bool canDial(AsyncCall
&c
) {
155 return JobDialer
<C
>::canDial(c
) &&
156 this->params
.syncWithComm();
159 virtual void print(std::ostream
&os
) const {
161 this->params
.print(os
);
169 virtual void doDial() { ((&(*this->job
))->*method
)(this->params
); }
173 // accept (IOACB) dialer
174 class CommAcceptCbPtrFun
: public CallDialer
,
175 public CommDialerParamsT
<CommAcceptCbParams
>
178 typedef CommAcceptCbParams Params
;
180 CommAcceptCbPtrFun(IOACB
*aHandler
, const CommAcceptCbParams
&aParams
);
183 virtual void print(std::ostream
&os
) const;
189 // connect (CNCB) dialer
190 class CommConnectCbPtrFun
: public CallDialer
,
191 public CommDialerParamsT
<CommConnectCbParams
>
194 typedef CommConnectCbParams Params
;
196 CommConnectCbPtrFun(CNCB
*aHandler
, const Params
&aParams
);
199 virtual void print(std::ostream
&os
) const;
206 // read/write (IOCB) dialer
207 class CommIoCbPtrFun
: public CallDialer
,
208 public CommDialerParamsT
<CommIoCbParams
>
211 typedef CommIoCbParams Params
;
213 CommIoCbPtrFun(IOCB
*aHandler
, const Params
&aParams
);
216 virtual void print(std::ostream
&os
) const;
224 class CommCloseCbPtrFun
: public CallDialer
,
225 public CommDialerParamsT
<CommCloseCbParams
>
228 typedef CommCloseCbParams Params
;
230 CommCloseCbPtrFun(PF
*aHandler
, const Params
&aParams
);
233 virtual void print(std::ostream
&os
) const;
239 class CommTimeoutCbPtrFun
:public CallDialer
,
240 public CommDialerParamsT
<CommTimeoutCbParams
>
243 typedef CommTimeoutCbParams Params
;
245 CommTimeoutCbPtrFun(PF
*aHandler
, const Params
&aParams
);
248 virtual void print(std::ostream
&os
) const;
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
258 template <class Dialer
>
259 class CommCbFunPtrCallT
: public AsyncCall
262 typedef typename
Dialer::Params Params
;
264 inline CommCbFunPtrCallT(int debugSection
, int debugLevel
,
265 const char *callName
, const Dialer
&aDialer
);
267 virtual CallDialer
* getDialer() { return &dialer
; }
273 inline virtual bool canFire();
274 inline virtual void fire();
277 // Conveninece wrapper: It is often easier to call a templated function than
278 // to create a templated class.
279 template <class Dialer
>
281 CommCbFunPtrCallT
<Dialer
> *commCbCall(int debugSection
, int debugLevel
,
282 const char *callName
, const Dialer
&dialer
)
284 return new CommCbFunPtrCallT
<Dialer
>(debugSection
, debugLevel
, callName
,
288 /* inlined implementation of templated methods */
290 /* CommCbFunPtrCallT */
292 template <class Dialer
>
293 CommCbFunPtrCallT
<Dialer
>::CommCbFunPtrCallT(int aDebugSection
, int aDebugLevel
,
294 const char *callName
, const Dialer
&aDialer
):
295 AsyncCall(aDebugSection
, aDebugLevel
, callName
),
301 template <class Dialer
>
303 CommCbFunPtrCallT
<Dialer
>::canFire()
305 if (!AsyncCall::canFire())
308 if (!cbdataReferenceValid(dialer
.params
.data
))
309 return cancel("callee gone");
311 if (!dialer
.params
.syncWithComm())
312 return cancel("out of sync w/comm");
315 return cancel("no callback requested");
320 template <class Dialer
>
322 CommCbFunPtrCallT
<Dialer
>::fire()
327 #endif /* SQUID_COMMCALLS_H */