]> git.ipfire.org Git - thirdparty/squid.git/blame - src/comm/IoCallback.cc
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / comm / IoCallback.cc
CommitLineData
bbc27441 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
bbc27441
AJ
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
f7f3304a 9#include "squid.h"
ec41b64c 10#include "ClientInfo.h"
b0388924 11#include "comm/Connection.h"
ec41b64c 12#include "comm/IoCallback.h"
d841c88d 13#include "comm/Loops.h"
ec41b64c
AJ
14#include "comm/Write.h"
15#include "CommCalls.h"
16#include "fde.h"
582c2af2 17#include "globals.h"
ec41b64c
AJ
18
19Comm::CbEntry *Comm::iocb_table;
20
21void
22Comm::CallbackTableInit()
23{
24 // XXX: convert this to a std::map<> ?
25 iocb_table = static_cast<CbEntry*>(xcalloc(Squid_MaxFD, sizeof(CbEntry)));
cbebe602 26 for (int pos = 0; pos < Squid_MaxFD; ++pos) {
ec41b64c 27 iocb_table[pos].fd = pos;
ec41b64c 28 iocb_table[pos].readcb.type = IOCB_READ;
ec41b64c
AJ
29 iocb_table[pos].writecb.type = IOCB_WRITE;
30 }
31}
32
33void
34Comm::CallbackTableDestruct()
35{
b0388924 36 // release any Comm::Connections being held.
cbebe602 37 for (int pos = 0; pos < Squid_MaxFD; ++pos) {
aee3523a
AR
38 iocb_table[pos].readcb.conn = nullptr;
39 iocb_table[pos].writecb.conn = nullptr;
b0388924 40 }
ec41b64c
AJ
41 safe_free(iocb_table);
42}
43
44/**
45 * Configure Comm::Callback for I/O
46 *
47 * @param fd filedescriptor
48 * @param t IO callback type (read or write)
49 * @param cb callback
50 * @param buf buffer, if applicable
51 * @param func freefunc, if applicable
52 * @param sz buffer size
53 */
54void
55Comm::IoCallback::setCallback(Comm::iocb_type t, AsyncCall::Pointer &cb, char *b, FREE *f, int sz)
56{
57 assert(!active());
58 assert(type == t);
aee3523a 59 assert(cb != nullptr);
ec41b64c
AJ
60
61 callback = cb;
62 buf = b;
63 freefunc = f;
64 size = sz;
65 offset = 0;
66}
67
68void
69Comm::IoCallback::selectOrQueueWrite()
70{
9a0a18de 71#if USE_DELAY_POOLS
b27668ec
EB
72 if (BandwidthBucket *bucket = BandwidthBucket::SelectBucket(&fd_table[conn->fd])) {
73 bucket->scheduleWrite(this);
74 return;
ec41b64c
AJ
75 }
76#endif
77
8bbb16e3 78 SetSelect(conn->fd, COMM_SELECT_WRITE, Comm::HandleWrite, this, 0);
ec41b64c
AJ
79}
80
81void
82Comm::IoCallback::cancel(const char *reason)
83{
84 if (!active())
85 return;
86
87 callback->cancel(reason);
aee3523a 88 callback = nullptr;
ec41b64c
AJ
89 reset();
90}
91
92void
93Comm::IoCallback::reset()
94{
aee3523a 95 conn = nullptr;
ec41b64c
AJ
96 if (freefunc) {
97 freefunc(buf);
aee3523a
AR
98 buf = nullptr;
99 freefunc = nullptr;
ec41b64c
AJ
100 }
101 xerrno = 0;
102
9a0a18de 103#if USE_DELAY_POOLS
ec41b64c
AJ
104 quotaQueueReserv = 0;
105#endif
106}
107
108// Schedule the callback call and clear the callback
109void
c8407295 110Comm::IoCallback::finish(Comm::Flag code, int xerrn)
ec41b64c 111{
872a54ba 112 debugs(5, 3, "called for " << conn << " (" << code << ", " << xerrn << ")");
ec41b64c
AJ
113 assert(active());
114
115 /* free data */
5ddf7edc 116 if (freefunc && buf) {
ec41b64c 117 freefunc(buf);
aee3523a
AR
118 buf = nullptr;
119 freefunc = nullptr;
ec41b64c
AJ
120 }
121
aee3523a 122 if (callback != nullptr) {
ec41b64c
AJ
123 typedef CommIoCbParams Params;
124 Params &params = GetCommParams<Params>(callback);
aee3523a 125 if (conn != nullptr) params.fd = conn->fd; // for legacy write handlers...
b0388924 126 params.conn = conn;
ec41b64c
AJ
127 params.buf = buf;
128 params.size = offset;
129 params.flag = code;
130 params.xerrno = xerrn;
131 ScheduleCallHere(callback);
aee3523a 132 callback = nullptr;
ec41b64c
AJ
133 }
134
135 /* Reset for next round. */
136 reset();
137}
f53969cc 138