ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/src/select.C
Revision: 1.6
Committed: Wed Sep 5 11:23:15 2007 UTC (19 years, 1 month ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.5: +7 -7 lines
Log Message:
removed GPLed code and put license back to BSD

File Contents

# User Rev Content
1 pippijn 1.1 /*
2     * select.C: select(2) socket engine.
3 pippijn 1.2 * Rights to this code are documented in doc/pod/license.pod.
4 pippijn 1.1 *
5 pippijn 1.4 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
6 pippijn 1.1 */
7    
8 pippijn 1.6 static char const rcsid[] = "$Id: select.C,v 1.5 2007-08-30 19:56:26 pippijn Exp $";
9 pippijn 1.1
10     #include "atheme.h"
11     #include "internal.h"
12 pippijn 1.4 #include "datastream.h"
13 pippijn 1.1
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 pippijn 1.6 connection_t::list_type::iterator it = connection_t::list.begin ();
50     connection_t::list_type::iterator et = connection_t::list.end ();
51 pippijn 1.1 connection_t *cptr;
52    
53     FD_ZERO (&readfds);
54     FD_ZERO (&writefds);
55    
56 pippijn 1.4 while (it != et)
57     {
58     cptr = *it;
59    
60 pippijn 1.5 if (cptr->is_connecting ())
61 pippijn 1.4 FD_SET (cptr->fd, &writefds);
62    
63 pippijn 1.5 else if (cptr->is_listening ())
64 pippijn 1.4 FD_SET (cptr->fd, &readfds);
65    
66     if (sendq_nonempty (cptr))
67     FD_SET (cptr->fd, &writefds);
68    
69     else
70     FD_SET (cptr->fd, &readfds);
71    
72     ++it;
73     }
74 pippijn 1.1 }
75    
76     /*
77 pippijn 1.5 * connection_t::select()
78 pippijn 1.1 *
79     * inputs:
80     * delay in nanoseconds
81     *
82     * outputs:
83     * none
84     *
85     * side effects:
86     * registered sockets and their associated handlers are acted on.
87     */
88     void
89 pippijn 1.5 connection_t::select (time_t delay)
90 pippijn 1.1 {
91     int sr;
92 pippijn 1.6 connection_t::list_type::iterator it = connection_t::list.begin ();
93     connection_t::list_type::iterator et = connection_t::list.end ();
94 pippijn 1.1 connection_t *cptr;
95 pippijn 1.4 timeval to;
96 pippijn 1.1
97     update_select_sets ();
98     to.tv_sec = 0;
99     to.tv_usec = delay;
100    
101 pippijn 1.4 if ((sr = select (system_state.maxfd + 1, &readfds, &writefds, NULL, &to)) > 0)
102 pippijn 1.1 {
103     /* Iterate twice, so we don't touch freed memory if
104     * a connection is closed.
105     * -- jilles */
106 pippijn 1.4 while (it != et)
107     {
108     cptr = *it;
109    
110     if (FD_ISSET (cptr->fd, &writefds))
111     {
112 pippijn 1.5 if (cptr->is_connecting ())
113 pippijn 1.4 cptr.callback.connected (cptr);
114     else
115     cptr->write_handler (cptr);
116     }
117    
118     ++it;
119     }
120    
121 pippijn 1.6 for (it = connection_t::list.begin (), et = connection_t::list.end (); it != et; ++it)
122 pippijn 1.4 {
123     cptr = *it;
124    
125     if (FD_ISSET (cptr->fd, &readfds))
126     {
127     cptr->read_handler (cptr);
128     }
129     }
130    
131 pippijn 1.6 for (it = connection_t::list.begin (), et = connection_t::list.end (); it != et; ++it)
132 pippijn 1.4 {
133     cptr = *it;
134     if (cptr->flags & CF_DEAD)
135 pippijn 1.5 cptr->close ();
136 pippijn 1.4 }
137 pippijn 1.1 }
138     else
139     {
140     if (sr == 0)
141     {
142     /* select() timed out */
143     }
144     else if ((sr == -1) && (errno == EINTR))
145     {
146     /* some signal interrupted us, restart select() */
147     }
148     else if (sr == -1)
149     {
150     return;
151     }
152     }
153     }
154    
155     /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
156     * vim:ts=8
157     * vim:sw=8
158     * vim:noexpandtab
159     */