| 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 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include "common/setup_before.h" |
| 23 | #include "fdwatch.h" |
| 24 | |
| 25 | #include <cstring> |
| 26 | |
| 27 | #include "common/eventlog.h" |
| 28 | #include "common/rlimit.h" |
| 29 | #include "fdwatch_select.h" |
| 30 | #include "fdwatch_poll.h" |
| 31 | #include "fdwatch_kqueue.h" |
| 32 | #include "fdwatch_epoll.h" |
| 33 | #include "fdwbackend.h" |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | unsigned fdw_maxcons; |
| 40 | t_fdwatch_fd *fdw_fds = NULL; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | |
| 45 | FDWBackend * fdw = NULL; |
| 46 | typedef elist<t_fdwatch_fd> FDWList; |
| 47 | FDWList freelist(&t_fdwatch_fd::freelist); |
| 48 | FDWList uselist(&t_fdwatch_fd::uselist); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | extern int fdwatch_init(int maxcons) |
| 53 | { |
| 54 | unsigned i; |
| 55 | int maxsys; |
| 56 | |
| 57 | maxsys = get_socket_limit(); |
| 58 | if (maxsys > 0) maxcons = (maxcons < maxsys) ? maxcons : maxsys; |
| 59 | if (maxcons < 32) { |
| 60 | eventlog(eventlog_level_fatal, __FUNCTION__, "too few sockets available ({})", maxcons); |
| 61 | return -1; |
| 62 | } |
| 63 | fdw_maxcons = maxcons; |
| 64 | |
| 65 | fdw_fds = new t_fdwatch_fd[fdw_maxcons]; |
| 66 | /* add all slots to the freelist */ |
| 67 | for (i = 0; i < fdw_maxcons; i++) |
| 68 | freelist.push_back(fdw_fds[i]); |
| 69 | |
| 70 | #ifdef HAVE_EPOLL |
| 71 | try { |
| 72 | fdw = new FDWEpollBackend(fdw_maxcons); |
| 73 | return 0; |
| 74 | } |
| 75 | catch (const FDWBackend::InitError&) { |
| 76 | } |
| 77 | #endif |
| 78 | #ifdef HAVE_KQUEUE |
| 79 | try { |
| 80 | fdw = new FDWKqueueBackend(fdw_maxcons); |
| 81 | return 0; |
| 82 | } |
| 83 | catch (const FDWBackend::InitError&) { |
| 84 | } |
| 85 | #endif |
| 86 | #ifdef HAVE_POLL |
| 87 | try { |
| 88 | fdw = new FDWPollBackend(fdw_maxcons); |
| 89 | return 0; |
| 90 | } |
| 91 | catch (const FDWBackend::InitError&) { |
| 92 | } |
| 93 | #endif |
| 94 | #ifdef HAVE_SELECT |
| 95 | try { |
| 96 | fdw = new FDWSelectBackend(fdw_maxcons); |
| 97 | return 0; |
| 98 | } |
| 99 | catch (const FDWBackend::InitError&) { |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | eventlog(eventlog_level_fatal, __FUNCTION__, "Found no working fdwatch layer"); |
| 104 | fdw = NULL; |
| 105 | fdwatch_close(); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | extern int fdwatch_close(void) |
| 110 | { |
| 111 | if (fdw) { delete fdw; fdw = NULL; } |
| 112 | if (fdw_fds) { delete[] fdw_fds; fdw_fds = NULL; } |
| 113 | freelist.clear(); |
| 114 | uselist.clear(); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | extern int fdwatch_add_fd(int fd, unsigned rw, fdwatch_handler h, void *data) |
| 120 | { |
| 121 | /* max sockets reached */ |
| 122 | if (freelist.empty()) return -1; |
| 123 | |
| 124 | t_fdwatch_fd *cfd = &freelist.front(); |
| 125 | fdw_fd(cfd) = fd; |
| 126 | |
| 127 | if (fdw->add(fdw_idx(cfd), rw)) return -1; |
| 128 | |
| 129 | /* add it to used sockets list, remove it from free list */ |
| 130 | uselist.push_back(*cfd); |
| 131 | freelist.remove(*cfd); |
| 132 | |
| 133 | fdw_rw(cfd) = rw; |
| 134 | fdw_data(cfd) = data; |
| 135 | fdw_hnd(cfd) = h; |
| 136 | |
| 137 | return fdw_idx(cfd); |
| 138 | } |
| 139 | |
| 140 | extern int fdwatch_update_fd(int idx, unsigned rw) |
| 141 | { |
| 142 | if (idx < 0 || idx >= fdw_maxcons) { |
| 143 | ERROR2("out of bounds idx [{}] (max: {})", idx, fdw_maxcons); |
| 144 | return -1; |
| 145 | } |
| 146 | /* do not allow completly reset the access because then backend codes |
| 147 | * can get confused */ |
| 148 | if (!rw) { |
| 149 | ERROR0("tried to reset rw, not allowed"); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | if (!fdw_rw(fdw_fds + idx)) { |
| 154 | ERROR0("found reseted rw"); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | if (fdw->add(idx, rw)) return -1; |
| 159 | fdw_rw(&fdw_fds[idx]) = rw; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | extern int fdwatch_del_fd(int idx) |
| 165 | { |
| 166 | if (idx < 0 || idx >= fdw_maxcons) { |
| 167 | ERROR2("out of bounds idx [{}] (max: {})", idx, fdw_maxcons); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | t_fdwatch_fd *cfd = fdw_fds + idx; |
| 172 | if (!fdw_rw(cfd)) { |
| 173 | ERROR0("found reseted rw"); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | fdw->del(idx); |
| 178 | |
| 179 | /* remove it from uselist, add it to freelist */ |
| 180 | uselist.remove(*cfd); |
| 181 | freelist.push_back(*cfd); |
| 182 | |
| 183 | fdw_fd(cfd) = 0; |
| 184 | fdw_rw(cfd) = 0; |
| 185 | fdw_data(cfd) = NULL; |
| 186 | fdw_hnd(cfd) = NULL; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | extern int fdwatch(long timeout_msec) |
| 192 | { |
| 193 | return fdw->watch(timeout_msec); |
| 194 | } |
| 195 | |
| 196 | extern void fdwatch_handle(void) |
| 197 | { |
| 198 | fdw->handle(); |
| 199 | } |
| 200 | |
| 201 | extern void fdwatch_traverse(t_fdw_cb cb, void *data) |
| 202 | { |
| 203 | for (FDWList::iterator it(uselist.begin()); it != uselist.end(); ++it) { |
| 204 | if (cb(&*it, data)) break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | } |