Debian Codegraph @

src/common/fdwatch_epoll.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 * Linux epoll(4) 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 #ifdef HAVE_EPOLL
26 #include "fdwatch_epoll.h"
27
28 #include <cstring>
29
30 #include "common/eventlog.h"
31 #include "fdwatch.h"
32 #include "common/setup_after.h"
33
34 namespace pvpgn
35 {
36
37 FDWEpollBackend::FDWEpollBackend(int nfds_)
38 :FDWBackend(nfds_), sr(0)
39 {
40 if ((epfd = epoll_create(nfds)) < 0)
41 throw InitError("failed to open epoll device");
42 epevents.reset(new struct epoll_event[nfds]);
43
44 std::memset(epevents.get(), 0, sizeof(struct epoll_event) * nfds);
45
46 INFO1("fdwatch epoll() based layer initialized (max {} sockets)", nfds);
47 }
48
49 FDWEpollBackend::~FDWEpollBackend() throw()
50 {}
51
52 int
53 FDWEpollBackend::add(int idx, unsigned rw)
54 {
55 // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {} rw: {}", fd, rw);
56
57 struct epoll_event tmpev;
58 std::memset(&tmpev, 0, sizeof(tmpev));
59 tmpev.events = 0;
60 if (rw & fdwatch_type_read)
61 tmpev.events |= EPOLLIN;
62 if (rw & fdwatch_type_write)
63 tmpev.events |= EPOLLOUT;
64
65 int op = fdw_rw(fdw_fds + idx) ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
66
67 tmpev.data.fd = idx;
68 if (epoll_ctl(epfd, op, fdw_fd(fdw_fds + idx), &tmpev)) {
69 ERROR0("got error from epoll_ctl()");
70 return -1;
71 }
72
73 return 0;
74 }
75
76 int
77 FDWEpollBackend::del(int idx)
78 {
79 // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {}", fd);
80 if (sr > 0)
81 ERROR0("BUG: called while still handling sockets");
82
83 if (fdw_rw(fdw_fds + idx)) {
84 struct epoll_event tmpev;
85 std::memset(&tmpev, 0, sizeof(tmpev));
86 tmpev.events = 0;
87 tmpev.data.fd = idx;
88 if (epoll_ctl(epfd, EPOLL_CTL_DEL, fdw_fd(fdw_fds + idx), &tmpev)) {
89 ERROR0("got error from epoll_ctl()");
90 return -1;
91 }
92 }
93
94 return 0;
95 }
96
97 int
98 FDWEpollBackend::watch(long timeout_msec)
99 {
100 return (sr = epoll_wait(epfd, epevents.get(), fdw_maxcons, timeout_msec));
101 }
102
103 void
104 FDWEpollBackend::handle()
105 {
106 // eventlog(eventlog_level_trace, __FUNCTION__, "called");
107 for (struct epoll_event *ev = epevents.get(); sr; sr--, ev++)
108 {
109 // eventlog(eventlog_level_trace, __FUNCTION__, "checking {} ident: {} read: {} write: {}", i, kqevents[i].ident, kqevents[i].filter & EVFILT_READ, kqevents[i].filter & EVFILT_WRITE);
110 t_fdwatch_fd *cfd = fdw_fds + ev->data.fd;
111
112 if (fdw_rw(cfd) & fdwatch_type_read && ev->events & (EPOLLIN | EPOLLERR | EPOLLHUP))
113 if (fdw_hnd(cfd) (fdw_data(cfd), fdwatch_type_read) == -2)
114 continue;
115
116 if (fdw_rw(cfd) & fdwatch_type_write && ev->events & (EPOLLOUT | EPOLLERR | EPOLLHUP))
117 fdw_hnd(cfd) (fdw_data(cfd), fdwatch_type_write);
118 }
119 sr = 0;
120 }
121
122 }
123
124 #endif /* HAVE_EPOLL */