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