]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/comm/IoCallback.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / comm / IoCallback.cc
index 4026737754f8837cfe4e1ab8e09d7f860e976ed0..324688b51660e5c9c411f715cc64ade22524f979 100644 (file)
@@ -1,10 +1,20 @@
-#include "config.h"
+/*
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
 #include "ClientInfo.h"
+#include "comm/Connection.h"
 #include "comm/IoCallback.h"
 #include "comm/Loops.h"
 #include "comm/Write.h"
 #include "CommCalls.h"
 #include "fde.h"
+#include "globals.h"
 
 Comm::CbEntry *Comm::iocb_table;
 
@@ -13,11 +23,9 @@ Comm::CallbackTableInit()
 {
     // XXX: convert this to a std::map<> ?
     iocb_table = static_cast<CbEntry*>(xcalloc(Squid_MaxFD, sizeof(CbEntry)));
-    for (int pos = 0; pos < Squid_MaxFD; pos++) {
+    for (int pos = 0; pos < Squid_MaxFD; ++pos) {
         iocb_table[pos].fd = pos;
-        iocb_table[pos].readcb.fd = pos;
         iocb_table[pos].readcb.type = IOCB_READ;
-        iocb_table[pos].writecb.fd = pos;
         iocb_table[pos].writecb.type = IOCB_WRITE;
     }
 }
@@ -25,6 +33,11 @@ Comm::CallbackTableInit()
 void
 Comm::CallbackTableDestruct()
 {
+    // release any Comm::Connections being held.
+    for (int pos = 0; pos < Squid_MaxFD; ++pos) {
+        iocb_table[pos].readcb.conn = NULL;
+        iocb_table[pos].writecb.conn = NULL;
+    }
     safe_free(iocb_table);
 }
 
@@ -56,17 +69,13 @@ void
 Comm::IoCallback::selectOrQueueWrite()
 {
 #if USE_DELAY_POOLS
-    // stand in line if there is one
-    if (ClientInfo *clientInfo = fd_table[fd].clientInfo) {
-        if (clientInfo->writeLimitingActive) {
-            quotaQueueReserv = clientInfo->quotaEnqueue(fd);
-            clientInfo->kickQuotaQueue();
-            return;
-        }
+    if (BandwidthBucket *bucket = BandwidthBucket::SelectBucket(&fd_table[conn->fd])) {
+        bucket->scheduleWrite(this);
+        return;
     }
 #endif
 
-    SetSelect(fd, COMM_SELECT_WRITE, Comm::HandleWrite, this, 0);
+    SetSelect(conn->fd, COMM_SELECT_WRITE, Comm::HandleWrite, this, 0);
 }
 
 void
@@ -83,6 +92,7 @@ Comm::IoCallback::cancel(const char *reason)
 void
 Comm::IoCallback::reset()
 {
+    conn = NULL;
     if (freefunc) {
         freefunc(buf);
         buf = NULL;
@@ -97,13 +107,13 @@ Comm::IoCallback::reset()
 
 // Schedule the callback call and clear the callback
 void
-Comm::IoCallback::finish(comm_err_t code, int xerrn)
+Comm::IoCallback::finish(Comm::Flag code, int xerrn)
 {
-    debugs(5, 3, HERE << "called for FD " << fd << " (" << code << ", " << xerrno << ")");
+    debugs(5, 3, "called for " << conn << " (" << code << ", " << xerrn << ")");
     assert(active());
 
     /* free data */
-    if (freefunc) {
+    if (freefunc && buf) {
         freefunc(buf);
         buf = NULL;
         freefunc = NULL;
@@ -112,7 +122,8 @@ Comm::IoCallback::finish(comm_err_t code, int xerrn)
     if (callback != NULL) {
         typedef CommIoCbParams Params;
         Params &params = GetCommParams<Params>(callback);
-        params.fd = fd;
+        if (conn != NULL) params.fd = conn->fd; // for legacy write handlers...
+        params.conn = conn;
         params.buf = buf;
         params.size = offset;
         params.flag = code;
@@ -124,3 +135,4 @@ Comm::IoCallback::finish(comm_err_t code, int xerrn)
     /* Reset for next round. */
     reset();
 }
+