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(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
62 // should not be needed and not yet implemented
63 CommCommonCbParams
&operator =(const CommCommonCbParams
¶ms
);
67 class CommAcceptCbParams
: public CommCommonCbParams
70 CommAcceptCbParams(void *aData
);
72 void print(std::ostream
&os
) const;
75 Comm::ConnectionPointer details
;
76 int nfd
; // TODO: rename to fdNew or somesuch
80 class CommConnectCbParams
: public CommCommonCbParams
83 CommConnectCbParams(void *aData
);
85 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
87 void print(std::ostream
&os
) const;
90 Comm::ConnectionPointer conn
;
93 // read/write (I/O) parameters
94 class CommIoCbParams
: public CommCommonCbParams
97 CommIoCbParams(void *aData
);
99 void print(std::ostream
&os
) const;
100 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
108 class CommCloseCbParams
: public CommCommonCbParams
111 CommCloseCbParams(void *aData
);
114 class CommTimeoutCbParams
: public CommCommonCbParams
117 CommTimeoutCbParams(void *aData
);
120 // Interface to expose comm callback parameters of all comm dialers.
121 // GetCommParams() uses this interface to access comm parameters.
122 template <class Params_
>
123 class CommDialerParamsT
126 typedef Params_ Params
;
127 CommDialerParamsT(const Params
&io
): params(io
) {}
133 // Get comm params of an async comm call
134 template <class Params
>
135 Params
&GetCommParams(AsyncCall::Pointer
&call
)
137 typedef CommDialerParamsT
<Params
> DialerParams
;
138 DialerParams
*dp
= dynamic_cast<DialerParams
*>(call
->getDialer());
144 // All job dialers with comm parameters are merged into one since they
145 // all have exactly one callback argument and differ in Params type only
146 template <class C
, class Params_
>
147 class CommCbMemFunT
: public JobDialer
, public CommDialerParamsT
<Params_
>
150 typedef Params_ Params
;
151 typedef void (C::*Method
)(const Params
&io
);
153 CommCbMemFunT(C
*obj
, Method meth
): JobDialer(obj
),
154 CommDialerParamsT
<Params
>(obj
), object(obj
), method(meth
) {}
156 virtual bool canDial(AsyncCall
&c
) {
157 return JobDialer::canDial(c
) &&
158 this->params
.syncWithComm();
161 virtual void print(std::ostream
&os
) const {
163 this->params
.print(os
);
172 virtual void doDial() { (object
->*method
)(this->params
); }
176 // accept (IOACB) dialer
177 class CommAcceptCbPtrFun
: public CallDialer
,
178 public CommDialerParamsT
<CommAcceptCbParams
>
181 typedef CommAcceptCbParams Params
;
183 CommAcceptCbPtrFun(IOACB
*aHandler
, const CommAcceptCbParams
&aParams
);
186 virtual void print(std::ostream
&os
) const;
192 // connect (CNCB) dialer
193 class CommConnectCbPtrFun
: public CallDialer
,
194 public CommDialerParamsT
<CommConnectCbParams
>
197 typedef CommConnectCbParams Params
;
199 CommConnectCbPtrFun(CNCB
*aHandler
, const Params
&aParams
);
202 virtual void print(std::ostream
&os
) const;
209 // read/write (IOCB) dialer
210 class CommIoCbPtrFun
: public CallDialer
,
211 public CommDialerParamsT
<CommIoCbParams
>
214 typedef CommIoCbParams Params
;
216 CommIoCbPtrFun(IOCB
*aHandler
, const Params
&aParams
);
219 virtual void print(std::ostream
&os
) const;
227 class CommCloseCbPtrFun
: public CallDialer
,
228 public CommDialerParamsT
<CommCloseCbParams
>
231 typedef CommCloseCbParams Params
;
233 CommCloseCbPtrFun(PF
*aHandler
, const Params
&aParams
);
236 virtual void print(std::ostream
&os
) const;
242 class CommTimeoutCbPtrFun
:public CallDialer
,
243 public CommDialerParamsT
<CommTimeoutCbParams
>
246 typedef CommTimeoutCbParams Params
;
248 CommTimeoutCbPtrFun(PF
*aHandler
, const Params
&aParams
);
251 virtual void print(std::ostream
&os
) const;
257 // AsyncCall to comm handlers implemented as global functions.
258 // The dialer is one of the Comm*CbPtrFunT above
259 // TODO: Get rid of this class by moving canFire() to canDial() method
261 template <class Dialer
>
262 class CommCbFunPtrCallT
: public AsyncCall
265 typedef typename
Dialer::Params Params
;
267 inline CommCbFunPtrCallT(int debugSection
, int debugLevel
,
268 const char *callName
, const Dialer
&aDialer
);
270 virtual CallDialer
* getDialer() { return &dialer
; }
276 inline virtual bool canFire();
277 inline virtual void fire();
280 // Conveninece wrapper: It is often easier to call a templated function than
281 // to create a templated class.
282 template <class Dialer
>
284 CommCbFunPtrCallT
<Dialer
> *commCbCall(int debugSection
, int debugLevel
,
285 const char *callName
, const Dialer
&dialer
)
287 return new CommCbFunPtrCallT
<Dialer
>(debugSection
, debugLevel
, callName
,
291 /* inlined implementation of templated methods */
293 /* CommCbFunPtrCallT */
295 template <class Dialer
>
296 CommCbFunPtrCallT
<Dialer
>::CommCbFunPtrCallT(int aDebugSection
, int aDebugLevel
,
297 const char *callName
, const Dialer
&aDialer
):
298 AsyncCall(aDebugSection
, aDebugLevel
, callName
),
304 template <class Dialer
>
306 CommCbFunPtrCallT
<Dialer
>::canFire()
308 if (!AsyncCall::canFire())
311 if (!cbdataReferenceValid(dialer
.params
.data
))
312 return cancel("callee gone");
314 if (!dialer
.params
.syncWithComm())
315 return cancel("out of sync w/comm");
318 return cancel("no callback requested");
323 template <class Dialer
>
325 CommCbFunPtrCallT
<Dialer
>::fire()
330 #endif /* SQUID_COMMCALLS_H */