]> git.ipfire.org Git - thirdparty/squid.git/blame - src/CommRead.h
Bootstrapped
[thirdparty/squid.git] / src / CommRead.h
CommitLineData
6039b729 1
2/*
a553a5a3 3 * $Id: CommRead.h,v 1.7 2006/08/07 02:28:22 robertc Exp $
6039b729 4 *
5 * DEBUG: section 5 Comms
6 * AUTHOR: Robert Collins <robertc@squid-cache.org>
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 *
35 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
36 */
37
38#include "config.h"
39
40#ifndef COMMREAD_H
41#define COMMREAD_H
42
43#include "squid.h"
a553a5a3 44#include "comm.h"
6039b729 45#include "List.h"
46
2d8c0b1a 47template<class C>
48
49class CallBack
50{
51
52public:
53 CallBack() : handler(NULL), data(NULL){}
54
42d9b8e5 55 CallBack(C *aHandler, void *someData) : handler(aHandler), data (NULL)
56 {
57 if (someData)
58 data = cbdataReference(someData);
59 }
2d8c0b1a 60
42d9b8e5 61 CallBack(CallBack const &old) : handler(old.handler)
62 {
63 if (old.data)
64 data = cbdataReference (old.data);
65 else
66 data = NULL;
67 }
68
69 ~CallBack()
70 {
9a91f4b1 71 replaceData (NULL);
42d9b8e5 72 }
73
74 CallBack &operator = (CallBack const & rhs)
75 {
76 handler = rhs.handler;
77
9a91f4b1 78 replaceData (rhs.data);
42d9b8e5 79
80 return *this;
81 }
82
85a56f15 83 bool dataValid()
84 {
85 return cbdataReferenceValid(data);
86 }
42d9b8e5 87
9a91f4b1 88 bool operator == (CallBack const &rhs) { return handler==rhs.handler && data==rhs.data;}
2d8c0b1a 89
90#if 0
91 // twould be nice - RBC 20030307
92 C callback;
93#endif
94
95 C *handler;
96 void *data;
9a91f4b1 97
98private:
99 void replaceData(void *someData)
100 {
101 void *temp = NULL;
102
103 if (someData)
104 temp = cbdataReference(someData);
105
106 if (data)
107 cbdataReferenceDone(data);
108
109 data = temp;
110 }
2d8c0b1a 111};
112
113#if 0
114// twould be nice - RBC 20030307
115void
116CallBack<IOCB>::callback(int fd, char *buf, size_t size , comm_err_t errcode, int xerrno, void *tempData)
117{
118 assert (tempData == data);
119 handler (fd, buf, size , errcode, xerrno, data);
120 *this = CallBack();
121}
122
123#endif
124
6039b729 125class CommRead
126{
127
128public:
129 CommRead ();
130 CommRead (int fd, char *buf, int len, IOCB *handler, void *data);
2d8c0b1a 131 void queueCallback(size_t retval, comm_err_t errcode, int xerrno);
132 bool hasCallback() const;
133 void hasCallbackInvariant() const;
134 void hasNoCallbackInvariant() const;
135 void tryReading();
136 void read();
137 void initiateActualRead();
138 void nullCallback();
139 void doCallback(comm_err_t errcode, int xerrno);
6039b729 140 int fd;
141 char *buf;
142 int len;
2d8c0b1a 143 CallBack<IOCB> callback;
144 static void ReadTry(int fd, void *data);
6039b729 145};
146
147class DeferredRead
148{
149
150public:
151 typedef void DeferrableRead(void *context, CommRead const &);
6039b729 152 DeferredRead ();
153 DeferredRead (DeferrableRead *, void *, CommRead const &);
154 void markCancelled();
155 DeferrableRead *theReader;
156 void *theContext;
157 CommRead theRead;
158 bool cancelled;
159
160private:
6039b729 161};
162
163class DeferredReadManager
164{
165
166public:
167 ~DeferredReadManager();
168 void delayRead(DeferredRead const &);
169 void kickReads(int const count);
170
171private:
172 static PF CloseHandler;
173 static DeferredRead popHead(ListContainer<DeferredRead> &deferredReads);
174 void kickARead(DeferredRead const &);
175 void flushReads();
176 ListContainer<DeferredRead> deferredReads;
177};
178
179
180#endif /* COMMREAD_H */