Debian Codegraph @

src/common/fdwatch_poll.cpp

raw symbols SCIP
1 /*
2 * Abstraction API/layer for the various ways PvPGN can inspect sockets state
3 * 2003 (C)
4 *
5 * Code is based on the ideas found in thttpd project.
6 *
7 * poll(2) based backend
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
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 General Public License for more details.
18 *
19 * You should have received a copy of the GNU 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 #include "common/setup_before.h"
25 #include "fdwatch_poll.h"
26
27 #include <cstring>
28
29 #include "common/eventlog.h"
30 #include "fdwatch.h"
31 #include "common/setup_after.h"
32
33 #ifdef HAVE_POLL
34 namespace pvpgn
35 {
36
37 FDWPollBackend::FDWPollBackend(int nfds_)
38 :FDWBackend(nfds_), sr(0), fds(new struct pollfd[nfds]), rridx(new int[nfds]), ridx(new int[nfds]), nofds(0)
39 {
40 std::memset(fds.get(), 0, sizeof(struct pollfd) * nfds);
41 std::memset(rridx.get(), 0, sizeof(int)* nfds);
42 /* I would use a memset with 255 but that is dirty and doesnt gain us anything */
43 for (int i = 0; i < nfds; i++) ridx[i] = -1;
44
45 INFO1("fdwatch poll() based layer initialized (max {} sockets)", nfds);
46 }
47
48 FDWPollBackend::~FDWPollBackend() throw()
49 {}
50
51 int
52 FDWPollBackend::add(int idx, unsigned rw)
53 {
54 int idxr;
55
56 // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {} rw: {}", fd, rw);
57 if (ridx[idx] < 0) {
58 idxr = nofds++;
59 fds[idxr].fd = fdw_fd(fdw_fds + idx);
60 ridx[idx] = idxr;
61 rridx[idxr] = idx;
62 // eventlog(eventlog_level_trace, __FUNCTION__, "adding new fd on {}", ridx);
63 }
64 else {
65 if (fds[ridx[idx]].fd != fdw_fd(fdw_fds + idx)) {
66 ERROR0("BUG: found existent poll_fd entry for same idx with different fd");
67 return -1;
68 }
69 idxr = ridx[idx];
70 // eventlog(eventlog_level_trace, __FUNCTION__, "updating fd on {}", ridx);
71 }
72
73 fds[idxr].events = 0;
74 if (rw & fdwatch_type_read) fds[idxr].events |= POLLIN;
75 if (rw & fdwatch_type_write) fds[idxr].events |= POLLOUT;
76
77 return 0;
78 }
79
80 int
81 FDWPollBackend::del(int idx)
82 {
83 // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {}", fd);
84 if (ridx[idx] < 0 || !nofds) return -1;
85 if (sr > 0)
86 ERROR0("BUG: called while still handling sockets");
87
88 /* move the last entry to the deleted one and decrement nofds count */
89 nofds--;
90 if (ridx[idx] < nofds) {
91 // eventlog(eventlog_level_trace, __FUNCTION__, "not last, moving {}", tfds[nofds].fd);
92 ridx[rridx[nofds]] = ridx[idx];
93 rridx[ridx[idx]] = rridx[nofds];
94 std::memcpy(fds.get() + ridx[idx], fds.get() + nofds, sizeof(struct pollfd));
95 }
96 ridx[idx] = -1;
97
98 return 0;
99 }
100
101 int
102 FDWPollBackend::watch(long timeout_msec)
103 {
104 return (sr = poll(fds.get(), nofds, timeout_msec));
105 }
106
107 void
108 FDWPollBackend::handle()
109 {
110 for (unsigned i = 0; i < nofds && sr; i++) {
111 bool changed = false;
112 t_fdwatch_fd *cfd = fdw_fds + rridx[i];
113
114 if (fdw_rw(cfd) & fdwatch_type_read &&
115 fds[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))
116 {
117 if (fdw_hnd(cfd)(fdw_data(cfd), fdwatch_type_read) == -2) {
118 sr--;
119 continue;
120 }
121 changed = true;
122 }
123
124 if (fdw_rw(cfd) & fdwatch_type_write &&
125 fds[i].revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL))
126 {
127 fdw_hnd(cfd)(fdw_data(cfd), fdwatch_type_write);
128 changed = true;
129 }
130
131 if (changed) sr--;
132 }
133 }
134
135 }
136
137 #endif /* HAVE_POLL */