6 #ifndef SQUID_COMMCALLS_H
7 #define SQUID_COMMCALLS_H
9 #include "base/AsyncCall.h"
10 #include "base/AsyncJobCalls.h"
11 #include "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 several comm_* callback kinds:
24 * and a special callback kind for passing pipe FD, disk FD or fd_table index 'FD' to the handler:
25 * - FD passing callback (FDECB)
28 class CommAcceptCbParams
;
29 typedef void IOACB(const CommAcceptCbParams
¶ms
);
31 typedef void CNCB(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
, void *data
);
32 typedef void IOCB(const Comm::ConnectionPointer
&conn
, char *, size_t size
, comm_err_t flag
, int xerrno
, void *data
);
34 class CommTimeoutCbParams
;
35 typedef void CTCB(const CommTimeoutCbParams
¶ms
);
37 class CommCloseCbParams
;
38 typedef void CLCB(const CommCloseCbParams
¶ms
);
41 typedef void FDECB(const FdeCbParams
¶ms
);
44 * TODO: When there are no function-pointer-based callbacks left, all
45 * this complexity can be removed. Jobs that need comm services will just
46 * implement CommReader, CommWriter, etc. interfaces and receive calls
47 * using general (not comm-specific) AsyncCall code. For now, we have to
48 * allow the caller to create a callback that comm can modify to set
49 * parameters, which is not trivial when the caller type/kind is not
50 * known to comm and there are many kinds of parameters.
53 /* Comm*CbParams classes below handle callback parameters */
55 // Maintains parameters common to all comm callbacks
56 class CommCommonCbParams
59 CommCommonCbParams(void *aData
);
60 CommCommonCbParams(const CommCommonCbParams
¶ms
);
61 ~CommCommonCbParams();
63 /// adjust using the current Comm state; returns false to cancel the call
64 // not virtual because callers know dialer type
65 bool syncWithComm() { return true; }
67 void print(std::ostream
&os
) const;
70 void *data
; // cbdata-protected
72 /** The connection which this call pertains to.
73 * \itemize On accept() calls this is the new client connection.
74 * \itemize On connect() finished calls this is the newely opened connection.
75 * \itemize On write calls this is the connection just written to.
76 * \itemize On read calls this is the connection just read from.
77 * \itemize On close calls this describes the connection which is now closed.
78 * \itemize On timeouts this is the connection whose operation timed out.
79 * NP: timeouts might also return to the connect/read/write handler with COMM_ERR_TIMEOUT.
81 Comm::ConnectionPointer conn
;
83 comm_err_t flag
; ///< comm layer result status.
84 int xerrno
; ///< The last errno to occur. non-zero if flag is COMM_ERR.
86 int fd
; ///< FD which the call was about. Set by the async call creator.
88 // should not be needed and not yet implemented
89 CommCommonCbParams
&operator =(const CommCommonCbParams
¶ms
);
93 class CommAcceptCbParams
: public CommCommonCbParams
96 CommAcceptCbParams(void *aData
);
100 class CommConnectCbParams
: public CommCommonCbParams
103 CommConnectCbParams(void *aData
);
105 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
108 // read/write (I/O) parameters
109 class CommIoCbParams
: public CommCommonCbParams
112 CommIoCbParams(void *aData
);
114 void print(std::ostream
&os
) const;
115 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
123 class CommCloseCbParams
: public CommCommonCbParams
126 CommCloseCbParams(void *aData
);
129 class CommTimeoutCbParams
: public CommCommonCbParams
132 CommTimeoutCbParams(void *aData
);
135 /// Special Calls parameter, for direct use of an FD without a controlling Comm::Connection
136 /// This is used for pipe() FD with helpers, and internally by Comm when handling some special FD actions.
137 class FdeCbParams
: public CommCommonCbParams
140 FdeCbParams(void *aData
);
141 // TODO make this a standalone object with FD value and pointer to fde table entry.
142 // that requires all the existing Comm handlers to be updated first though
145 // Interface to expose comm callback parameters of all comm dialers.
146 // GetCommParams() uses this interface to access comm parameters.
147 template <class Params_
>
148 class CommDialerParamsT
151 typedef Params_ Params
;
152 CommDialerParamsT(const Params
&io
): params(io
) {}
158 // Get comm params of an async comm call
159 template <class Params
>
160 Params
&GetCommParams(AsyncCall::Pointer
&call
)
162 typedef CommDialerParamsT
<Params
> DialerParams
;
163 DialerParams
*dp
= dynamic_cast<DialerParams
*>(call
->getDialer());
168 // All job dialers with comm parameters are merged into one since they
169 // all have exactly one callback argument and differ in Params type only
170 template <class C
, class Params_
>
171 class CommCbMemFunT
: public JobDialer
<C
>, public CommDialerParamsT
<Params_
>
174 typedef Params_ Params
;
175 typedef void (C::*Method
)(const Params
&io
);
177 CommCbMemFunT(const CbcPointer
<C
> &job
, Method meth
): JobDialer
<C
>(job
),
178 CommDialerParamsT
<Params_
>(job
.get()),
181 virtual bool canDial(AsyncCall
&c
) {
182 return JobDialer
<C
>::canDial(c
) &&
183 this->params
.syncWithComm();
186 virtual void print(std::ostream
&os
) const {
188 this->params
.print(os
);
196 virtual void doDial() { ((&(*this->job
))->*method
)(this->params
); }
199 // accept (IOACB) dialer
200 class CommAcceptCbPtrFun
: public CallDialer
,
201 public CommDialerParamsT
<CommAcceptCbParams
>
204 typedef CommAcceptCbParams Params
;
205 typedef RefCount
<CommAcceptCbPtrFun
> Pointer
;
207 CommAcceptCbPtrFun(IOACB
*aHandler
, const CommAcceptCbParams
&aParams
);
208 CommAcceptCbPtrFun(const CommAcceptCbPtrFun
&o
);
212 virtual void print(std::ostream
&os
) const;
218 // connect (CNCB) dialer
219 class CommConnectCbPtrFun
: public CallDialer
,
220 public CommDialerParamsT
<CommConnectCbParams
>
223 typedef CommConnectCbParams Params
;
225 CommConnectCbPtrFun(CNCB
*aHandler
, const Params
&aParams
);
228 virtual void print(std::ostream
&os
) const;
234 // read/write (IOCB) dialer
235 class CommIoCbPtrFun
: public CallDialer
,
236 public CommDialerParamsT
<CommIoCbParams
>
239 typedef CommIoCbParams Params
;
241 CommIoCbPtrFun(IOCB
*aHandler
, const Params
&aParams
);
244 virtual void print(std::ostream
&os
) const;
250 // close (CLCB) dialer
251 class CommCloseCbPtrFun
: public CallDialer
,
252 public CommDialerParamsT
<CommCloseCbParams
>
255 typedef CommCloseCbParams Params
;
257 CommCloseCbPtrFun(CLCB
*aHandler
, const Params
&aParams
);
260 virtual void print(std::ostream
&os
) const;
266 class CommTimeoutCbPtrFun
:public CallDialer
,
267 public CommDialerParamsT
<CommTimeoutCbParams
>
270 typedef CommTimeoutCbParams Params
;
272 CommTimeoutCbPtrFun(CTCB
*aHandler
, const Params
&aParams
);
275 virtual void print(std::ostream
&os
) const;
281 /// FD event (FDECB) dialer
282 class FdeCbPtrFun
: public CallDialer
,
283 public CommDialerParamsT
<FdeCbParams
>
286 typedef FdeCbParams Params
;
288 FdeCbPtrFun(FDECB
*aHandler
, const Params
&aParams
);
290 virtual void print(std::ostream
&os
) const;
296 // AsyncCall to comm handlers implemented as global functions.
297 // The dialer is one of the Comm*CbPtrFunT above
298 // TODO: Get rid of this class by moving canFire() to canDial() method
300 template <class Dialer
>
301 class CommCbFunPtrCallT
: public AsyncCall
304 typedef RefCount
<CommCbFunPtrCallT
<Dialer
> > Pointer
;
305 typedef typename
Dialer::Params Params
;
307 inline CommCbFunPtrCallT(int debugSection
, int debugLevel
,
308 const char *callName
, const Dialer
&aDialer
);
310 inline CommCbFunPtrCallT(const CommCbFunPtrCallT
&o
) :
311 AsyncCall(o
.debugSection
, o
.debugLevel
, o
.name
),
314 ~CommCbFunPtrCallT() {}
316 virtual CallDialer
* getDialer() { return &dialer
; }
322 inline virtual bool canFire();
323 inline virtual void fire();
326 CommCbFunPtrCallT
& operator=(const CommCbFunPtrCallT
&); // not defined. not permitted.
329 // Conveninece wrapper: It is often easier to call a templated function than
330 // to create a templated class.
331 template <class Dialer
>
333 CommCbFunPtrCallT
<Dialer
> *commCbCall(int debugSection
, int debugLevel
,
334 const char *callName
, const Dialer
&dialer
)
336 return new CommCbFunPtrCallT
<Dialer
>(debugSection
, debugLevel
, callName
,
340 /* inlined implementation of templated methods */
342 /* CommCbFunPtrCallT */
344 template <class Dialer
>
345 CommCbFunPtrCallT
<Dialer
>::CommCbFunPtrCallT(int aDebugSection
, int aDebugLevel
,
346 const char *callName
, const Dialer
&aDialer
):
347 AsyncCall(aDebugSection
, aDebugLevel
, callName
),
352 template <class Dialer
>
354 CommCbFunPtrCallT
<Dialer
>::canFire()
356 if (!AsyncCall::canFire())
359 if (!cbdataReferenceValid(dialer
.params
.data
))
360 return cancel("callee gone");
362 if (!dialer
.params
.syncWithComm())
363 return cancel("out of sync w/comm");
366 return cancel("no callback requested");
371 template <class Dialer
>
373 CommCbFunPtrCallT
<Dialer
>::fire()
378 #endif /* SQUID_COMMCALLS_H */