1 |
/* |
2 |
* select.C: select(2) socket engine. |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id"; |
9 |
|
10 |
#include "atheme.h" |
11 |
#include "internal.h" |
12 |
|
13 |
extern list_t connection_list; /* this lives in connection.c */ |
14 |
fd_set readfds, writefds; |
15 |
|
16 |
/* |
17 |
* init_socket_queues() |
18 |
* |
19 |
* inputs: |
20 |
* none |
21 |
* |
22 |
* outputs: |
23 |
* none |
24 |
* |
25 |
* side effects: |
26 |
* when using select, we don't need to do anything here. |
27 |
*/ |
28 |
void |
29 |
init_socket_queues (void) |
30 |
{ |
31 |
return; |
32 |
} |
33 |
|
34 |
/* |
35 |
* update_select_sets() |
36 |
* |
37 |
* inputs: |
38 |
* none |
39 |
* |
40 |
* outputs: |
41 |
* none |
42 |
* |
43 |
* side effects: |
44 |
* registered sockets are prepared for the select() loop. |
45 |
*/ |
46 |
static void |
47 |
update_select_sets (void) |
48 |
{ |
49 |
node_t *n; |
50 |
connection_t *cptr; |
51 |
|
52 |
FD_ZERO (&readfds); |
53 |
FD_ZERO (&writefds); |
54 |
|
55 |
LIST_FOREACH (n, connection_list.head) |
56 |
{ |
57 |
cptr = n->data; |
58 |
|
59 |
if (CF_IS_CONNECTING (cptr)) |
60 |
FD_SET (cptr->fd, &writefds); |
61 |
|
62 |
else if (CF_IS_LISTENING (cptr)) |
63 |
FD_SET (cptr->fd, &readfds); |
64 |
|
65 |
if (sendq_nonempty (cptr)) |
66 |
FD_SET (cptr->fd, &writefds); |
67 |
|
68 |
else |
69 |
FD_SET (cptr->fd, &readfds); |
70 |
} |
71 |
} |
72 |
|
73 |
/* |
74 |
* connection_select() |
75 |
* |
76 |
* inputs: |
77 |
* delay in nanoseconds |
78 |
* |
79 |
* outputs: |
80 |
* none |
81 |
* |
82 |
* side effects: |
83 |
* registered sockets and their associated handlers are acted on. |
84 |
*/ |
85 |
void |
86 |
connection_select (time_t delay) |
87 |
{ |
88 |
int sr; |
89 |
node_t *n, *tn; |
90 |
connection_t *cptr; |
91 |
struct timeval to; |
92 |
|
93 |
update_select_sets (); |
94 |
to.tv_sec = 0; |
95 |
to.tv_usec = delay; |
96 |
|
97 |
if ((sr = select (claro_state.maxfd + 1, &readfds, &writefds, NULL, &to)) > 0) |
98 |
{ |
99 |
/* Iterate twice, so we don't touch freed memory if |
100 |
* a connection is closed. |
101 |
* -- jilles */ |
102 |
LIST_FOREACH_SAFE (n, tn, connection_list.head) |
103 |
{ |
104 |
cptr = n->data; |
105 |
|
106 |
if (FD_ISSET (cptr->fd, &writefds)) |
107 |
{ |
108 |
if (CF_IS_CONNECTING (cptr)) |
109 |
hook_call_event ("connected", cptr); |
110 |
else |
111 |
cptr->write_handler (cptr); |
112 |
} |
113 |
} |
114 |
|
115 |
LIST_FOREACH_SAFE (n, tn, connection_list.head) |
116 |
{ |
117 |
cptr = n->data; |
118 |
|
119 |
if (FD_ISSET (cptr->fd, &readfds)) |
120 |
{ |
121 |
cptr->read_handler (cptr); |
122 |
} |
123 |
} |
124 |
|
125 |
LIST_FOREACH_SAFE (n, tn, connection_list.head) |
126 |
{ |
127 |
cptr = n->data; |
128 |
if (cptr->flags & CF_DEAD) |
129 |
connection_close (cptr); |
130 |
} |
131 |
} |
132 |
else |
133 |
{ |
134 |
if (sr == 0) |
135 |
{ |
136 |
/* select() timed out */ |
137 |
} |
138 |
else if ((sr == -1) && (errno == EINTR)) |
139 |
{ |
140 |
/* some signal interrupted us, restart select() */ |
141 |
} |
142 |
else if (sr == -1) |
143 |
{ |
144 |
return; |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
150 |
* vim:ts=8 |
151 |
* vim:sw=8 |
152 |
* vim:noexpandtab |
153 |
*/ |