]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/Read.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 05 Socket Functions */
13 #include "comm/IoCallback.h"
14 #include "comm/Loops.h"
15 #include "comm/Read.h"
16 #include "comm_internal.h"
17 #include "CommCalls.h"
22 #include "StatCounters.h"
25 // Does comm check this fd for read readiness?
26 // Note that when comm is not monitoring, there can be a pending callback
27 // call, which may resume comm monitoring once fired.
29 Comm::MonitorsRead(int fd
)
31 assert(isOpen(fd
) && COMMIO_FD_READCB(fd
));
32 // Being active is usually the same as monitoring because we always
33 // start monitoring the FD when we configure Comm::IoCallback for I/O
34 // and we usually configure Comm::IoCallback for I/O when we starting
35 // monitoring a FD for reading.
36 return COMMIO_FD_READCB(fd
)->active();
40 Comm::Read(const Comm::ConnectionPointer
&conn
, AsyncCall::Pointer
&callback
)
42 // TODO: move comm_read_base() internals into here
43 // when comm_read() char* API is no longer needed
44 comm_read_base(conn
, NULL
, 0, callback
);
49 * If a buffer is given the callback is scheduled when the read
50 * completes, on error, or on file descriptor close.
52 * If no buffer (NULL) is given the callback is scheduled when
53 * the socket FD is ready for a read(2)/recv(2).
56 comm_read_base(const Comm::ConnectionPointer
&conn
, char *buf
, int size
, AsyncCall::Pointer
&callback
)
58 debugs(5, 5, "comm_read, queueing read for " << conn
<< "; asynCall " << callback
);
60 /* Make sure we are open and not closing */
61 assert(Comm::IsConnOpen(conn
));
62 assert(!fd_table
[conn
->fd
].closing());
63 Comm::IoCallback
*ccb
= COMMIO_FD_READCB(conn
->fd
);
65 // Make sure we are either not reading or just passively monitoring.
66 // Active/passive conflicts are OK and simply cancel passive monitoring.
68 // if the assertion below fails, we have an active comm_read conflict
69 assert(fd_table
[conn
->fd
].halfClosedReader
!= NULL
);
70 commStopHalfClosedMonitor(conn
->fd
);
71 assert(!ccb
->active());
76 ccb
->setCallback(Comm::IOCB_READ
, callback
, (char *)buf
, NULL
, size
);
77 Comm::SetSelect(conn
->fd
, COMM_SELECT_READ
, Comm::HandleRead
, ccb
, 0);
81 Comm::ReadNow(CommIoCbParams
¶ms
, SBuf
&buf
)
84 ++ statCounter
.syscalls
.sock
.reads
;
85 const SBuf::size_type sz
= buf
.spaceSize();
86 char *inbuf
= buf
.rawSpace(sz
);
88 const int retval
= FD_READ_METHOD(params
.conn
->fd
, inbuf
, sz
);
89 params
.xerrno
= errno
;
91 debugs(5, 3, params
.conn
<< ", size " << sz
<< ", retval " << retval
<< ", errno " << params
.xerrno
);
93 if (retval
> 0) { // data read most common case
94 buf
.append(inbuf
, retval
);
95 fd_bytes(params
.conn
->fd
, retval
, FD_READ
);
96 params
.flag
= Comm::OK
;
99 } else if (retval
== 0) { // remote closure (somewhat less) common
100 // Note - read 0 == socket EOF, which is a valid read.
101 params
.flag
= Comm::ENDFILE
;
103 } else if (retval
< 0) { // connection errors are worst-case
104 debugs(5, 3, params
.conn
<< " Comm::COMM_ERROR: " << xstrerr(params
.xerrno
));
105 if (ignoreErrno(params
.xerrno
))
106 params
.flag
= Comm::INPROGRESS
;
108 params
.flag
= Comm::COMM_ERROR
;
115 * Handle an FD which is ready for read(2).
117 * If there is no provided buffer to fill call the callback.
119 * Otherwise attempt a read into the provided buffer.
120 * If the read attempt succeeds or fails, call the callback.
121 * Else, wait for another IO notification.
124 Comm::HandleRead(int fd
, void *data
)
126 Comm::IoCallback
*ccb
= (Comm::IoCallback
*) data
;
128 assert(data
== COMMIO_FD_READCB(fd
));
129 assert(ccb
->active());
131 // Without a buffer, just call back.
132 // The callee may ReadMore() to get the data.
134 ccb
->finish(Comm::OK
, 0);
138 /* For legacy callers : Attempt a read */
139 // Keep in sync with Comm::ReadNow()!
140 ++ statCounter
.syscalls
.sock
.reads
;
142 int retval
= FD_READ_METHOD(fd
, ccb
->buf
, ccb
->size
);
143 debugs(5, 3, "FD " << fd
<< ", size " << ccb
->size
<< ", retval " << retval
<< ", errno " << errno
);
145 /* See if we read anything */
146 /* Note - read 0 == socket EOF, which is a valid read */
148 fd_bytes(fd
, retval
, FD_READ
);
149 ccb
->offset
= retval
;
150 ccb
->finish(Comm::OK
, errno
);
153 } else if (retval
< 0 && !ignoreErrno(errno
)) {
154 debugs(5, 3, "comm_read_try: scheduling Comm::COMM_ERROR");
156 ccb
->finish(Comm::COMM_ERROR
, errno
);
160 /* Nope, register for some more IO */
161 Comm::SetSelect(fd
, COMM_SELECT_READ
, Comm::HandleRead
, data
, 0);
165 * Cancel a pending read. Assert that we have the right parameters,
166 * and that there are no pending read events!
168 * XXX: We do not assert that there are no pending read events and
169 * with async calls it becomes even more difficult.
170 * The whole interface should be reworked to do callback->cancel()
171 * instead of searching for places where the callback may be stored and
172 * updating the state of those places.
174 * AHC Don't call the comm handlers?
177 comm_read_cancel(int fd
, IOCB
*callback
, void *data
)
180 debugs(5, 4, "fails: FD " << fd
<< " closed");
184 Comm::IoCallback
*cb
= COMMIO_FD_READCB(fd
);
185 // TODO: is "active" == "monitors FD"?
187 debugs(5, 4, "fails: FD " << fd
<< " inactive");
191 typedef CommCbFunPtrCallT
<CommIoCbPtrFun
> Call
;
192 Call
*call
= dynamic_cast<Call
*>(cb
->callback
.getRaw());
194 debugs(5, 4, "fails: FD " << fd
<< " lacks callback");
198 call
->cancel("old comm_read_cancel");
200 typedef CommIoCbParams Params
;
201 const Params
¶ms
= GetCommParams
<Params
>(cb
->callback
);
203 /* Ok, we can be reasonably sure we won't lose any data here! */
204 assert(call
->dialer
.handler
== callback
);
205 assert(params
.data
== data
);
207 /* Delete the callback */
208 cb
->cancel("old comm_read_cancel");
210 /* And the IO event */
211 Comm::SetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
215 Comm::ReadCancel(int fd
, AsyncCall::Pointer
&callback
)
217 callback
->cancel("comm_read_cancel");
220 debugs(5, 4, "fails: FD " << fd
<< " closed");
224 Comm::IoCallback
*cb
= COMMIO_FD_READCB(fd
);
227 debugs(5, 4, "fails: FD " << fd
<< " inactive");
231 AsyncCall::Pointer call
= cb
->callback
;
233 /* Ok, we can be reasonably sure we won't lose any data here! */
234 assert(call
== callback
);
236 /* Delete the callback */
237 cb
->cancel("comm_read_cancel");
239 /* And the IO event */
240 Comm::SetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);