1 #ifndef SQUID_COMMCALLS_H
2 #define SQUID_COMMCALLS_H
4 #include "base/AsyncCall.h"
5 #include "base/AsyncJobCalls.h"
6 #include "comm/forward.h"
7 #include "comm_err_t.h"
8 #include "MasterXaction.h"
10 /* CommCalls implement AsyncCall interface for comm_* callbacks.
11 * The classes cover two call dialer kinds:
12 * - A C-style call using a function pointer (depricated);
13 * - A C++-style call to an AsyncJob child.
14 * and several comm_* callback kinds:
20 * and a special callback kind for passing pipe FD, disk FD or fd_table index 'FD' to the handler:
21 * - FD passing callback (FDECB)
24 class CommAcceptCbParams
;
25 typedef void IOACB(const CommAcceptCbParams
¶ms
);
27 typedef void CNCB(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
, void *data
);
28 typedef void IOCB(const Comm::ConnectionPointer
&conn
, char *, size_t size
, comm_err_t flag
, int xerrno
, void *data
);
30 class CommTimeoutCbParams
;
31 typedef void CTCB(const CommTimeoutCbParams
¶ms
);
33 class CommCloseCbParams
;
34 typedef void CLCB(const CommCloseCbParams
¶ms
);
37 typedef void FDECB(const FdeCbParams
¶ms
);
40 * TODO: When there are no function-pointer-based callbacks left, all
41 * this complexity can be removed. Jobs that need comm services will just
42 * implement CommReader, CommWriter, etc. interfaces and receive calls
43 * using general (not comm-specific) AsyncCall code. For now, we have to
44 * allow the caller to create a callback that comm can modify to set
45 * parameters, which is not trivial when the caller type/kind is not
46 * known to comm and there are many kinds of parameters.
49 /* Comm*CbParams classes below handle callback parameters */
51 // Maintains parameters common to all comm callbacks
52 class CommCommonCbParams
55 CommCommonCbParams(void *aData
);
56 CommCommonCbParams(const CommCommonCbParams
¶ms
);
57 ~CommCommonCbParams();
59 /// adjust using the current Comm state; returns false to cancel the call
60 // not virtual because callers know dialer type
61 bool syncWithComm() { return true; }
63 void print(std::ostream
&os
) const;
66 void *data
; // cbdata-protected
68 /** The connection which this call pertains to.
69 * - On accept() calls this is the new client connection.
70 * - On connect() finished calls this is the newely opened connection.
71 * - On write calls this is the connection just written to.
72 * - On read calls this is the connection just read from.
73 * - On close calls this describes the connection which is now closed.
74 * - On timeouts this is the connection whose operation timed out.
75 * + NP: timeouts might also return to the connect/read/write handler with COMM_ERR_TIMEOUT.
77 Comm::ConnectionPointer conn
;
79 comm_err_t flag
; ///< comm layer result status.
80 int xerrno
; ///< The last errno to occur. non-zero if flag is COMM_ERR.
82 int fd
; ///< FD which the call was about. Set by the async call creator.
84 // should not be needed and not yet implemented
85 CommCommonCbParams
&operator =(const CommCommonCbParams
¶ms
);
89 class CommAcceptCbParams
: public CommCommonCbParams
92 CommAcceptCbParams(void *aData
);
94 void print(std::ostream
&os
) const;
96 /// Transaction which this call is part of.
97 MasterXaction::Pointer xaction
;
100 // connect parameters
101 class CommConnectCbParams
: public CommCommonCbParams
104 CommConnectCbParams(void *aData
);
106 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
111 // read/write (I/O) parameters
112 class CommIoCbParams
: public CommCommonCbParams
115 CommIoCbParams(void *aData
);
117 void print(std::ostream
&os
) const;
118 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
123 SBuf
*buf2
; // alternative buffer for use when buf is unset
127 class CommCloseCbParams
: public CommCommonCbParams
130 CommCloseCbParams(void *aData
);
133 class CommTimeoutCbParams
: public CommCommonCbParams
136 CommTimeoutCbParams(void *aData
);
139 /// Special Calls parameter, for direct use of an FD without a controlling Comm::Connection
140 /// This is used for pipe() FD with helpers, and internally by Comm when handling some special FD actions.
141 class FdeCbParams
: public CommCommonCbParams
144 FdeCbParams(void *aData
);
145 // TODO make this a standalone object with FD value and pointer to fde table entry.
146 // that requires all the existing Comm handlers to be updated first though
149 // Interface to expose comm callback parameters of all comm dialers.
150 // GetCommParams() uses this interface to access comm parameters.
151 template <class Params_
>
152 class CommDialerParamsT
155 typedef Params_ Params
;
156 CommDialerParamsT(const Params
&io
): params(io
) {}
162 // Get comm params of an async comm call
163 template <class Params
>
164 Params
&GetCommParams(AsyncCall::Pointer
&call
)
166 typedef CommDialerParamsT
<Params
> DialerParams
;
167 DialerParams
*dp
= dynamic_cast<DialerParams
*>(call
->getDialer());
172 // All job dialers with comm parameters are merged into one since they
173 // all have exactly one callback argument and differ in Params type only
174 template <class C
, class Params_
>
175 class CommCbMemFunT
: public JobDialer
<C
>, public CommDialerParamsT
<Params_
>
178 typedef Params_ Params
;
179 typedef void (C::*Method
)(const Params
&io
);
181 CommCbMemFunT(const CbcPointer
<C
> &aJob
, Method aMeth
): JobDialer
<C
>(aJob
),
182 CommDialerParamsT
<Params_
>(aJob
->toCbdata()),
185 virtual bool canDial(AsyncCall
&c
) {
186 return JobDialer
<C
>::canDial(c
) &&
187 this->params
.syncWithComm();
190 virtual void print(std::ostream
&os
) const {
192 this->params
.print(os
);
200 virtual void doDial() { ((&(*this->job
))->*method
)(this->params
); }
203 // accept (IOACB) dialer
204 class CommAcceptCbPtrFun
: public CallDialer
,
205 public CommDialerParamsT
<CommAcceptCbParams
>
208 typedef CommAcceptCbParams Params
;
209 typedef RefCount
<CommAcceptCbPtrFun
> Pointer
;
211 CommAcceptCbPtrFun(IOACB
*aHandler
, const CommAcceptCbParams
&aParams
);
212 CommAcceptCbPtrFun(const CommAcceptCbPtrFun
&o
);
216 virtual void print(std::ostream
&os
) const;
222 // connect (CNCB) dialer
223 class CommConnectCbPtrFun
: public CallDialer
,
224 public CommDialerParamsT
<CommConnectCbParams
>
227 typedef CommConnectCbParams Params
;
229 CommConnectCbPtrFun(CNCB
*aHandler
, const Params
&aParams
);
232 virtual void print(std::ostream
&os
) const;
238 // read/write (IOCB) dialer
239 class CommIoCbPtrFun
: public CallDialer
,
240 public CommDialerParamsT
<CommIoCbParams
>
243 typedef CommIoCbParams Params
;
245 CommIoCbPtrFun(IOCB
*aHandler
, const Params
&aParams
);
248 virtual void print(std::ostream
&os
) const;
254 // close (CLCB) dialer
255 class CommCloseCbPtrFun
: public CallDialer
,
256 public CommDialerParamsT
<CommCloseCbParams
>
259 typedef CommCloseCbParams Params
;
261 CommCloseCbPtrFun(CLCB
*aHandler
, const Params
&aParams
);
264 virtual void print(std::ostream
&os
) const;
270 class CommTimeoutCbPtrFun
:public CallDialer
,
271 public CommDialerParamsT
<CommTimeoutCbParams
>
274 typedef CommTimeoutCbParams Params
;
276 CommTimeoutCbPtrFun(CTCB
*aHandler
, const Params
&aParams
);
279 virtual void print(std::ostream
&os
) const;
285 /// FD event (FDECB) dialer
286 class FdeCbPtrFun
: public CallDialer
,
287 public CommDialerParamsT
<FdeCbParams
>
290 typedef FdeCbParams Params
;
292 FdeCbPtrFun(FDECB
*aHandler
, const Params
&aParams
);
294 virtual void print(std::ostream
&os
) const;
300 // AsyncCall to comm handlers implemented as global functions.
301 // The dialer is one of the Comm*CbPtrFunT above
302 // TODO: Get rid of this class by moving canFire() to canDial() method
304 template <class Dialer
>
305 class CommCbFunPtrCallT
: public AsyncCall
308 typedef RefCount
<CommCbFunPtrCallT
<Dialer
> > Pointer
;
309 typedef typename
Dialer::Params Params
;
311 inline CommCbFunPtrCallT(int debugSection
, int debugLevel
,
312 const char *callName
, const Dialer
&aDialer
);
314 inline CommCbFunPtrCallT(const CommCbFunPtrCallT
&o
) :
315 AsyncCall(o
.debugSection
, o
.debugLevel
, o
.name
),
318 ~CommCbFunPtrCallT() {}
320 virtual CallDialer
* getDialer() { return &dialer
; }
326 inline virtual bool canFire();
327 inline virtual void fire();
330 CommCbFunPtrCallT
& operator=(const CommCbFunPtrCallT
&); // not defined. not permitted.
333 // Conveninece wrapper: It is often easier to call a templated function than
334 // to create a templated class.
335 template <class Dialer
>
337 CommCbFunPtrCallT
<Dialer
> *commCbCall(int debugSection
, int debugLevel
,
338 const char *callName
, const Dialer
&dialer
)
340 return new CommCbFunPtrCallT
<Dialer
>(debugSection
, debugLevel
, callName
,
344 /* inlined implementation of templated methods */
346 /* CommCbFunPtrCallT */
348 template <class Dialer
>
349 CommCbFunPtrCallT
<Dialer
>::CommCbFunPtrCallT(int aDebugSection
, int aDebugLevel
,
350 const char *callName
, const Dialer
&aDialer
):
351 AsyncCall(aDebugSection
, aDebugLevel
, callName
),
356 template <class Dialer
>
358 CommCbFunPtrCallT
<Dialer
>::canFire()
360 if (!AsyncCall::canFire())
363 if (!cbdataReferenceValid(dialer
.params
.data
))
364 return cancel("callee gone");
366 if (!dialer
.params
.syncWithComm())
367 return cancel("out of sync w/comm");
370 return cancel("no callback requested");
375 template <class Dialer
>
377 CommCbFunPtrCallT
<Dialer
>::fire()
382 #endif /* SQUID_COMMCALLS_H */