Debian Codegraph @

src/common/fdwatch_select.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 * select() 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 #define FDWATCH_BACKEND
25 #include "common/setup_before.h"
26 #include "fdwatch_select.h"
27
28 #include <cstring>
29
30 #include "common/eventlog.h"
31 #include "common/setup_after.h"
32
33 #ifdef HAVE_SELECT
34
35 namespace pvpgn
36 {
37
38 FDWSelectBackend::FDWSelectBackend(int nfds_)
39 :FDWBackend(nfds_), sr(0), smaxfd(0)
40 {
41 /* this should not happen */
42 if (nfds > FD_SETSIZE)
43 throw InitError("nfds over FD_SETSIZE");
44
45 rfds.reset(new t_psock_fd_set);
46 wfds.reset(new t_psock_fd_set);
47 trfds.reset(new t_psock_fd_set);
48 twfds.reset(new t_psock_fd_set);
49
50 PSOCK_FD_ZERO(trfds.get()); PSOCK_FD_ZERO(twfds.get());
51
52 INFO1("fdwatch select() based layer initialized (max {} sockets)", nfds);
53 }
54
55 FDWSelectBackend::~FDWSelectBackend() throw()
56 {}
57
58 int
59 FDWSelectBackend::add(int idx, unsigned rw)
60 {
61 // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {} rw: {}", fd, rw);
62 int fd = fdw_fd(fdw_fds + idx);
63
64 /* select() interface is limited by FD_SETSIZE max socket value */
65 if (fd >= FD_SETSIZE) return -1;
66
67 if (rw & fdwatch_type_read) PSOCK_FD_SET(fd, trfds.get());
68 else PSOCK_FD_CLR(fd, trfds.get());
69 if (rw & fdwatch_type_write) PSOCK_FD_SET(fd, twfds.get());
70 else PSOCK_FD_CLR(fd, twfds.get());
71 if (smaxfd < fd) smaxfd = fd;
72
73 return 0;
74 }
75
76 int
77 FDWSelectBackend::del(int idx)
78 {
79 int fd = fdw_fd(fdw_fds + idx);
80 // eventlog(eventlog_level_trace, __FUNCTION__, "called fd: {}", fd);
81 if (sr > 0)
82 ERROR0("BUG: called while still handling sockets");
83 PSOCK_FD_CLR(fd, trfds.get());
84 PSOCK_FD_CLR(fd, twfds.get());
85
86 return 0;
87 }
88
89 int
90 FDWSelectBackend::watch(long timeout_msec)
91 {
92 static struct timeval tv;
93
94 tv.tv_sec = timeout_msec / 1000;
95 tv.tv_usec = timeout_msec % 1000;
96
97 /* set the working sets based on the templates */
98 std::memcpy(rfds.get(), trfds.get(), sizeof(t_psock_fd_set));
99 std::memcpy(wfds.get(), twfds.get(), sizeof(t_psock_fd_set));
100
101 return (sr = psock_select(smaxfd + 1, rfds.get(), wfds.get(), NULL, &tv));
102 }
103
104 namespace
105 {
106
107 int fdw_select_cb(t_fdwatch_fd *cfd, void *data)
108 {
109 FDWSelectBackend *obj = static_cast<FDWSelectBackend*>(data);
110
111 return obj->cb(cfd);
112 }
113
114 }
115
116 int
117 FDWSelectBackend::cb(t_fdwatch_fd* cfd)
118 {
119 // eventlog(eventlog_level_trace, __FUNCTION__, "idx: {} fd: {}", idx, fdw_fd->fd);
120 if (fdw_rw(cfd) & fdwatch_type_read && PSOCK_FD_ISSET(fdw_fd(cfd), rfds.get())
121 && fdw_hnd(cfd)(fdw_data(cfd), fdwatch_type_read) == -2) return 0;
122 if (fdw_rw(cfd) & fdwatch_type_write && PSOCK_FD_ISSET(fdw_fd(cfd), wfds.get()))
123 fdw_hnd(cfd)(fdw_data(cfd), fdwatch_type_write);
124
125 return 0;
126 }
127
128 void
129 FDWSelectBackend::handle()
130 {
131 // eventlog(eventlog_level_trace, __FUNCTION__, "called nofds: {}", fdw_nofds);
132 fdwatch_traverse(fdw_select_cb, this);
133 sr = 0;
134 }
135
136 }
137
138 #endif /* HAVE_SELECT */