source: sources/src/ioEventManager.cc @ 1464:3f6711617be1

Revision 1464:3f6711617be1, 7.5 KB checked in by niam, 22 months ago (diff)

{issue #58[resolved]} support for non-c++ exceptions

Line 
1/***************************************************************************
2 *            ioEventManager.cc
3 *
4 *  Thu Sep 09 2006
5 *  Copyright  2006  Dmytro Milinevskyy
6 *  milinevskyy@gmail.com
7 ****************************************************************************/
8
9/*
10 *  This program is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU Lesser General Public License version 2.1 as published by
12 *  the Free Software Foundation;
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU Library General Public License for more details.
18 *
19 *  You should have received a copy of the GNU Lesser General Public License
20 *  along with this program; if not, write to the Free Software
21 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/**
25 * vim indentation settings
26 * set tabstop=4
27 * set shiftwidth=4
28 */
29
30#include <libdodo/directives.h>
31
32#include <poll.h>
33#include <errno.h>
34#include <string.h>
35
36#include <libdodo/ioEventManager.h>
37#include <libdodo/ioEventManagerEx.h>
38#include <libdodo/types.h>
39#include <libdodo/toolsMisc.h>
40#include <libdodo/ioEventDescriptor.h>
41#include <libdodo/pcSyncProcess.h>
42#include <libdodo/pcSyncStack.h>
43
44using namespace dodo::io::event;
45
46manager::manager(manager &)
47{
48}
49
50//-------------------------------------------------------------------
51
52manager::manager() : descs(0),
53                     keeper(new pc::sync::process(0))
54{
55}
56
57//-------------------------------------------------------------------
58
59manager::~manager()
60{
61    delete keeper;
62}
63
64//-------------------------------------------------------------------
65
66int
67manager::add(const descriptor &fl)
68{
69    pc::sync::stack pg(keeper);
70
71    __descriptors__ tempD;
72
73    tempD.position = ++descs;
74    tempD.in = fl.inDescriptor();
75    tempD.out = fl.outDescriptor();
76
77    desc.push_back(tempD);
78
79    return tempD.position;
80}
81
82//-------------------------------------------------------------------
83
84dodoArray<bool>
85manager::isReadable(const dodoArray<int> &pos,
86                    int                  timeout) const
87{
88    pc::sync::stack pg(keeper);
89
90    int count = -1;
91
92    pollfd *fds = new pollfd[pos.size()];
93
94    dodoArray<__descriptors__>::const_iterator i(desc.begin()), j(desc.end());
95    for (; i != j; ++i) {
96        dodoArray<int>::const_iterator m(pos.begin()), n(pos.end());
97        for (; m != n; ++m) {
98            if (i->position == *m) {
99                ++count;
100
101                fds[count].fd = i->in;
102                fds[count].events = POLLIN | POLLPRI;
103            }
104        }
105    }
106
107    ++count;
108
109    dodoArray<bool> tempRB;
110
111    if (count > 0) {
112        int res = poll(fds, count, timeout);
113
114        if (res > 0) {
115            for (int i = 0; i < count; ++i) {
116                if (isSetFlag(fds[i].revents, POLLIN) || isSetFlag(fds[i].revents, POLLPRI))
117                    tempRB.push_back(true);
118                else
119                    tempRB.push_back(false);
120            }
121
122            delete [] fds;
123
124            return tempRB;
125        } else {
126            if (res == 0) {
127                for (int i = 0; i < count; ++i)
128                    tempRB.push_back(false);
129
130                delete [] fds;
131
132                return tempRB;
133            } else {
134                delete [] fds;
135
136                dodo_throw exception::basic(exception::MODULE_IOEVENTMANAGER, MANAGEREX_ISREADABLE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
137            }
138        }
139    }
140
141    delete [] fds;
142
143    for (int i = 0; i < count; ++i)
144        tempRB.push_back(false);
145
146    return tempRB;
147}
148
149//-------------------------------------------------------------------
150
151dodoArray<bool>
152manager::isWritable(const dodoArray<int> &pos,
153                    int                  timeout) const
154{
155    pc::sync::stack pg(keeper);
156
157    int count = -1;
158
159    pollfd *fds = new pollfd[pos.size()];
160
161    dodoArray<__descriptors__>::const_iterator i(desc.begin()), j(desc.end());
162    for (; i != j; ++i) {
163        dodoArray<int>::const_iterator m(pos.begin()), n(pos.end());
164        for (; m != n; ++m) {
165            if (i->position == *m) {
166                ++count;
167
168                fds[count].fd = i->out;
169                fds[count].events = POLLOUT;
170            }
171        }
172    }
173
174    ++count;
175
176    dodoArray<bool> tempRB;
177
178    if (count > 0) {
179        int res = poll(fds, count, timeout);
180
181        if (res > 0) {
182            for (int i = 0; i < count; ++i) {
183                if (isSetFlag(fds[i].revents, POLLOUT))
184                    tempRB.push_back(true);
185                else
186                    tempRB.push_back(false);
187            }
188
189            delete [] fds;
190
191            return tempRB;
192        } else {
193            if (res == 0) {
194                delete [] fds;
195
196                for (int i = 0; i < count; ++i)
197                    tempRB.push_back(false);
198
199                return tempRB;
200            } else {
201                delete [] fds;
202
203                dodo_throw exception::basic(exception::MODULE_IOEVENTMANAGER, MANAGEREX_ISWRITABLE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
204            }
205        }
206    }
207
208    delete [] fds;
209
210    for (int i = 0; i < count; ++i)
211        tempRB.push_back(false);
212
213    return tempRB;
214}
215
216//-------------------------------------------------------------------
217
218bool
219manager::isReadable(int pos,
220                    int timeout) const
221{
222    pc::sync::stack pg(keeper);
223
224    pollfd fd;
225
226    dodoArray<__descriptors__>::const_iterator i(desc.begin()), j(desc.end());
227    for (; i != j; ++i) {
228        if (i->position == pos) {
229            fd.fd = i->in;
230            fd.events = POLLIN | POLLPRI;
231
232            int res = poll(&fd, 1, timeout);
233
234            if (res > 0) {
235                if (isSetFlag(fd.revents, POLLIN) || isSetFlag(fd.revents, POLLPRI))
236                    return true;
237                else
238                    return false;
239            } else {
240                if (res == 0)
241                    return false;
242                else
243                    dodo_throw exception::basic(exception::MODULE_IOEVENTMANAGER, MANAGEREX_ISREADABLE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
244            }
245        }
246    }
247
248    return false;
249}
250
251//-------------------------------------------------------------------
252
253void
254manager::remove(int pos)
255{
256    pc::sync::stack pg(keeper);
257
258    dodoArray<__descriptors__>::iterator i(desc.begin()), j(desc.end());
259    for (; i != j; ++i) {
260        if (i->position == pos) {
261            desc.erase(i);
262
263            break;
264        }
265    }
266}
267
268//-------------------------------------------------------------------
269
270bool
271manager::isWritable(int pos,
272                    int timeout) const
273{
274    pc::sync::stack pg(keeper);
275
276    pollfd fd;
277
278    dodoArray<__descriptors__>::const_iterator i(desc.begin()), j(desc.end());
279    for (; i != j; ++i) {
280        if (i->position == pos) {
281            fd.fd = i->out;
282            fd.events = POLLOUT;
283
284            int res = poll(&fd, 1, timeout);
285
286            if (res > 0) {
287                if (isSetFlag(fd.revents, POLLOUT))
288                    return true;
289                else
290                    return false;
291            } else {
292                if (res == 0)
293                    return false;
294                else
295                    dodo_throw exception::basic(exception::MODULE_IOEVENTMANAGER, MANAGEREX_ISWRITABLE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__);
296            }
297        }
298    }
299
300    return false;
301}
302
303//-------------------------------------------------------------------
304
Note: See TracBrowser for help on using the repository browser.