1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* s_bsd_select.c: select() compatible network routines. |
4 |
* |
5 |
* Originally by Adrian Chadd <adrian@creative.net.au> |
6 |
* Copyright (C) 2002 Hybrid Development Team |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
21 |
* USA |
22 |
* |
23 |
* $Id$ |
24 |
*/ |
25 |
|
26 |
#include "stdinc.h" |
27 |
#if USE_IOPOLL_MECHANISM == __IOPOLL_MECHANISM_SELECT |
28 |
#include "list.h" |
29 |
#include "fdlist.h" |
30 |
#include "hook.h" |
31 |
#include "ircd.h" |
32 |
#include "s_bsd.h" |
33 |
#include "log.h" |
34 |
|
35 |
/* |
36 |
* Note that this is only a single list - multiple lists is kinda pointless |
37 |
* under select because the list size is a function of the highest FD :-) |
38 |
* -- adrian |
39 |
*/ |
40 |
|
41 |
static fd_set select_readfds, tmpreadfds; |
42 |
static fd_set select_writefds, tmpwritefds; |
43 |
static int highest_fd = -1; |
44 |
|
45 |
|
46 |
/* |
47 |
* init_netio |
48 |
* |
49 |
* This is a needed exported function which will be called to initialise |
50 |
* the network loop code. |
51 |
*/ |
52 |
void |
53 |
init_netio(void) |
54 |
{ |
55 |
FD_ZERO(&select_readfds); |
56 |
FD_ZERO(&select_writefds); |
57 |
} |
58 |
|
59 |
/* |
60 |
* comm_setselect |
61 |
* |
62 |
* This is a needed exported function which will be called to register |
63 |
* and deregister interest in a pending IO state for a given FD. |
64 |
*/ |
65 |
void |
66 |
comm_setselect(fde_t *F, unsigned int type, PF *handler, |
67 |
void *client_data, time_t timeout) |
68 |
{ |
69 |
int new_events; |
70 |
|
71 |
if ((type & COMM_SELECT_READ)) |
72 |
{ |
73 |
F->read_handler = handler; |
74 |
F->read_data = client_data; |
75 |
} |
76 |
|
77 |
if ((type & COMM_SELECT_WRITE)) |
78 |
{ |
79 |
F->write_handler = handler; |
80 |
F->write_data = client_data; |
81 |
} |
82 |
|
83 |
new_events = (F->read_handler ? COMM_SELECT_READ : 0) | |
84 |
(F->write_handler ? COMM_SELECT_WRITE : 0); |
85 |
|
86 |
if (timeout != 0) |
87 |
F->timeout = CurrentTime + (timeout / 1000); |
88 |
|
89 |
if (new_events != F->evcache) |
90 |
{ |
91 |
if ((new_events & COMM_SELECT_READ)) |
92 |
FD_SET(F->fd, &select_readfds); |
93 |
else |
94 |
{ |
95 |
FD_CLR(F->fd, &select_readfds); |
96 |
FD_CLR(F->fd, &tmpreadfds); |
97 |
} |
98 |
|
99 |
if ((new_events & COMM_SELECT_WRITE)) |
100 |
FD_SET(F->fd, &select_writefds); |
101 |
else |
102 |
{ |
103 |
FD_CLR(F->fd, &select_writefds); |
104 |
FD_CLR(F->fd, &tmpwritefds); |
105 |
} |
106 |
|
107 |
if (new_events == 0) |
108 |
{ |
109 |
if (highest_fd == F->fd) |
110 |
while (highest_fd >= 0 && (FD_ISSET(highest_fd, &select_readfds) || |
111 |
FD_ISSET(highest_fd, &select_writefds))) |
112 |
highest_fd--; |
113 |
} |
114 |
else if (F->evcache == 0) |
115 |
if (F->fd > highest_fd) |
116 |
highest_fd = F->fd; |
117 |
|
118 |
F->evcache = new_events; |
119 |
} |
120 |
} |
121 |
|
122 |
/* |
123 |
* comm_select |
124 |
* |
125 |
* Called to do the new-style IO, courtesy of squid (like most of this |
126 |
* new IO code). This routine handles the stuff we've hidden in |
127 |
* comm_setselect and fd_table[] and calls callbacks for IO ready |
128 |
* events. |
129 |
*/ |
130 |
void |
131 |
comm_select(void) |
132 |
{ |
133 |
struct timeval to; |
134 |
int num, fd; |
135 |
fde_t *F; |
136 |
PF *hdl; |
137 |
|
138 |
/* Copy over the read/write sets so we don't have to rebuild em */ |
139 |
memcpy(&tmpreadfds, &select_readfds, sizeof(fd_set)); |
140 |
memcpy(&tmpwritefds, &select_writefds, sizeof(fd_set)); |
141 |
|
142 |
to.tv_sec = 0; |
143 |
to.tv_usec = SELECT_DELAY * 1000; |
144 |
num = select(highest_fd + 1, &tmpreadfds, &tmpwritefds, NULL, &to); |
145 |
|
146 |
set_time(); |
147 |
|
148 |
if (num < 0) |
149 |
{ |
150 |
#ifdef HAVE_USLEEP |
151 |
usleep(50000); |
152 |
#endif |
153 |
return; |
154 |
} |
155 |
|
156 |
for (fd = 0; fd <= highest_fd && num > 0; fd++) |
157 |
if (FD_ISSET(fd, &tmpreadfds) || FD_ISSET(fd, &tmpwritefds)) |
158 |
{ |
159 |
num--; |
160 |
|
161 |
F = lookup_fd(fd); |
162 |
if (F == NULL || !F->flags.open) |
163 |
continue; |
164 |
|
165 |
if (FD_ISSET(fd, &tmpreadfds)) |
166 |
if ((hdl = F->read_handler) != NULL) |
167 |
{ |
168 |
F->read_handler = NULL; |
169 |
hdl(F, F->read_data); |
170 |
if (!F->flags.open) |
171 |
continue; |
172 |
} |
173 |
|
174 |
if (FD_ISSET(fd, &tmpwritefds)) |
175 |
if ((hdl = F->write_handler) != NULL) |
176 |
{ |
177 |
F->write_handler = NULL; |
178 |
hdl(F, F->write_data); |
179 |
if (!F->flags.open) |
180 |
continue; |
181 |
} |
182 |
|
183 |
comm_setselect(F, 0, NULL, NULL, 0); |
184 |
} |
185 |
} |
186 |
#endif |