6 #ifndef SQUID_COMMCALLS_H
7 #define SQUID_COMMCALLS_H
10 #include "ConnectionDetail.h"
11 #include "AsyncCall.h"
12 #include "AsyncJobCalls.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:
25 * TODO: When there are no function-pointer-based callbacks left, all
26 * this complexity can be removed. Jobs that need comm services will just
27 * implement CommReader, CommWriter, etc. interfaces and receive calls
28 * using general (not comm-specific) AsyncCall code. For now, we have to
29 * allow the caller to create a callback that comm can modify to set
30 * parameters, which is not trivial when the caller type/kind is not
31 * known to comm and there are many kinds of parameters.
35 /* Comm*CbParams classes below handle callback parameters */
37 // Maintains parameters common to all comm callbacks
38 class CommCommonCbParams
41 CommCommonCbParams(void *aData
);
42 CommCommonCbParams(const CommCommonCbParams
¶ms
);
43 ~CommCommonCbParams();
45 /// adjust using the current Comm state; returns false to cancel the call
46 // not virtual because callers know dialer type
47 bool syncWithComm() { return true; }
49 void print(std::ostream
&os
) const;
52 void *data
; // cbdata-protected
58 // should not be needed and not yet implemented
59 CommCommonCbParams
&operator =(const CommCommonCbParams
¶ms
);
63 class CommAcceptCbParams
: public CommCommonCbParams
66 CommAcceptCbParams(void *aData
);
68 void print(std::ostream
&os
) const;
71 ConnectionDetail details
;
72 int nfd
; // TODO: rename to fdNew or somesuch
76 class CommConnectCbParams
: public CommCommonCbParams
79 CommConnectCbParams(void *aData
);
81 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
84 // read/write (I/O) parameters
85 class CommIoCbParams
: public CommCommonCbParams
88 CommIoCbParams(void *aData
);
90 void print(std::ostream
&os
) const;
91 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
99 class CommCloseCbParams
: public CommCommonCbParams
102 CommCloseCbParams(void *aData
);
105 class CommTimeoutCbParams
: public CommCommonCbParams
108 CommTimeoutCbParams(void *aData
);
111 // Interface to expose comm callback parameters of all comm dialers.
112 // GetCommParams() uses this interface to access comm parameters.
113 template <class Params_
>
114 class CommDialerParamsT
117 typedef Params_ Params
;
118 CommDialerParamsT(const Params
&io
): params(io
) {}
124 // Get comm params of an async comm call
125 template <class Params
>
126 Params
&GetCommParams(AsyncCall::Pointer
&call
)
128 typedef CommDialerParamsT
<Params
> DialerParams
;
129 DialerParams
*dp
= dynamic_cast<DialerParams
*>(call
->getDialer());
135 // All job dialers with comm parameters are merged into one since they
136 // all have exactly one callback argument and differ in Params type only
137 template <class C
, class Params_
>
138 class CommCbMemFunT
: public JobDialer
, public CommDialerParamsT
<Params_
>
141 typedef Params_ Params
;
142 typedef void (C::*Method
)(const Params
&io
);
144 CommCbMemFunT(C
*obj
, Method meth
): JobDialer(obj
),
145 CommDialerParamsT
<Params
>(obj
), object(obj
), method(meth
) {}
147 virtual bool canDial(AsyncCall
&c
) {
148 return JobDialer::canDial(c
) &&
149 this->params
.syncWithComm();
152 virtual void print(std::ostream
&os
) const {
154 this->params
.print(os
);
163 virtual void doDial() { (object
->*method
)(this->params
); }
167 // accept (IOACB) dialer
168 class CommAcceptCbPtrFun
: public CallDialer
,
169 public CommDialerParamsT
<CommAcceptCbParams
>
172 typedef CommAcceptCbParams Params
;
174 CommAcceptCbPtrFun(IOACB
*aHandler
, const CommAcceptCbParams
&aParams
);
177 virtual void print(std::ostream
&os
) const;
183 // connect (CNCB) dialer
184 class CommConnectCbPtrFun
: public CallDialer
,
185 public CommDialerParamsT
<CommConnectCbParams
>
188 typedef CommConnectCbParams Params
;
190 CommConnectCbPtrFun(CNCB
*aHandler
, const Params
&aParams
);
193 virtual void print(std::ostream
&os
) const;
200 // read/write (IOCB) dialer
201 class CommIoCbPtrFun
: public CallDialer
,
202 public CommDialerParamsT
<CommIoCbParams
>
205 typedef CommIoCbParams Params
;
207 CommIoCbPtrFun(IOCB
*aHandler
, const Params
&aParams
);
210 virtual void print(std::ostream
&os
) const;
218 class CommCloseCbPtrFun
: public CallDialer
,
219 public CommDialerParamsT
<CommCloseCbParams
>
222 typedef CommCloseCbParams Params
;
224 CommCloseCbPtrFun(PF
*aHandler
, const Params
&aParams
);
227 virtual void print(std::ostream
&os
) const;
233 class CommTimeoutCbPtrFun
:public CallDialer
,
234 public CommDialerParamsT
<CommTimeoutCbParams
>
237 typedef CommTimeoutCbParams Params
;
239 CommTimeoutCbPtrFun(PF
*aHandler
, const Params
&aParams
);
242 virtual void print(std::ostream
&os
) const;
248 // AsyncCall to comm handlers implemented as global functions.
249 // The dialer is one of the Comm*CbPtrFunT above
250 // TODO: Get rid of this class by moving canFire() to canDial() method
252 template <class Dialer
>
253 class CommCbFunPtrCallT
: public AsyncCall
256 typedef typename
Dialer::Params Params
;
258 inline CommCbFunPtrCallT(int debugSection
, int debugLevel
,
259 const char *callName
, const Dialer
&aDialer
);
261 virtual CallDialer
* getDialer() { return &dialer
; }
267 inline virtual bool canFire();
268 inline virtual void fire();
271 // Conveninece wrapper: It is often easier to call a templated function than
272 // to create a templated class.
273 template <class Dialer
>
275 CommCbFunPtrCallT
<Dialer
> *commCbCall(int debugSection
, int debugLevel
,
276 const char *callName
, const Dialer
&dialer
)
278 return new CommCbFunPtrCallT
<Dialer
>(debugSection
, debugLevel
, callName
,
282 /* inlined implementation of templated methods */
284 /* CommCbFunPtrCallT */
286 template <class Dialer
>
287 CommCbFunPtrCallT
<Dialer
>::CommCbFunPtrCallT(int debugSection
, int debugLevel
,
288 const char *callName
, const Dialer
&aDialer
):
289 AsyncCall(debugSection
, debugLevel
, callName
),
295 template <class Dialer
>
297 CommCbFunPtrCallT
<Dialer
>::canFire()
299 if (!AsyncCall::canFire())
302 if (!cbdataReferenceValid(dialer
.params
.data
))
303 return cancel("callee gone");
305 if (!dialer
.params
.syncWithComm())
306 return cancel("out of sync w/comm");
311 template <class Dialer
>
313 CommCbFunPtrCallT
<Dialer
>::fire()
318 #endif /* SQUID_COMMCALLS_H */