CommCalls.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_COMMCALLS_H
10#define SQUID_COMMCALLS_H
11
12#include "base/AsyncCall.h"
13#include "base/AsyncJobCalls.h"
14#include "comm/Flag.h"
15#include "comm/forward.h"
16#include "MasterXaction.h"
17
18/* CommCalls implement AsyncCall interface for comm_* callbacks.
19 * The classes cover two call dialer kinds:
20 * - A C-style call using a function pointer (deprecated);
21 * - A C++-style call to an AsyncJob child.
22 * and several comm_* callback kinds:
23 * - accept (IOACB)
24 * - connect (CNCB)
25 * - I/O (IOCB)
26 * - timeout (CTCB)
27 * - close (CLCB)
28 */
29
31typedef void IOACB(const CommAcceptCbParams &params);
32
33typedef void CNCB(const Comm::ConnectionPointer &conn, Comm::Flag status, int xerrno, void *data);
34typedef void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data);
35
37typedef void CTCB(const CommTimeoutCbParams &params);
38
40typedef void CLCB(const CommCloseCbParams &params);
41
42/*
43 * TODO: When there are no function-pointer-based callbacks left, all
44 * this complexity can be removed. Jobs that need comm services will just
45 * implement CommReader, CommWriter, etc. interfaces and receive calls
46 * using general (not comm-specific) AsyncCall code. For now, we have to
47 * allow the caller to create a callback that comm can modify to set
48 * parameters, which is not trivial when the caller type/kind is not
49 * known to comm and there are many kinds of parameters.
50 */
51
52/* Comm*CbParams classes below handle callback parameters */
53
54// Maintains parameters common to all comm callbacks
56{
57public:
58 CommCommonCbParams(void *aData);
61
63 // not virtual because callers know dialer type
64 bool syncWithComm() { return true; }
65
66 void print(std::ostream &os) const;
67
68public:
69 void *data; // cbdata-protected
70
81
83 int xerrno;
84
85 int fd;
86private:
87 // should not be needed and not yet implemented
89};
90
91// accept parameters
93{
94public:
95 CommAcceptCbParams(void *aData);
96
97 void print(std::ostream &os) const;
98
101};
102
103// connect parameters
105{
106public:
107 CommConnectCbParams(void *aData);
108
109 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
110};
111
112// read/write (I/O) parameters
114{
115public:
116 CommIoCbParams(void *aData);
117
118 void print(std::ostream &os) const;
119 bool syncWithComm(); // see CommCommonCbParams::syncWithComm
120
121public:
122 char *buf;
123 size_t size;
124};
125
126// close parameters
128{
129public:
130 CommCloseCbParams(void *aData);
131};
132
134{
135public:
136 CommTimeoutCbParams(void *aData);
137};
138
139// Interface to expose comm callback parameters of all comm dialers.
140// GetCommParams() uses this interface to access comm parameters.
141template <class Params_>
143{
144public:
145 typedef Params_ Params;
146 CommDialerParamsT(const Params &io): params(io) {}
147
148public:
150};
151
152// Get comm params of an async comm call
153template <class Params>
155{
156 typedef CommDialerParamsT<Params> DialerParams;
157 DialerParams *dp = dynamic_cast<DialerParams*>(call->getDialer());
158 assert(dp);
159 return dp->params;
160}
161
162// All job dialers with comm parameters are merged into one since they
163// all have exactly one callback argument and differ in Params type only
164template <class C, class Params_>
165class CommCbMemFunT: public JobDialer<C>, public CommDialerParamsT<Params_>
166{
167public:
168 typedef Params_ Params;
169 typedef void (C::*Method)(const Params &io);
170
171 CommCbMemFunT(const CbcPointer<C> &aJob, Method aMeth): JobDialer<C>(aJob),
172 CommDialerParamsT<Params_>(aJob->toCbdata()),
173 method(aMeth) {}
174
175 bool canDial(AsyncCall &c) override {
176 return JobDialer<C>::canDial(c) &&
177 this->params.syncWithComm();
178 }
179
180 void print(std::ostream &os) const override {
181 os << '(';
182 this->params.print(os);
183 os << ')';
184 }
185
186public:
188
189protected:
190 void doDial() override { ((&(*this->job))->*method)(this->params); }
191};
192
193// accept (IOACB) dialer
195 public CommDialerParamsT<CommAcceptCbParams>
196{
197public:
200
201 CommAcceptCbPtrFun(IOACB *aHandler, const CommAcceptCbParams &aParams);
203
204 void dial();
205
206 void print(std::ostream &os) const override;
207
208public:
210};
211
212// connect (CNCB) dialer
214 public CommDialerParamsT<CommConnectCbParams>
215{
216public:
218
219 CommConnectCbPtrFun(CNCB *aHandler, const Params &aParams);
220 void dial();
221
222 void print(std::ostream &os) const override;
223
224public:
226};
227
228// read/write (IOCB) dialer
230 public CommDialerParamsT<CommIoCbParams>
231{
232public:
234
235 CommIoCbPtrFun(IOCB *aHandler, const Params &aParams);
236 void dial();
237
238 void print(std::ostream &os) const override;
239
240public:
242};
243
244// close (CLCB) dialer
246 public CommDialerParamsT<CommCloseCbParams>
247{
248public:
250
251 CommCloseCbPtrFun(CLCB *aHandler, const Params &aParams);
252 void dial();
253
254 void print(std::ostream &os) const override;
255
256public:
258};
259
261 public CommDialerParamsT<CommTimeoutCbParams>
262{
263public:
265
266 CommTimeoutCbPtrFun(CTCB *aHandler, const Params &aParams);
267 void dial();
268
269 void print(std::ostream &os) const override;
270
271public:
273};
274
275// AsyncCall to comm handlers implemented as global functions.
276// The dialer is one of the Comm*CbPtrFunT above
277// TODO: Get rid of this class by moving canFire() to canDial() method
278// of dialers.
279template <class Dialer>
281{
282public:
284 typedef typename Dialer::Params Params;
285
287 const char *callName, const Dialer &aDialer);
288
291 dialer(o.dialer) {}
292
293 ~CommCbFunPtrCallT() override {}
294
295 CallDialer* getDialer() override { return &dialer; }
296
297public:
298 Dialer dialer;
299
300protected:
301 inline bool canFire() override;
302 inline void fire() override;
303
304private:
305 CommCbFunPtrCallT & operator=(const CommCbFunPtrCallT &); // not defined. not permitted.
306};
307
308// Conveninece wrapper: It is often easier to call a templated function than
309// to create a templated class.
310template <class Dialer>
311inline
312CommCbFunPtrCallT<Dialer> *commCbCall(int debugSection, int debugLevel,
313 const char *callName, const Dialer &dialer)
314{
315 return new CommCbFunPtrCallT<Dialer>(debugSection, debugLevel, callName,
316 dialer);
317}
318
319/* inlined implementation of templated methods */
320
321/* CommCbFunPtrCallT */
322
323template <class Dialer>
324CommCbFunPtrCallT<Dialer>::CommCbFunPtrCallT(int aDebugSection, int aDebugLevel,
325 const char *callName, const Dialer &aDialer):
326 AsyncCall(aDebugSection, aDebugLevel, callName),
327 dialer(aDialer)
328{
329}
330
331template <class Dialer>
332bool
334{
335 if (!AsyncCall::canFire())
336 return false;
337
338 if (!cbdataReferenceValid(dialer.params.data))
339 return cancel("callee gone");
340
341 if (!dialer.params.syncWithComm())
342 return cancel("out of sync w/comm");
343
344 if (!dialer.handler)
345 return cancel("no callback requested");
346
347 return true;
348}
349
350template <class Dialer>
351void
353{
354 dialer.dial();
355}
356
357#endif /* SQUID_COMMCALLS_H */
358
CommCbFunPtrCallT< Dialer > * commCbCall(int debugSection, int debugLevel, const char *callName, const Dialer &dialer)
Definition: CommCalls.h:312
void CTCB(const CommTimeoutCbParams &params)
Definition: CommCalls.h:37
void IOCB(const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int xerrno, void *data)
Definition: CommCalls.h:34
void IOACB(const CommAcceptCbParams &params)
Definition: CommCalls.h:31
void CLCB(const CommCloseCbParams &params)
Definition: CommCalls.h:40
void CNCB(const Comm::ConnectionPointer &conn, Comm::Flag status, int xerrno, void *data)
Definition: CommCalls.h:33
Params & GetCommParams(AsyncCall::Pointer &call)
Definition: CommCalls.h:154
int size
Definition: ModDevPoll.cc:75
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:17
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:265
const int debugLevel
Definition: AsyncCall.h:77
virtual bool canFire()
Definition: AsyncCall.cc:67
const int debugSection
Definition: AsyncCall.h:76
virtual CallDialer * getDialer()=0
const char *const name
Definition: AsyncCall.h:71
AnyP::PortCfgPointer port
the configuration listening port this call relates to (may be nil)
Definition: CommCalls.h:100
void print(std::ostream &os) const
Definition: CommCalls.cc:60
CommAcceptCbParams(void *aData)
Definition: CommCalls.cc:51
RefCount< CommAcceptCbPtrFun > Pointer
Definition: CommCalls.h:199
CommAcceptCbPtrFun(IOACB *aHandler, const CommAcceptCbParams &aParams)
Definition: CommCalls.cc:143
CommAcceptCbParams Params
Definition: CommCalls.h:198
void print(std::ostream &os) const override
Definition: CommCalls.cc:163
RefCount< CommCbFunPtrCallT< Dialer > > Pointer
Definition: CommCalls.h:283
CommCbFunPtrCallT & operator=(const CommCbFunPtrCallT &)
~CommCbFunPtrCallT() override
Definition: CommCalls.h:293
void fire() override
Definition: CommCalls.h:352
Dialer::Params Params
Definition: CommCalls.h:284
CallDialer * getDialer() override
Definition: CommCalls.h:295
CommCbFunPtrCallT(int debugSection, int debugLevel, const char *callName, const Dialer &aDialer)
Definition: CommCalls.h:324
bool canFire() override
Definition: CommCalls.h:333
CommCbFunPtrCallT(const CommCbFunPtrCallT &o)
Definition: CommCalls.h:289
Method method
Definition: CommCalls.h:187
void print(std::ostream &os) const override
Definition: CommCalls.h:180
void(C::* Method)(const Params &io)
Definition: CommCalls.h:169
void doDial() override
Definition: CommCalls.h:190
bool canDial(AsyncCall &c) override
Definition: CommCalls.h:175
CommCbMemFunT(const CbcPointer< C > &aJob, Method aMeth)
Definition: CommCalls.h:171
Params_ Params
Definition: CommCalls.h:168
CommCloseCbParams(void *aData)
Definition: CommCalls.cc:129
void print(std::ostream &os) const override
Definition: CommCalls.cc:231
CommCloseCbParams Params
Definition: CommCalls.h:249
CommCloseCbPtrFun(CLCB *aHandler, const Params &aParams)
Definition: CommCalls.cc:217
bool syncWithComm()
adjust using the current Comm state; returns false to cancel the call
Definition: CommCalls.h:64
CommCommonCbParams & operator=(const CommCommonCbParams &params)
int xerrno
The last errno to occur. non-zero if flag is Comm::COMM_ERROR.
Definition: CommCalls.h:83
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:85
void print(std::ostream &os) const
Definition: CommCalls.cc:34
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:82
Comm::ConnectionPointer conn
Definition: CommCalls.h:80
CommCommonCbParams(void *aData)
Definition: CommCalls.cc:18
CommConnectCbParams(void *aData)
Definition: CommCalls.cc:70
void print(std::ostream &os) const override
Definition: CommCalls.cc:186
CommConnectCbPtrFun(CNCB *aHandler, const Params &aParams)
Definition: CommCalls.cc:172
CommConnectCbParams Params
Definition: CommCalls.h:217
CommDialerParamsT(const Params &io)
Definition: CommCalls.h:146
void print(std::ostream &os) const
Definition: CommCalls.cc:118
bool syncWithComm()
Definition: CommCalls.cc:106
CommIoCbParams(void *aData)
Definition: CommCalls.cc:100
CommIoCbPtrFun(IOCB *aHandler, const Params &aParams)
Definition: CommCalls.cc:195
CommIoCbParams Params
Definition: CommCalls.h:233
IOCB * handler
Definition: CommCalls.h:241
void print(std::ostream &os) const override
Definition: CommCalls.cc:208
CommTimeoutCbParams(void *aData)
Definition: CommCalls.cc:136
CommTimeoutCbParams Params
Definition: CommCalls.h:264
void print(std::ostream &os) const override
Definition: CommCalls.cc:254
CommTimeoutCbPtrFun(CTCB *aHandler, const Params &aParams)
Definition: CommCalls.cc:240
virtual bool canDial(AsyncCall &call)
JobPointer job
Definition: AsyncJobCalls.h:37
static uint32 C
Definition: md4.c:43
Flag
Definition: Flag.h:15

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors