Squid Web Cache master
Loading...
Searching...
No Matches
IoCallback.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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#include "squid.h"
10#include "ClientInfo.h"
11#include "comm/Connection.h"
12#include "comm/IoCallback.h"
13#include "comm/Loops.h"
14#include "comm/Write.h"
15#include "CommCalls.h"
16#include "fde.h"
17#include "globals.h"
18
19namespace Comm
20{
21
22// XXX: Add API to react to Squid_MaxFD changes.
25static CbEntry *
27{
28 // XXX: convert this to a std::map<> ?
29 // XXX: Stop bypassing CbEntry-associated constructors! Refactor to use new() instead.
30 const auto iocb_table = static_cast<CbEntry*>(xcalloc(Squid_MaxFD, sizeof(CbEntry)));
31 for (int pos = 0; pos < Squid_MaxFD; ++pos) {
32 iocb_table[pos].fd = pos;
33 iocb_table[pos].readcb.type = IOCB_READ;
34 iocb_table[pos].writecb.type = IOCB_WRITE;
35 }
36 return iocb_table;
37}
38
39} // namespace Comm
40
42Comm::ioCallbacks(const int fd)
43{
44 static const auto table = MakeCallbackTable();
45 assert(fd < Squid_MaxFD);
46 return table[fd];
47}
48
59void
61{
62 assert(!active());
63 assert(type == t);
64 assert(cb != nullptr);
65
66 callback = cb;
67 buf = b;
68 freefunc = f;
69 size = sz;
70 offset = 0;
71}
72
73void
75{
76#if USE_DELAY_POOLS
77 if (BandwidthBucket *bucket = BandwidthBucket::SelectBucket(&fd_table[conn->fd])) {
78 bucket->scheduleWrite(this);
79 return;
80 }
81#endif
82
84}
85
86void
87Comm::IoCallback::cancel(const char *reason)
88{
89 if (!active())
90 return;
91
92 callback->cancel(reason);
93 callback = nullptr;
94 reset();
95}
96
97void
99{
100 conn = nullptr;
101 if (freefunc) {
102 freefunc(buf);
103 buf = nullptr;
104 freefunc = nullptr;
105 }
106 xerrno = 0;
107
108#if USE_DELAY_POOLS
109 quotaQueueReserv = 0;
110#endif
111}
112
113// Schedule the callback call and clear the callback
114void
116{
117 debugs(5, 3, "called for " << conn << " (" << code << ", " << xerrn << ")");
118 assert(active());
119
120 /* free data */
121 if (freefunc && buf) {
122 freefunc(buf);
123 buf = nullptr;
124 freefunc = nullptr;
125 }
126
127 if (callback != nullptr) {
128 typedef CommIoCbParams Params;
129 Params &params = GetCommParams<Params>(callback);
130 if (conn != nullptr) params.fd = conn->fd; // for legacy write handlers...
131 params.conn = conn;
132 params.buf = buf;
133 params.size = offset;
134 params.flag = code;
135 params.xerrno = xerrn;
136 ScheduleCallHere(callback);
137 callback = nullptr;
138 }
139
140 /* Reset for next round. */
141 reset();
142}
143
#define ScheduleCallHere(call)
Definition AsyncCall.h:166
#define assert(EX)
Definition assert.h:17
Base class for Squid-to-client bandwidth limiting.
static BandwidthBucket * SelectBucket(fde *f)
void setCallback(iocb_type type, AsyncCall::Pointer &cb, char *buf, FREE *func, int sz)
Definition IoCallback.cc:60
bool active() const
Definition IoCallback.h:45
void selectOrQueueWrite()
called when fd needs to write but may need to wait in line for its quota
Definition IoCallback.cc:74
AsyncCall::Pointer callback
Definition IoCallback.h:34
void finish(Comm::Flag code, int xerrn)
finish the IO operation immediately and schedule the callback with the current state.
void cancel(const char *reason)
Actively cancel the given callback.
Definition IoCallback.cc:87
iocb_type type
Definition IoCallback.h:32
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define COMM_SELECT_WRITE
Definition defines.h:25
#define fd_table
Definition fde.h:189
int Squid_MaxFD
void FREE(void *)
Definition forward.h:37
Abstraction layer for TCP, UDP, TLS, UDS and filedescriptor sockets.
PF HandleWrite
Definition forward.h:33
CbEntry & ioCallbacks(int fd)
Definition IoCallback.cc:42
iocb_type
Type of IO callbacks the Comm layer deals with.
Definition IoCallback.h:22
@ IOCB_WRITE
Definition IoCallback.h:25
@ IOCB_READ
Definition IoCallback.h:24
static CbEntry * MakeCallbackTable()
Definition IoCallback.cc:26
Flag
Definition Flag.h:15
void SetSelect(int, unsigned int, PF *, void *, time_t)
Mark an FD to be watched for its IO status.
void * xcalloc(size_t n, size_t sz)
Definition xalloc.cc:71