6 #ifndef SQUID_COMMCALLS_H
7 #define SQUID_COMMCALLS_H
9 #include "base/AsyncCall.h"
10 #include "base/AsyncJobCalls.h"
11 #include "comm/comm_err_t.h"
12 #include "comm/forward.h"
14 /* CommCalls implement AsyncCall interface for comm_* callbacks.
15 * The classes cover two call dialer kinds:
16 * - A C-style call using a function pointer (depricated);
17 * - A C++-style call to an AsyncJob child.
18 * and three comm_* callback kinds:
24 typedef void IOACB(int fd
, int nfd
, Comm::ConnectionPointer
&details
, comm_err_t flag
, int xerrno
, void *data
);
25 typedef void CNCB(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
, void *data
);
26 typedef void IOCB(int fd
, char *, size_t size
, comm_err_t flag
, int xerrno
, void *data
);
29 * TODO: When there are no function-pointer-based callbacks left, all
30 * this complexity can be removed. Jobs that need comm services will just
31 * implement CommReader, CommWriter, etc. interfaces and receive calls
32 * using general (not comm-specific) AsyncCall code. For now, we have to
33 * allow the caller to create a callback that comm can modify to set
34 * parameters, which is not trivial when the caller type/kind is not
35 * known to comm and there are many kinds of parameters.
39 /* Comm*CbParams classes below handle callback parameters */
41 // Maintains parameters common to all comm callbacks
42 class CommCommonCbParams
45 CommCommonCbParams(void *aData
);
46 CommCommonCbParams(const CommCommonCbParams
¶ms
);
47 ~CommCommonCbParams();
49 /// adjust using the current Comm state; returns false to cancel the call
50 // not virtual because callers know dialer type
51 bool syncWithComm() { return true; }
53 void print(std::ostream
&os
) const;
56 void *data
; // cbdata-protected
57 Comm::ConnectionPointer conn
;
58 int fd
; // raw FD from legacy calls. use conn instead.
63 // should not be needed and not yet implemented
64 CommCommonCbParams
&operator =(const CommCommonCbParams
¶ms
);
68 class CommAcceptCbParams
: public CommCommonCbParams
71 CommAcceptCbParams(void *aData
);
73 void print(std::ostream
&os
) const;
76 Comm::ConnectionPointer details
;
77 int nfd
; // TODO: rename to fdNew or somesuch
81 class CommConnectCbParams
: public CommCommonCbParams
84 CommConnectCbParams(void *aData
);
86 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
89 // read/write (I/O) parameters
90 class CommIoCbParams
: public CommCommonCbParams
93 CommIoCbParams(void *aData
);
95 void print(std::ostream
&os
) const;
96 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
104 class CommCloseCbParams
: public CommCommonCbParams
107 CommCloseCbParams(void *aData
);
110 class CommTimeoutCbParams
: public CommCommonCbParams
113 CommTimeoutCbParams(void *aData
);
116 // Interface to expose comm callback parameters of all comm dialers.
117 // GetCommParams() uses this interface to access comm parameters.
118 template <class Params_
>
119 class CommDialerParamsT
122 typedef Params_ Params
;
123 CommDialerParamsT(const Params
&io
): params(io
) {}
129 // Get comm params of an async comm call
130 template <class Params
>
131 Params
&GetCommParams(AsyncCall::Pointer
&call
)
133 typedef CommDialerParamsT
<Params
> DialerParams
;
134 DialerParams
*dp
= dynamic_cast<DialerParams
*>(call
->getDialer());
140 // All job dialers with comm parameters are merged into one since they
141 // all have exactly one callback argument and differ in Params type only
142 template <class C
, class Params_
>
143 class CommCbMemFunT
: public JobDialer
<C
>, public CommDialerParamsT
<Params_
>
146 typedef Params_ Params
;
147 typedef void (C::*Method
)(const Params
&io
);
149 CommCbMemFunT(const CbcPointer
<C
> &job
, Method meth
): JobDialer
<C
>(job
),
150 CommDialerParamsT
<Params_
>(job
.get()),
153 virtual bool canDial(AsyncCall
&c
) {
154 return JobDialer
<C
>::canDial(c
) &&
155 this->params
.syncWithComm();
158 virtual void print(std::ostream
&os
) const {
160 this->params
.print(os
);
168 virtual void doDial() { ((&(*this->job
))->*method
)(this->params
); }
172 // accept (IOACB) dialer
173 class CommAcceptCbPtrFun
: public CallDialer
,
174 public CommDialerParamsT
<CommAcceptCbParams
>
177 typedef CommAcceptCbParams Params
;
179 CommAcceptCbPtrFun(IOACB
*aHandler
, const CommAcceptCbParams
&aParams
);
182 virtual void print(std::ostream
&os
) const;
188 // connect (CNCB) dialer
189 class CommConnectCbPtrFun
: public CallDialer
,
190 public CommDialerParamsT
<CommConnectCbParams
>
193 typedef CommConnectCbParams Params
;
195 CommConnectCbPtrFun(CNCB
*aHandler
, const Params
&aParams
);
198 virtual void print(std::ostream
&os
) const;
205 // read/write (IOCB) dialer
206 class CommIoCbPtrFun
: public CallDialer
,
207 public CommDialerParamsT
<CommIoCbParams
>
210 typedef CommIoCbParams Params
;
212 CommIoCbPtrFun(IOCB
*aHandler
, const Params
&aParams
);
215 virtual void print(std::ostream
&os
) const;
223 class CommCloseCbPtrFun
: public CallDialer
,
224 public CommDialerParamsT
<CommCloseCbParams
>
227 typedef CommCloseCbParams Params
;
229 CommCloseCbPtrFun(PF
*aHandler
, const Params
&aParams
);
232 virtual void print(std::ostream
&os
) const;
238 class CommTimeoutCbPtrFun
:public CallDialer
,
239 public CommDialerParamsT
<CommTimeoutCbParams
>
242 typedef CommTimeoutCbParams Params
;
244 CommTimeoutCbPtrFun(PF
*aHandler
, const Params
&aParams
);
247 virtual void print(std::ostream
&os
) const;
253 // AsyncCall to comm handlers implemented as global functions.
254 // The dialer is one of the Comm*CbPtrFunT above
255 // TODO: Get rid of this class by moving canFire() to canDial() method
257 template <class Dialer
>
258 class CommCbFunPtrCallT
: public AsyncCall
261 typedef typename
Dialer::Params Params
;
263 inline CommCbFunPtrCallT(int debugSection
, int debugLevel
,
264 const char *callName
, const Dialer
&aDialer
);
266 virtual CallDialer
* getDialer() { return &dialer
; }
272 inline virtual bool canFire();
273 inline virtual void fire();
276 // Conveninece wrapper: It is often easier to call a templated function than
277 // to create a templated class.
278 template <class Dialer
>
280 CommCbFunPtrCallT
<Dialer
> *commCbCall(int debugSection
, int debugLevel
,
281 const char *callName
, const Dialer
&dialer
)
283 return new CommCbFunPtrCallT
<Dialer
>(debugSection
, debugLevel
, callName
,
287 /* inlined implementation of templated methods */
289 /* CommCbFunPtrCallT */
291 template <class Dialer
>
292 CommCbFunPtrCallT
<Dialer
>::CommCbFunPtrCallT(int aDebugSection
, int aDebugLevel
,
293 const char *callName
, const Dialer
&aDialer
):
294 AsyncCall(aDebugSection
, aDebugLevel
, callName
),
300 template <class Dialer
>
302 CommCbFunPtrCallT
<Dialer
>::canFire()
304 if (!AsyncCall::canFire())
307 if (!cbdataReferenceValid(dialer
.params
.data
))
308 return cancel("callee gone");
310 if (!dialer
.params
.syncWithComm())
311 return cancel("out of sync w/comm");
314 return cancel("no callback requested");
319 template <class Dialer
>
321 CommCbFunPtrCallT
<Dialer
>::fire()
326 #endif /* SQUID_COMMCALLS_H */