Write.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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 "cbdata.h"
11 #include "comm/Connection.h"
12 #include "comm/IoCallback.h"
13 #include "comm/Loops.h"
14 #include "comm/Write.h"
15 #include "fd.h"
16 #include "fde.h"
17 #include "globals.h"
18 #include "MemBuf.h"
19 #include "SquidTime.h"
20 #include "StatCounters.h"
21 #if USE_DELAY_POOLS
22 #include "ClientInfo.h"
23 #endif
24 
25 #include <cerrno>
26 
27 void
29 {
30  Comm::Write(conn, mb->buf, mb->size, callback, mb->freeFunc());
31 }
32 
33 void
34 Comm::Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func)
35 {
36  debugs(5, 5, HERE << conn << ": sz " << size << ": asynCall " << callback);
37 
38  /* Make sure we are open, not closing, and not writing */
39  assert(fd_table[conn->fd].flags.open);
40  assert(!fd_table[conn->fd].closing());
42  assert(!ccb->active());
43 
44  fd_table[conn->fd].writeStart = squid_curtime;
45  ccb->conn = conn;
46  /* Queue the write */
47  ccb->setCallback(IOCB_WRITE, callback, (char *)buf, free_func, size);
48  ccb->selectOrQueueWrite();
49 }
50 
56 void
57 Comm::HandleWrite(int fd, void *data)
58 {
59  Comm::IoCallback *state = static_cast<Comm::IoCallback *>(data);
60  int len = 0;
61  int nleft;
62 
63  assert(state->conn != NULL);
64  assert(state->conn->fd == fd);
65 
66  debugs(5, 5, HERE << state->conn << ": off " <<
67  (long int) state->offset << ", sz " << (long int) state->size << ".");
68 
69  nleft = state->size - state->offset;
70 
71 #if USE_DELAY_POOLS
73  if (bucket) {
74  assert(bucket->selectWaiting);
75  bucket->selectWaiting = false;
76  if (nleft > 0 && !bucket->applyQuota(nleft, state)) {
77  return;
78  }
79  }
80 #endif /* USE_DELAY_POOLS */
81 
82  /* actually WRITE data */
83  int xerrno = errno = 0;
84  len = FD_WRITE_METHOD(fd, state->buf + state->offset, nleft);
85  xerrno = errno;
86  debugs(5, 5, HERE << "write() returns " << len);
87 
88 #if USE_DELAY_POOLS
89  if (bucket) {
90  /* we wrote data - drain them from bucket */
91  bucket->reduceBucket(len);
92  }
93 #endif /* USE_DELAY_POOLS */
94 
95  fd_bytes(fd, len, FD_WRITE);
96  ++statCounter.syscalls.sock.writes;
97  // After each successful partial write,
98  // reset fde::writeStart to the current time.
99  fd_table[fd].writeStart = squid_curtime;
100 
101  if (len == 0) {
102  /* Note we even call write if nleft == 0 */
103  /* We're done */
104  if (nleft != 0)
105  debugs(5, DBG_IMPORTANT, "FD " << fd << " write failure: connection closed with " << nleft << " bytes remaining.");
106 
107  state->finish(nleft ? Comm::COMM_ERROR : Comm::OK, 0);
108  } else if (len < 0) {
109  /* An error */
110  if (fd_table[fd].flags.socket_eof) {
111  debugs(50, 2, "FD " << fd << " write failure: " << xstrerr(xerrno) << ".");
112  state->finish(nleft ? Comm::COMM_ERROR : Comm::OK, xerrno);
113  } else if (ignoreErrno(xerrno)) {
114  debugs(50, 9, "FD " << fd << " write failure: " << xstrerr(xerrno) << ".");
115  state->selectOrQueueWrite();
116  } else {
117  debugs(50, 2, "FD " << fd << " write failure: " << xstrerr(xerrno) << ".");
118  state->finish(nleft ? Comm::COMM_ERROR : Comm::OK, xerrno);
119  }
120  } else {
121  /* A successful write, continue */
122  state->offset += len;
123 
124  if (state->offset < state->size) {
125  /* Not done, reinstall the write handler and write some more */
126  state->selectOrQueueWrite();
127  } else {
128  state->finish(nleft ? Comm::OK : Comm::COMM_ERROR, 0);
129  }
130  }
131 }
132 
const char * xstrerr(int error)
Definition: xstrerror.cc:83
char * buf
Definition: MemBuf.h:134
FREE * freeFunc()
Definition: MemBuf.cc:305
void FREE(void *)
Definition: forward.h:36
mb_size_t size
Definition: MemBuf.h:135
bool active() const
Definition: IoCallback.h:45
struct StatCounters::@136::@141 sock
void setCallback(iocb_type type, AsyncCall::Pointer &cb, char *buf, FREE *func, int sz)
Definition: IoCallback.cc:55
@ OK
Definition: Flag.h:16
#define COMMIO_FD_WRITECB(fd)
Definition: IoCallback.h:79
#define DBG_IMPORTANT
Definition: Debug.h:41
Comm::ConnectionPointer conn
Definition: IoCallback.h:33
struct StatCounters::@136 syscalls
int size
Definition: ModDevPoll.cc:76
#define NULL
Definition: types.h:166
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
Details about a particular Comm IO callback event.
Definition: IoCallback.h:30
virtual void reduceBucket(const int len)
Decreases the bucket level.
virtual bool applyQuota(int &nleft, Comm::IoCallback *state)
@ IOCB_WRITE
Definition: IoCallback.h:25
Definition: MemBuf.h:24
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:152
Base class for Squid-to-client bandwidth limiting.
int conn
the current server connection FD
Definition: Transport.cc:26
#define assert(EX)
Definition: assert.h:19
static BandwidthBucket * SelectBucket(fde *f)
@ COMM_ERROR
Definition: Flag.h:17
void selectOrQueueWrite()
called when fd needs to write but may need to wait in line for its quota
Definition: IoCallback.cc:69
int FD_WRITE_METHOD(int fd, const char *buf, int len)
Definition: fde.h:200
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:34
time_t squid_curtime
Definition: stub_time.cc:17
void fd_bytes(int fd, int len, unsigned int type)
Definition: fd.cc:227
int ignoreErrno(int ierrno)
Definition: comm.cc:1411
#define fd_table
Definition: fde.h:189
void finish(Comm::Flag code, int xerrn)
finish the IO operation immediately and schedule the callback with the current state.
Definition: IoCallback.cc:110
PF HandleWrite
Definition: forward.h:33
bool selectWaiting
is between commSetSelect and commHandleWrite
@ FD_WRITE
Definition: enums.h:24
StatCounters statCounter
Definition: StatCounters.cc:12

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors